> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# TF Object Detection API适配Python 3
- URL: https://yinguobing.com/blog/tensorflow-object-detection-with-python-3/
- Published: 2018-07-31T11:51:28.000Z
- Updated: 2018-08-23T12:20:44.000Z
- Description: TensorFlow的Object Detection API集成了众多用于物体检测的神经网络模型。该API目前仍处于活跃的开发中，从实际情况来看对Python3的支持还不是很好。
- Author: 尹国冰
- Tags: TensorFlow

TensorFlow的Object Detection API集成了众多用于物体检测的神经网络模型。该API目前仍处于活跃的开发中，从实际情况来看对Python3的支持还不是很好。

填过的坑列在这里，方便以后回顾。

### **RuntimeError: main thread is not in main loop**

原因是指定matplotlib的backend要在import之前。使用正确顺序导入matplotlib即可。

修改文件 `/research/object_detection/eval_util.py`，将 visualization\_utils的import放在最前边。修改后如下：

```
from object_detection.utils import visualization_utils as vis_utils
from object_detection.core import box_list
from object_detection.core import box_list_opsfrom object_detection.core 
import keypoint_ops
```

参考：[https://github.com/tensorflow/models/issues/4777](https://github.com/tensorflow/models/issues/4777?ref=yinguobing.com)

### **SyntaxError: invalid syntax**

```
print 'Scores and tpfp per class label: {}'.format(class_index)
```

原因是使用了Python2的 `print` 方法。

修改文件 `/research/object_detection/utils/object_detection_evaluation.py`，给 `print` 函数加上括号即可。

参考：[https://github.com/tensorflow/models/issues/4776](https://github.com/tensorflow/models/issues/4776?ref=yinguobing.com)

### **AttributeError: 'dict' object has no attribute 'itervalues'**

Python2与Python3的差异。

修改文件 `/research/object_detection/model_lib.py`，将 `itervalues()` 修改为 `values()`.

参考：[https://github.com/tensorflow/models/issues/4860](https://github.com/tensorflow/models/issues/4860?ref=yinguobing.com)

### **TypeError: can't pickle dict\_values objects**

Python2与Python3的差异。

修改文件 `/research/object_detection/model_lib.py`，显式转换为 `list`。 

```
category_index.values() -> list(category_index.values())
```

参考：[https://github.com/tensorflow/models/issues/4780](https://github.com/tensorflow/models/issues/4780?ref=yinguobing.com)

### Paddings must be non-negative

这个不是Python的问题，是TensorFlow的一个BUG。

作者已经修正了BUG，等待PR通过更新就好。临时的解决方案是使用 `legacy` 目录下的 `train.py` 文件。

参考：[https://github.com/tensorflow/models/issues/4798](https://github.com/tensorflow/models/issues/4798?ref=yinguobing.com)