输入端,yolov5在yolov4基础上,将自适应锚框计算嵌套入代码中,不比yolov4是一个单独程序;Mosaic数据增强中,新增对照片的裁剪、缩放,排比变换。 backbone中,Focus结构能对特征图进行切片操作(如:4x4x3>&...
首先让咱们对yolov5有一定了解
输入端,yolov5在yolov4基础上,将自适应锚框计算嵌套入代码中,不比yolov4是一个单独程序;Mosaic数据增强中,新增对照片的裁剪、缩放,排比变换。
backbone中,Focus结构能对特征图进行切片操作(如:4x4x3>>2x2x12)
Prediction中,计算损失函数变为GIOU-LOSS,不同于yolov4中的CIOU-LOSS(个人认为是不是CIOU-LOSS更好呢?)
训练开始

代码下载

大家可以去github或者gitee下载yolov5源码,我是去gitee下载的源码,github有时候实在进不去。

coco数据集下载

数据集下载好后(训练集,验证集,测试集都要有相应TXT文件,不然会报错),数据集位置不用移动,可以将源码中的文件coco.yaml中的path、train、val、test路径修改

依赖下载

这里建议大家使用镜像下载,我在师兄帮助下使用的清华镜像

detect.py运行

环境依赖安装好了后,可以运行detect.py看看是否能成功运行

train.py运行

服务器上,nvidia驱动不够,cuda版本为10.0,导致GPU运行不起来,CPU训练一个epoch需要8个小时,所以我一共训练了 21个epoch。训练出来的模型在最新的权重文件中。
前言 最近在用YOLOv4做大规模检测,

训练coco数据集单类

前言
最近在用YOLOv4做大规模检测,但是发现这方面的资料实在是太少了。就连大规模检测是干什么的也没有一个较准确的定义。导师给我的任务是密集人群检测,就是场景中有很多人保证检测的准确性。
正好最近在用yolov4,想着用v4去训练一下coco中的人单个类别。坐下实验,看下效果。
之后再拿yolov5训练一下。
实验

数据集准备

先下载coco数据集,我用的是coco2017数据集,下载地址网上搜一下,有很多,我的博客里面也有。

提取单类别(以人为例)

并没有使用网上那种从json文件中挑选出哪一类再生成id的方法,太麻烦了。所幸我从YOLOv5的github项目中找到了TXT格式的label文件,从图片名的label文件中找是否有你需要的类别的id,如果有就根据label文件名去找对应的image。
代码如下:
import os import shutil import inspect def select_person(source_label_path, output_label_path, source_images_path, output_images_path):if not os.path.exists(output_label_path): os.makedirs(output_label_path)if not os.path.exists(output_images_path): os.makedirs(output_images_path)for txt_filein os.listdir(source_label_path):# print(txt_file)# 得到了txt_file的集合 txt_name, extension= os.path.splitext(txt_file)# print(txt_name) with open(os.path.join(source_label_path, txt_file),"r") as f: fb= f.readlines()for linein fb[:]:# line = line.strip() # 错误的,使用这句会将换行符 /n 去掉,使输出变为一行 linelist= line.split(" ") first= linelist[0]# output_file = os.path.join(output_path, txt_file)if"0"== linelist[0]: outfile= open(os.path.join(output_label_path, txt_file),"a+")# w这种打开方式会每次打开都把以前的覆盖掉,使用a或者a+,追加写 outfile.write(line) src= os.path.join(source_images_path, txt_name +".jpg") dst= os.path.join(output_images_path, txt_name +".jpg") shutil.copy(src, dst) print("已完成", dst)if __name__=='__main__': train_source_labels="/home/xiaopeng/coco/labels/train2017" train_output_labels="/home/xiaopeng/coco_only_person/labels/train2017" train_source_images="/home/xiaopeng/coco/images/train2017" train_output_images="/home/xiaopeng/coco_only_person/images/train2017" select_person(train_source_labels, train_output_labels, train_source_images, train_output_images) valid_source_labels="/home/xiaopeng/coco/labels/val2017" valid_output_labels="/home/xiaopeng/coco_only_person/labels/val2017" valid_source_images="/home/xiaopeng/coco/images/val2017" valid_output_images="/home/xiaopeng/coco_only_person/images/val2017" select_person(valid_source_labels, valid_output_labels, valid_source_images, valid_output_images)

结果:

对于person来说,训练集中有64115张图片,验证集有2693张。

训练

因为我的数据集和darknet程序是独立的,如下,
所以我使用如下命令:
./darknet detector train data/obj.data cfg/yolov4.cfg../backup/yolov4_last.weights -map
哦对了,cfg文件还有,data文件都要修改。这部分很简单官网都有,就不赘述了。

开始训练

YOLOv4结果

垃圾显卡得1964小时…
10000batch的APdetections_count= 188270, unique_truth_count= 10777 class_id= 0, name= person, ap= 24.21%(TP= 6760, FP= 16472)for conf_thresh= 0.25, precision= 0.29, recall= 0.63, F1-score= 0.40for conf_thresh= 0.25, TP= 6760, FP= 16472, FN= 4017, average IoU= 22.16 % IoU threshold= 50 %, used Area-Under-Curvefor each unique Recall mean average precision(mAP@0.50)= 0.242147, or 24.21 % Total Detection Time: 305 Seconds20000个batch的APLoading weights from../backup/yolov4_20000.weights... seen 64, trained: 1280 K-images(20 Kilo-batches_64) Done! Loaded 162 layers from weights-file calculation mAP(mean average precision)... Detection layer: 139 -type= 28 Detection layer: 150 -type= 28 Detection layer: 161 -type= 28 2696 detections_count= 135166, unique_truth_count= 10777 class_id= 0, name= person, ap= 68.28%(TP= 7521, FP= 4854)for conf_thresh= 0.25, precision= 0.61, recall= 0.70, F1-score= 0.65for conf_thresh= 0.25, TP= 7521, FP= 4854, FN= 3256, average IoU= 48.22 % IoU threshold= 50 %, used Area-Under-Curvefor each unique Recall mean average precision(mAP@0.50)= 0.682761, or 68.28 % Total Detection Time: 297 Seconds30000个batch的APLoading weights from../backup/yolov4_30000.weights... seen 64, trained: 1920 K-images(30 Kilo-batches_64) Done! Loaded 162 layers from weights-file calculation mAP(mean average precision)... Detection layer: 139 -type= 28 Detection layer: 150 -type= 28 Detection layer: 161 -type= 28 2696 detections_count= 74317, unique_truth_count= 10777 class_id= 0, name= person, ap= 72.13%(TP= 7355, FP= 3165)for conf_thresh= 0.25, precision= 0.70, recall= 0.68, F1-score= 0.69for conf_thresh= 0.25, TP= 7355, FP= 3165, FN= 3422, average IoU= 56.45 % IoU threshold= 50 %, used Area-Under-Curvefor each unique Recall mean average precision(mAP@0.50)= 0.721290, or 72.13 % Total Detection Time: 287 Seconds40000个batch的APLoading weights from../backup/yolov4_40000.weights... seen 64, trained: 2560 K-images(40 Kilo-batches_64) Done! Loaded 162 layers from weights-file calculation mAP(mean average precision)... Detection layer: 139 -type= 28 Detection layer: 150 -type= 28 Detection layer: 161 -type= 28 2696 detections_count= 112421, unique_truth_count= 10777 class_id= 0, name= person, ap= 69.88%(TP= 8055, FP= 6296)for conf_thresh= 0.25, precision= 0.56, recall= 0.75, F1-score= 0.64for conf_thresh= 0.25, TP= 8055, FP= 6296, FN= 2722, average IoU= 45.02 % IoU threshold= 50 %, used Area-Under-Curvefor each unique Recall mean average precision(mAP@0.50)= 0.698818, or 69.88 % Total Detection Time: 297 Seconds50000个batch的APdetections_count= 101082, unique_truth_count= 10777 class_id= 0, name= person, ap= 73.55%(TP= 7895, FP= 4391)for conf_thresh= 0.25, precision= 0.64, recall= 0.73, F1-score= 0.68for conf_thresh= 0.25, TP= 7895, FP= 4391, FN= 2882, average IoU= 52.26 % IoU threshold= 50 %, used Area-Under-Curvefor each unique Recall mean average precision(mAP@0.50)= 0.735530, or 73.55 % Total Detection Time: 290 Seconds