DEV Community

Madhu Kumar for Docker

Posted on • Updated on

Machine Learning with TensorFlow Object Detection running on Docker

Image description

TensorFlow

TensorFlow is a free and open-source software library for machine learning and artificial intelligence. It can be used across a range of tasks but has a particular focus on training and inference of deep neural networks.

TensorFlow was developed by the Google Brain team for internal Google use in research and production. The initial version was released under the Apache License 2.0 in 2015. Google released the updated version of TensorFlow, named TensorFlow 2.0, in September 2019.

TensorFlow can be used in a wide variety of programming languages, most notably Python, as well as Javascript, C++, and Java. This flexibility lends itself to a range of applications in many different sectors.

Create the Dockerfile

Tensorflow Object Detection API depends on the following libraries:

  • Protobuf 2.6
  • Pillow 1.0
  • lxml
  • tf Slim
  • Jupyter notebook
  • Matplotlib
  • Tensorflow

For detailed steps how to install Tensorflow, follow the Tensorflow installation instructions. For Dockerfile, we will use the below command:

RUN pip install tensorflow
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Copy TensorFlow Models into Docker image:

RUN git clone https://github.com/tensorflow/models.git /tensorflow/models
Enter fullscreen mode Exit fullscreen mode

Make /tensorflow/models/research as a working directory:

WORKDIR /tensorflow/models/research
Enter fullscreen mode Exit fullscreen mode

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=.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

echo line is setting root password for web interface for jupyter notebook

To process requests from host machine we need to expose a port:

EXPOSE 8888
Enter fullscreen mode Exit fullscreen mode

Finally, we will run the Jupyter notebook with TensorFlow models:

CMD ["jupyter", "notebook", "--allow-root", "--notebook-dir=/tensorflow/models/research/object_detection", "--ip=0.0.0.0", "--port=8888", "--no-browser"]
Enter fullscreen mode Exit fullscreen mode

The full Dockerfile should look like below:

FROM "ubuntu:bionic"
RUN apt-get update && yes | apt-get upgrade
RUN mkdir -p /tensorflow/models
RUN apt-get install -y git python-pip
RUN pip install --upgrade pip
RUN pip install tensorflow
RUN apt-get install -y protobuf-compiler python-pil python-lxml
RUN pip install jupyter
RUN pip install matplotlib
RUN git clone https://github.com/tensorflow/models.git /tensorflow/models
WORKDIR /tensorflow/models/research
RUN protoc object_detection/protos/*.proto --python_out=.
RUN export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
RUN jupyter notebook --generate-config --allow-root
RUN echo "c.NotebookApp.password = u'sha1:6a3f528eec40:6e896b6e4828f525a6e20e5411cd1c8075d68619'" >> /root/.jupyter/jupyter_notebook_config.py
EXPOSE 8888
CMD ["jupyter", "notebook", "--allow-root", "--notebook-dir=/tensorflow/models/research/object_detection", "--ip=0.0.0.0", "--port=8888", "--no-browser"]
Enter fullscreen mode Exit fullscreen mode

Build the Docker image

docker build -t tensorflow .

Run the Docker container

docker run --rm --name tensorflow -p 8888:8888 -d tensorflow

Run the Application

Open http://localhost:8888
Image description

Enter the password as root and click Log in

Image description

Open object_detection_tutorial.ipynb

Image description

Run the object detection from the menu “Cell → Run all”

Image description

Results of the object detection

Image description
Image description

Stop the TensorFlow Docker container

docker rm -f tensorflow

Top features that make TensorFlow a preferred library among developers

  1. It offers multiple levels of abstraction and various APIs that makes model building easy.
  2. Models can be trained using different programming languages like Python, JavaScript, or Swift.
  3. TensorFlow supports various platforms for deploying ML models, be it desktop, mobile, web, or even cloud.
  4. Being an open-source platform, TensorFlow is backed by huge community support where one can interact with developers, problem solvers, and tinkerers and share their ideas.

Why Docker?

Docker is my favourite containerisation platform. Why? Docker provides a way to run applications securely isolated in a container, packaged with all its dependencies and libraries which are required for the application to run. Please refer to my other Docker blogs for more learning on Docker.

Conclusion

We just created a Docker image with TensorFlow and ran a container based on the Docker image. We have used the Jupyter notebook to test our examples in the browser.

As per the StackOverflow Developers Survey 2020, TensorFlow is one of the most popular frameworks among developers. Around 65% of the surveyed respondents have expressed their interest in continuing to develop models using TensorFlow. Also, with Google’s support, the library will be enhanced regularly to fulfill the growing needs of developers.

Discussion (0)