TensorFlow's Object Detection API

Revision as of 10:10, 11 April 2020 by (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


[edit] Install TensorFlow's GPU Object Detection API on Windows

From document: Link to PDF

From site: TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10

[edit] Install Anaconda


Download: https://www.anaconda.com/download/

[edit] Install TensorFlow GPU


  • Nvidia GPU (GTX 650 or newer)
  • CUDA Toolkit v9.0
  • CuDNN v7.0.5
  • Anaconda with Python 3.7 (Optional)

[edit] Install CUDA Toolkit


Follow this link to download and install CUDA Toolkit v9.0.

[edit] Install CUDNN


  • Go to https://developer.nvidia.com/rdp/cudnn-download
  • Create a user profile if needed and log in
  • Select cuDNN v7.0.5 (Feb 28, 2018), for CUDA 9.0
  • Download cuDNN v7.0.5 Library for Windows 10
  • Extract the contents of the zip file (i.e. the folder named cuda) inside <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v9.0\,
    where <INSTALL_PATH> points to the installation directory specified during the installation of the CUDA Toolkit.
    By default <INSTALL_PATH> = C:\Program Files.

[edit] Environment Setup


Go to Start and Search “environment variables”

  • Click the Environment Variables button
  • Click on the Path system variable and select edit
  • Add the following paths:
    • <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin
    • <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v9.0\libnvvp
    • <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v9.0\extras\CUPTI\libx64
    • <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v9.0\cuda\bin

[edit] Create a new Conda virtual environment

  • Open a new Anaconda/Command Prompt window
  • Type the following command:
conda create -n tensorflow_gpu3 pip python=3.5
  • Now lets activate the newly created virtual environment by running the following in the Anaconda Promt window:
conda activate tensorflow_gpu3

[edit] Install TensorFlow GPU for Python

  • Type the following on the command line:
(tensorflow1_gpu3) C:> pip install --ignore-installed --upgrade tensorflow-gpu==1.9

[edit] Test your Installation

  • Start a new Python interpreter session by running:
(tensorflow1_gpu3) C:> python
  • type:
>>> import tensorflow as tf
  • Then run the following:
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
  • Finally run the following:
>>> print(sess.run(hello))
b'Hello, TensorFlow!'

[edit] TensorFlow Models Installation

Now that you have installed TensorFlow, it is time to install the models used by TensorFlow.

[edit] Install Prerequisites

Prerequisite packages:

Name Version
pillow 5.4.1-py36hdc69c19_0
lxml 4.3.1-py36h1350720_0
jupyter 1.0.0-py36_7
matplotlib 3.0.2-py36hc8f65d3_0
opencv 3.4.2-py36h40b0b35_0
setuptools 39.1.0
(tensorflow1_gpu3) C:> pip install pillow lxml jupyter matplotlib opencv-python pandas

[edit] Downloading the TensorFlow Models

(tensorflow1_gpu3) C:> cd C:\tensorflow3

Clone Tensorflow model with git:

(tensorflow1_gpu3) C:\tensorflow3> git clone https://github.com/tensorflow/models.git
(tensorflow1_gpu3) C:\tensorflow3> cd models
(tensorflow1_gpu3) C:\tensorflow3\models> git checkout 4b566d4e800ff82579eda1f682f9ce7aa8792ea8

[edit] Compile Protobufs and run setup.py

(tensorflow1_gpu3) C:> cd C:\tensorflow3\models\research


(tensorflow1_gpu3) C:\tensorflow3\models\research> 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

This creates a name_pb2.py file from every name.proto file in the \object_detection\protos folder.

Run the following commands from the C:\tensorflow1\models\research directory:

(tensorflow1_gpu3) C:\tensorflow3\models\research> python setup.py build
(tensorflow1_gpu3) C:\tensorflow3\models\research> python setup.py install

[edit] Adding necessary Environment Variables


The following folder must be added to your PYTHONPATH environment variable (See Environment Setup):

  • <PATH_TO_TF>\TensorFlow\models\research\object_detection
  • <PATH_TO_TF>\TensorFlow\models\research
  • <PATH_TO_TF>\TensorFlow\models\research\slim

[edit] Test TensorFlow setup to verify it works

The TensorFlow Object Detection API is now all set up to use pre-trained models for object detection, or to train a new one.

You can test it out and verify your installation is workin

(tensorflow_gpu3) C:\tensorflow1\models\research\object_detection> jupyter notebook object_detection_tutorial.ipynb

[edit] Download the Faster-RCNN-Inception-V2-COCO model from TensorFlow's model zoo

Download the model here

Extract the faster_rcnn_inception_v2_coco_2018_01_28 folder to the C:\tensorflow3\models\research\object_detection folder.

[edit] Download tutorial's repository from GitHub

Tutorial's repository

And extract all the contents directly into the C:\tensorflow3\models\research\object_detection directory.

If you want to train your own object detector, delete the following files (do not delete the folders):

  • All files in \object_detection\images\train
  • All files in \object_detection\images\test
  • The “test_labels.csv” files in \object_detection\images
  • The “train_labels.csv” files in \object_detection\images
  • All files in \object_detection\training
  • All files in \object_detection\inference_graph

[edit] Gather and Label Pictures

Now that the TensorFlow Object Detection API is all set up and ready to go, we need to provide the images it will use to train a new detection classifier.

[edit] Gather Pictures

TensorFlow needs hundreds of images of an object to train a good detection classifier.

To train a robust classifier, the training images should have random objects in the image along with the desired objects, and should have a variety of backgrounds and lighting conditions.

There should be some images where the desired object is partially obscured, overlapped with something else, or only halfway in the picture.

Make sure the images aren’t too large. They should be less than 200KB each, and their resolution shouldn’t be more than 720x1280.

The larger the images are, the longer it will take to train the classifier.

You can use the resizer.py script in this repository to reduce the size of the images.

After you have all the pictures you need, move:

  • 20% of them to the \object_detection\images\test directory
  • 80% of them to the \object_detection\images\train directory.

Make sure there are a variety of pictures in both the \test and \train directories

[edit] Label Pictures

LabelImg is a great tool for labeling images, and its GitHub page has very clear instructions on how to install and use it.

LabelImg GitHub link

LabelImg download link

LabelImg saves a .xml file containing the label data for each image.

These .xml files will be used to generate TFRecords, which are one of the inputs to the TensorFlow trainer.

Once you have labeled and saved each image, there will be one .xml file for each image in the \test and \train directories.

[edit] Generate Training Data

With the images labeled, it’s time to generate the TFRecords that serve as input data to the TensorFlow training model.

This tutorial uses the xml_to_csv.py and generate_tfrecord.py scripts.

First, the image .xml data will be used to create .csv files containing all the data for the train and test images.

From the \object_detection folder, issue the following command in the Anaconda command prompt:

(tensorflow_gpu3) C:\tensorflow3\models\research\object_detection> python xml_to_csv.py

This creates a train_labels.csv and test_labels.csv file in the \object_detection\images folder.

Next, open the generate_tfrecord.py file in a text editor.

Replace the label map starting at line 31 with your own label map, where each object is assigned an ID number.

# TO-DO replace this with label map
def class_text_to_int(row_label):
   if row_label == 'blue_cube':
       return 1
   elif row_label == 'orange_cylinder':
       return 2
       return 0

Then, generate the TFRecord files by issuing these commands from the \object_detection folder:

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

[edit] Create Label Map and Configure Training

The last thing to do before training is to create a label map and edit the training configuration file.

[edit] Label map

The label map tells the trainer what each object is by defining a mapping of class names to class ID numbers.

Create a new file and save it as labelmap.pbtxt in the C:\tensorflow1\models\research\object_detection\training folder.

In the text editor, copy or type in the label map in the format below:

item {
  id: 1
  name: 'blue_cube'

item {
  id: 2
  name: 'orange_cylinder'

The label map ID numbers should be the same as what is defined in the generate_tfrecord.py file.

[edit] Configure training

Finally, the object detection training pipeline must be configured. It defines which model and what parameters will be used for training.

Navigate to C:\tensorflow3\models\research\object_detection\samples\configs and copy the faster_rcnn_inception_v2_pets.config file into the \object_detection\training directory.

Make the following changes:

  • Line 9. Change num_classes to the number of different objects you want the classifier to detect. In our case It will be num_classes : 2.
  • Line 106. Change fine_tune_checkpoint to:
    • fine_tune_checkpoint : "C:/tensorflow3/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:/tensorflow3/models/research/object_detection/train.record"
    • label_map_path: "C:/tensorflow3/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:/tensorflow3/models/research/object_detection/test.record"
    • label_map_path: "C:/tensorflow3/models/research/object_detection/training/labelmap.pbtxt"

[edit] Run the Training

From the \object_detection directory, issue the following command to begin training:

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

[edit] View the progress of the training

You can view the progress of the training job by using TensorBoard.

Open a new instance of Anaconda Prompt, activate the tensorflow virtual environment, change to the C:\tensorflow3\models\research\object_detection directory, and issue the following command:

(tensorflow_gpu3) C:\tensorflow1\models\research\object_detection>tensorboard --logdir=training

This will create a webpage on your local machine at YourPCName:6006, which can be viewed through a web browser.

The TensorBoard page provides information and graphs that show how the training is progressing.

One important graph is the Loss graph, which shows the overall loss of the classifier over time.

[edit] Export Inference Graph

Now that training is complete, the last step is to generate the frozen inference graph (.pb file).

From the \object_detection folder, issue the following command, where “XXXX” in “model.ckpt-XXXX” should be replaced with the highest-numbered .ckpt file in the training folder:

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

This creates a frozen_inference_graph.pb file in the \object_detection\inference_graph folder. The .pb file contains the object detection classifier.

[edit] Use Your Newly Trained Object Detection Classifier!

To test your object detector, move a picture of the object or objects into the \object_detection folder, and change the IMAGE_NAME variable in the Object_detection_image.py to match the file name of the picture.

To run any of the scripts, type “idle” in the Anaconda Command Prompt (with the “tensorflow1” virtual environment activated) and press ENTER.

This will open IDLE, and from there, you can open any of the scripts and run them.

Personal tools
MediaWiki Appliance - Powered by TurnKey Linux