yolov5 训练crowded human visible body detection

Posted CV-杨帆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yolov5 训练crowded human visible body detection相关的知识,希望对你有一定的参考价值。

0 前言

我使用的GPU平台:https://cloud.videojj.com/auth/register?inviter=18452&activityChannel=student_invite
知乎:
github:https://github.com/Whiffe/yolov5-visible-and-full-person-crowdhuman
b站:https://www.bilibili.com/video/BV1KY4y1r7TQ
arxiv:

在对拥挤人群(我应用场景是学生课堂)进行检测时,采用现有的模型代码,有一定问题,比如直接采用yolov5、yolov3、faster rcnn等,在拥挤场景的检测效果不佳,但使用crowded human数据集重训练后的yolov5,效果很好。crowded human数据集标签如下,有Head BBox、Visible BBox、Full BBox。如下图显示:

在拥挤人群中的检测,我发现Full BBox会额外把其他人框进来,那么就会出现一个框里出现多个人,
这里需要些张对比图

full body detection

visible body detection



但是我查的资料中,关于yolov5的检测结果只用了head detection和 full body detection ,并没有visible body detection。于是我使用yolov5对head detection、visible body detection进行重训练。

目录

1 相关资料

1.1,CrowdHuman的论文

CrowdHuman: A Benchmark for Detecting Human in a Crowd:https://arxiv.org/pdf/1805.00123.pdf

1.2,yolov5-crowdhuman的代码,这里是训练后的head detection和 full body detection 的模型,并没有 visible body detection。

yolov5-crowdhuman:https://github.com/deepakcrk/yolov5-crowdhuman

1.3,这是githun中对CrowdHuman论文的翻译

PaperWeekly/CrowdHuman.md:https://github.com/Mycenae/PaperWeekly/blob/master/CrowdHuman.md

1.4,这一个博客就非常重要了,是我能完成这篇博客的核心

目标检测 YOLOv5 CrowdHuman数据集格式转YOLOv5格式:https://blog.csdn.net/flyfish1986/article/details/115485814

1.5,这个代码链接是上一个博客所使用的代码工具

YOLOv5-Tools:https://gitcode.net/mirrors/shaoshengsong/YOLOv5-Tools/-/tree/main/CrowHuman2YOLO/data

1.6 b站YOLOV5训练自己的目标检测模型的视频

手把手教你使用YOLOV5训练自己的目标检测模型:https://www.bilibili.com/video/BV1YL4y1J7xz?p=1

2 crowded human数据集下载

2.1 官网数据集下载

CrowdHuman dataset下载链接:https://www.crowdhuman.org/download.html

下载后有这些文件:

2.2 数据集上传AI平台

我用的AI平台:https://cloud.videojj.com/auth/register?inviter=18452&activityChannel=student_invite

将数据集上传到AI平台中,一般就放在:/user-data 路径下

我是讲crowded human的数据集压缩为:crowdedhuman.zip,然后上传 /user-data/crowdedHuman中,上传方法在:数据传输https://cloud.videojj.com/handbook/guide/data_manage/#%E6%95%B0%E6%8D%AE%E4%BC%A0%E8%BE%93

3 YOLOv5-Tools

3.1 YOLOv5-Tools 安装

YOLOv5-Tools的功能之一就是讲crowded human转化为yolov5可以使用的数据集格式,即coco数据集格式。
YOLOv5-Tools代码链接:https://gitcode.net/mirrors/shaoshengsong/YOLOv5-Tools

我也将这个同步到了自己的github中:https://github.com/Whiffe/YOLOv5-Tools-main
也同步到了码云:https://gitee.com/YFwinston/YOLOv5-Tools-main

AI平台中搭建项目
pytorch:1.8.0, python:3.8, CUDA:11.1.1

cd /home
git clone https://gitee.com/YFwinston/YOLOv5-Tools-main

3.2 YOLOv5-Tools中关键文件

YOLOv5-Tools中有2个关键文件:
YOLOv5-Tools/CrowHuman2YOLO/data/prepare_vbody_data.sh
YOLOv5-Tools/CrowHuman2YOLO/data/gen_vbody_txts.py

其中我在原作者的基础上的改动如下图(gen_vbody_txts.py中的内容),主要将fbox改为了vbox

