Tensorflow 对象检测 API - 'ValueError:anchor_strides 必须是与 self._box_specs 长度相同的列表'

Posted

技术标签:

【中文标题】Tensorflow 对象检测 API - \'ValueError:anchor_strides 必须是与 self._box_specs 长度相同的列表\'【英文标题】:Tensorflow Object Detection API - 'ValueError: anchor_strides must be a list with the same length as self._box_specs'Tensorflow 对象检测 API - 'ValueError:anchor_strides 必须是与 self._box_specs 长度相同的列表' 【发布时间】:2019-05-02 23:44:45 【问题描述】:

我正在使用 Tensorflow 的对象检测 API 解决对象检测问题,特别是在开放图像数据集上训练的 facessd 模型。谁能澄清一下

anchor_strides must be a list with the same length as self.box_specs

是什么意思?我正在浏览源代码,但找不到 self._box_specs 的定义位置。我假设它是在推理过程中最终绘制的边界框。我尝试调整图像大小,但没有任何改变。

每次运行模型时,我都会收到以下错误和回溯:

Traceback (most recent call last):  
  File "object_detection/model_main.py", line 109, in <module>
    tf.app.run()  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))  
  File "object_detection/model_main.py", line 105, in main
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/estimator/training.py", line 471, in train_and_evaluate
    return executor.run()  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/estimator/training.py", line 610, in run
    return self.run_local()  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/estimator/training.py", line 711, in run_local
    saving_listeners=saving_listeners)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 354, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 1207, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 1234, in _train_model_default
    input_fn, model_fn_lib.ModeKeys.TRAIN))  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 1075, in _get_features_and_labels_from_input_fn
    self._call_input_fn(input_fn, mode))  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 1162, in _call_input_fn
    return input_fn(**kwargs)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/inputs.py", line 479, in _train_input_fn
    batch_size=params['batch_size'] if params else train_config.batch_size)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/builders/dataset_builder.py", line 145, in build
    num_parallel_calls=num_parallel_calls)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1040, in map
    return ParallelMapDataset(self, map_func, num_parallel_calls)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2649, in __init__
    use_inter_op_parallelism)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2611, in __init__
    map_func, "Dataset.map()", input_dataset)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1860, in __init__
    self._function.add_to_graph(ops.get_default_graph())  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 479, in add_to_graph  
    self._create_definition_if_needed()  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 335, in _create_definition_if_needed
    self._create_definition_if_needed_impl()  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 344, in _create_definition_if_needed_impl
    self._capture_by_value, self._caller_device)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/function.py", line 864, in func_graph_from_py_func
    outputs = func(*func_graph.inputs)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1794, in tf_data_structured_function_wrapper  
    ret = func(*nested_args)  
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/builders/dataset_builder.py", line 129, in process_fn
    processed_tensors = transform_input_data_fn(processed_tensors)
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/inputs.py", line 456, in transform_and_pad_input_data_fn
    model = model_builder.build(model_config, is_training=True)
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/builders/model_builder.py", line 118, in build
    return _build_ssd_model(model_config.ssd, is_training, add_summaries)
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/builders/model_builder.py", line 244, in _build_ssd_model
    ssd_config.anchor_generator)
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/builders/anchor_generator_builder.py", line 81, in build
    ssd_anchor_generator_config.reduce_boxes_in_lowest_layer))
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/anchor_generators/multiple_grid_anchor_generator.py", line 334, in create_ssd_anchors
    anchor_strides, anchor_offsets)
  File "/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/models/research/object_detection/anchor_generators/multiple_grid_anchor_generator.py", line 121, in __init__
    'as self._box_specs' % arg_name)
ValueError: anchor_strides must be a list with the same length as self._box_specs

编辑:当我打印 box_specs_list 和 anchor_strides 时,这是我的输出:

    [[(0.1, 1.0), (0.20000000298023224, 2.0), (0.20000000298023224, 0.5)], [(0.35, 1.0), (0.35, 2.0), (0.35, 0.5), (0.35, 3.0), (0.35, 0.33329999446868896), (0.41833001202031717, 1.0)], [(0.4999999970197678, 1.0), (0.4999999970197678, 2.0), (0.4999999970197678, 0.5), (0.4999999970197678, 3.0), (0.4999999970197678, 0.33329999446868896), (0.5700877082367386, 1.0)], [(0.6499999940395356, 1.0), (0.6499999940395356, 2.0), (0.6499999940395356, 0.5), (0.6499999940395356, 3.0), (0.6499999940395356, 0.33329999446868896), (0.7211102477570095, 1.0)], [(0.7999999910593033, 1.0), (0.7999999910593033, 2.0), (0.7999999910593033, 0.5), (0.7999999910593033, 3.0), (0.7999999910593033, 0.33329999446868896), (0.8717797783669882, 1.0)], [(0.949999988079071, 1.0), (0.949999988079071, 2.0), (0.949999988079071, 0.5), (0.949999988079071, 3.0), (0.949999988079071, 0.33329999446868896), (0.9746794283655889, 1.0)]]
[(16, 16), (32, 32), (64, 64), (128, 128), (256, 256), (512, 512)]

它们的长度相同(6 项),所以我非常困惑。

【问题讨论】:

【参考方案1】:

更新!我进入源代码 --> tensorflow/models/research/object_detection/anchor_generators/multiple_grid_anchor_generator.py 并将第 100 行从:

self._anchor_strides = anchor_strides

self._anchor_strides = list(anchor_strides)

我通过反复进入 multiple_grid_anchor_generator.py 并打印 box_specs 和 anchor_strides 解决了这个问题,直到我发现列表的长度不是问题,实际上是 anchor_strides 被存储为一个 zip 对象而不是而不是一个列表。 :)

【讨论】:

以上是关于Tensorflow 对象检测 API - 'ValueError:anchor_strides 必须是与 self._box_specs 长度相同的列表'的主要内容,如果未能解决你的问题,请参考以下文章

具有奇怪检测结果的 TensorFlow 对象检测 api

Tensorflow 对象检测 API 中的过拟合

Tensorflow 对象检测 API - 验证丢失行为

Tensorflow 2 对象检测 API:Numpy 版本错误

TensorFlow对象检测API教程中获取边界框坐标

Tensorflow 对象检测 API:TensorBoard 中损坏的训练图像