Tensorflow,Python,对象检测API,Google Colab

问题描述 投票:1回答:1

我正在尝试在google colab上安装tensorflow对象检测。我执行了GitHub上给出的步骤。但是,现在当我尝试测试安装时,即当我运行“!python3 object_detection / builders / model_builder_test.py”时,我遇到了问题:AttributeError:模块'tensorflow'没有属性'contrib' 。这是我的代码段:

%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

它在google colab上。我收到错误消息:

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'
python-3.x tensorflow object-detection tensorflow2.0 object-detection-api
1个回答
0
投票
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
© www.soinside.com 2019 - 2024. All rights reserved.