Justin
Justin

Reputation: 137

Missing modules and attributes for training in TensorFlow's Object Detection API

I'm currently attempting to train an object detection model. I'm following Gilbert Tanner's tutorial on YouTube. I am running TF version 1.9.0.

It seems as though I'm missing the necessary modules. When I run the following command:

python model_main.py --logtostderr --model_dir=training/ --pipeline_config_path=traini
ng/faster_rcnn_inception_v2_pets.config

I get the following error:

Traceback (most recent call last):
  File "model_main.py", line 26, in <module>
    from object_detection import model_lib
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\model_lib.py", line 28, in <module>
    from object_detection import exporter as exporter_lib
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\exporter.py", line 24, in <module>
    from object_detection.builders import model_builder
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\builders\model_builder.py", line 35, in <module>
    from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\models\faster_rcnn_inception_resnet_v2_feature_extractor
.py", line 30, in <module>
    from nets import inception_resnet_v2
  File "C:\Users\Admin\Desktop\ObjectDetection\models\research\object_detection\nets\inception_resnet_v2.py", line 375, in <module>
    batch_norm_updates_collections=tf.compat.v1.GraphKeys.UPDATE_OPS,
AttributeError: module 'tensorflow.compat' has no attribute 'v1'

For some reason, I've had to fix other problems with certain modules not being in the correct place (for instance, the nets module wasn't placed under the models/research/object_detection directory upon installation, it was instead placed under models/research/slim).

I'm not sure exactly how to fix this issue. I've tried bouncing between different 1.x versions of TensorFlow but each time I am met with similar errors, such as not having the 'v2' attribute.

I suspect I could be lacking a package that should be installed in my environment, but I'm not sure what it could be. I'm also unsure about why the necessary modules aren't properly installed. Here are all of the packages that are installed in my environment:

Package Version Lastest Version
absl-py 0.9.0   0.8.1
astor   0.8.1   0.8.0
biwrap  0.1.6   
bleach  1.5.0   3.1.0
certifi 2019.11.28  2019.11.28
gast    0.3.3   0.3.2
grpcio  1.27.0  1.16.1
h5py    2.10.0  2.10.0
html5lib    0.9999999   1.0.1
keras-applications  1.0.8   1.0.8
keras-preprocessing 1.1.0   1.1.0
markdown    3.1.1   3.1.1
mock    3.0.5   3.0.5
numpy   1.18.1  1.18.1
object-detection    0.1 
pandas  1.0.0   1.0.0
pillow  7.0.0   7.0.0
pip 20.0.2  20.0.2
protobuf    3.11.3  3.11.2
pycocotools 2.0 
python  3.6.10  3.8.1
python-dateutil 2.8.1   2.8.1
pytz    2019.3  2019.3
setuptools  39.1.0  45.1.0
six 1.14.0  1.14.0
sqlite  3.31.1  3.31.1
tensorboard 1.9.0   2.0.0
tensorflow  1.9.0   2.0.0
tensorflow-estimator    1.13.0  2.0.0
tensorflow-plot 0.3.0   
tensorflow-tensorboard  1.5.1   
termcolor   1.1.0   1.1.0
vc  14.1    14.1
vs2015_runtime  14.16.27012 14.16.27012
werkzeug    0.16.1  0.16.1
wheel   0.34.2  0.34.2
wincertstore    0.2 0.2

Am I missing any necessary packages? Any help on this issue is appreciated. Please let me know if I have not included information that would be helpful.

EDIT: Line 375 in C:\Users\Admin\Desktop\ObjectDetection\models\research\object_detection\nets\inception_resnet_v2.py is bolded below:

def inception_resnet_v2_arg_scope(
    weight_decay=0.00004,
    batch_norm_decay=0.9997,
    batch_norm_epsilon=0.001,
    activation_fn=tf.nn.relu,
    **batch_norm_updates_collections=tf.compat.v1.GraphKeys.UPDATE_OPS**,
    batch_norm_scale=False):

Here is the link to the video I'm referring to. My problem is occurring when I run the command at 18:01. https://www.youtube.com/watch?v=HjiBbChYRDw I realize the command I provided above is slightly different than the one shown in the video. However, in the written version of the tutorial, Gilbert Tanner has updated the command to the one I provided above.

Changing all references on tf.compat.v1.GraphKeys to tf.GraphKeys works, but more errors arise:

