In this post, we’re going to train machine learning models capable of localizing and identifying multiple objects in an image. You’ll need to install TensorFlow and you’ll need to understand how to use the command line.

Tensorflow Object Detection API


The TensorFlow Object Detection API built on top of TensorFlow that makes it easy to construct, train and deploy object detection models.

This post walks through the steps required to train an object detection model locally.

1. Clone Repository


you can download directly ZIP file.

2.Installation


Tensorflow Object Detection API depends on the following libraries.

  • Protobuf 2.6
  • Pillow 1.0
  • Lxml

1.Protobufs to configure model and training parameters. Before the framework can be used, the Protobuf libraries must be compiled. This should be done by running the following command from the tensorflow/models directory:

Add Libraries to PYTHONPATH

When running locally, the tensorflow/models/ and slim directories should be appended to PYTHONPATH. This can be done by running the following from tensorflow/models/:

Note: This command needs to run from every new terminal you start. If you wish to avoid running this manually, you can add it as a new line to the end of your ~/.bashrc file.

Testing the Installation

You can test that you have correctly installed the Tensorflow Object Detection API by running the following command:

Above command generate following output.

Install Object Detection API

3.Preparing Inputs


Tensorflow Object Detection API reads data using the TFRecord file format. Two sample scripts (create_pascal_tf_record.py and create_pet_tf_record.py) are provided to convert dataset to TFRecords.

Directory Structure for Training input data

  • To prepare the input file for the sample scripts you need to consider two things. Firstly, you need an RGB image which is encoded as jpg or png and secondly, you need a list of bounding boxes (xmin, ymin, xmax, ymax) for the image and the class of the object in the bounding box.
  • Here is a subset of the pet image data set that I collected in images folder:

 

Afterward, labeled them manually with LabelImg. LabelImg is a graphical image annotation tool that is written in Python. It’s super easy to use and the annotations are saved as XML files.Save image annotations xml in /annotations/xmls folder.

Image Annotation

Create trainval.txt in annotations folder which content name of the images without extension.Use the following command to generate trainval.txt.

Label Maps

Each dataset is required to have a label map associated with it. This label map defines a mapping from string class names to integer class Ids.Label maps should always start from id 1.Create label.pbtxt file with the following label map:

Generating the Pet TFRecord files.

Run the following commands.

You should end up with two TFRecord files named    pet_train.record and pet_val.record in the tensorflow/modelsdirectory.

4.Training the model


After creating the required input file for the API, Now you can train your model.For training, you need the following command:

An object detection training pipeline also provide sample config files on the repo. For my training, I used ssd_mobilenet_v1_pets.config basis. I needed to adjust the num_classes to one and also set the path (PATH_TO_BE_CONFIGURED) for the model checkpoint, the train, and test data files as well as the label map. In terms of other configurations like the learning rate, batch size and many more, I used their default settings.

5.Running the Evaluation Job


Evaluation is run as a separate job. The eval job will periodically poll the training directory for new checkpoints and evaluate them on a test dataset. The job can be run using the following command:

where ${PATH_TO_YOUR_PIPELINE_CONFIG} points to the pipeline config, ${PATH_TO_TRAIN_DIR} points to the directory in which training checkpoints were saved (same as the training job) and ${PATH_TO_EVAL_DIR} points to the directory in which evaluation events will be saved. As with the training job, the eval job run until terminated by default.

6.Running TensorBoard


Progress for training and eval jobs can be inspected using TensorBoard. If using the recommended directory structure, TensorBoard can be run using the following command:

where ${PATH_TO_MODEL_DIRECTORY} points to the directory that contains the train and eval directories. Please note it may take TensorBoard a couple minutes to populate with data.

7.Exporting the Tensorflow Graph


After your model has been trained, you should export it to a Tensorflow graph proto. First, you need to identify a candidate checkpoint to export. The checkpoint will typically consist of three files in pet folder:

  1.  model.ckpt-${CHECKPOINT_NUMBER}.data-00000-of-00001
  2. model.ckpt-${CHECKPOINT_NUMBER}.index
  3. model.ckpt-${CHECKPOINT_NUMBER}.meta

Run the following command to export Tensorflow graph.Change the checkpoint number.

Related Post

TenserFlow Lite

Train Image classifier with TensorFlow

Android TensorFlow Machine Learning

