Skip to content

Instantly share code, notes, and snippets.

@xiaxx244
Forked from douglasrizzo/tf_obj_tutorial.md
Created July 23, 2019 15:33
Show Gist options
  • Save xiaxx244/e9a0c3e01cd423d2eafa5d2ab6c54c8b to your computer and use it in GitHub Desktop.
Save xiaxx244/e9a0c3e01cd423d2eafa5d2ab6c54c8b to your computer and use it in GitHub Desktop.
TensorFlow Object Detection Model Training

TensorFlow Object Detection Model Training

This is a summary of this nice tutorial.

Prerequisites

  1. Install TensorFlow.
  2. Download the TensorFlow models repository.

Annotating images and serializing the dataset

All the scripts mentioned in this section receive arguments from the command line and have help messages through the -h/--help flags. Also check the README from the repo they come from to get more details, if needed.

  1. Install labelImg. This is a Python package, which means you can install it via pip, but the one from GitHub is better. It saves annotations in the PASCAL VOC format.
  2. Annotate your dataset using labelImg.
  3. Use this script to convert the XML files generated by labelImg into a single CSV file.
  4. Use this script to separate the CSV file into two, one with training examples and one with evaluation examples. Images will be selected randomly and there are options to stratify examples by class, making sure that objects from all classes are pesent in both datasets. The usual proportions are 75% to 80% of the annotated objects used for training and the rest for the evaluation dataset.
  5. Use this script to convert the two CSV files (eg. train.csv and eval.csv) into TFRecord files (eg. train.record and eval.record), the a serialized data format that TensorFlow is most familiar with. You'll need a label map for your classes in order to complete this step (some examples). If you don't have one yet, you can generate one from your original CSV file with this script.

Choosing a neural network and preparing the training pipeline...

  1. Download one of the neural network models provided in this page. The ones trained in the COCO dataset are the best ones, since they were also trained on objects.
  2. Provide a training pipeline, which is a config file that usually comes in the tar.gz file downloaded in the previous step. If they don’t come in the tar.gz, they can be found here (they need some tweaking before using, for example, changing number of classes). You can find a tutorial on how to create your own here.
    • The pipeline config file has some fields that must be adjusted before training is started. Its header describes which ones. Usually, they are the fields that point to the label map, the training and evaluation directories and the neural network checkpoint. In case you downloaded one of the models provided in this page, you should untar the tar.gz file and point the checkpoint path inside the pipeline config file to the "untarred" directory of the model (see this answer for help).
    • You should also check the number of classes. COCO has 90 classes, but your problem may have more or less.
    • There are additional parameters that may affect how much RAM is consumed by the training process, as well as the quality of the training, but I won't go over those here.

Training the network

  1. Train the model. This is how you do it locally. Optional: in order to check training progress, TensorBoard can be started pointing its --logdir to the --model_dir of object_detection/model_main.py.
  2. Export the network, like this.
  3. Use the exported .pb in your object detector.

Tips

In the data augmentation section of the training pipeline, some options can be added or removed to try and make the training better. Some of the options are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment