Target detection | YOLO v3 configuration notes -- reprint

Posted by TripleDES on Fri, 04 Mar 2022 01:28:21 +0100

Source: https://blog.csdn.net/xiao_lxl/article/details/85047103

YOLO v3 configuration

Get the YOLO v3 installation file and compile it

git clone https://github.com/pjreddie/darknet
cd darknet
make

   
  • 1
  • 2
  • 3

Download the trained YOLO v3 model

wget https://pjreddie.com/media/files/yolov3.weights

   
  • 1

Detection effect

./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

   
  • 1

see the detect:

105 conv    255  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 255  0.754 BFLOPs
  106 yolo
Loading weights from yolov3.weights...Done!
data/dog.jpg: Predicted in 0.043505 seconds.
dog: 100%
truck: 92%
bicycle: 99%

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Use detector test mode to detect pictures

./darknet detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights data/dog.jpg

   
  • 1

see the detect:

105 conv    255  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 255  0.754 BFLOPs
  106 yolo
Loading weights from yolov3.weights...Done!
data/dog.jpg: Predicted in 0.043032 seconds.
dog: 100%
truck: 92%
bicycle: 99%

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Set the threshold to 0

./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg -thresh 0

   
  • 1

yolov3 detects video files

./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights /media/****/****/video/aa.mp4

   
  • 1

Tiny YOLOv3

Get Tiny YOLOv3 model

 wget https://pjreddie.com/media/files/yolov3-tiny.weights

   
  • 1

Tiny YOLOv3 model detection picture

./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg

   
  • 1

Training Pasal VOC dataset

Get dataset

First, soft connect the VOC database on the local computer

cd data
ln -s /media/****/****/data/VOCdevkit .

   
  • 1
  • 2

If there is no VOC database on this machine, it can be downloaded online

wget https://pjreddie.com/media/files/VOCtrainval_11-May-2012.tar
wget https://pjreddie.com/media/files/VOCtrainval_06-Nov-2007.tar
wget https://pjreddie.com/media/files/VOCtest_06-Nov-2007.tar
tar xf VOCtrainval_11-May-2012.tar
tar xf VOCtrainval_06-Nov-2007.tar
tar xf VOCtest_06-Nov-2007.tar

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Make label file

The VOC in the scripts folder_ label. Copy the PY file to the data path

cp ../scripts/voc_label.py .

   
  • 1

Generate label file

python voc_label.py
cat 2007_train.txt 2007_val.txt 2012_*.txt > train.txt

   
  • 1
  • 2

Modify configuration parameters

Modify Cfg for Pascal VOC Data cfg/voc.data

classes= 20
train  = /media/****/****/darknet-yolov3/darknet/data/train.txt
valid  = /media/****/****/darknet-yolov3/darknet/data/2007_test.txt
names = data/voc.names
backup = backup

   
  • 1
  • 2
  • 3
  • 4
  • 5

Pay attention to modify CFG / yolov3 VOC Parameters of CFG

   [net]
    # Testing
    # batch=1
    # subdivisions=1
    # Training
     batch=64
     subdivisions=16

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

During training, batch=64, subdivisions=16,
When testing, batch=1, subdivisions=1

Download the pre training model darknet53

wget https://pjreddie.com/media/files/darknet53.conv.74

   
  • 1

Training model

./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74

   
  • 1

Training model

Modify CFG / yolov3 VOC CFG parameters, otherwise the target will not be detected, and the detection box is not drawn

 [net]
        # Testing
        batch=1
        subdivisions=1
        # Training
	  # batch=64
	  #subdivisions=16

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
./darknet detector demo cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights /media/****/****/video/aa.mp4

./darknet detector test cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights data/dog.jpg -thresh 0.01

  • 1
  • 2
  • 3
  • 4

The detection video is 30 ~ 47fps, and the detection single picture is 0.022s

Verify training results

Batch test chart and output test result text

./darknet detector valid cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc.backup -out ""

 
  • 1

When the terminal returns to use, it is in/ result.

Use VOC under py fast RCNN_ eval. Py calculate mAP

Py fast RCNN / lib / datasets / VOC_ eval. Copy py to the darknet root directory
voc_eval.py Download: https://github.com/rbgirshick/py-faster-rcnn/blob/master/lib/datasets/voc_eval.py

VOC in py fast RCNN_ eval. Py interpretation
https://blog.csdn.net/qq_34806812/article/details/82018072

New compute_mAP.py

from voc_eval import voc_eval
print(voc_eval('/home/****/****/darknet/results/{}.txt', '/home/****/****/darknet/voc/VOCdevkit/VOC2012/Annotations/{}.xml', '/home/****/****/darknet/voc/2012_val.txt', 'egret', '.'))

 
  • 1
  • 2

The first parameter is the txt path of the previous step,
The second is the xml tag path corresponding to the validation set,
The third is the txt text path of the verification set,
The fourth is the class alias to be verified,
The fifth is the path to save the pkl file
Note: the txt content of the verifier must be a picture name without path and suffix.

Execute compute with python 3_ mAP. py

