YOLO algorithm is a very famous target detection algorithm. You Only Look Once: Unified, real-time Object Detection

  • Look Once: algorithm of one-stage (one-shot object), it is an algorithm of two tasks of object detection in one step.
  • Unified: provides end-to-end training and prediction.
  • Real-time: real-time performance. FPS 45 and mAP 63.4 are the indicators given in the original paper.

YOLOv4: Optimal Speed and Accuracy of Object Detection, which was published in April this year, adopts many excellent optimization techniques in the CNN field in recent years. It balances accuracy and speed and has the highest accuracy among real-time target detection algorithms.

Thesis Address:

  • YOLO: arxiv.org/abs/1506.02…
  • YOLO v4: arxiv.org/abs/2004.10…

Source code address:

  • YOLO: github.com/pjreddie/da…
  • YOLO v4: github.com/AlexeyAB/da…

This article introduces the official Darknet implementation of YOLOv4 and how to use it for Docker compilation. And how to select some objects from MS COCO 2017 dataset to train the model.

The main contents are:

  • Prepare the Docker image
  • Prepare the COCO dataset
  • Extrapolation using a pre-training model
  • Prepare the COCO data subset
  • Train your models and make inferences
  • Refer to the content

Prepare the Docker image

First, prepare the Docker, please see: Docker: Nvidia Driver, Nvidia Docker recommended installation steps.

After that, prepare the image, from bottom to top:

  • Nvidia/cuda: hub.docker.com/r/nvidia/cu…
  • OpenCV: github.com/opencv/open…
  • Darknet: github.com/AlexeyAB/da…

nvidia/cuda

Prepare Nvidia Base CUDA images. We chose CUDA 10.2 instead of the latest CUDA 11 because PyTorch and others are still 10.2.

Pull mirror:

Docker pull nvidia/cuda: 10.2 - cudnn7 - devel - ubuntu18.04Copy the code

Test image:

$docker run --gpus all Nvidia/CUDA :10.2- CUDN7 - vel- Ubuntu18.04 Nvidia - smI Sun Aug 8 00:00:00 2020 + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + | NVIDIA SMI 440.100 Driver Version: CUDA 440.100 Version: 10.2 | | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + | GPU Name Persistence -m | Bus - Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | |===============================+======================+======================| | 0 GeForce RTX 208... On | Off | 00000000:07:00. 0 N/A | | 0% 48 c | 340 mib P8 14 w / 300 w / 11016 mib 2% Default | | +-------------------------------+----------------------+----------------------+ | 1 GeForce RTX 208... Off | 00000000:08:00. Off | 0 N/A | | 0% 45 c P8 | 1 mib 19 w / 300 w / 11019 mib | 0% Default | +-------------------------------+----------------------+----------------------+ +-----------------------------------------------------------------------------+ | Processes: GPU Memory | | GPU PID Type Process name Usage | | = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = | +-----------------------------------------------------------------------------+Copy the code

OpenCV

Build OpenCV image based on NVIDIA/CUDA image:

cdDocker/Ubuntu18.04-Cuda10.2 / Opencv4.4.0 / docker build \ -i Joinaero/Ubuntu18.04-Cuda10.2: Opencv4.4.0 \ --build-arg Opencv_ver = 4.4.0 \ - build - arg opencv_url=https://gitee.com/cubone/opencv.git \ - build - arg opencv_contrib_url=https://gitee.com/cubone/opencv_contrib.git \ .Copy the code

Its Dockerfile can be seen here: github.com/ikuokuo/sta… .

Darknet

Build a Darknet image based on OpenCV image:

cdDocker/ubuntu18.04 - cuda10.2 / opencv4.4.0 / darknet/docker build \ - t joinaero/ubuntu18.04 - cuda10.2: opencv4.4.0 - darknet \.Copy the code

Its Dockerfile can be seen here: github.com/ikuokuo/sta… .

The image has been uploaded to the Docker Hub. If the Nvidia driver supports CUDA 10.2, you can pull the image directly:

Docker pull joinaero/ubuntu18.04 - cuda10.2: opencv4.4.0 - darknetCopy the code

Prepare the COCO dataset

Download MS COCO 2017 at cocodataset.org/#download

