Deploying Deep Learning Models using Docker — YOLO v3

shreyash
4 min readJul 5, 2021

--

Recently, I have been working on a robotics project to enable differently challenged people to perform daily tasks without any human support. It requires a lot of components to work together in tandem to perform the task like detecting objects, grasping the objects, feeding the human, etc. While working in a big team, it’s often difficult to manage dependencies and version mismatch between different components or services. Here is where containerizing the components help in the long term.

In this post, I will walk through how to run a detection algorithm — YOLO in a containerized environment. In the next few posts, I will try to explain how to communicate between different containers and deploy them in a production cluster.

Running YOLO in local environment

YOLO (You Only Look Once) is a state-of-the-art, real time object detection algorithm. We use the darknet implementation of YOLO v3 pre-trained on MS COCO dataset. Then we will run the algorithm in a containerized environment.

Create a working directory and clone the darknet implementation and download the pre-trained weights (MS COCO dataset).

git clone https://github.com/pjreddie/darknetwget https://pjreddie.com/media/files/yolov3.weights

Once cloned, we will create a simple detect.py file to get the desired results i.e run the detection algorithm and get the class labels and bounding boxes.

Follow this link to get better understanding of the below code. (https://medium.com/analytics-vidhya/object-detection-using-yolo-v3-and-deploying-it-on-docker-and-minikube-c1192e81ae7a)

Lets try to run it on below image.

python3 detect.py test_img.jpg

Running YOLO in docker

Now, let’s try to run it in docker.

STEP 1 : Installing Docker Desktop. Follow the installation instructions in the link — https://www.docker.com/products/docker-desktop

(If you have never used Docker before, please follow below links and try to run a simple container to understand the basics —

  1. https://docs.docker.com/

2. https://www.youtube.com/watch?v=fqMOX6JJhGo)

STEP 2 : Create a working directory and inside it create a Dockerfile. Dockerfile a simple text file which contains set of commands to create a docker image.

For running YOLO I have used python3 image and then installed set of libraries required for running the detection algorithm. Check the Dockerfile below. We use python:3 base image (ubuntu) and then install the libraries required to run YOLO. We clone the darknet implementation and also download the weights. We also copy our detect.py file and a test image to our home directory in the container. Now lets run the container and see the results.

FROM python:3RUN \
apt-get install -y \
wget \
unzip \
git
RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get install -y ffmpeg
RUN pip3 install numpy
RUN pip3 install opencv-python
RUN pip3 install requests
RUN pip3 install numba
RUN pip3 install imutils
WORKDIR home/COPY detect.py /home/
COPY test_img.jpg /home/
RUN git clone https://github.com/pjreddie/darknet
WORKDIR darknet/
RUN wget https://pjreddie.com/media/files/yolov3.weights -P weights/
WORKDIR /home

STEP 3 : Build the image. Run the below command in the directory where you created the Dockerfile. You will get output as below after running the build command.

docker build -t obj_detection .

STEP 4 : After building the image, let’s run our container. You can use docker images command to see the list of images. We will run the image in interactive and detach mode so that we can check the output inside our container. Run the below command next.

docker run -it -d obj_detection

The output will be some id. Now if you run docker ps, it should show you the list of containers running right now. Next let’s run the exec command to get inside the container’s shell. First run docker ps command to get the container id as below.

docker psCONTAINER ID   IMAGE           COMMAND     CREATED         STATUS         PORTS     NAMESeb3745329125   obj_detection   "python3"   3 seconds ago   Up 3 seconds             cool_gates

Now run the exec command to get into the containers shell as below.

docker exec -it eb3745329125 sh

After running the above command, you should be able to access the container’s internal directories. You can do ls to check the files which we copied under the /home folder.

# pwd/home# lsdarknet detect.py test_img.jpg## python3 detect.py test_img.jpg[INFO] loading YOLO from disk…[INFO] YOLO took 0.554473 seconds[[‘chair’, 0.9977081418037415, 716, 195, 1076, 294], 
[‘diningtable’, 0.9959062933921814, 30, 252, 1234, 707],
[‘chair’, 0.9931201934814453, 460, 4, 768, 275],
[‘bottle’, 0.9758939743041992, 591, 307, 661, 542],
[‘cup’, 0.8904291391372681, 664, 546, 775, 660],
[‘bottle’, 0.7830495834350586, 451, 388, 546, 633],
[‘scissors’, 0.6897088289260864, 858, 412, 933, 536],
[‘spoon’, 0.6690514087677002, 222, 419, 392, 452],

You can see the output as bounding boxes and the class labels. You can install the cv2 libraries to print the image as well.

That’s it. It is that simple to run any model in docker. In the next few posts, we will check how to run vision or deep learning models using NVIDIA GPU. We will also see how to communicate between different containers over network and deploy them.

References :

https://medium.com/analytics-vidhya/object-detection-using-yolo-v3-and-deploying-it-on-docker-and-minikube-c1192e81ae7a

https://abhishekbose550.medium.com/deep-learning-for-production-deploying-yolo-using-docker-2c32bb50e8d6

--

--

shreyash
shreyash

No responses yet