Training TensorFlow Object Detection Models
Let's take a look at training TensorFlow object detection models as well as explore a two-minute video for a quick demo.
Join the DZone community and get the full member experience.Join For Free
tensorflow object detection is a powerful technology that can recognize different objects in images, including their positions. the trained object detection models can be run on mobile and edge devices to execute predictions very quickly. i've used this technology to build a demo where anki overdrive cars and obstacles are detected via an ios app. when obstacles are detected, the cars are stopped automatically.
check out the short video (only 2 mins) for a quick demo.
this picture shows the track with two cars and a phone and the ios app which draws rectangles around the objects.
i have open sourced the code on github. the repo includes two parts:
- trained deep learning model to recognize items on anki overdrive tracks with an ios app
- documentation how to train tensorflow object detection models
the instructions in the readme are pretty detailed. below is a quick overview of the main steps that you can follow to train models to detect your own objects.
1) development environment setup
first, you need to download the trained mobilenet model, which is an optimized model for mobile devices. rather than training a new model from scratch, transfer learning is used. basically, the last layer of the neural network is replaced with your own objects.
to make the setup of the development environment as simple as possible, docker containers are provided.
2) labelimg of images
while for visual recognition models only images and the names of the categories need to be provided, the labeling for object detection is more sophisticated. in addition to the list of objects you also need to provide their positions, i've used labelimg to create the labels and rectangles as shown in the screenshot.
from what i've read, these are some best practices on how to create the training data:
- take/get at least 50 pictures per object.
- use a rather small resolution, for example, 640 x 480.
- use different sizes of your objects and different angles.
- use pictures that have multiple objects in them.
- when marking the objects with labelimg, put the rectangles as closely as possible around the objects.
the images and the annotations exported from the labelimg tool need to be converted into a certain format (tfrecords) which tensorflow object detection expects.
3) training of the model
trainings with just a few training steps can be run locally . this is useful, for example, if you want to test whether the code runs. when you can use a gpu, trainings with many steps should also be possible locally, but i haven't tried it.
i've used kubernetes on the ibm cloud to run the training. the 17.000 training steps took roughly 1.5 days. in order to also leverage gpus, i want to look at ffdl . i'll write about it when i find out more. if you want to use the ibm cloud, you can get a free account .
after the training, a frozen graph of the model needs to be created. the repo contains a script and a docker container to do this.
4) usage of the model in notebooks and apps
the training model can be tested with a python notebook . the screenshot shows the detected objects in a test image.
connecting the cars and the ios app to the watson iot platform
in order to stop the anki overdrive cars when phones are put on the track, you need to set up additional components, especially the node.js controller and the watson iot platform . in order to do this, follow the instructions from my project node-mqtt-for-anki-overdrive .
here is a diagram of the high-level architecture:
the next screenshot shows a simple node-red flow that stops the cars when obstacles are detected.
Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.