Images, including:

  • 2017 Train images [118K/18GB]
    • Images.cocodataset.org/zips/train2…
  • 2017 Val images [5K/1GB]
    • Images.cocodataset.org/zips/val201…
  • 2017 Test images [41K/6GB]
    • Images.cocodataset.org/zips/test20…
  • 2017 Unlabeled images [123K/19GB]
    • Images.cocodataset.org/zips/unlabe…

Notes, including:

  • 2017 Train/Val annotations [241MB]
    • Images.cocodataset.org/annotations…
  • 2017 Stuff Train/Val annotations [1.1GB]
    • Images.cocodataset.org/annotations…
  • 2017 Panoptic Train/Val annotations [821MB]
    • Images.cocodataset.org/annotations…
  • 2017 Testing Image info [1MB]
    • Images.cocodataset.org/annotations…
  • 2017 Unlabeled Image info [4MB]
    • Images.cocodataset.org/annotations…

Extrapolation using a pre-training model

Pre-training model yolov4. Weights, download github.com/AlexeyAB/da… .

Run the image:

xhost +local:docker

docker run -it --gpus all \
-e DISPLAY \
-e QT_X11_NO_MITSHM=1 \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v $HOME/.Xauthority:/root/.Xauthority \
--name darknet \
--mount type=bind.source=$HOME/Codes/devel/datasets/coco2017,target=/home/coco2017 \
--mount type=bind.source=$HOME/ Codes/devel/models/yolov4, target = / home/yolov4 \ joinaero/ubuntu18.04 - cuda10.2: opencv4.4.0 - darknetCopy the code

To infer:

./darknet detector testCFG/coco. Data CFG/yolov4. CFG/home/yolov4 / yolov4 weights \ - 0.25 ext_output thresh - show - out /home/coco2017/result.json \ /home/coco2017/test2017/000000000001.jpgCopy the code

Inferred results:

Prepare the COCO data subset

The MS COCO 2017 dataset has 80 object labels. We take the objects we care about and reorganize the subsets.

First, sample code is obtained:

git clone https://github.com/ikuokuo/start-yolov4.git
Copy the code
  • Scripts /coco2yolo. Py: script for transferring COCO data sets to YOLO data sets
  • Scripts /coco/label.py: What are the object labels of the coco dataset
  • CFG /coco/ coke.names: Edit those object labels we want

After that, prepare the data set:

cd start-yolov4/
pip install -r scripts/requirements.txt

export COCO_DIR=$HOME/Codes/devel/datasets/coco2017

# train
python scripts/coco2yolo.py \
--coco_img_dir $COCO_DIR/train2017/ \
--coco_ann_file $COCO_DIR/annotations/instances_train2017.json \
--yolo_names_file ./cfg/coco/coco.names \
--output_dir ~/yolov4/coco2017/ \
--output_name train2017 \
--output_img_prefix /home/yolov4/coco2017/train2017/

# valid
python scripts/coco2yolo.py \
--coco_img_dir $COCO_DIR/val2017/ \
--coco_ann_file $COCO_DIR/annotations/instances_val2017.json \
--yolo_names_file ./cfg/coco/coco.names \
--output_dir ~/yolov4/coco2017/ \
--output_name val2017 \
--output_img_prefix /home/yolov4/coco2017/val2017/
Copy the code

Data set with the following contents:

~/yolov4/coco2017/
├── train2017/
│   ├── 000000000071.jpg
│   ├── 000000000071.txt
│   ├── ...
│   ├── 000000581899.jpg
│   └── 000000581899.txt
├── train2017.txt
├── val2017/
│   ├── 000000001353.jpg
│   ├── 000000001353.txt
│   ├── ...
│   ├── 000000579818.jpg
│   └── 000000579818.txt
└── val2017.txt
Copy the code

Train your models and make inferences