3.3 crowded human数据转化为coco数据集格式

crowded human数据转化为coco数据集格式,执行下面的代码
转化前,需要安装zip,然后将/user-data/crowdedHuman/crowdedhuman.zip复制到/home/YOLOv5-Tools-main/CrowHuman2YOLO/data/raw

apt-get update
apt-get install zip
apt-get install unzip

cp  /user-data/crowdedHuman/crowdedhuman.zip /home/YOLOv5-Tools-main/CrowHuman2YOLO/data/raw

cd /home/YOLOv5-Tools-main/CrowHuman2YOLO/data/raw
unzip crowdedhuman.zip
rm crowdedhuman.zip

cd /home/YOLOv5-Tools-main/CrowHuman2YOLO/data/
bash ./prepare_vbody_data.sh 608x608

3.4 转化后的结果展示

然后目录/home/YOLOv5-Tools-main/CrowHuman2YOLO/data/raw/Images的结构如下

Images
├── 273271,1017c000ac1360b7.jpg
├── 273271,10355000e3a458a6.jpg
├── 273271,1039400091556057.jpg
├── 273271,104ec00067d5b782.jpg
├── ...
└── 284193,ff25000b6a403e9.jpg

使用文件计数命令可以数出Images文件夹下文件数量

ls -l|grep "^-"| wc -l

结果是:19370

还有个路径也有生成结果:/home/YOLOv5-Tools-main/CrowHuman2YOLO/data/crowdhuman-608x608
结构如下

crowdhuman-608x608
├── 273271,1017c000ac1360b7.jpg
├── 273271,1017c000ac1360b7.txt
├── 273271,10355000e3a458a6.jpg
├── 273271,10355000e3a458a6.txt
├── ...
├── 284193,ff25000b6a403e9.jpg
├── 284193,ff25000b6a403e9.txt
├── test.txt
└── train.txt

我们看看273271,1017c000ac1360b7.txt的内容

再看看test.txt的内容

再看看train.txt的内容

讲crowded human生成coco标准文件夹格式

3.5 图像文件和标注文本文件 重构

3.3节只是将crowded human数据集转化为了coco结构的数据集,但是图像文件和标注文本文件需要重构,

首先按照下面路径创建文件夹:

to_train_img_path = '/user-data/crowdedHuman/images/train/' 
to_val_img_path = '/user-data/crowdedHuman/images/val/'
to_train_label_path = '/user-data/crowdedHuman/labels/train/'
to_val_label_path = '/user-data/crowdedHuman/labels/val/'

然后重构命令:

cd /home/YOLOv5-Tools-main/CrowHuman2YOLO/data/
python gen_coco_stru.py

结果如下:
user-data/crowdedHuman/images/

crowdedHuman
├── Images
│   ├── train
│   │   ├── 273271,1017c000ac1360b7.jpg
│   │   ├── 273271,10355000e3a458a6.jpg
│   │   ├── 273271,1039400091556057.jpg
│   │   ├── ...
│   │   └── 284193,ff01000db10348e.jpg
│   ├── val
│   │   ├── 273271,104ec00067d5b782.jpg
│   │   ├── 273271,10f400006b6fb935.jpg
│   │   ├── 273271,118910008d823f61.jpg
│   │   ├── ...
│   │   └── 284193,ff25000b6a403e9.jpg
└── labels 
    ├── train
    │   ├── 273271,1017c000ac1360b7.txt
    │   ├── 273271,10355000e3a458a6.txt
    │   ├── 273271,1039400091556057.txt
    │   ├── ...
    │   └── 284193,ff01000db10348e.txt
    └── val
        ├── 273271,104ec00067d5b782.txt
        ├── 273271,10f400006b6fb935.txt
        ├── 273271,118910008d823f61.txt
        ├── ...
        └── 284193,ff25000b6a403e9.txt

4 yolov5

4.1 yolov5 安装

cd /home
git clone https://gitee.com/YFwinston/yolov5.git
cd yolov5
pip install -r requirements.txt 
pip install opencv-python-headless==4.1.2.30
mkdir -p /root/.config/Ultralytics
wget  https://ultralytics.com/assets/Arial.ttf -O /root/.config/Ultralytics/Arial.ttf

4.2 预训练模型

