Train your Object Detection model locally with TensorFlow

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 is an open source framework 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.Cloning an Object Detection API repository

or you can download directly ZIP file.


Tensorflow Object Detection API depends on the following libraries.

  • Protobuf 2.6
  • Protobuf 2.6
  • Pillow 1.0
  • Lxml

The Tensorflow Object Detection API uses 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:

  • Jupyter notebook

  • Matplotlib

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 ( and 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.
  • I scraped 200 pet from Google Images.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.

Running the Evaluation Job

Evaluation is run as a separate job. The eval job will periodically poll the train 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.

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.

5.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 check point number.

Related Post

Introduction TensorFlow Machine Learning Library

TenserFlow Lite

Train Image classifier with TensorFlow

Android TensorFlow Machine Learning

11 Replies to “Train your Object Detection model locally with TensorFlow”

  1. i went through steps descibed by you but when i run
    python –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/

    I am getting error:
    Traceback (most recent call last):
    File “object_detection/”, line 213, in
    File “/usr/lib/python2.7/site-packages/tensorflow/python/platform/”, line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
    File “object_detection/”, line 208, in main
    image_dir, train_examples)
    File “object_detection/”, line 177, in create_tf_record
    tf_example = dict_to_tf_example(data, label_map_dict, image_dir)
    File “object_detection/”, line 120, in dict_to_tf_example
    class_name = get_class_name_from_filename(data[‘filename’])
    File “object_detection/”, 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’,

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

  6. Following instructions to create exactly pet_train.record and pet_val.record files, you should change ‘flags.faces_only’ variable to False in ‘’ 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.’)

Leave a Reply

Your email address will not be published. Required fields are marked *