对象检测 Google Colab:属性错误:模块 'tensorflow' 没有属性 'contrib'



我一直在谷歌Colab中为对象检测编写代码,如1和2中所示,但在使用以下代码示例时:

!git clone --quiet https://github.com/tensorflow/models.git
!apt-get install -qq protobuf-compiler python-tk
!pip install -q Cython contextlib2 pillow lxml matplotlib PyDrive
!pip install -q pycocotools
!pip install tensorflow-object-detection-api
%cd ~/models/research
!protoc object_detection/protos/*.proto --python_out=.
import os
os.environ['PYTHONPATH'] += ':/content/models/research/:/content/models/research/slim/'
!python object_detection/builders/model_builder_test.py

我得到这个错误:

Traceback (most recent call last):
File "object_detection/builders/model_builder_test.py", line 20, in <module>
from object_detection.builders import model_builder
File "/usr/local/lib/python3.7/dist-packages/object_detection/builders/model_builder.py", line 22, in <module>
from object_detection.builders import box_predictor_builder
File "/usr/local/lib/python3.7/dist-packages/object_detection/builders/box_predictor_builder.py", line 20, in <module>
from object_detection.predictors import convolutional_box_predictor
File "/usr/local/lib/python3.7/dist-packages/object_detection/predictors/convolutional_box_predictor.py", line 22, in <module>
slim = tf.contrib.slim
AttributeError: module 'tensorflow' has no attribute 'contrib'

如何将"contrib"和其他tensorflow v1属性与tensorflow v2一起使用?感谢您的帮助!提前谢谢。

我已经像下面这样修改了您的代码,并确认它不会在Colaboratory(CPU后端(上引发异常。奇怪的是,我没有得到你提到的contrib错误。。。

Gist:https://colab.research.google.com/gist/yumemio/343d342898b2e7c396967477b1756cf5/so_q71844660.ipynb

!git clone --quiet https://github.com/tensorflow/models.git
!apt-get install -qq protobuf-compiler python-tk
!pip install -q Cython contextlib2 pillow lxml matplotlib PyDrive
!pip install -q pycocotools
!pip install tensorflow-object-detection-api
!pip install tf_slim tensorflow_io   # added
%cd /content/models/research   # modified
!protoc object_detection/protos/*.proto --python_out=.
import os
os.environ['PYTHONPATH'] += ':/content/models/research/:/content/models/research/slim/:/content/models/'   # modified
!python object_detection/builders/model_builder_test.py

如何将"contrib"和其他tensorflow v1属性与tensorflow v2一起使用?

不能在TF2.x中使用contrib,因为它已被弃用并删除。TensorFlow v1和v2非常不同,即使不是不可能,也很难使这两个版本的行为共存。

对象检测框架同时支持TF1和TF2,尽管维护人员建议使用后者。由于第二篇文章链接了目标TF2.x,我建议您坚持使用TF2。

如果需要使用TF1.x,可以使用%tensorflow_version魔术命令切换到TF1.x。

# Use TF1.x (available only in Colab)
%tensorflow_version 1.x

相关内容

  • 没有找到相关文章

最新更新