我会使用预训练模型对crowded human中的head、visible body进行训练,我采用yolov5m的网络架构与预训练模型:https://github.com/ultralytics/yolov5/releases


执行下面的代码

cd /home/yolov5
mkdir pretrained
cd pretrained
wget https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5m.pt

4.3 训练

训练需要在 /home/yolov5/data 下创建:crowdhuman.yaml,其内容如下:

执行下面代码:

cd  /home/yolov5/data
touch crowdhuman.yaml
train: /user-data/crowdedHuman/images/train
val: /user-data/crowdedHuman/images/val
#test: test.txt

#number of classes
nc: 2

# class names
names: ['head', 'Vperson']

执行下面的训练代码:

cd  /home/yolov5/
python train.py --data ./data/crowdhuman.yaml --cfg ./models/yolov5m.yaml --weights ./pretrained/yolov5m.pt --batch-size 16 --epochs 200

4.4 demo测试

cd /home/yolov5
python ./detect.py --weights ./crowdhuman_yolov5m_visible_body.pt --source ./1.jpeg --save-txt --save-conf  --hide-labels --line-thickness 4 --classes 1

4.5 训练过程与结果的截图





从结果上来看,我使用的是默认epoch,300次,但是只训练了286次,原因是第186次的训练达到最优,之后100次再也没有超过186的结果,所以训练停止。

训练时间:31.2小时

4.6 实时查看GPU,CPU和内存使用情况

如果在训练过程中,实时查看GPU,CPU和内存使用情况执行下面的代码:

pip install gpustat
gpustat -cp -i 1

4.7 分析训练结果

confusion_matrix.png

F1_curve.png

hyp.yaml

lr0: 0.01
lrf: 0.01
momentum: 0.937
weight_decay: 0.0005
warmup_epochs: 3.0
warmup_momentum: 0.8
warmup_bias_lr: 0.1
box: 0.05
cls: 0.5
cls_pw: 1.0
obj: 1.0
obj_pw: 1.0
iou_t: 0.2
anchor_t: 4.0
fl_gamma: 0.0
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
degrees: 0.0
translate: 0.1
scale: 0.5
shear: 0.0
perspective: 0.0
flipud: 0.0
fliplr: 0.5
mosaic: 1.0
mixup: 0.0
copy_paste: 0.0

labels_correlogram.jpg

labels.jpg
opt.yaml

weights: ./pretrained/yolov5m.pt
cfg: ./models/yolov5m.yaml
data: ./data/crowdhuman.yaml
hyp: data/hyps/hyp.scratch-low.yaml
epochs: 300
batch_size: 16
imgsz: 640
rect: false
resume: false
nosave: false
noval: false
noautoanchor: false
noplots: false
evolve: null
bucket: ''
cache: null
image_weights: false
device: ''
multi_scale: false
single_cls: false
optimizer: SGD
sync_bn: false
workers: 8
project: runs/train
name: exp
exist_ok: false
quad: false
cos_lr: false
label_smoothing: 0.0
patience: 100
freeze:
- 0
save_period: -1
local_rank: -1
entity: null
upload_dataset: false
bbox_interval: -1
artifact_alias: latest
save_dir: runs/train/exp5

P_curve.png

PR_curve.png

R_curve.png