14 thoughts on “Train your Object Detection model locally with TensorFlow”

  1. i went through steps descibed by you but when i run
    python create_pet_tf_record.py –data_dir=/home/atul/webdemo/object_detection/models/car/images –output dir=/home/atul/webdemo/object_detection/models/car/output

    it throws me error-
    tensorflow.python.framework.errors_impl.NotFoundError: /home/atul/webdemo/object_detection/models/car/images/annotations/trainval.txt

    help me to resolve this

  2. Thank you very much for your explanation
    I’ve tried your tutorial and I got some error when I trained the data
    it still worked until several steps (mine = 193 step) but after that I got the errors and I need help to resolve this

    or is there any device requirement to train our data using tensorflow object detection API since I am using GTX 950M 2GB?

  3. After running the following command:
    # From the tensorflow/models/ directory
    python object_detection/create_pet_tf_record.py
    –label_map_path=object_detection/label.pbtxt
    –data_dir=pwd
    –output_dir=`pwd

    I am getting error:
    Traceback (most recent call last):
    File “object_detection/create_pet_tf_record.py”, line 213, in
    tf.app.run()
    File “/usr/lib/python2.7/site-packages/tensorflow/python/platform/app.py”, line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
    File “object_detection/create_pet_tf_record.py”, line 208, in main
    image_dir, train_examples)
    File “object_detection/create_pet_tf_record.py”, line 177, in create_tf_record
    tf_example = dict_to_tf_example(data, label_map_dict, image_dir)
    File “object_detection/create_pet_tf_record.py”, line 120, in dict_to_tf_example
    class_name = get_class_name_from_filename(data[‘filename’])
    File “object_detection/create_pet_tf_record.py”, line 61, in get_class_name_from_filename
    return match.groups()[0]
    AttributeError: ‘NoneType’ object has no attribute ‘groups’

    Please can you help

  4. In label.pbtxt for multiple classes is this way the file should be shaped:

    item {
    id: 1
    name: ‘Dog’,
    id:2
    name:’Cat’
    }

  5. model checkpoint file not created , after running : object_detection/create_pet_tf_record.py
    –getting an output as :
    if not xml:
    wheelchair_0.jpg

  6. Following instructions to create exactly pet_train.record and pet_val.record files, you should change ‘flags.faces_only’ variable to False in ‘create_pet_tf_record.py’ file.
    Instead of
    flags.DEFINE_boolean(‘faces_only’, True, ‘If True, generates bounding boxes ‘
    ‘for pet faces. Otherwise generates bounding boxes (as ‘
    ‘well as segmentations for full pet bodies). Note that ‘
    ‘in the latter case, the resulting files are much larger.’)
    It should be
    flags.DEFINE_boolean(‘faces_only’, False, ‘If True, generates bounding boxes ‘
    ‘for pet faces. Otherwise generates bounding boxes (as ‘
    ‘well as segmentations for full pet bodies). Note that ‘
    ‘in the latter case, the resulting files are much larger.’)

  7. Hi Vadim,

    I always see the following log when I train my model. I didn’t see any loss and increment the global step value. Do you know what I did wrong?

    INFO:tensorflow:Restoring parameters from ssd_mobilenet_v1_coco_11_06_2017/model.ckpt
    INFO:tensorflow:Starting Session.
    INFO:tensorflow:Saving checkpoint to path training/model.ckpt
    INFO:tensorflow:Starting Queues.
    INFO:tensorflow:global_step/sec: 0
    INFO:tensorflow:global_step/sec: 0

  8. Hi,

    After running this command:
    # From the tensorflow/models/ directory
    python object_detection/train.py
    –logtostderr
    –pipeline_config_path=/tensorflow/models/object_detection/samples/configs/ssd_mobilenet_v1_pets.config
    –train_dir=/tensorflow/models/pet

    I am getting this:
    WARNING:tensorflow:From /home/bruno/Desktop/Entrevista/final/models/research/object_detection/trainer.py:228: create_global_step (from tensorflow.contrib.framework.python.ops.variables) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please switch to tf.train.create_global_step
    INFO:tensorflow:depth of additional conv before box predictor: 0
    INFO:tensorflow:depth of additional conv before box predictor: 0
    INFO:tensorflow:depth of additional conv before box predictor: 0
    INFO:tensorflow:depth of additional conv before box predictor: 0
    INFO:tensorflow:depth of additional conv before box predictor: 0
    INFO:tensorflow:depth of additional conv before box predictor: 0
    INFO:tensorflow:Summary name /clone_loss is illegal; using clone_loss instead.
    WARNING:tensorflow:From /home/bruno/.local/lib/python3.5/site-packages/tensorflow/contrib/slim/python/slim/learning.py:736: Supervisor.__init__ (from tensorflow.python.training.supervisor) is deprecated and will be removed in a future version.
    Instructions for updating:
    Please switch to tf.train.MonitoredTrainingSession
    2018-02-12 00:23:04.293917: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
    INFO:tensorflow:Restoring parameters from pet/model.ckpt-0
    INFO:tensorflow:Starting Session.
    INFO:tensorflow:Saving checkpoint to path pet/model.ckpt
    INFO:tensorflow:Starting Queues.
    INFO:tensorflow:global_step/sec: 0
    INFO:tensorflow:Recording summary at step 0.
    INFO:tensorflow:global step 1: loss = 24.6861 (73.742 sec/step)
    INFO:tensorflow:global_step/sec: 0.00870801
    INFO:tensorflow:Recording summary at step 1.
    INFO:tensorflow:global step 2: loss = 19.1530 (196.682 sec/step)
    INFO:tensorflow:global_step/sec: 0.00592479
    INFO:tensorflow:global step 3: loss = 16.5035 (89.268 sec/step)
    INFO:tensorflow:global_step/sec: 0.00968654
    finished by force

    help please!
    Thanks

  9. how to train 2 classes using the same method?
    what are the changes need to be done?
    how the label.pbtxt will be written?
    is both images need to be collected in 1 folder?

Leave a Reply