'dict' object has no attribute 'itervalues':TF objectdetection API
问题
在准备好tensorflow objectdetection API所需要的数据集,准备开始训练时候,出现这个报错。
eval_metric_ops = {str(k): v for k, v in eval_metric_ops.iteritems()}
AttributeError: 'dict' object has no attribute 'iteritems'
losses = [loss_tensor for loss_tensor in losses_dict.itervalues()]
AttributeError: 'dict' object has no attribute 'itervalues'
搜索了一下原因,是python版本导致。
楼主所用python 3.6.0,但是可能API的代码先前就有的,所以有一些函数在新版本里换了名称。
具体可参照【Python】python2的代码转python3遇到的问题
解决方法
我们可以仔细阅读报错内容,尤其是最近报错定位的代码。
WARNING:tensorflow:Estimator's model_fn (<function create_model_fn.<locals>.model_fn at 0x00000256F9B69268>) includes params argument, but params are not passed to Estimator.
WARNING:tensorflow:num_readers has been reduced to 1 to match input file shards.
2018-09-28 09:48:07.799422: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
File "model_main.py", line 127, in <module>
tf.app.run()
File "D:\python360\lib\site-packages\tensorflow\python\platform\app.py", line 126, in run
_sys.exit(main(argv))
File "model_main.py", line 123, in main
tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
File "D:\python360\lib\site-packages\tensorflow\python\estimator\training.py", line 439, in train_and_evaluate
executor.run()
File "D:\python360\lib\site-packages\tensorflow\python\estimator\training.py", line 518, in run
self.run_local()
File "D:\python360\lib\site-packages\tensorflow\python\estimator\training.py", line 657, in run_local
eval_result = evaluator.evaluate_and_export()
File "D:\python360\lib\site-packages\tensorflow\python\estimator\training.py", line 847, in evaluate_and_export
hooks=self._eval_spec.hooks)
File "D:\python360\lib\site-packages\tensorflow\python\estimator\estimator.py", line 425, in evaluate
name=name)
File "D:\python360\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1087, in _evaluate_model
features, labels, model_fn_lib.ModeKeys.EVAL, self.config)
File "D:\python360\lib\site-packages\tensorflow\python\estimator\estimator.py", line 831, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "D:\python_objectdetection\models-master\models-master\research\object_detection\model_lib.py", line 391, in model_fn
eval_metric_ops = {str(k): v for k, v in eval_metric_ops.iteritems()}
AttributeError: 'dict' object has no attribute 'iteritems'
最后一句的报错定位在这个文件的这句代码:
"D:\python_objectdetection\models-master\models-master\research\object_detection\model_lib.py", line 391, in model_fn
OK,在工程中找到这个函数,然后Ctrl+F,找到精确位置。
下图是在notepad中编辑查找的,定位后修改为新版本所认可的函数名称即可。
在配置环境遇到很多问题,有点会焦躁不安。
但是笔者认为:
一帆风顺也并不是最好的;现在遇到的麻烦,都是未来解决棘手问题的基石,是财富。
程序如此,生活更如此。