Tensorflow Object Detection API Tutorial Readthedocs Io en Latest
Tensorflow Object Detection API Tutorial Readthedocs Io en Latest
Lyudmil Vladimirov
1 Installation 3
1.1 General Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Anaconda Python 3.7 (Optional) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Install Anaconda Python 3.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.2 Create a new Anaconda virtual environment . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.3 Activate the Anaconda virtual environment . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 TensorFlow Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.1 Install the TensorFlow PIP package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.2 Verify your Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.3 GPU Support (Optional) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.3.1 Install CUDA Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.3.2 Install CUDNN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.3.3 Environment Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3.3.4 Update your GPU drivers (Optional) . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3.3.5 Verify the installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 TensorFlow Object Detection API Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4.1 Downloading the TensorFlow Model Garden . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4.2 Protobuf Installation/Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4.3 COCO API installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4.4 Install the Object Detection API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.5 Test your Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.6 Try out the examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
i
3 Examples 37
3.1 Detect Objects Using Your Webcam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.1.1 Create the data directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.1.2 Download the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.1.3 Load the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.1.4 Load label map data (for plotting) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.1.5 Define the video stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.1.6 Putting everything together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.2 Object Detection From TF1 Saved Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.2.1 Download the test images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.2.2 Download the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.2.3 Download the labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.2.4 Load the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.2.5 Load label map data (for plotting) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.2.6 Putting everything together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.3 Object Detection From TF2 Saved Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.3.1 Download the test images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.3.2 Download the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.3.3 Download the labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.3.4 Load the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.3.5 Load label map data (for plotting) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.3.6 Putting everything together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.4 Object Detection From TF2 Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.4.1 Download the test images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.4.2 Download the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.4.3 Download the labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.4.4 Load the model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.4.5 Load label map data (for plotting) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.4.6 Putting everything together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4 Common issues 59
4.1 Python crashes - TensorFlow GPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.2 Cleaning up Nvidia containers (TensorFlow GPU) . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.3 “WARNING:tensorflow:Entity <bound method X of <Y>> could not be transformed . . . ” . . . 60
4.4 “AttributeError: module ‘google.protobuf.descriptor’ has no attribute ‘_internal_create_key” . . . . . 61
4.5 “TypeError: Expected Operation, Variable, or Tensor, got level_5” . . . . . . . . . . . . . . . . . . . 61
ii
TensorFlow setup Documentation
Important: This tutorial is intended for TensorFlow 2.2, which (at the time of writing this tutorial) is the latest stable
version of TensorFlow 2.x.
A version for TensorFlow 1.14 can be found here.
This is a step-by-step tutorial/guide to setting up and using TensorFlow’s Object Detection API to perform, namely,
object detection in images/video.
The software tools which we shall use throughout this tutorial are listed in the table below:
CONTENTS 1
TensorFlow setup Documentation
2 CONTENTS
CHAPTER
ONE
INSTALLATION
• In contrast to TensorFlow 1.x, where different Python packages needed to be installed for one to run TensorFlow
on either their CPU or GPU (namely tensorflow and tensorflow-gpu), TensorFlow 2.x only requires
that the tensorflow package is installed and automatically checks to see if a GPU can be successfully regis-
tered.
Although having Anaconda is not a requirement in order to install and use TensorFlow, I suggest doing so, due to it’s
intuitive way of managing packages and setting up new virtual environments. Anaconda is a pretty useful tool, not
only for working with TensorFlow, but in general for anyone working in Python, so if you haven’t had a chance to
work with it, now is a good chance.
Windows
• Go to https://www.anaconda.com/products/individual and click the “Download” button
• Download the Python 3.7 64-Bit Graphical Installer or the 32-Bit Graphical Installer installer, per your system
requirements
• Run the downloaded executable (.exe) file to begin the installation. See here for more details
• (Optional) In the next step, check the box “Add Anaconda3 to my PATH environment variable”. This will
make Anaconda your default Python distribution, which should ensure that you have the same default Python
distribution across all editors.
Linux
• Go to https://www.anaconda.com/products/individual and click the “Download” button
• Download the Python 3.7 64-Bit (x86) Installer
• Run the downloaded bash script (.sh) file to begin the installation. See here for more details.
• When prompted with the question “Do you wish the installer to prepend the Anaconda<2 or 3> install location
to PATH in your /home/<user>/.bashrc ?”, answer “Yes”. If you enter “No”, you must manually add the path to
Anaconda or conda will not work.
3
TensorFlow setup Documentation
• The above will create a new virtual environment with name tensorflow
Important: The term Terminal will be used to refer to the Terminal of your choice (e.g. Command Prompt, Power-
shell, etc.)
• Activating the newly created virtual environment is achieved by running the following in the Terminal window:
• Once you have activated your virtual environment, the name of the environment should be displayed within
brackets at the beggining of your cmd path specifier, e.g.:
(tensorflow) C:\Users\sglvladi>
Important: Throughout the rest of the tutorial, execution of any commands in a Terminal window should be done
after the Anaconda virtual environment has been activated!
• Once the above is run, you should see a print-out similar to the one bellow:
4 Chapter 1. Installation
TensorFlow setup Documentation
˓→your machine.
˓→gpu for how to download and setup the required libraries for your
˓→platform.
˓→matrix:
Although using a GPU to run TensorFlow is not necessary, the computational gains are substantial. Therefore, if your
machine is equipped with a compatible CUDA-enabled GPU, it is recommended that you follow the steps listed below
to install the relevant libraries necessary to enable TensorFlow to make use of your GPU.
By default, when TensorFlow is run it will attempt to register compatible GPU devices. If this fails, TensorFlow will
resort to running on the platform’s CPU. This can also be observed in the printout shown in the previous section, un-
der the “Verify the install” bullet-point, where there are a number of messages which report missing library files
(e.g. Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.
dll not found).
In order for TensorFlow to run on your GPU, the following requirements must be met:
Prerequisites
Nvidia GPU (GTX 650 or newer)
CUDA Toolkit v10.1
CuDNN 7.6.5
Windows
• Follow this link to download and install CUDA Toolkit 10.1
• Installation instructions can be found here
Linux
• Follow this link to download and install CUDA Toolkit 10.1 for your Linux distribution.
• Installation instructions can be found here
Windows
• Go to https://developer.nvidia.com/rdp/cudnn-download
• Create a user profile if needed and log in
• Select cuDNN v7.6.5 (Nov 5, 2019), for CUDA 10.1
• Download cuDNN v7.6.5 Library for Windows 10
• Extract the contents of the zip file (i.e. the folder named cuda) inside <INSTALL_PATH>\NVIDIA
GPU Computing Toolkit\CUDA\v10.1\, where <INSTALL_PATH> points to the installation direc-
tory specified during the installation of the CUDA Toolkit. By default <INSTALL_PATH> = C:\Program
Files.
Linux
• Go to https://developer.nvidia.com/rdp/cudnn-download
• Create a user profile if needed and log in
• Select cuDNN v7.6.5 (Nov 5, 2019), for CUDA 10.1
• Download cuDNN v7.6.5 Library for Linux
• Follow the instructions under Section 2.3.1 of the CuDNN Installation Guide to install CuDNN.
6 Chapter 1. Installation
TensorFlow setup Documentation
Windows
• Go to Start and Search “environment variables”
• Click “Edit the system environment variables”. This should open the “System Properties” window
• In the opened window, click the “Environment Variables. . . ” button to open the “Environment Variables” win-
dow.
• Under “System variables”, search for and click on the Path system variable, then click “Edit. . . ”
• Add the following paths, then click “OK” to save the changes:
– <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin
– <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp
– <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\CUPTI\
libx64
– <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v10.1\cuda\bin
Linux
As per Section 7.1.1 of the CUDA Installation Guide for Linux, append the following lines to ~/.bashrc:
If during the installation of the CUDA Toolkit (see Install CUDA Toolkit) you selected the Express Installation option,
then your GPU drivers will have been overwritten by those that come bundled with the CUDA toolkit. These drivers
are typically NOT the latest drivers and, thus, you may wish to update your drivers.
• Go to http://www.nvidia.com/Download/index.aspx
• Select your GPU version to download
• Install the driver for your chosen OS
Important: A new terminal window must be opened for the changes to the Environmental variables
to take effect!!
• Once the above is run, you should see a print-out similar to the one bellow:
˓→matrix:
8 Chapter 1. Installation
TensorFlow setup Documentation
˓→name: GeForce GTX 1070 Ti, pci bus id: 0000:02:00.0, compute
˓→capability: 6.1)
• Notice from the lines highlighted above that the library files are now Successfully opened and a debug-
ging message is presented to confirm that TensorFlow has successfully Created TensorFlow device.
Now that you have installed TensorFlow, it is time to install the TensorFlow Object Detection API.
• Create a new folder under a path of your choice and name it TensorFlow. (e.g. C:\Users\sglvladi\
Documents\TensorFlow).
• From your Terminal cd into the TensorFlow directory.
• To download the models you can either use Git to clone the TensorFlow Models repository inside the
TensorFlow folder, or you can simply download it as a ZIP and extract its contents inside the TensorFlow
folder. To keep things consistent, in the latter case you will have to rename the extracted folder
models-master to models.
• You should now have a single folder named models under your TensorFlow folder, which contains another
4 folders as such:
TensorFlow/
models/
community/
official/
orbit/
research/
...
The Tensorflow Object Detection API uses Protobufs to configure model and training parameters. Before the frame-
work can be used, the Protobuf libraries must be downloaded and compiled.
This should be done as follows:
• Head to the protoc releases page
• Download the latest protoc-*-*.zip release (e.g. protoc-3.12.3-win64.zip for 64-bit Windows)
• Extract the contents of the downloaded protoc-*-*.zip in a directory <PATH_TO_PB> of your choice
(e.g. C:\Program Files\Google Protobuf)
• Add <PATH_TO_PB> to your Path environment variable (see Environment Setup)
• In a new Terminal1 , cd into TensorFlow/models/research/ directory and run the following command:
Important: If you are on Windows and using Protobuf 3.5 or later, the multi-file selection wildcard (i.e *.proto)
may not work but you can do one of the following:
Windows Powershell
Command Prompt
As of TensorFlow 2.x, the pycocotools package is listed as a dependency of the Object Detection API. Ideally,
this package should get installed when installing the Object Detection API as documented in the Install the Object
Detection API section below, however the installation can fail for various reasons and therefore it is simpler to just
install the package beforehand, in which case later installation will be skipped.
Windows
Run the following command to install pycocotools with Windows support:
Note that, according to the package’s instructions, Visual C++ 2015 build tools must be installed and on your path. If
they are not, make sure to install them from here.
Linux
1 NOTE: You MUST open a new Terminal for the changes in the environment variables to take effect.
10 Chapter 1. Installation
TensorFlow setup Documentation
Download cocoapi to a directory of your choice, then make and copy the pycocotools subfolder to the Tensorflow/
models/research directory, as such:
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools <PATH_TO_TF>/TensorFlow/models/research/
Note: The default metrics are based on those used in Pascal VOC evaluation.
• To use the COCO object detection metrics add metrics_set: "coco_detection_metrics" to the
eval_config message in the config file.
• To use the COCO instance segmentation metrics add metrics_set: "coco_mask_metrics" to the
eval_config message in the config file.
Installation of the Object Detection API is achieved by installing the object_detection package. This is done
by running the following commands from within Tensorflow\models\research:
# From within TensorFlow/models/research/
cp object_detection/packages/tf2/setup.py .
python -m pip install .
Note: During the above installation, you may observe the following error:
ERROR: Command errored out with exit status 1:
command: 'C:\Users\sglvladi\Anaconda3\envs\tf2\python.exe' -u -c
˓→'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\sglvladi\\
˓→AppData\\Local\\Temp\\pip-install-yn46ecei\\pycocotools\\setup.py'"'"'; __
˓→file__='"'"'C:\\Users\\sglvladi\\AppData\\Local\\Temp\\pip-install-
˓→yn46ecei\\pycocotools\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"',
˓→open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.
˓→'C:\Users\sglvladi\AppData\Local\Temp\pip-record-wpn7b6qo\install-record.
˓→Users\sglvladi\Anaconda3\envs\tf2\Include\pycocotools'
cwd: C:\Users\sglvladi\AppData\Local\Temp\pip-install-yn46ecei\
˓→pycocotools\
----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\Users\sglvladi\Anaconda3\
˓→envs\tf2\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0]
˓→= '"'"'C:\\Users\\sglvladi\\AppData\\Local\\Temp\\pip-install-yn46ecei\\
˓→pycocotools\\setup.py'"'"'; __file__='"'"'C:\\Users\\sglvladi\\AppData\\
˓→Local\\Temp\\pip-install-yn46ecei\\pycocotools\\setup.py'"'"';
˓→record-wpn7b6qo\install-record.txt' --single-version-externally-managed --
This is caused because installation of the pycocotools package has failed. To fix this have a look at the COCO
API installation section and rerun the above commands.
To test the installation, run the following command from within Tensorflow\models\research:
Once the above is run, allow some time for the test to complete and once done you should observe a printout similar
to the one below:
...
[ OK ] ModelBuilderTF2Test.test_create_ssd_models_from_config
[ RUN ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update
[ OK ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update
[ RUN ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold
[ OK ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold
[ RUN ] ModelBuilderTF2Test.test_invalid_model_config_proto
[ OK ] ModelBuilderTF2Test.test_invalid_model_config_proto
[ RUN ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size
[ OK ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size
[ RUN ] ModelBuilderTF2Test.test_session
[ SKIPPED ] ModelBuilderTF2Test.test_session
[ RUN ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor
[ OK ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor
[ RUN ] ModelBuilderTF2Test.test_unknown_meta_architecture
[ OK ] ModelBuilderTF2Test.test_unknown_meta_architecture
[ RUN ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
[ OK ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
----------------------------------------------------------------------
Ran 20 tests in 68.510s
OK (skipped=1)
12 Chapter 1. Installation
TensorFlow setup Documentation
If the previous step completed successfully it means you have successfully installed all the components necessary to
perform object detection using pre-trained models.
If you want to play around with some examples to see how this can be done, now would be a good time to have a look
at the Examples section.
14 Chapter 1. Installation
CHAPTER
TWO
1. If you have followed the tutorial, you should by now have a folder Tensorflow, placed under
<PATH_TO_TF> (e.g. C:/Users/sglvladi/Documents), with the following directory tree:
TensorFlow/
addons/ (Optional)
labelImg/
models/
community/
official/
orbit/
research/
...
2. Now create a new folder under TensorFlow and call it workspace. It is within the workspace that
we will store all our training set-ups. Now let’s go under workspace and create another folder named
training_demo. Now our directory structure should be as so:
TensorFlow/
addons/ (Optional)
labelImg/
models/
community/
(continues on next page)
15
TensorFlow setup Documentation
3. The training_demo folder shall be our training folder, which will contain all files related to our model
training. It is advisable to create a separate training folder each time we wish to train on a different dataset. The
typical structure for training folders is shown below.
training_demo/
annotations/
exported-models/
images/
test/
train/
models/
pre-trained-models/
README.md
Here’s an explanation for each of the folders/filer shown in the above tree:
• annotations: This folder will be used to store all *.csv files and the respective TensorFlow *.record
files, which contain the list of annotations for our dataset images.
• exported-models: This folder will be used to store exported versions of our trained model(s).
• images: This folder contains a copy of all the images in our dataset, as well as the respective *.xml files
produced for each one, once labelImg is used to annotate objects.
– images/train: This folder contains a copy of all images, and the respective *.xml files, which will
be used to train our model.
– images/test: This folder contains a copy of all images, and the respective *.xml files, which will be
used to test our model.
• models: This folder will contain a sub-folder for each of training job. Each subfolder will contain the training
pipeline configuration file *.config, as well as all files generated during the training and evaluation of our
model.
• pre-trained-models: This folder will contain the downloaded pre-trained models, which shall be used as
a starting checkpoint for our training jobs.
• README.md: This is an optional file which provides some general information regarding the training conditions
of our model. It is not used by TensorFlow in any way, but it generally helps when you have a few training folders
and/or you are revisiting a trained model after some time.
If you do not understand most of the things mentioned above, no need to worry, as we’ll see how all the files are
generated further down.
There exist several ways to install labelImg. Below are 3 of the most common.
1. Open a new Terminal window and activate the tensorflow_gpu environment (if you have not done so already)
2. Run the following command to install labelImg:
labelImg
# or
labelImg [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
Precompiled binaries for both Windows and Linux can be found here .
Installation is the done in three simple steps:
1. Inside you TensorFlow folder, create a new directory, name it addons and then cd into it.
2. Download the latest binary for your OS from here. and extract its contents under Tensorflow/addons/
labelImg.
3. You should now have a single folder named addons/labelImg under your TensorFlow folder, which
contains another 4 folders as such:
TensorFlow/
addons/
labelImg/
models/
community/
official/
orbit/
research/
...
TensorFlow/
addons
labelImg/
models/
community/
official/
orbit/
research/
...
Linux
• Once you have collected all the images to be used to test your model (ideally more than 100 per class), place
them inside the folder training_demo/images.
• Open a new Terminal window.
• Next go ahead and start labelImg, pointing it to your training_demo/images folder.
– If you installed labelImg Using PIP (Recommended):
labelImg <PATH_TO_TF>/TensorFlow/workspace/training_demo/images
• A File Explorer Dialog windows should open, which points to the training_demo/images folder.
• Press the “Select Folder” button, to start annotating your images.
Once open, you should see a window similar to the one below:
I won’t be covering a tutorial on how to use labelImg, but you can have a look at labelImg’s repo for more de-
tails. A nice Youtube video demonstrating how to use labelImg is also available here. What is important is that
once you annotate all your images, a set of new *.xml files, one for each image, should be generated inside your
training_demo/images folder.
Once you have finished annotating your image dataset, it is a general convention to use only part of it for training, and
the rest is used for evaluation purposes (e.g. as discussed in Evaluating the Model (Optional)).
Typically, the ratio is 9:1, i.e. 90% of the images are used for training and the rest 10% is maintained for testing, but
you can chose whatever ratio suits your needs.
Once you have decided how you will be splitting your dataset, copy all training images, together with their corre-
sponding *.xml files, and place them inside the training_demo/images/train folder. Similarly, copy all
testing images, with their *.xml files, and paste them inside training_demo/images/test.
For lazy people like myself, who cannot be bothered to do the above, I have put together a simple script that automates
the above process:
""" usage: partition_dataset.py [-h] [-i IMAGEDIR] [-o OUTPUTDIR] [-r RATIO] [-x]
optional arguments:
-h, --help show this help message and exit
-i IMAGEDIR, --imageDir IMAGEDIR
Path to the folder where the image dataset is stored. If not
˓→specified, the CWD will be used.
-x, --xml Set this flag if you want the xml annotation files to be
˓→processed and copied over.
"""
import os
import re
from shutil import copyfile
import argparse
import math
import random
if not os.path.exists(train_dir):
os.makedirs(train_dir)
if not os.path.exists(test_dir):
os.makedirs(test_dir)
num_images = len(images)
num_test_images = math.ceil(ratio*num_images)
def main():
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument(
'-i', '--imageDir',
help='Path to the folder where the image dataset is stored. If not specified,
˓→the CWD will be used.',
type=str,
default=os.getcwd()
)
parser.add_argument(
'-o', '--outputDir',
help='Path to the output folder where the train and test dirs should be
˓→created. '
default=0.1,
type=float)
parser.add_argument(
'-x', '--xml',
help='Set this flag if you want the xml annotation files to be processed and
˓→copied over.',
action='store_true'
)
args = parser.parse_args()
if args.outputDir is None:
args.outputDir = args.imageDir
(continues on next page)
if __name__ == '__main__':
main()
• Under the TensorFlow folder, create a new folder TensorFlow/scripts, which we can use to store some
useful scripts.
• To make things even tidier, let’s create a new folder TensorFlow/scripts/preprocessing, where we
shall store scripts that we can use to preprocess our training inputs. Below is out TensorFlow directory tree
structure, up to now:
TensorFlow/
addons/ (Optional)
labelImg/
models/
community/
official/
orbit/
research/
...
scripts/
preprocessing/
workspace/
training_demo/
• Click here to download the above script and save it inside TensorFlow/scripts/preprocessing.
• Then, cd into TensorFlow/scripts/preprocessing and run:
# For example
# python partition_dataset.py -x -i C:/Users/sglvladi/Documents/
˓→Tensorflow/workspace/training_demo/images -r 0.1
Once the script has finished, two new folders should have been created under training_demo/images, namely
training_demo/images/train and training_demo/images/test, containing 90% and 10% of the
images (and *.xml files), respectively. To avoid loss of any files, the script will not delete the images under
training_demo/images. Once you have checked that your images have been safely copied over, you can delete
the images under training_demo/images manually.
TensorFlow requires a label map, which namely maps each of the used labels to an integer values. This label map is
used both by the training and detection processes.
Below we show an example label map (e.g label_map.pbtxt), assuming that our dataset containes 2 labels, dogs
and cats:
item {
id: 1
(continues on next page)
item {
id: 2
name: 'dog'
}
Label map files have the extention .pbtxt and should be placed inside the training_demo/annotations
folder.
Now that we have generated our annotations and split our dataset into the desired training and testing subsets, it is
time to convert our annotations into the so called TFRecord format.
To do this we can write a simple script that iterates through all *.xml files in the training_demo/images/
train and training_demo/images/test folders, and generates a *.record file for each of the two. Here
is an example script that allows us to do just that:
""" Sample TensorFlow XML-to-TFRecord converter
usage: generate_tfrecord.py [-h] [-x XML_DIR] [-l LABELS_PATH] [-o OUTPUT_PATH] [-i
˓→IMAGE_DIR] [-c CSV_PATH]
optional arguments:
-h, --help show this help message and exit
-x XML_DIR, --xml_dir XML_DIR
Path to the folder where the input .xml files are stored.
-l LABELS_PATH, --labels_path LABELS_PATH
Path to the labels (.pbtxt) file.
-o OUTPUT_PATH, --output_path OUTPUT_PATH
Path of output TFRecord (.record) file.
-i IMAGE_DIR, --image_dir IMAGE_DIR
Path to the folder where the input image files are stored.
˓→Defaults to the same directory as XML_DIR.
"""
import os
import glob
import pandas as pd
import io
import xml.etree.ElementTree as ET
import argparse
"written.",
type=str, default=None)
args = parser.parse_args()
if args.image_dir is None:
args.image_dir = args.xml_dir
label_map = label_map_util.load_labelmap(args.labels_path)
label_map_dict = label_map_util.get_label_map_dict(label_map)
def xml_to_csv(path):
"""Iterates through all .xml files (generated by labelImg) in a given directory
˓→and combines
Parameters:
----------
path : str
The path containing the .xml files
Returns
-------
Pandas DataFrame
The produced dataframe
"""
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
tree = ET.parse(xml_file)
root = tree.getroot()
for member in root.findall('object'):
value = (root.find('filename').text,
(continues on next page)
def class_text_to_int(row_label):
return label_map_dict[row_label]
filename = group.filename.encode('utf8')
image_format = b'jpg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(filename),
'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
'image/format': dataset_util.bytes_feature(image_format),
(continues on next page)
def main(_):
writer = tf.python_io.TFRecordWriter(args.output_path)
path = os.path.join(args.image_dir)
examples = xml_to_csv(args.xml_dir)
grouped = split(examples, 'filename')
for group in grouped:
tf_example = create_tf_example(group, path)
writer.write(tf_example.SerializeToString())
writer.close()
print('Successfully created the TFRecord file: {}'.format(args.output_path))
if args.csv_path is not None:
examples.to_csv(args.csv_path, index=None)
print('Successfully created the CSV file: {}'.format(args.csv_path))
if __name__ == '__main__':
tf.app.run()
• Click here to download the above script and save it inside TensorFlow/scripts/preprocessing.
• Install the pandas package:
conda install pandas # Anaconda
# or
pip install pandas # pip
˓→train.record
˓→test.record
# For example
# python generate_tfrecord.py -x C:/Users/sglvladi/Documents/Tensorflow/
˓→workspace/training_demo/images/train -l C:/Users/sglvladi/Documents/
˓→Tensorflow/workspace/training_demo/annotations/label_map.pbtxt -o C:/
˓→Users/sglvladi/Documents/Tensorflow/workspace/training_demo/annotations/
˓→train.record
˓→Tensorflow2/workspace/training_demo/annotations/label_map.pbtxt
(continues -o C:/
on next page)
˓→Users/sglvladi/Documents/Tensorflow/workspace/training_demo/annotations/
˓→test.record
Once the above is done, there should be 2 new files under the training_demo/annotations folder, named
test.record and train.record, respectively.
For the purposes of this tutorial we will not be creating a training job from scratch, but rather we will reuse one of the
pre-trained models provided by TensorFlow. If you would like to train an entirely new model, you can have a look at
TensorFlow’s tutorial.
The model we shall be using in our examples is the SSD ResNet50 V1 FPN 640x640 model, since it provides a
relatively good trade-off between performance and speed. However, there exist a number of other models you can use,
all of which are listed in TensorFlow 2 Detection Model Zoo.
To begin with, we need to download the latest pre-trained network for the model we wish to use. This can be done by
simply clicking on the name of the desired model in the table found in TensorFlow 2 Detection Model Zoo. Clicking
on the name of your model should initiate a download for a *.tar.gz file.
Once the *.tar.gz file has been downloaded, open it using a decompression program of your choice (e.g. 7zip,
WinZIP, etc.). Next, open the *.tar folder that you see when the compressed folder is opened, and extract its contents
inside the folder training_demo/pre-trained-models. Since we downloaded the SSD ResNet50 V1 FPN
640x640 model, our training_demo directory should now look as follows:
training_demo/
...
pre-trained-models/
ssd_resnet50_v1_fpn_640x640_coco17_tpu-8/
checkpoint/
saved_model/
pipeline.config
...
Note that the above process can be repeated for all other pre-trained models you wish to experiment with. For example,
if you wanted to also configure a training job for the EfficientDet D1 640x640 model, you can download the model
and after extracting its context the demo directory will be:
training_demo/
...
pre-trained-models/
efficientdet_d1_coco17_tpu-32/
checkpoint/
saved_model/
pipeline.config
ssd_resnet50_v1_fpn_640x640_coco17_tpu-8/
checkpoint/
saved_model/
pipeline.config
...
Now that we have downloaded and extracted our pre-trained model, let’s create a directory for our training job.
Under the training_demo/models create a new directory named my_ssd_resnet50_v1_fpn and copy
the training_demo/pre-trained-models/ssd_resnet50_v1_fpn_640x640_coco17_tpu-8/
pipeline.config file inside the newly created directory. Our training_demo/models directory should
now look like this:
training_demo/
...
models/
my_ssd_resnet50_v1_fpn/
pipeline.config
...
Now, let’s have a look at the changes that we shall need to apply to the pipeline.config file (highlighted in
yellow):
1 model {
2 ssd {
3 num_classes: 1 # Set this to the number of different label classes
4 image_resizer {
5 fixed_shape_resizer {
6 height: 640
7 width: 640
8 }
9 }
10 feature_extractor {
11 type: "ssd_resnet50_v1_fpn_keras"
12 depth_multiplier: 1.0
13 min_depth: 16
14 conv_hyperparams {
15 regularizer {
16 l2_regularizer {
17 weight: 0.00039999998989515007
18 }
19 }
20 initializer {
21 truncated_normal_initializer {
22 mean: 0.0
23 stddev: 0.029999999329447746
24 }
25 }
26 activation: RELU_6
27 batch_norm {
28 decay: 0.996999979019165
29 scale: true
30 epsilon: 0.0010000000474974513
31 }
32 }
33 override_base_feature_extractor_hyperparams: true
34 fpn {
35 min_level: 3
36 max_level: 7
37 }
38 }
39 box_coder {
(continues on next page)
132 data_augmentation_options {
133 random_horizontal_flip {
134 }
135 }
136 data_augmentation_options {
137 random_crop_image {
138 min_object_covered: 0.0
139 min_aspect_ratio: 0.75
140 max_aspect_ratio: 3.0
141 min_area: 0.75
142 max_area: 1.0
143 overlap_thresh: 0.0
144 }
145 }
146 sync_replicas: true
147 optimizer {
148 momentum_optimizer {
149 learning_rate {
150 cosine_decay_learning_rate {
151 learning_rate_base: 0.03999999910593033
152 total_steps: 25000
(continues on next page)
168 use_bfloat16: false # Set this to false if you are not training on a TPU
169 fine_tune_checkpoint_version: V2
170 }
171 train_input_reader {
172 label_map_path: "annotations/label_map.pbtxt" # Path to label map file
173 tf_record_input_reader {
174 input_path: "annotations/train.record" # Path to training TFRecord file
175 }
176 }
177 eval_config {
178 metrics_set: "coco_detection_metrics"
179 use_moving_averages: false
180 }
181 eval_input_reader {
182 label_map_path: "annotations/label_map.pbtxt" # Path to label map file
183 shuffle: false
184 num_epochs: 1
185 tf_record_input_reader {
186 input_path: "annotations/test.record" # Path to testing TFRecord
187 }
188 }
It is worth noting here that the changes to lines 178 to 179 above are optional. These should only be used if you
installed the COCO evaluation tools, as outlined in the COCO API installation section, and you intend to run evaluation
(see Evaluating the Model (Optional)).
Once the above changes have been applied to our config file, go ahead and save it.
Before we begin training our model, let’s go and copy the TensorFlow/models/research/
object_detection/model_main_tf2.py script and paste it straight into our training_demo folder. We
will need this script in order to train our model.
Now, to initiate a new training job, open a new Terminal, cd inside the training_demo folder and run the following
command:
Once the training process has been initiated, you should see a series of print outs similar to the one below (plus/minus
some warnings):
...
WARNING:tensorflow:Unresolved object in checkpoint: (root).model._box_predictor._base_
˓→tower_layers_for_heads.class_predictions_with_background.4.10.gamma
˓→4.10.gamma
˓→4.10.beta
˓→4.10.moving_mean
˓→4.10.moving_variance
˓→specific issues. Use expect_partial() on the load status object, e.g. tf.train.
˓→were used. See above for specific issues. Use expect_partial() on the load status
...
Important: The output will normally look like it has “frozen”, but DO NOT rush to cancel the process. The training
outputs logs only every 100 steps by default, therefore if you wait for a while, you should see a log for the loss at step
100.
The time you should wait can vary greatly, depending on whether you are using a GPU and the chosen value for
batch_size in the config file, so be patient.
If you ARE observing a similar output to the above, then CONGRATULATIONS, you have successfully started your
first training job. Now you may very well treat yourself to a cold beer, as waiting on the training to finish is likely
to take a while. Following what people have said online, it seems that it is advisable to allow you model to reach
a TotalLoss of at least 2 (ideally 1 and lower) if you want to achieve “fair” detection results. Obviously, lower
TotalLoss is better, however very low TotalLoss should be avoided, as the model may end up overfitting the
dataset, meaning that it will perform poorly when applied to images outside the dataset. To monitor TotalLoss, as
well as a number of other metrics, while your model is training, have a look at Monitor Training Job Progress using
TensorBoard.
If you ARE NOT seeing a print-out similar to that shown above, and/or the training job crashes after a few seconds,
then have a look at the issues and proposed solutions, under the Common issues section, to see if you can find a
solution. Alternatively, you can try the issues section of the official Tensorflow Models repo.
By default, the training process logs some basic measures of training performance. These seem to change depending
on the installed version of Tensorflow.
As you will have seen in various parts of this tutorial, we have mentioned a few times the optional utilisation of the
COCO evaluation metrics. Also, under section Partition the Dataset we partitioned our dataset in two parts, where one
was to be used for training and the other for evaluation. In this section we will look at how we can use these metrics,
along with the test images, to get a sense of the performance achieved by our model as it is being trained.
Firstly, let’s start with a brief explanation of what the evaluation process does. While the training process runs, it will
occasionally create checkpoint files inside the training_demo/training folder, which correspond to snapshots
of the model at given steps. When a set of such new checkpoint files is generated, the evaluation process uses these
files and evaluates how well the model performs in detecting objects in the test dataset. The results of this evaluation
are summarised in the form of some metrics, which can be examined over time.
The steps to run the evaluation are outlined below:
1. Firstly we need to download and install the metrics we want to use.
• For a description of the supported object detection evaluation metrics, see here.
• The process of installing the COCO evaluation metrics is described in COCO API installation.
2. Secondly, we must modify the configuration pipeline (*.config script).
• See lines 178-179 of the script in Configure the Training Pipeline.
3. The third step is to actually run the evaluation. To do so, open a new Terminal, cd inside the training_demo
folder and run the following command:
˓→checkpoint_dir=models/my_ssd_resnet50_v1_fpn
Once the above is run, you should see a checkpoint similar to the one below (plus/minus some
warnings):
...
WARNING:tensorflow:From C:\Users\sglvladi\Anaconda3\envs\tf2\lib\site-
˓→packages\object_detection\inputs.py:79: sparse_to_dense (from
˓→version.
While the evaluation process is running, it will periodically check (every 300 sec by default) and use the
latest models/my_ssd_resnet50_v1_fpn/ckpt-* checkpoint files to evaluate the performance of the
model. The results are stored in the form of tf event files (events.out.tfevents.*) inside models/
my_ssd_resnet50_v1_fpn/eval_0. These files can then be used to monitor the computed metrics, using
the process described by the next section.
A very nice feature of TensorFlow, is that it allows you to coninuously monitor and visualise a number of differ-
ent training/evaluation metrics, while your model is being trained. The specific tool that allows us to do all that is
Tensorboard.
To start a new TensorBoard server, we follow the following steps:
• Open a new Anaconda/Command Prompt
• Activate your TensorFlow conda environment (if you have one), e.g.:
activate tensorflow_gpu
tensorboard --logdir=models/my_ssd_resnet50_v1_fpn
The above command will start a new TensorBoard server, which (by default) listens to port 6006 of your machine.
Assuming that everything went well, you should see a print-out similar to the one below (plus/minus some warnings):
...
TensorBoard 2.2.2 at http://localhost:6006/ (Press CTRL+C to quit)
Once this is done, go to your browser and type http://localhost:6006/ in your address bar, following which
you should be presented with a dashboard similar to the one shown below (maybe less populated if your model has
just started training):
Once your training job is complete, you need to extract the newly trained inference graph, which will be later used to
perform the object detection. This can be done as follows:
• Copy the TensorFlow/models/research/object_detection/exporter_main_v2.py script
and paste it straight into your training_demo folder.
• Now, open a Terminal, cd inside your training_demo folder, and run the following command:
After the above process has completed, you should find a new folder my_model under the training_demo/
exported-models, that has the following structure:
training_demo/
...
exported-models/
my_model/
checkpoint/
saved_model/
pipeline.config
...
Note: You may get the following error when trying to export your model:
_run_main(main, args)
...
File "C:\Users\sglvladi\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\
˓→keras\engine\base_layer.py", line 1627, in get_losses_for
If this happens, have a look at the “TypeError: Expected Operation, Variable, or Tensor, got level_5” issue section for
a potential solution.
THREE
EXAMPLES
This demo will take you through the steps of running an “out-of-the-box” detection model to detect objects in the
video stream extracted from your camera.
The snippet shown below will create the data directory where all our data will be stored. The code will create a
directory structure as shown bellow:
data
models
where the models folder will will contain the downloaded models.
import os
The code snippet shown below is used to download the object detection model checkpoint file, as well as the labels
file (.pbtxt) which contains a list of strings used to add the correct label to each detection (e.g. person).
The particular detection algorithm we will use is the SSD ResNet101 V1 FPN 640x640. More models can be found in
the TensorFlow 2 Detection Model Zoo. To use a different model you will need the URL name of the specific model.
This can be done as follows:
1. Right click on the Model name of the model you would like to use;
2. Click on Copy link address to copy the download link of the model;
3. Paste the link in a text editor of your choice. You should observe a link similar to download.tensorflow.
org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz;
37
TensorFlow setup Documentation
4. Copy the XXXXXXXXX part of the link and use it to replace the value of the MODEL_NAME variable in the code
shown below;
5. Copy the YYYYYYYY part of the link and use it to replace the value of the MODEL_DATE variable in the code
shown below.
For example, the download link for the model used below is: download.tensorflow.org/models/
object_detection/tf2/20200711/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.tar.gz
import tarfile
import urllib.request
38 Chapter 3. Examples
TensorFlow setup Documentation
# Restore checkpoint
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()
@tf.function
def detect_fn(image):
"""Detect objects in image."""
Label maps correspond index numbers to category names, so that when our convolution network predicts 5, we know
that this corresponds to airplane. Here we use internal utility functions, but anything that returns a dictionary mapping
integers to appropriate string labels would be fine.
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
use_display_
˓→name=True)
We will use OpenCV to capture the video stream generated by our webcam. For more information you can refer to the
OpenCV-Python Tutorials
import cv2
cap = cv2.VideoCapture(0)
The code shown below loads an image, runs it through the detection model and visualizes the detection results, includ-
ing the keypoints.
Note that this will take a long time (several minutes) the first time you run this code due to tf.function’s trace-
compilation — on subsequent runs (e.g. on new images), things will be faster.
Here are some simple things to try out if you are curious:
• Modify some of the input images and see if detection still works. Some simple things to try out here (just
uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale
(note that we still expect the input image to have 3 channels).
• Print out detections[‘detection_boxes’] and try to match the box locations to the boxes in the image. Notice that
coordinates are given in normalized form (i.e., in the interval [0, 1]).
• Set min_score_thresh to other values (between 0 and 1) to allow more detections in or to filter out more
detections.
import numpy as np
while True:
# Read frame from camera
ret, image_np = cap.read()
# Expand dimensions since the model expects images to have shape: [1, None, None,
˓→ 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Things to try:
# Flip horizontally
# image_np = np.fliplr(image_np).copy()
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'][0].numpy(),
(detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
detections['detection_scores'][0].numpy(),
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=.30,
agnostic_mode=False)
# Display output
cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))
cap.release()
cv2.destroyAllWindows()
40 Chapter 3. Examples
TensorFlow setup Documentation
This demo will take you through the steps of running an “out-of-the-box” TensorFlow 1 compatible detection model
on a collection of images. More specifically, in this example we will be using the Saved Model Format to load the
model.
First we will download the images that we will use throughout this tutorial. The code snippet shown bellow will
download the test images from the TensorFlow Model Garden and save them inside the data/images folder.
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)
import pathlib
import tensorflow as tf
def download_images():
base_url = 'https://raw.githubusercontent.com/tensorflow/models/master/research/
˓→object_detection/test_images/'
IMAGE_PATHS = download_images()
The code snippet shown below is used to download the pre-trained object detection model we shall use to perform
inference. The particular detection algorithm we will use is the SSD MobileNet v2. More models can be found in the
TensorFlow 1 Detection Model Zoo. To use a different model you will need the URL name of the specific model. This
can be done as follows:
1. Right click on the Model name of the model you would like to use;
2. Click on Copy link address to copy the download link of the model;
3. Paste the link in a text editor of your choice. You should observe a link similar to download.tensorflow.
org/models/object_detection/XXXXXXXXX.tar.gz;
4. Copy the XXXXXXXXX part of the link and use it to replace the value of the MODEL_NAME variable in the code
shown below;
For example, the download link for the model used below is: download.tensorflow.org/models/
object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz
# Download and extract model
def download_model(model_name):
base_url = 'http://download.tensorflow.org/models/object_detection/'
model_file = model_name + '.tar.gz'
model_dir = tf.keras.utils.get_file(fname=model_name,
origin=base_url + model_file,
untar=True)
return str(model_dir)
MODEL_NAME = 'ssd_mobilenet_v2_coco_2018_03_29'
PATH_TO_MODEL_DIR = download_model(MODEL_NAME)
The coode snippet shown below is used to download the labels file (.pbtxt) which contains a list of strings used to
add the correct label to each detection (e.g. person). Since the pre-trained model we will use has been trained on the
COCO dataset, we will need to download the labels file corresponding to this dataset, named mscoco_label_map.
pbtxt. A full list of the labels files included in the TensorFlow Models Garden can be found here.
# Download labels file
def download_labels(filename):
base_url = 'https://raw.githubusercontent.com/tensorflow/models/master/research/
˓→object_detection/data/'
label_dir = tf.keras.utils.get_file(fname=filename,
origin=base_url + filename,
untar=False)
label_dir = pathlib.Path(label_dir)
return str(label_dir)
LABEL_FILENAME = 'mscoco_label_map.pbtxt'
PATH_TO_LABELS = download_labels(LABEL_FILENAME)
end_time = time.time()
elapsed_time = end_time - start_time
print('Done! Took {} seconds'.format(elapsed_time))
42 Chapter 3. Examples
TensorFlow setup Documentation
Out:
Loading model...Done! Took 9.374149322509766 seconds
Label maps correspond index numbers to category names, so that when our convolution network predicts 5, we know
that this corresponds to airplane. Here we use internal utility functions, but anything that returns a dictionary mapping
integers to appropriate string labels would be fine.
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
use_display_
˓→name=True)
The code shown below loads an image, runs it through the detection model and visualizes the detection results, includ-
ing the keypoints.
Note that this will take a long time (several minutes) the first time you run this code due to tf.function’s trace-
compilation — on subsequent runs (e.g. on new images), things will be faster.
Here are some simple things to try out if you are curious:
• Modify some of the input images and see if detection still works. Some simple things to try out here (just
uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale
(note that we still expect the input image to have 3 channels).
• Print out detections[‘detection_boxes’] and try to match the box locations to the boxes in the image. Notice that
coordinates are given in normalized form (i.e., in the interval [0, 1]).
• Set min_score_thresh to other values (between 0 and 1) to allow more detections in or to filter out more
detections.
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore') # Suppress Matplotlib warnings
def load_image_into_numpy_array(path):
"""Load an image from file into a numpy array.
Args:
path: the file path to the image
Returns:
uint8 numpy array with shape (img_height, img_width, 3)
"""
return np.array(Image.open(path))
image_np = load_image_into_numpy_array(image_path)
# Things to try:
# Flip horizontally
# image_np = np.fliplr(image_np).copy()
detections = detect_fn(input_tensor)
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes'],
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=.30,
agnostic_mode=False)
plt.figure()
plt.imshow(image_np_with_detections)
print('Done')
plt.show()
# sphinx_gallery_thumbnail_number = 2
44 Chapter 3. Examples
TensorFlow setup Documentation
•
Out:
This demo will take you through the steps of running an “out-of-the-box” TensorFlow 2 compatible detection model
on a collection of images. More specifically, in this example we will be using the Saved Model Format to load the
model.
First we will download the images that we will use throughout this tutorial. The code snippet shown bellow will
download the test images from the TensorFlow Model Garden and save them inside the data/images folder.
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)
import pathlib
import tensorflow as tf
46 Chapter 3. Examples
TensorFlow setup Documentation
def download_images():
base_url = 'https://raw.githubusercontent.com/tensorflow/models/master/research/
˓→object_detection/test_images/'
IMAGE_PATHS = download_images()
The code snippet shown below is used to download the pre-trained object detection model we shall use to perform
inference. The particular detection algorithm we will use is the CenterNet HourGlass104 1024x1024. More models
can be found in the TensorFlow 2 Detection Model Zoo. To use a different model you will need the URL name of the
specific model. This can be done as follows:
1. Right click on the Model name of the model you would like to use;
2. Click on Copy link address to copy the download link of the model;
3. Paste the link in a text editor of your choice. You should observe a link similar to download.tensorflow.
org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz;
4. Copy the XXXXXXXXX part of the link and use it to replace the value of the MODEL_NAME variable in the code
shown below;
5. Copy the YYYYYYYY part of the link and use it to replace the value of the MODEL_DATE variable in the code
shown below.
For example, the download link for the model used below is: download.tensorflow.org/models/
object_detection/tf2/20200711/centernet_hg104_1024x1024_coco17_tpu-32.tar.gz
untar=True)
return str(model_dir)
MODEL_DATE = '20200711'
(continues on next page)
The coode snippet shown below is used to download the labels file (.pbtxt) which contains a list of strings used to
add the correct label to each detection (e.g. person). Since the pre-trained model we will use has been trained on the
COCO dataset, we will need to download the labels file corresponding to this dataset, named mscoco_label_map.
pbtxt. A full list of the labels files included in the TensorFlow Models Garden can be found here.
label_dir = tf.keras.utils.get_file(fname=filename,
origin=base_url + filename,
untar=False)
label_dir = pathlib.Path(label_dir)
return str(label_dir)
LABEL_FILENAME = 'mscoco_label_map.pbtxt'
PATH_TO_LABELS = download_labels(LABEL_FILENAME)
import time
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils
end_time = time.time()
elapsed_time = end_time - start_time
print('Done! Took {} seconds'.format(elapsed_time))
Out:
48 Chapter 3. Examples
TensorFlow setup Documentation
Label maps correspond index numbers to category names, so that when our convolution network predicts 5, we know
that this corresponds to airplane. Here we use internal utility functions, but anything that returns a dictionary mapping
integers to appropriate string labels would be fine.
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
use_display_
˓→name=True)
The code shown below loads an image, runs it through the detection model and visualizes the detection results, includ-
ing the keypoints.
Note that this will take a long time (several minutes) the first time you run this code due to tf.function’s trace-
compilation — on subsequent runs (e.g. on new images), things will be faster.
Here are some simple things to try out if you are curious:
• Modify some of the input images and see if detection still works. Some simple things to try out here (just
uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale
(note that we still expect the input image to have 3 channels).
• Print out detections[‘detection_boxes’] and try to match the box locations to the boxes in the image. Notice that
coordinates are given in normalized form (i.e., in the interval [0, 1]).
• Set min_score_thresh to other values (between 0 and 1) to allow more detections in or to filter out more
detections.
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore') # Suppress Matplotlib warnings
def load_image_into_numpy_array(path):
"""Load an image from file into a numpy array.
Args:
path: the file path to the image
Returns:
uint8 numpy array with shape (img_height, img_width, 3)
"""
return np.array(Image.open(path))
image_np = load_image_into_numpy_array(image_path)
(continues on next page)
# Things to try:
# Flip horizontally
# image_np = np.fliplr(image_np).copy()
# input_tensor = np.expand_dims(image_np, 0)
detections = detect_fn(input_tensor)
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes'],
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=.30,
agnostic_mode=False)
plt.figure()
plt.imshow(image_np_with_detections)
print('Done')
plt.show()
# sphinx_gallery_thumbnail_number = 2
50 Chapter 3. Examples
TensorFlow setup Documentation
•
Out:
This demo will take you through the steps of running an “out-of-the-box” TensorFlow 2 compatible detection model
on a collection of images. More specifically, in this example we will be using the Checkpoint Format to load the
model.
First we will download the images that we will use throughout this tutorial. The code snippet shown bellow will
download the test images from the TensorFlow Model Garden and save them inside the data/images folder.
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)
import pathlib
import tensorflow as tf
52 Chapter 3. Examples
TensorFlow setup Documentation
def download_images():
base_url = 'https://raw.githubusercontent.com/tensorflow/models/master/research/
˓→object_detection/test_images/'
IMAGE_PATHS = download_images()
The code snippet shown below is used to download the pre-trained object detection model we shall use to perform
inference. The particular detection algorithm we will use is the CenterNet HourGlass104 1024x1024. More models
can be found in the TensorFlow 2 Detection Model Zoo. To use a different model you will need the URL name of the
specific model. This can be done as follows:
1. Right click on the Model name of the model you would like to use;
2. Click on Copy link address to copy the download link of the model;
3. Paste the link in a text editor of your choice. You should observe a link similar to download.tensorflow.
org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz;
4. Copy the XXXXXXXXX part of the link and use it to replace the value of the MODEL_NAME variable in the code
shown below;
5. Copy the YYYYYYYY part of the link and use it to replace the value of the MODEL_DATE variable in the code
shown below.
For example, the download link for the model used below is: download.tensorflow.org/models/
object_detection/tf2/20200711/centernet_hg104_1024x1024_coco17_tpu-32.tar.gz
untar=True)
return str(model_dir)
MODEL_DATE = '20200711'
(continues on next page)
The coode snippet shown below is used to download the labels file (.pbtxt) which contains a list of strings used to
add the correct label to each detection (e.g. person). Since the pre-trained model we will use has been trained on the
COCO dataset, we will need to download the labels file corresponding to this dataset, named mscoco_label_map.
pbtxt. A full list of the labels files included in the TensorFlow Models Garden can be found here.
label_dir = tf.keras.utils.get_file(fname=filename,
origin=base_url + filename,
untar=False)
label_dir = pathlib.Path(label_dir)
return str(label_dir)
LABEL_FILENAME = 'mscoco_label_map.pbtxt'
PATH_TO_LABELS = download_labels(LABEL_FILENAME)
import time
from object_detection.utils import label_map_util
from object_detection.utils import config_util
from object_detection.utils import visualization_utils as viz_utils
from object_detection.builders import model_builder
# Restore checkpoint
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()
@tf.function
def detect_fn(image):
"""Detect objects in image."""
54 Chapter 3. Examples
TensorFlow setup Documentation
return detections
end_time = time.time()
elapsed_time = end_time - start_time
print('Done! Took {} seconds'.format(elapsed_time))
Out:
Label maps correspond index numbers to category names, so that when our convolution network predicts 5, we know
that this corresponds to airplane. Here we use internal utility functions, but anything that returns a dictionary mapping
integers to appropriate string labels would be fine.
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
use_display_
˓→name=True)
The code shown below loads an image, runs it through the detection model and visualizes the detection results, includ-
ing the keypoints.
Note that this will take a long time (several minutes) the first time you run this code due to tf.function’s trace-
compilation — on subsequent runs (e.g. on new images), things will be faster.
Here are some simple things to try out if you are curious:
• Modify some of the input images and see if detection still works. Some simple things to try out here (just
uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale
(note that we still expect the input image to have 3 channels).
• Print out detections[‘detection_boxes’] and try to match the box locations to the boxes in the image. Notice that
coordinates are given in normalized form (i.e., in the interval [0, 1]).
• Set min_score_thresh to other values (between 0 and 1) to allow more detections in or to filter out more
detections.
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore') # Suppress Matplotlib warnings
def load_image_into_numpy_array(path):
"""Load an image from file into a numpy array.
Args:
path: the file path to the image
Returns:
uint8 numpy array with shape (img_height, img_width, 3)
"""
return np.array(Image.open(path))
image_np = load_image_into_numpy_array(image_path)
# Things to try:
# Flip horizontally
# image_np = np.fliplr(image_np).copy()
detections = detect_fn(input_tensor)
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes']+label_id_offset,
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=.30,
agnostic_mode=False)
plt.figure()
plt.imshow(image_np_with_detections)
print('Done')
(continues on next page)
56 Chapter 3. Examples
TensorFlow setup Documentation
# sphinx_gallery_thumbnail_number = 2
•
Out:
58 Chapter 3. Examples
CHAPTER
FOUR
COMMON ISSUES
Below is a list of common issues encountered while using TensorFlow for objects detection.
If you are using GPU Support (Optional) and when you try to run some Python object detection script (e.g. Test
your Installation), after a few seconds, Windows reports that Python has crashed then have a look at the Ana-
conda/Command Prompt window you used to run the script and check for a line similar (maybe identical) to the
one below:
2018-03-22 03:07:54.623130: E C:\tf_jenkins\workspace\rel-win\M\windows-gpu\
˓→PY\36\tensorflow\stream_executor\cuda\cuda_dnn.cc:378] Loaded runtime
˓→CuDNN library: 7101 (compatibility version 7100) but source was compiled
If the above line is present in the printed debugging, it means that you have not installed the correct version of the
cuDNN libraries. In this case make sure you re-do the Install CUDNN step, making sure you instal cuDNN v7.0.5.
Sometimes, when terminating a TensorFlow training process, the Nvidia containers associated to the process are not
cleanly terminated. This can lead to bogus errors when we try to run a new TensorFlow process.
Some known issues caused by the above are presented below:
• Failure to restart training of a model. Look for the following errors in the debugging:
2018-03-23 03:03:10.326902: E C:\tf_jenkins\workspace\rel-win\M\windows-
˓→gpu\PY\36\tensorflow\stream_executor\cuda\cuda_dnn.cc:385] could not
59
TensorFlow setup Documentation
˓→Memcpy failed
To solve such issues in Windows, open a Task Manager windows, look for Tasks with name NVIDIA Container
and kill them by selecting them and clicking the End Task button at the bottom left corner of the window.
If the issue persists, then you’re probably running out of memory. Try closing down anything else that might be eating
up your GPU memory (e.g. Youtube videos, webpages etc.)
In some versions of Tensorflow, you may see errors that look similar to the ones below:
...
WARNING:tensorflow:Entity <bound method Conv.call of <tensorflow.python.layers.
˓→convolutional.Conv2D object at 0x000001E92103EDD8>> could not be transformed and
˓→will be executed as-is. Please report this to the AutgoGraph team. When filing the
˓→bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach
˓→transformed and will be executed as-is. Please report this to the AutgoGraph team.
˓→When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_
˓→VERBOSITY=10`) and attach the full output. Cause: converting <bound method
˓→BatchNormalization.call of <tensorflow.python.layers.normalization.
...
These warnings appear to be harmless form my experience, however they can saturate the console with unnecessary
messages, which makes it hard to scroll through the output of the training/evaluation process.
As reported here, this issue seems to be caused by a mismatched version of gast. Simply downgrading gast to version
0.2.2 seems to remove the warnings. This can be done by running:
It is possible that when executing from object_detection.utils import label_map_util you may
get the above error. As per the discussion is in this Stack Overflow thread, upgrading the Python protobuf version
seems to solve this issue:
When trying to export oyu trained model using the exporter_main_v2.py script, you may come across an error
that looks like this:
5 _run_main(main, args)
6 ...
7 File "C:\Users\sglvladi\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\
˓→keras\engine\base_layer.py", line 1627, in get_losses_for
This error seems to come from TensorFlow itself and a discussion on the issue can be found here. As discussed there,
a fix to the above issue can be achieved by opening the tf_utils.py file and adding a line of code. Below is a
summary of how this can be done:
• Look at the line that corresponds to line 9 (highlighted) in the above error print out.
• Copy the path to the tf_utils.py file; in my case this was C:\Users\sglvladi\Anaconda3\envs\
tf2\lib\site-packages\tensorflow\python\keras\utils\tf_utils.py
• Open the file and replace line 140 of the file as follows:
– Change:
to:
At the time of writting this tutorial, a fix to the issue had not been implemented in the version of TensorFlow installed
using pip. It is possible that this will get incorporated at some later point.