AttributeError: module 'tensorflow.compat' has no attribute 'v2'

on this function signature:

def global_pool(input_tensor, pool_op=tf.compat.v2.nn.avg_pool2d)

When I change it to this:

def global_pool(input_tensor, pool_op=tf.nn.avg_pool2d)

I get this error:

AttributeError: module 'tensorflow.nn' has no attribute 'avg_pool2d'

There is no documentation for avg_pool2d for TensorFlow 1.x and there is for TensorFlow 2.x, so I'm not sure why it's in this file if I have TensorFlow 1.9.

I notice tf.nn has attributes avg_pool and avg_pool3d, however, changing it to these causes a TypeError:

Traceback (most recent call last):
  File "model_main.py", line 109, in <module>
    tf.app.run()
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
    _sys.exit(main(argv))
  File "model_main.py", line 105, in main
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\estimator\training.py", line 447, in train_and_evaluate
    return executor.run()
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\estimator\training.py", line 531, in run
    return self.run_local()
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\estimator\training.py", line 669, in run_local
    hooks=train_hooks)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\estimator\estimator.py", line 366, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1119, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1129, in _train_model_default
    input_fn, model_fn_lib.ModeKeys.TRAIN))
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\estimator\estimator.py", line 985, in _get_features_and_labels_from_input_fn
    result = self._call_input_fn(input_fn, mode)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1074, in _call_input_fn
    return input_fn(**kwargs)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\inputs.py", line 504, in _train_input_fn
    params=params)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\inputs.py", line 607, in train_input
    batch_size=params['batch_size'] if params else train_config.batch_size)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\builders\dataset_builder.py", line 155, in build
    dataset = data_map_fn(process_fn, num_parallel_calls=num_parallel_calls)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 882, in map
    return ParallelMapDataset(self, map_func, num_parallel_calls)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 1899, in __init__
    super(ParallelMapDataset, self).__init__(input_dataset, map_func)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 1868, in __init__
    self._map_func.add_to_graph(ops.get_default_graph())
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\framework\function.py", line 475, in add_to_graph
    self._create_definition_if_needed()
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\framework\function.py", line 331, in _create_definition_if_needed
    self._create_definition_if_needed_impl()
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\framework\function.py", line 340, in _create_definition_if_needed_impl
    self._capture_by_value, self._caller_device)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\framework\function.py", line 804, in func_graph_from_py_func
    outputs = func(*func_graph.inputs)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 1833, in tf_map_func
    ret = map_func(nested_args)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\builders\dataset_builder.py", line 134, in process_fn
    processed_tensors = decoder.decode(value)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\data_decoders\tf_example_decoder.py", line 388, in decod
e
    tensors = decoder.decode(serialized_example, items=keys)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\contrib\slim\python\slim\data\tfexample_decoder.py", line 520, in decode
    outputs.append(handler.tensors_to_item(keys_to_tensors))
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\data_decoders\tf_example_decoder.py", line 129, in tenso
rs_to_item
    item = self._handler.tensors_to_item(keys_to_tensors)
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\data_decoders\tf_example_decoder.py", line 98, in tensor
s_to_item
    return tf.maximum(self._name_to_id_table.lookup(unmapped_tensor),
  File "C:\Users\Admin\Anaconda3\envs\object_detection\lib\site-packages\tensorflow\python\ops\lookup_ops.py", line 223, in lookup
    (self._key_dtype, keys.dtype))
TypeError: Signature mismatch. Keys must be dtype <dtype: 'float32'>, got <dtype: 'string'>.


Here is line 98 in tensors_to_item:

    return tf.maximum(self._name_to_id_table.lookup(unmapped_tensor),
                      self._display_name_to_id_table.lookup(unmapped_tensor))

I'm not sure how to handle this issue and it seems like I shouldn't have changed the function signature. Is having to make this many changes to the modules normal?

Upvotes: 2

Views: 1048

Answers (1)

Bernardo Duarte
Bernardo Duarte

Reputation: 4264

This code tf.compat.v1.GraphKeys.UPDATE_OPS is not available on Tensorflow==1.9.0, this is the same for tf.compat.v2.nn.avg_pool2d.

To have those features update your version to 1.15 with conda install tensorflow=1.15. That will match the tutorial's version. As obtained from it's repository it uses tensorflow-gpu==1.15.2.

Upvotes: 1

Related Questions