0

I've been working on codes for Object Detection in Google Colab as in 1 and in 2 but, when using the following code sample:

!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

I'm obtaining this error:

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'

How can I use 'contrib' and other tensorflow v1 attributes with tensorflow v2? Appreciate any kind of help! Thank you in advance.

andreadaou
  • 43
  • 6

1 Answers1

0

I have modified your code like the following, and confirmed it throws no exception on Colaboratory (CPU backend). It is weird that I did not get the contrib error you mentioned...

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

How can I use 'contrib' and other tensorflow v1 attributes with tensorflow v2?

You cannot use contrib in TF2.x, since it was deprecated and removed. TensorFlow v1 and v2 are quite different, and it is hard, if not impossible, to make the two versions' behaviours coexist.

The Object Detection framework provides support for both TF1 and TF2, although the maintainers recommend the latter. Since the second article you linked targets TF2.x, I would recommend you to stick to TF2.

If you need to use TF1.x, you can use %tensorflow_version magic command to switch to TF1.x.

# Use TF1.x (available only in Colab)
%tensorflow_version 1.x
yumemio
  • 353
  • 4
  • 11