Prepare necessary documents

  • cfg/coco/coco.names <cfg/coco/coco.names.bak has original 80 objects>

    • Edit: keep desired objects
  • cfg/coco/yolov4.cfg <cfg/coco/yolov4.cfg.bak is original file>

    • Download yolov4.cfg, then changed:
    • batch= 64,subdivisions=32 <32 for 8-12 GB GPU-VRAM>
    • width= 512,height=512 <any value multiple of 32>
    • classes=<your number of objects in each of 3 [yolo]-layers>
    • max_batches=<classes*2000, but not less than number of training images and not less than 6000>
    • steps> = <, max_batches max_batches * 0.8 * 0.9
    • filters=<(classes+5)x3, in the 3 [convolutional] before each [yolo] layer>
    • filters=<(classes+9)x3, in the 3 [convolutional] before each [Gaussian_yolo] layer>
  • cfg/coco/coco.data

    • Edit: train.valid to YOLO datas
  • csdarknet53-omega.conv.105

    • Download csdarknet53-omega_final.weights, then run:
    docker run -it --rm --gpus all \
    --mount type=bind.source=$HOME/ Codes/devel/models/yolov4, target = / home/yolov4 \ joinaero/ubuntu18.04 - cuda10.2: opencv4.4.0 - darknet. / darknet partial cfg/csdarknet53-omega.cfg /home/yolov4/csdarknet53-omega_final.weights /home/yolov4/csdarknet53-omega.conv.105 105Copy the code

Train your model

Run the image:

cd start-yolov4/

xhost +local:docker

docker run -it --gpus all \
-e DISPLAY \
-e QT_X11_NO_MITSHM=1 \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v $HOME/.Xauthority:/root/.Xauthority \
--name darknet \
--mount type=bind.source=$HOME/Codes/devel/models/yolov4,target=/home/yolov4 \
--mount type=bind.source=$HOME/yolov4/coco2017,target=/home/yolov4/coco2017 \
--mount type=bind.source=$PWD/ CFG/coco, target = / home/CFG \ joinaero/ubuntu18.04 - cuda10.2: opencv4.4.0 - darknetCopy the code

To train:

mkdir -p /home/yolov4/coco2017/backup

# Training command
./darknet detector train /home/cfg/coco.data /home/cfg/yolov4.cfg /home/yolov4/csdarknet53-omega.conv.105 -map
Copy the code

You can interrupt your training and continue like this:

# Continue training
./darknet detector train /home/cfg/coco.data /home/cfg/yolov4.cfg /home/yolov4/coco2017/backup/yolov4_last.weights -map
Copy the code

Yolov4_last. weights 100 iterations are recorded.

After 1000 iterations, add the parameter -gpus 0,1 and continue:

# How to train with multi-GPU
# 1. Train it first on 1 GPU for like 1000 iterations
# 2. Then stop and by using partially-trained model `/backup/yolov4_1000.weights` run training with multigpu./darknet detector train /home/cfg/coco.data /home/cfg/yolov4.cfg /home/yolov4/coco2017/backup/yolov4_1000.weights -gpus 0, 1 - the mapCopy the code

The training process was recorded as follows:

If -map is added, a red line map is displayed in the figure above.

View the model mAP@IoU=50 precision:

$ ./darknet detector map /home/cfg/coco.data /home/cfg/yolov4.cfg /home/yolov4/coco2017/backup/yolov4_final.weights
...
Loading weights from /home/yolov4/coco2017/backup/yolov4_final.weights...
 seen 64, trained: 384 K-images (6 Kilo-batches_64)
Done! Loaded 162 layers from weights-file

 calculation mAP (mean average precision)...
 Detection layer: 139 - type = 27
 Detection layer: 150 - type = 27
 Detection layer: 161 - typeDetections_count = 745, unique_truth_count = 190 class_id = 0, name = train, ap = 80.61% (TP = 142, FP = 18)forConf_thresh = 0.25, Precision = 0.89, recall = 0.75, F1-score = 0.81forConf_thresh = 0.25, TP = 142, FP = 18, FN = 48, Average IoU = 75.31% IoU threshold = 50%, used area-under-curveforEach unique Recall mean average precision ([email protected]) = 0.806070, or 80.61% Total Detection Time: 4 SecondsCopy the code

To infer:

./darknet detector test /home/cfg/coco.data /home/cfg/yolov4.cfg /home/yolov4/coco2017/backup/yolov4_final.weights \
-ext_output -show /home/yolov4/coco2017/val2017/000000006040.jpg
Copy the code

Inferred results:

Refer to the content

  • Train Detector on MS COCO (trainvalno5k 2014) dataset
  • How to evaluate accuracy and speed of YOLOv4
  • How to train (to detect your custom objects)

conclusion

Why Docker? Docker export image to simplify environment deployment. PyTorch, for example, is also mirrored and can be used directly.

What else about Darknet? Next time, Darknet is compiled in Ubuntu and uses the Python interface.

Let’s go coding ~


Share practical tips and knowledge in Coding! Welcome to pay attention and grow together!