Создание среды Yolov4
Окружение здесь примерно такое же, как и у yolov3, разница в основном в предпоездных весах и условных соединениях.
Cloning and Building Darknet
clone darknet from AlexeyAB's famous repository,
git clone https://github.com/AlexeyAB/darknet
adjust the Makefile to enable OPENCV and GPU for darknet
cd darknet
sed -i 's/OPENCV=0/OPENCV=1/' Makefile
sed -i 's/GPU=0/GPU=1/' Makefile
sed -i 's/CUDNN=0/CUDNN=1/' Makefile
sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/' Makefile
build darknet
make
Pre-trained yolov4 weights
YOLOv4 has been trained already on the coco dataset which has 80 classes that it can predict.
wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
Test env Enabled
./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights data/person.jpg
Multiple Images at Once
-
make a
.txt
file which has the paths to several images want to be detected at oncedata/person.jpg data/horses.jpg data/giraffe.jpg data/dog.jpg
-
save result to
.json
file./darknet detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights -ext_output -dont_show -out result.json < list.txt
[{ "frame_id": 1, "filename": "data/person.jpg", "objects": [{ "class_id": 17, "name": "horse", "relative_coordinates": { "center_x": 0.783550, "center_y": 0.566949, "width": 0.335207, "height": 0.486880 }, "confidence": 0.997604 }, { "class_id": 16, "name": "dog", "relative_coordinates": { "center_x": 0.206590, "center_y": 0.722102, "width": 0.229715, "height": 0.210134 }, "confidence": 0.994348 }, { "class_id": 0, "name": "person", "relative_coordinates": { "center_x": 0.364771, "center_y": 0.558493, "width": 0.134738, "height": 0.669826 }, "confidence": 0.999949 } ] }, //... ]
Yolo command line flags
-
-thresh
: add a threshold for confidences on the detections, only detections with a confidence level above the threshold will be returned -
-dont_show
: not have the image outputted after running darknet -
-ext_output
: output bounding box coordinatesdog: 99% (left_x: 59 top_y: 262 width: 147 height: 89) person: 100% (left_x: 190 top_y: 95 width: 86 height: 284) horse: 100% (left_x: 394 top_y: 137 width: 215 height: 206)
Пользовательский набор данных для обучения Yolov4
Общий метод тот же, что и у yolov3, но на вчерашнем обучении использовались более зрелые решения, и конкретные шаги не до конца понятны, поэтому сегодня я решил еще раз подробно изучить с нуля.
- Labeled Custom Dataset
- Custom .cfg file
- obj.data and obj.names files
- train.txt file (test.txt is optional here as well)
Gathering and Labeling a Custom Dataset
Using Google's Open Images Dataset
Потому что задачи лаборатории специфичны для нескольких видов игрушек, и жестких требований по масштабируемости нет. Таким образом, набор данных с открытым исходным кодом Google используется только для обучения, или метод маркировки набора данных сам по себе используется для создания набора данных.
Manually Labeling Images with labelImg(Annotation Tool)
На данный момент подготовлены наборы данных для обучения и проверки.
Configuring Files for Training
cfg file
edit the yolov4.cfg
to fit the needs based on the object detector
-
bash=64
&subdivisions=16
: Параметры, рекомендованные в ИнтернетеЗдесь ограничена мощность сервера, установленная SubDivision 32, но скорость все равно очень низкая
-
classes=4
in the three YOLO layers -
filters=(classes + 5) * 3
: three convolutional layers before the YOLO layers -
width=416
&height=416
: any multiple of 32, 416 is standard- improve results by making value larger like 608 but will slow down training
-
max_batches=(# of classes) * 2000
: but no less than 6000 -
steps=(80% of max_batches), (90% of max_batches)
-
random=1
: if run into memory issues or find the training taking a super long time, change three yolo layers from 1 to 0 to speed up training but slightly reduce accurancy of model
obj.names
one class name per line in the same order as dataset generation step
NOTE: don't have spaces in class name, use _
for replacement
sheep
giraffe
cloud
snow
obj.data
classes= 4
train = data/train.txt
valid = data/test.txt
names = data/obj.names
backup = backup
-
backup
: where save the weights to of the model throughout training
train.txt and test.txt
hold the reletive paths to all the training images and valididation images, it contain one line for each training image path or validation image path
Train Custom Object Detector
Download pre-trained weights for the convolutional layers. By using these weights it helps custom object detector to be way more accurate and not have to train as long.
wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137
train
./darknet detector train ../../data/obj.data cfg/yolov4_custom.cfg yolov4.conv.137 -dont_show
Checking the mAP of the Model
mAP: mean average precision
./darknet detector map ../../data/obj.data cfg/yolov4_custom.cfg backup/yolov4_custom_last.weights
the highest mAP, the most accurate is