results.csv

               epoch,      train/box_loss,      train/obj_loss,      train/cls_loss,   metrics/precision,      metrics/recall,     metrics/mAP_0.5,metrics/mAP_0.5:0.95,        val/box_loss,        val/obj_loss,        val/cls_loss,               x/lr0,               x/lr1,               x/lr2
                   0,            0.070957,             0.17379,            0.013804,             0.79331,             0.62187,             0.70443,             0.36578,            0.053743,             0.15582,           0.0086178,           0.0033298,           0.0033298,            0.070032
                   1,             0.05886,              0.1723,           0.0089482,             0.83852,              0.6577,             0.74977,             0.42579,            0.050081,             0.15285,           0.0079355,           0.0066411,           0.0066411,             0.04001
                   2,            0.057335,             0.17261,           0.0085512,             0.83284,             0.64311,              0.7332,             0.41763,            0.050096,             0.15692,           0.0079001,           0.0099305,           0.0099305,            0.009966
                   3,            0.054982,             0.17262,           0.0082141,             0.83876,             0.65609,             0.74388,             0.43208,            0.048579,             0.15438,           0.0074132,            0.009901,            0.009901,            0.009901
                   4,            0.053255,              0.1713,            0.007933,             0.84424,              0.6712,              0.7579,             0.45066,            0.047554,             0.15219,            0.007289,            0.009901,            0.009901,            0.009901
                   5,            0.052224,             0.16798,           0.0077461,             0.84808,             0.67017,             0.76022,             0.45863,             0.04693,             0.15142,           0.0071035,            0.009868,            0.009868,            0.009868
                   6,            0.051811,             0.16789,            0.007649,             0.85649,             0.67891,             0.76965,             0.46254,            0.046728,             0.15132,           0.0070715,            0.009835,            0.009835,            0.009835
                   7,            0.051141,             0.16573,           0.0075969,             0.84552,             0.68172,               0.767,             0.46258,            0.046536,             0.15094,           0.0070178,            0.009802,            0.009802,            0.009802
                   8,            0.050914,             0.16508,              0.0075,             0.84846,             0.69117,             0.77498,               0.471,            0.046007,             0.14988,           0.0069287,            0.009769,            0.009769,            0.009769
                   9,            0.050611,             0.16475,           0.0074872,             0.85132,             0.69204,             0.77705,             0.47654,             0.04582,             0.14921,           0.0068846,            0.009736,            0.009736,            0.009736
                  10,            0.050199,               0.163,           0.0074224,             0.85678,             0.68784,             0.77632,              0.4748,            0.045586,             0.14801,           0.0068159,            0.009703,            0.009703,            0.009703
                  11,            0.049954,             0.16256,            0.007362,             0.85262,             0.69381,             0.77985,             0.47737,            0.045618,             0.14902,            0.006824,             0.00967,             0.00967,             0.00967
                  12,            0.049833,             0.16187,           0.0073681,             0.85211,              0.6944,             0.78143,             0.47993,            0.045415,             0.14804,            0.006827,            0.009637,            0.009637,            0.009637
                  13,            0.049665,               0.162,           0.0073031,             0.84996,             0.69458,             0.78038,             0.48119,            0.045284,             0.14824,           0.0067918,            0.009604,            0.009604,            0.009604
                  14,            0.049569,             0.16097,           0.0073169,             0.85464,              0.6983,             0.78471,             0.48393,            0.045131,             0.14808,           0.0067935,            0.009571,            0.009571,            0.009571
                  15,            0.049421,             0.16206,           0.0072589,             0.84949,             0.70118,             0.78366,             0.48418,            0.045037,             0.14721,            0.006754,            0.009538,            0.009538,            0.009538
                  16,            0.049319,             0.16144,           0.0072539,             0.85332,             0.69859,             0.78399,             0.48428,            0.044933,             0.14729,           0.0067212,            0.009505,            0.009505,            0.009505
                  17,            0.049186,              0.1602,           0.0072142,             0.85533,             0.69967,             0.78586,             0.48799,            0.044799,             0.14681,           0.0066843,            0.009472,            0.009472,            0.009472
                  18,             0.04885,             0.15964,            0.007186,             0.85214,             0.70254,             0.78647,              0.4895,            0.044724,             0.14673,            0.006694,            0.009439,            0.009439,            0.009439
                  19,            0.049002,             0.16097,           0.0071914,             0.85537,             0.70358,             0.78861,              0.4921,             0.04452,             0.14594,           0.0066627,            0.009406,            0.009406,            0.009406
                  20,            0.049029,              0.1607,           0.0072433,             0.86134,             0.69844,             0.78795,             0.49224,            0.044505,             0.14587,           0.0066331,            0.009373,            0.009373,            0.009373
                  21,            0.048741,              0.1592,           0.0071965,              0.8593,             0.70288,             0.79033,             0.49497,            0.044323,             0.14558,           0.0066193,             0.00934, 

以上是关于yolov5 训练crowded human visible body detection的主要内容,如果未能解决你的问题,请参考以下文章

图像识别之Yolov5训练自己的模型

图像识别之Yolov5训练自己的模型

深度学习Yolov5训练意外中断后如何接续训练详解;yolov5中断后继续训练

Yolov5 物体检测训练

博客3:YOLOv5车牌识别实战教程:模型训练与评估

yolov5模型训练流程