Containers help improve security, reproducibility, and scalability in software development, data science, and deep learning. Using containers is an excellent way to create easy-to-deploy, repeatable environments for your deep learning projects. With Docker containers as the development environment, you don’t need to install and set up the environment for the various frameworks. Experiments are guaranteed to run everywhere and will run the same every time. In this article we’ll explain how to deploy TensorFlow using Docker containers.
What is Docker and How is it Used with TensorFlow?
Docker is a platform used to develop, deploy, and run applications by using the benefits of containerization. It ensures that all applications remain isolated in a container and packaged with all their dependencies and libraries. Docker containers are used to create virtual environments, which can run TensorFlow.
Within these environments, TensorFlow can, for example, share resources with its host machine, access directories, use a GPU, and connect to the internet, while remaining separate from the rest of the system.
Advantages of Using a Docker Image to set up TensorFlow
– Dependencies and requirements are satisfied for all the different frameworks
– Getting the right Anaconda distribution and the correct version of Python
– Setting up the paths and the correct versions of different packages
– Ensuring the installation does not interfere with other Python-based installations.
You can also test each release of the TensorFlow Docker images in these virtual environments. Using Docker also makes sharing projects with others a painless process. You don’t have to worry about environments not being compatible, missing dependencies or even platform conflicts.
Using Docker for Deep Learning in Research Environments
Many modern deep learning researchers rely on more than one framework. Running several deep learning frameworks or multiple versions of a framework on a single machine can be challenging.
Docker provides ready-to-go containers for each framework, which gives you the flexibility to experiment, without the risk of damaging your current environment. With Docker, the frameworks are entirely self-contained. If something is not working correctly, simply remove the entire container and start over, by performing a “docker run” command.
If you are only using one framework, like TensorFlow, in an experiment, it is probably already containerized. This means you can simply execute TensorFlow using the “docker run” command and it will not compromise your local workstation. You can simply download it, test it, and use it, and if the experiment doesn’t fit your needs, discard the container and move on
Using Docker for Deep Learning in Development Environments
As with any development project, it is important to make experiments and results reproducible. For deep learning, you should implement practices that effectively track code and ensure the training of data, weights, statistics, and files. You should also ensure this code and data can be rerun and reused with subsequent experiments. When you use containerized environments within Docker and images from Docker hub, you can achieve reproducible results in your deep learning experiments.
Containerized environments and images can be advantageous when you deploy deep learning at large organizations. You can also effectively use containerized environments for distributed development environments where deep learning talent is spread across different organizations, departments, or geographical regions. Also, you can manage and customize your deployment further by using orchestration tools like Kubernetes or Docker Swarm.
Quick Tutorial: Real-Time TensorFlow Object Recognition with, Docker, and a GPU
In this tutorial, we package all the necessary tools needed for object recognition. The goal is to be able to run any computer vision algorithm on any machine within minutes.
1. Install Docker
2. Install nvidia-docker to leverage NVIDIA GPUs
2.1 Test your Installation
3. Run a Jupyter Notebook with TensorFlow powered by GPU and OpenCv
Then you will need to navigate to your localhost and use the port 8888 and paste your token to identify and access your Jupyter notebooks.
4. Real-Time Object Recognition
The following code tracks objects over frames with a webcam. It is a sample of code taken from the following GitHub repository.
First of all, we need to open access to the xserver to our docker image. There are different ways of doing so. We will open access to xserver to anyone.
Then we will bash to our Docker image using this command:
We will need to clone the github repository, which is a real-time object detector:
Finally, you can launch the python code:
Scaling Up TensorFlow Docker Deployments
Resource management – Deploying TensorFlow Docker images works well at a small scale but as you grow in the number of machines you need to orchestrate containers using Kubernetes and it can get very complex.
Experiment management – The visibility into what is running in each container is limited, you don’t have a central view of all the parameters and metrics.
Data management – Containers are immutable, you can’t put experiment data in a Docker container.