Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "FLAGS" (command line switches) in TensorFlow?

I am trying to set custom batch size in my application.

If I put the following code into my app

tf.app.flags.DEFINE_integer('batch_size', 128,
                            """Number of images to process in a batch.""")

it says the following error

argparse.ArgumentError: argument --batch_size: conflicting option string(s): --batch_size

and if I remove this statement, it swears:

usage: <myscript> [-h] [--batch_size BATCH_SIZE] [--data_dir DATA_DIR]
                      [--checkpoint_dir CHECKPOINT_DIR]

at the line where FLAGS.batch_size used.

myscript is the name of my script and I didn't write this message anywhere and don't expect these command line switches at all. Looks like TF uses some Python switch parsing library and expecting these switches somehow. How to avoid this and expect custom switches?

How to hardcode custom batch_size?

UPDATE

My command line is follows:

myscript image1.png image2.png image3.png

PNGs are images from CIFAR database I wish to recognize from command line. This is command line I wish it to be, I don't wish it contain options listed in "usage" output.

like image 380
Dims Avatar asked Feb 06 '16 13:02

Dims


People also ask

What is flags in Tensorflow?

flags module is a functionality provided by Tensorflow to implement command line flags for your Tensorflow program. As an example, the code you came across would do the following: flags. DEFINE_float('learning_rate', 0.01, 'Initial learning rate.

What is the flag Python?

flags defines a distributed command line system, replacing systems like getopt() , optparse , and manual argument processing. Rather than an application having to define all flags in or near main() , each Python module defines flags that are useful to it.

What does TF app run do?

Defined in tensorflow/python/platform/app.py . Runs the program with an optional 'main' function and 'argv' list.


2 Answers

From your update, it sounds like you don't want to use the FLAGS module at all. If you look at a program like cifar10_train.py, you'll see the following near the bottom of the script:

def main(argv=None):  # pylint: disable=unused-argument
  # ...

if __name__ == '__main__':
  tf.app.run()

The tf.app.run() invocation is a bit of boilerplate that ensures that any flags are parsed, and then invokes the main() function in the same module. Notice that main() has an argv argument. This will be filled with the remaining arguments to your program: in your example, it will be a list ["image1.png", "image2.png", "image3.png"]. Therefore you can simply write your main() function to be something like:

def main(argv=None):
  if argv:
    for filename in argv:
      run_inference_on_file(filename)
like image 122
mrry Avatar answered Nov 06 '22 01:11

mrry


I suspect you are importing cifar10.py that already has the batch_size flag defined, and the error is due to you trying to re-define a flag with the same name. If you are importing cifar10, you can simply use --batch_size from the command line, and FLAGS.batch_size in your code.

like image 39
keveman Avatar answered Nov 06 '22 02:11

keveman