In this tutorial I will try to demonstrate how to run a trained Tensorflow object detection model for "inference", or in other words, to make predictions on new images. If you follow the steps in the previous post on this topic, you will have successfully run the training for an object detector.
The code in this post can be found on my GitHub.
In the previous example; we had set out a folder structure to store all the various parts of the model (data, config files, record files etc) in order to train it. I will maintain consistency with that post and so your trained model checkpoints should have appeared inside the `training/` folder. This is the structure we were using;
Once training is complete the training folder will be full of a load of new files. The files we are interested in are those marked as checkpoints, with the extension
where the number represents the number of iterations in the training. Generally, we want to go for the file with the highest number.
So what is an iteration (quick recap)?
A CNN works by passing "training examples" forward and backward through the network one at a time. In our case a training example is a single image with objects in. After a number of training examples have been passed, the gradients and loss are updated.
A lot of the CNN code in keras displays training in terms of "epochs"; one epoch being the training on the full dataset. Each epoch is split into a number of batches (where the user determines the batch size, usually 32 or 64); each time a single batch is passed through the training this is a single iteration.
So iterations are just the number of epochs multiplied by the (total number of images over the batch size). For example, if I have a batch size of 32 and 320 images then a single epoch is 10 iterations. Often the object detectors run into the hundreds of thousands of iterations.
Anyway; the trained model checkpoints should be available for the next step so make sure you have them. We are going to convert the checkpoint into a saved model format which can then be used for inference. Luckily, there is a script already in the API to do this which you can call from the terminal;
Now we need to convert the model checkpoint into an "inference graph". Open the Anaconda Prompt
These steps in words are:
Move to the root directory (object_detector_example in the folder structure above).
Shift to the environment we created to work with Tensorflow in the previous post.
Create a directory to store the inference graph.
Add the slim subfolder from the Tensorflow API to the path.
Now run the following command, replacing the folder names as appropriate
You should see plenty of output, and ultimately a folder called saved_model should appear in the inference folder (alongside some other files and gubbins). If you want to share your model; you only need to share this single folder, as we will see.
Now we have prepared the model for inference lets check out a Jupyter notebook that will give you a quick and easy way to apply the CNN to some images. This file comes from my GitHub; and is provided alongside a pre-trained model which you can download to test the notebook with (beware when downloading, the file is over 100Mb). This pre-trained model is designed simply to spot mobile phones in X-ray images.
The loading of the inference graph is done in one line;
Now this function can be used to make predictions using the model.
I have provided some simple code which wraps around this function so you can load jpg images and see the result of the inference.
All of this can be run by downloading the relevant folder from my GitHub repo.
An example inference applied to one of the images supplied with the code
That's all! Hopefully with this information you can get your own detectors running; or if you've encountered a bug hopefully looking at all the steps above (and the previous posts in this series) will help you fix it.
Comments