Repeated execution, detection of other categories needs to be deleted/ darknet/annots.pkl, or change compute_ mAP. Path to save pkl file in PY
voc_ eval. The last value returned in py is AP, and the detection single class is mAP

Calculate recall

Modify examples / detector Validate under c_ detector_ Recall function

  1. Modify validate_ detector_ Definition of recall() function
//void validate_detector_recall(char *cfgfile, char *weightfile)
void validate_detector_recall(char *datacfg, char *cfgfile, char *weightfile)

 
  • 1
  • 2
  1. Modify validate_ detector_ Call of recall() function
  //else if(0==strcmp(argv[2], "recall")) validate_detector_recall(cfg, weights);
else if(0==strcmp(argv[2], "recall")) validate_detector_recall(datacfg,cfg, weights);

 
  • 1
  • 2
  1. Replace list *plist=get_paths(“data/coco_val_5k.list”); list *plist=get_paths(“voc/train.txt”); Own training set text
//list *plist = get_paths("data/coco_val_5k.list");
//char **paths = (char **)list_to_array(plist);

list options = read_data_cfg(datacfg);
char valid_images = option_find_str(options, "valid", "/media/**/801328a5-39c6-4e08-b070-19fc662a5236/darknet-yolov3/darknet/data/2007_val.txt");
list *plist = get_paths(valid_images);
char **paths = (char **)list_to_array(plist);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. Modify nboxes in the for loop.
 //for(k = 0; k < l.w*l.h*l.n; ++k){
for(k = 0; k < nboxes; ++k){

 
  • 1
  • 2

Pay attention to the above four modifications, otherwise the IOU may be NAN.

Modified examples / detector make again after C

./darknet detector recall cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc.backup

 
  • 1

Result: the last column is the recall value.

Directly calculate the mAP of all single categories and the total mAP

New computer_all_mAP.py

from voc_eval import voc_eval
import os
current_path = os.getcwd()
results_path = current_path+"/results"
sub_files = os.listdir(results_path)
mAP = []
for i in range(len(sub_files)):
    class_name = sub_files[i].split(".txt")[0]    rec, prec, ap = voc_eval('/home/peidashun/projects/darknet/results/{}.txt', '/home/peidashun/projects/darknet/voc/VOCdevkit/VOC2018/Annotations/{}.xml', '/home/peidashun/projects/darknet/voc/2018_test.txt', class_name, '/home/peidashun/projects/darknet/voc/VOCdevkit/VOC2018/mAP')
    print("{} :\t {} ".format(class_name, ap))
    mAP.append(ap)
mAP = tuple(mAP)
print("***************************")
print("mAP :\t {}".format( float( sum(mAP)/len(mAP)) ))

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Note: the results folder can only have 'class name' Txt 'file, 2018_test.txt file contains the picture names of txt files in all results (it is guaranteed to be a common verification set), and there is no pkl file in the final output path.
The AP in the above code is the AP calculated for the input single category.

Training COCO dataset

Download coco dataset

cp scripts/get_coco_dataset.sh data
cd data
bash get_coco_dataset.sh

 
  • 1
  • 2
  • 3

Where, get_coco_dataset.sh is

#!/bin/bash

Clone COCO API

git clone https://github.com/pdollar/coco
cd coco

mkdir images
cd images

Download Images

wget -c https://pjreddie.com/media/files/train2014.zip
wget -c https://pjreddie.com/media/files/val2014.zip

Unzip

unzip -q train2014.zip
unzip -q val2014.zip

cd ...

Download COCO Metadata

wget -c https://pjreddie.com/media/files/instances_train-val2014.zip
wget -c https://pjreddie.com/media/files/coco/5k.part
wget -c https://pjreddie.com/media/files/coco/trainvalno5k.part
wget -c https://pjreddie.com/media/files/coco/labels.tgz
tar xzf labels.tgz
unzip -q instances_train-val2014.zip

Set Up Image Lists

paste <(awk "{print "KaTeX parse error: Can't use function '\"' in math mode at position 4: PWD\̲"̲}" &lt;5k.part)...PWD"}" <trainvalno5k.part) trainvalno5k.part | tr -d '\t' > trainvalno5k.txt

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Modify the configuration parameter CFG / coco data

classes= 80
train  = /media/****/****/data/darkbet-coco/coco/trainvalno5k.txt
valid  = /media/****/****/data/darkbet-coco/coco/5k.txt
#valid = data/coco_val_5k.list
names = data/coco.names
backup = backup-coco
eval=coco
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Modify the configuration file Yolo cfg

Yolov3 A copy of CFG, renamed yolov3 coco CFG, and modify

[net]
# Testing
# batch=1
# subdivisions=1
# Training
batch=64
subdivisions=63

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

train

Training with two GPU s

./darknet detector train cfg/coco.data cfg/yolov3-coco.cfg darknet53.conv.74 -gpus 1,2

 
  • 1

Reference

[1] https://pjreddie.com/darknet/yolo/
[2] https://github.com/pjreddie/darknet

Topics: Deep Learning yolov3