Machine Learning with TensorFlow Object Detection running on Docker
Tags: docker, machinelearning, containers, tensorflow
Published: 2022-03-28 22:08:47

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ulk2x5etn35tds5d3xe.png)

## TensorFlow

[TensorFlow](https://www.tensorflow.org) 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](https://www.tensorflow.org/install). For Dockerfile, we will use the below command:
```
RUN pip install tensorflow
```

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
```

Copy TensorFlow Models into Docker image:
```
RUN git clone https://github.com/tensorflow/models.git /tensorflow/models
```

Make /tensorflow/models/research as a 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
```

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
```

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
```

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"]
```

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"]
```

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sebhmmlaq1ejj619tx8s.png)

## Enter the password as root and click Log in
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4e8pet6zs6zvrn1rnuq.png)

## Open object_detection_tutorial.ipynb

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8j0rl8t9gj6ptt3pyzrp.png)

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

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8o5vl0z11lhg58gdged.png)

## Results of the object detection

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1ef2ryhjyknkkf2xkf6.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyqeep9vvq0cq5txxjpm.png)
  

## 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](https://www.docker.com) 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.