Argparse error with TensorFlow's cifar10.py

回眸只為那壹抹淺笑 提交于 2019-12-02 17:43:27

问题


I get the following error when I run python cifar10.py:

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

Here's the full output of the run including a complete trace:

I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcublas.so.7.0 locally                          
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcudnn.so.6.5 locally                           
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcufft.so.7.0 locally                           
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcuda.so locally                                
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcurand.so.7.0 locally                          
Traceback (most recent call last):                                                                                                 
  File "cifar10.py", line 54, in <module>                                                                                          
    """Number of images to process in a batch.""")                                                                                 
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/default/_flags.py", line 86, in DEFINE_integer           
    _define_helper(flag_name, default_value, docstring, int)                                                                       
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/default/_flags.py", line 60, in _define_helper           
    type=flagtype)                                                                                                                 
  File "/usr/lib/python2.7/argparse.py", line 1297, in add_argument                                                                
    return self._add_action(action)                                                                                                
  File "/usr/lib/python2.7/argparse.py", line 1671, in _add_action                                                                 
    self._optionals._add_action(action)                                                                                            
  File "/usr/lib/python2.7/argparse.py", line 1498, in _add_action                                                                 
    action = super(_ArgumentGroup, self)._add_action(action)                                                                       
  File "/usr/lib/python2.7/argparse.py", line 1311, in _add_action                                                                 
    self._check_conflict(action)                                                                                                   
  File "/usr/lib/python2.7/argparse.py", line 1449, in _check_conflict                                                             
    conflict_handler(action, confl_optionals)                                                                                      
  File "/usr/lib/python2.7/argparse.py", line 1456, in _handle_conflict_error                                                      
    raise ArgumentError(action, message % conflict_string)                                                                         
argparse.ArgumentError: argument --batch_size: conflicting option string(s): --batch_size 

This error seems to come from the following line in cifar10.py: tf.app.flags.DEFINE_integer('batch_size', 128, """Number of images to process in a batch.""")

It seems like the argparse library thinks that I've already defined the option string --batch_size, but I haven't.

[Stack: Amazon g2.2xlarge spot instance, Python 2.7.6]


回答1:


In the cifr10.py file:

import tensorflow as tf

from tensorflow.models.image.cifar10 import cifar10_input

FLAGS = tf.app.flags.FLAGS

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

The error is produced by this last statement, which, in the _flags.py file, defines an argparse argument with that name. Evidently at this point the tf.app already has such an argument define.

So we need to look further back at import tensorflow as tf to see how tf.app was created?

What's the Amazon g2.2xlarge? Could that defining batch_size as well?

Looks like tf.app comes from

tensorflow/python/platform/app.py

which in turn gets it from something like

from tensorflow.python.platform.google._app import *

So if you are running this on some google or amazon platform that itself accepts batch_size parameter, it could produce this error.


Another question about cifr10 and the batch_size argument:

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

Same error here:

Tensorflow ArgumentError Running CIFAR-10 example The answer says to use cifar10_train.py,cifar10_eval.py, not cifar10.py.



来源:https://stackoverflow.com/questions/36120914/argparse-error-with-tensorflows-cifar10-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!