Update Feb/2020: Run the Tensorflow Object Detection API with Docker (Section at the end of the article, Code on Github)
Update Dez/2019: Installation now also available as a Jupyter notebook.
Update Nov/2019: Tried with Tensorflow v2.0 and it seems to work fine (Training isn't supported with Tensorflow 2 yet)
Object detection can be hard. Especially if you don't have any knowledge about it. This is why Tensorflow provides their Object Detection API, which not only allows us to easily use object detection models but also gives us the ability to train new ones using the power of transfer learning.
In this article, we will install the Tensorflow Object Detection API and test it out using the object_detection_tutorial.ipynb file. In the upcoming two articles I will show you how to detect objects from your webcam as well as how to train your own model from scratch.
This article is also available as a video:
Cloning the repository and installing dependencies
First we need to clone the Tensorflow models repository. This can be done by either cloning the repository directly or by typing git clone https://github.com/tensorflow/models --single-branch --branch r1.13.0 inside a command line.
After cloning the repository it is a good idea to install all the dependencies. This can be done by typing:
pip install --user Cython pip install --user contextlib2 pip install --user pillow pip install --user lxml pip install --user jupyter pip install --user matplotlib
Install the COCO API
COCO is a large image dataset designed for object detection, segmentation, person keypoints detection, stuff segmentation, and caption generation. If you want to use the data-set and evaluation metrics you need to clone the cocoapi repository and copy the pycocotools subfolder to the tensorflow/models/research directory.
git clone https://github.com/cocodataset/cocoapi.git cd cocoapi/PythonAPI make cp -r pycocotools <path_to_tensorflow>/models/research/
Using make won't work on windows. To install the cocoapi on windows the following command can be used:
pip install "git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI"
The Tensorflow Object Detection API uses .proto files. These files need to be compiled into .py files in order for the Object Detection API to work properly. Google provides a programmed called Protobuf that can compile these files.
Protobuf can be downloaded from this website. After downloading you can extract the folder in a directory of your choice.
After extracting the folder you need to go into models/research and use protobuf to extract python files from the proto files in the object_detection/protos directory.
The official installation guide uses protobuf like:
./bin/protoc object_detection/protos/*.proto --python_out=.
But the * which stands for all files didn’t work for me so I wrote a little Python script to execute the command for each .proto file.
import os import sys args = sys.argv directory = args protoc_path = args for file in os.listdir(directory): if file.endswith(".proto"): os.system(protoc_path+" "+directory+"/"+file+" --python_out=.")
This file needs to be saved inside the research folder and I named it use_protobuf.py. Now we can use it by going into the console and typing:
python use_protobuf.py <path to directory> <path to protoc file> Example: python use_protobuf.py object_detection/protos C:/Users/Gilbert/Downloads/bin/protoc
Add necessary environment variables and finish Tensorflow Object Detection API installation
Lastly we need to add the research and research slim folder to our environment variables and run the setup.py file.
To add the paths to environment variables in Linux you need to type:
export PYTHONPATH=$PYTHONPATH:<PATH_TO_TF>/TensorFlow/models/research export PYTHONPATH=$PYTHONPATH:<PATH_TO_TF>/TensorFlow/models/research/object_detection export PYTHONPATH=$PYTHONPATH:<PATH_TO_TF>/TensorFlow/models/research/slim
On windows you need to at the path of the research folder and the research/slim to your PYTHONPATH environment variable (See Environment Setup) .
To run the setup.py file we need to navigate to tensorflow/models/research and run:
# From within TensorFlow/models/research/ python setup.py build python setup.py install
This completes the installation of the object detection api. To test if everything is working correctly, run the object_detection_tutorial.ipynb notebook from the object_detection folder.
Causion: The new object_detection_tutorial.ipynb only works with Tensorflow 2.0. If you want to use Tensorflow 1.x get the file from my Github instead.
If your installation works correctly you should see the following output:
Install Tensorflow Object Detection on Google Colab
The Tensorflow Object Detection API can also be used with Google Colab. To install Google Colab passed in the following commands into the first cell.
!apt-get install protobuf-compiler python-pil python-lxml python-tk !pip install Cython #!pip install jupyter #!pip install matplotlib !git clone https://github.com/tensorflow/models.git %cd /content/models/research !protoc object_detection/protos/*.proto --python_out=. %set_env PYTHONPATH=/content/models/research:/content/models/research/slim !python object_detection/builders/model_builder_test.py
Run the Tensorflow Object Detection API with Docker
Installing the Tensorflow Object Detection API can be hard because there are lots of errors that can occur depending on your operating system. Docker makes it easy to setup the Tensorflow Object Detection API because you only need to download the files inside the docker folder and run docker-compose up.
After running the command docker should automatically download and install everything needed for the Tensorflow Object Detection API and open Jupyter on port 8888. If you also want to have access to the bash for training models you can simply say docker exec -it CONTAINER_ID. For more information check out Dockers documentation.
If you experience any problems with the docker files be sure to let me know.
That’s all from this article about how to install the Tensorflow Object Detection API. If you have any questions or just want to chat with me feel free to leave a comment below or contact me on social media. If you want to get continuous updates about my blog make sure to subscribe to my Youtube channel and join my newsletter.