Christopher
Stoll

Setup Tensorflow for Object Detection on macOS

Tensorflow is an amazing tool, but it can be intimidating to get it up and running. I wanted to help someone get started with Tensorflow on a Mac, and if I had written all of this down then I could have just given them a link. This post isn’t a captivating read, it’s basically a list of commands, but it allows me to easily share my setup instructions. I’ll try to give a short explanation of what needs to happen and then a block with required commands. If you are in a hurry, don’t care to know what is happening, or have an odd blind trust in me, then you can skip the explanation and type in the commands at the end of each section. Conversely, if you follow along in the explanation section, then you don’t need to re-type the commands that appear at the end of each section.

There are many different applications for Tensorflow. We are currently using Tensorflow for object detection, so these instructions install everything which is required for that task. Other task could require additional prerequisites. If you are still here, then you probably need to get Tensorflow Object Detection API set up. In that case, open up a terminal window and read on.

Just a general note; if something is not working properly, make sure that PYTHONPATH is set. This can be checked using echo $PYTHONPATH.

First, let’s make a project area to keep all of the Tensorflow repositories we are going to fetch.

mkdir -b ~/code/tensorflow
cd ~/code/tensorflow

Install Dependencies

Make sure pyenv or some other python version manager is installed. I prefer pyenv, so we are going to use that, but feel free to use some other tool. Check if pyenv is available with the command which pyenv. If it doesn’t print a function, then pyenv will need to be installed using brew install pyenv. Tensorflow relies upon protocol buffers, so make sure that is installed brew install protobuf. Next install all the python dependencies using pip install tensorflow Cython contextlib2 pillow lxml jupyter matplotlib. Finally, clone the Tensorflow models repository with git clone https://github.com/tensorflow/models.git.

COCO is short for common objects in context and is a large dataset of images that is commonly used to evaluate object detection techniques. We will not be using the COCO API directly since we are going to do transfer learning, but it is probably required if you define a new architecture and want to test and compare that architecture against existing ones.

Next we will compile our proto files (cd models/research && protoc object_detection/protos/*.proto --python_out=. && cd ../..) before setting the PYTHONPATH environment variable.

To see what your Python path should be, use the command echo $PYTHONPATH:$(pwd)/models:$(pwd)/models/research:$(pwd)/models/research/slim:$(pwd)/cocoapi/PythonAPI. The results of that command need to be added to your shell startup script. If you are using bash that will either be ~/.profile or ~/.bashrc. If you are using zsh, then it will be ~/.zshrc. Edit that file and add a line to the end which begins with export PYTHONPATH=. Directly after the equal sign, without any spaces, add the path which we printed out above.

brew install pyenv protobuf
pip install tensorflow Cython contextlib2 pillow lxml jupyter matplotlib
git clone https://github.com/tensorflow/models.git

git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools ../../models/research/
cd ../..

cd models/research
protoc object_detection/protos/*.proto --python_out=.
cd ../..

echo "export PYTHONPATH=$PYTHONPATH:`pwd`/models:`pwd`/models/research:`pwd`/models/research/slim:`pwd`/cocoapi/PythonAPI" >> ~/.profile

If everything is working properly then the following command should work without errors python object_detection/builders/model_builder_test.py.

Install Labeling Software

In order to train an object detection model, you will need to collect a lot of images containing the objects which need to be identified. Then, the objects will need to be labeled. That’s the process of drawing bounding boxes around the desired objects and assigning classes to the bounding boxes.

There are plenty of options for labeling software. Although Microsoft VoTT looks promising, our current workflow relies upon the PascalVOC files generated from labelImg. At some point we will update our workflow tools, but until then we will continue to use labelImg. To install labelImg just follow these steps:

brew install qt pyqt5 libxml2
git clone https://github.com/tzutalin/labelImg.git
cd labelImg
make qt5py3
pip install pyqt5 lxml

Once all of it’s dependencies are installed, labelImg can be ran with python labelImg.py from within the labelImg directory. With the ability to label images, you are ready to create custom models using the Tensorflow object detection API. In a future post I will give instructions on how to set up a training session.

Published: 2018-12-12
tensorflowobject detectioncomputer visionmacOS