TensorFlow Object Detection with Docker from scratch
In this article I want to show you how to create docker image with TensorFlow and run object detection example.
Why Docker?
Docker provides a way to run applications securely isolated in a container, packaged with all its dependencies and libraries. It’s easy to install. And I have interesting article about Docker: Making right things using Docker.
What is TensorFlow?
It’s an open-source software library for Machine Intelligence.
About TensorFlow
TensorFlow is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them. The flexible architecture allows you to deploy computation to one or more CPUs or GPUs in a desktop, server, or mobile device with a single API. TensorFlow was originally developed by researchers and engineers working on the Google Brain Team within Google’s Machine Intelligence research organization for the purposes of conducting machine learning and deep neural networks research, but the system is general enough to be applicable in a wide variety of other domains as well.
First we need to create directory and Dockerfile for building our image:
mkdir tensorflow
cd tensorflowtouch Dockerfile
We will use Ubuntu image as base, for that we should extend our new image from ubuntu official repository. And update packages after:
FROM "ubuntu:bionic"RUN apt-get update && yes | apt-get upgrade
Then we need to create working directory:
RUN mkdir -p /tensorflow/models
And install git with pip:
RUN apt-get install -y git python-pip
RUN pip install --upgrade pip
For now that’s all. As we are going to run object detection example we need to install all dependencies. All steps you can find on installation page.
Tensorflow Object Detection API depends on the following libraries:
- Protobuf 2.6
- Pillow 1.0
- lxml
- tf Slim (which is included in the “tensorflow/models/research/” checkout)
- Jupyter notebook
- Matplotlib
- Tensorflow
For detailed steps to install Tensorflow, follow the Tensorflow installation instructions. We will install Tensorflow using command:
RUN pip install tensorflow
The remaining libraries can be installed on Ubuntu using via apt-get:
RUN apt-get install protobuf-compiler python-pil python-lxml
RUN pip install jupyter
RUN pip install matplotlib
Next we need to copy example code into our image:
RUN git clone https://github.com/tensorflow/models.git /tensorflow/models
Let’s make /tensorflow/models/research our working directory:
WORKDIR /tensorflow/models/research
The Tensorflow Object Detection API uses Protobufs to configure model and training parameters. Before the framework can be used, the Protobuf libraries must be compiled. This should be done by running the following command:
RUN protoc object_detection/protos/*.proto --python_out=.
When running locally, the /tensorflow/models/research/ and slim directories should be appended to PYTHONPATH. This can be done by running the following command:
RUN export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
Next we need to configure jupyter notebook:
RUN jupyter notebook --generate-config --allow-root
RUN echo "c.NotebookApp.password = u'sha1:6a3f528eec40:6e896b6e4828f525a6e20e5411cd1c8075d68619'" >> /root/.jupyter/jupyter_notebook_config.py
Last line is setting root password for web interface.
To process requests from host machine we need to expose port:
EXPOSE 8888
And run the main process:
CMD ["jupyter", "notebook", "--allow-root", "--notebook-dir=/tensorflow/models/research/object_detection", "--ip=0.0.0.0", "--port=8888", "--no-browser"]
So full Dockerfile:
FROM "ubuntu:bionic"RUN apt-get update && yes | apt-get upgradeRUN mkdir -p /tensorflow/modelsRUN apt-get install -y git python-pip
RUN pip install --upgrade pipRUN pip install tensorflowRUN apt-get install -y protobuf-compiler python-pil python-lxmlRUN pip install jupyter
RUN pip install matplotlibRUN git clone https://github.com/tensorflow/models.git /tensorflow/modelsWORKDIR /tensorflow/models/researchRUN protoc object_detection/protos/*.proto --python_out=.RUN export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slimRUN jupyter notebook --generate-config --allow-root
RUN echo "c.NotebookApp.password = u'sha1:6a3f528eec40:6e896b6e4828f525a6e20e5411cd1c8075d68619'" >> /root/.jupyter/jupyter_notebook_config.pyEXPOSE 8888CMD ["jupyter", "notebook", "--allow-root", "--notebook-dir=/tensorflow/models/research/object_detection", "--ip=0.0.0.0", "--port=8888", "--no-browser"]
To build our image run:
docker build -t tensorflow .
And you’ll see the process:
To run container from the image use command:
docker run --name tensorflow -p 8888:8888 -d tensorflow
And open http://localhost:8888/:
Our password is root:
Open object_detection_tutorial.ipynb:
It’s Tensorflow Object Detection example, our goal.
Before run you need to make a small update. Change version of TensorFlow from 1.4.0 to 1.4.1 (latest version for now):
To run it by clicking top menu Cell -> Run all:
After you can find result of our job:
To stop it run:
docker rm -f tensorflow
That’s all. We just created docker image with Google TensorFlow and run container based on the image. Thanks to jupyter notebook we can test our examples in browser. In next article I’ll show how to use different models.
References
- Making right things using Docker
- TensorFlow
- TensorFlow Models
- Building a Real-Time Object Recognition App with Tensorflow and OpenCV
- How to train your own Object Detector with TensorFlow’s Object Detector API
- Is Google Tensorflow Object Detection API the easiest way to implement image recognition?
- Building a Toy Detector with Tensorflow Object Detection API
- Using Object detection for a Smarter Retail Checkout Experience
- Tensorflow detection model zoo