首页 > 解决方案 > TensorFlow 的对象检测 API 中缺少用于训练的模块和属性

问题描述

我目前正在尝试训练对象检测模型。我正在关注 Gilbert Tanner 在 YouTube 上的教程。我正在运行 TF 版本 1.9.0。

好像我缺少必要的模块。当我运行以下命令时:

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

我收到以下错误:

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'

出于某种原因,我不得不修复某些模块不在正确位置的其他问题(例如,nets 模块在安装时没有放在 models/research/object_detection 目录下,而是放在 models/研究/苗条)。

我不确定如何解决这个问题。我尝试在不同的 TensorFlow 1.x 版本之间切换,但每次遇到类似的错误,例如没有“v2”属性。

我怀疑我可能缺少应该安装在我的环境中的软件包,但我不确定它可能是什么。我也不确定为什么没有正确安装必要的模块。以下是我的环境中安装的所有软件包:

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

我是否缺少任何必要的包裹?对此问题的任何帮助表示赞赏。如果我没有提供有用的信息,请告诉我。

编辑: C:\Users\Admin\Desktop\ObjectDetection\models\research\object_detection\nets\inception_resnet_v2.py 中的第 375 行在下面加粗:

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):

这是我所指的视频的链接。我在 18:01 运行命令时出现问题。 https://www.youtube.com/watch?v=HjiBbChYRDw 我意识到我上面提供的命令与视频中显示的命令略有不同。但是,在本教程的书面版本中,Gilbert Tanner 已将命令更新为我上面提供的命令。

将 tf.compat.v1.GraphKeys 上的所有引用更改为 tf.GraphKeys 有效,但会出现更多错误:

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

在这个函数签名上:

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

当我将其更改为:

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

我收到此错误:

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

TensorFlow 1.x 没有 avg_pool2d 的文档,TensorFlow 2.x 有文档,所以如果我有 TensorFlow 1.9,我不确定为什么它在这个文件中。

我注意到 tf.nn 具有属性 avg_pool 和 avg_pool3d,但是,将其更改为这些会导致 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'>.


这是 tensors_to_item 中的第 98 行:

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

我不确定如何处理这个问题,而且似乎我不应该更改函数签名。必须对模块进行如此多的更改是否正常?

标签: pythontensorflowobject-detection

解决方案


此代码tf.compat.v1.GraphKeys.UPDATE_OPS在 上不可用Tensorflow==1.9.0,这对于tf.compat.v2.nn.avg_pool2d.

要让这些功能更新您的版本到 1.15,使用conda install tensorflow=1.15. 这将与教程的版本相匹配。从它使用的存储库tensorflow-gpu==1.15.2中获得。


推荐阅读