1
votes

I am trying to install tensorflow object detection on google colab. I performed the steps as given on GitHub. But, now I am facing the proble when I am trying to test my installation, that is when I ran "!python3 object_detection/builders/model_builder_test.py" it is giving error: AttributeError: module 'tensorflow' has no attribute 'contrib'. Here is my code snippet:

%cd /content/gdrive/My Drive/TFConfig/models/research/

!protoc object_detection/protos/*.proto --python_out=.

import os
os.environ['PYTHONPATH'] += ':/content/gdrive/My Drive/TFConfig/models/research/::/content/gdrive/My Drive/TFConfig/models/research/slim/'

!python setup.py build
!python setup.py install

!python3 object_detection/builders/model_builder_test.py

It is on google colab. and I am getting the error message:

Traceback (most recent call last):
File "object_detection/builders/model_builder_test.py", line 23, in <module>
from object_detection.builders import model_builder
File "/content/gdrive/My Drive/TFConfig/models/research/object_detection/builders/model_builder.py", line 22, in <module>
from object_detection.builders import box_predictor_builder
File "/content/gdrive/My Drive/TFConfig/models/research/object_detection/builders/box_predictor_builder.py", line 20, in <module>
from object_detection.predictors import convolutional_box_predictor
File "/content/gdrive/My Drive/TFConfig/models/research/object_detection/predictors/convolutional_box_predictor.py", line 23, in <module>
slim = tf.contrib.slim
AttributeError: module 'tensorflow' has no attribute 'contrib'
1
This might be an issue of tensorflow versions. The tools in tensorflow.contrib were contributed by people in the earlier days of tensorflow when they were missing some functionality. Later the tools were integrated into other modules.Adomas Baliuka

1 Answers

0
votes
I suggest you two ways to accomplish the task:-
First, migrate your code on Tensorflow 2 in which they removed contrib module and implemented in TensorFlow's other module.

Second You can install TensorFlow 1.* to get TensorFlow.contrib module.


if you want to try the first one for object detection here is all step mentioned...
1=>###images=>train and  test
2=>###select a model and place at object_detectiopn
3=>
(tensorflow1) C:\> conda install -c anaconda protobuf
(tensorflow1) C:\> pip install pillow
(tensorflow1) C:\> pip install lxml
(tensorflow1) C:\> pip install Cython
(tensorflow1) C:\> pip install contextlib2
(tensorflow1) C:\> pip install jupyter
(tensorflow1) C:\> pip install matplotlib
(tensorflow1) C:\> pip install pandas
(tensorflow1) C:\> pip install opencv-python
3=>(tensorflow1) C:\> set PYTHONPATH=C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\slim
4=>(tensorflow1) C:\> cd C:\tensorflow1\models\research
5=>protoc --python_out=. .\object_detection\protos\anchor_generator.proto .\object_detection\protos\argmax_matcher.proto .\object_detection\protos\bipartite_matcher.proto .\object_detection\protos\box_coder.proto .\object_detection\protos\box_predictor.proto .\object_detection\protos\eval.proto .\object_detection\protos\faster_rcnn.proto .\object_detection\protos\faster_rcnn_box_coder.proto .\object_detection\protos\grid_anchor_generator.proto .\object_detection\protos\hyperparams.proto .\object_detection\protos\image_resizer.proto .\object_detection\protos\input_reader.proto .\object_detection\protos\losses.proto .\object_detection\protos\matcher.proto .\object_detection\protos\mean_stddev_box_coder.proto .\object_detection\protos\model.proto .\object_detection\protos\optimizer.proto .\object_detection\protos\pipeline.proto .\object_detection\protos\post_processing.proto .\object_detection\protos\preprocessor.proto .\object_detection\protos\region_similarity_calculator.proto .\object_detection\protos\square_box_coder.proto .\object_detection\protos\ssd.proto .\object_detection\protos\ssd_anchor_generator.proto .\object_detection\protos\string_int_label_map.proto .\object_detection\protos\train.proto .\object_detection\protos\keypoint_box_coder.proto .\object_detection\protos\multiscale_anchor_generator.proto .\object_detection\protos\graph_rewriter.proto .\object_detection\protos\calibration.proto .\object_detection\protos\flexible_grid_anchor_generator.proto
6=>(tensorflow1) C:\tensorflow1\models\research> python setup.py build
(tensorflow1) C:\tensorflow1\models\research> python setup.py install
7=>###(tensorflow1) C:\tensorflow1\models\research\object_detection> python xml_to_csv.py
8=>###generate_tfrecord.py:
def class_text_to_int(row_label):
    if row_label == 'nine':
        return 1
    elif row_label == 'ten':
        return 2
    elif row_label == 'jack':
        return 3
    elif row_label == 'queen':
        return 4
    elif row_label == 'king':
        return 5
    elif row_label == 'ace':
        return 6
    else:
        None
9=>###python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record
python generate_tfrecord.py --csv_input=images\test_labels.csv --image_dir=images\test --output_path=test.record

10=>###C:\tensorflow1\models\research\object_detection\training labelmap.pbtxt
item {
  id: 1
  name: 'nine'
}

item {
  id: 2
  name: 'ten'
}
11=>###Navigate to C:\tensorflow1\models\research\object_detection\samples\configs and copy the faster_rcnn_inception_v2_pets.config file into the \object_detection\training directory. Then, open the file with a text editor. There are several changes to make to the .config file, mainly changing the number of classes and examples, and adding the file paths to the training data.

12=>###faster_rcnn_inception_v2_pets.config file.
Line 9. Change num_classes to the number of different objects you want the classifier to detect. For the above basketball, shirt, and shoe detector, it would be num_classes : 3 .

Line 106. Change fine_tune_checkpoint to:

fine_tune_checkpoint : "C:/tensorflow1/models/research/object_detection/faster_rcnn_inception_v2_coco_2018_01_28/model.ckpt"
Lines 123 and 125. In the train_input_reader section, change input_path and label_map_path to:

input_path : "C:/tensorflow1/models/research/object_detection/train.record"
label_map_path: "C:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"
Line 130. Change num_examples to the number of images you have in the \images\test directory.

Lines 135 and 137. In the eval_input_reader section, change input_path and label_map_path to:

input_path : "C:/tensorflow1/models/research/object_detection/test.record"
label_map_path: "C:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"

13=>###python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config

14=>python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training/model.ckpt-XXXX --output_directory inference_graph