Skip to content

gmontamat/python-darknet-docker

Repository files navigation

Darknet & Python Docker Images

Docker Cloud Automated build GitHub Workflow Status Docker Pulls GitHub

Python Docker image with the Darknet package included. These images eliminate the burden of compiling Darknet's library (libdarknet.so) and import it into Python using its wrapper to create YOLOv4, v3, v2 sample apps. Based on daisukekobayashi's darknet-docker images.

Base Image Tags

CPU images are based on Ubuntu Docker Official Images (ubuntu:20.04) and GPU images are based on nvidia/cuda (nvidia/cuda:11.2.2-cudnn8-runtime-ubuntu20.04). Images include python3.8, an updated version of pip, and the following libraries:

Tags indicate whether the image supports GPU or not and which base image to use. They also refer to different flags in the Makefile for building the library. You can check the meaning of each flag here.

  • cpu tag indicates images are built with AVX=1 and OPENMP=1
    • The noopt tag means the AVX option is disabled (AVX=0). If you use a cpu image and get errors, try this tag.
  • cv tag means the library is built with the OPENCV=1 flag
  • gpu tag means images are built with GPU=1 and CUDNN=1
    • The cc** tag indicates compute compatibility of GPU. Images with this tag are optimized for a certain GPU architecture. You can check compute compatibility of your GPU here. If compute compatibility is greater than or equal to 7.0, images are built with CUDNN_HALF=1.
  • u1804 tag means the images are based on ubuntu:18.04 (when CPU-based) or nvidia/cuda:10.1-cudnn7-runtime-ubuntu18.04 (when GPU-based), and include python3.6 instead of python3.8. Note that cc80 and cc86 tags are not compatible with ubuntu:18.04 base images.

Usage

Basic

The python interpreter runs by default when you start the container:

$ docker run -it --rm gmontamat/python-darknet:cpu
Python 3.8.10 (default, Jun  2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import darknet
>>> import numpy as np
>>> import cv2
>>> 

Run custom code

Use volumes to share Python code in /usr/src/app, define it as the working directory, and specify the command to be run. The following example shows how to do this using the code included which is taken from the Darknet repository:

$ git clone https://github.com/gmontamat/python-darknet-docker.git
$ cd python-darknet-docker
$ wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights \
       -O test/yolov4.weights
$ xhost +
$ docker run --gpus all -it --rm -v $(realpath ./test):/usr/src/app \
             -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY \
             -w /usr/src/app gmontamat/python-darknet:gpu python3 darknet_images.py \
             --input sample.jpg --weights yolov4.weights \
             --config_file cfg/yolov4.cfg --data_file cfg/coco.data

Check the troubleshooting docker GUI apps section for more information about the xhost command.

Video inference

Python is not the most suitable language to handle video streams. Compared to other compiled languages like C++ or SDKs such as NVIDIA DeepStream, video inference won't run as smoothly. However, it is possible to perform real-time tracking on Python using its multiprocessing module and a powerful GPU. YOLOv4 & SORT run at 15fps on a system with an Intel Core i7 and a GeForce GTX 1080. Mobile GPUs such as the Quadro M1000M run YOLOv4 at 3fps but can run YOLOv4-Tiny at 60fps.

You can test video inference using the sample code in this repo taken from the Darknet repository:

$ git clone https://github.com/gmontamat/python-darknet-docker.git
$ cd python-darknet-docker
$ wget https://github.com/intel-iot-devkit/sample-videos/raw/master/face-demographics-walking.mp4 \
       -O test/face-demographics-walking.mp4
$ wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights \
       -O test/yolov4-tiny.weights
$ xhost +
$ docker run --gpus all -it --rm -v $(realpath ./test):/usr/src/app \
             -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY \
             -w /usr/src/app gmontamat/python-darknet:gpu python3 darknet_video.py \
             --input face-demographics-walking.mp4 --weights yolov4-tiny.weights \
             --config_file cfg/yolov4-tiny.cfg --data_file cfg/coco.data

Or, if you prefer using your webcam (/dev/video0 on Linux):

$ git clone https://github.com/gmontamat/python-darknet-docker.git
$ cd python-darknet-docker
$ wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights \
       -O test/yolov4-tiny.weights
$ xhost +
$ docker run --gpus all -it --rm -v $(realpath ./test):/usr/src/app \
             -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY \
             --device=/dev/video0 -w /usr/src/app gmontamat/python-darknet:gpu \
             python3 darknet_video.py --weights yolov4-tiny.weights \
             --config_file cfg/yolov4-tiny.cfg --data_file cfg/coco.data

Troubleshooting docker GUI apps

Running xhost + grants any local user access to your X screen. That might be OK for a single-user machine, but usually not for a multi-user system. To get things back to normal, with controlled access to the X screen, run xhost -.

If you skip this command, you may encounter an error message like the following:

No protocol specified
Error: Can't open display X:X

It can also be fixed by running xhost local:root before you start the container, if you use Docker with sudo. Or, if you use the docker group to run containers without sudo, run xhost local:docker.

Advanced usage

These images can serve as base images for more complex Docker applications where additional packages and dependencies are required. In such case, begin the Dockerfile with:

FROM gmontamat/python-darknet:gpu

TODO

  • Use GitHub Actions instead of DockerHub hooks
  • Support other python versions (3.7/3.9)
  • Use different base images (ubuntu:18.04/ubuntu:20.04)
  • Compile OpenCV python library for CUDA support instead of just using pre-built binaries from opencv-python

These images are still a work in progress. Feel free to submit feature requests in the issues page.