环境配置的官方教程如下,如果一些库安装失败,导致安装中断,可以单独安装一些库,比如 Pytorch,然后再执行下列安装步骤,具体如下:
个人建议:在上述安装之前,先安装 Anaconda,然后创建虚拟环境。Anconda 以及相关 Pytorch 开发环境配置,可以参考本人的博客文章,相关的链接如下,
- Ubuntu:https://blog.csdn.net/kxh123456/article/details/121331727
- Windows:https://blog.csdn.net/kxh123456/article/details/110267660
注意几个问题:
- 下载特定版本的 yolo-v5:git clone -b v6.1 https://github.com/ultralytics/yolov5.git
- 最好是用 【git clone】的方式下载,这样工程在被改动的时候,会有标记,很容易恢复到原始版本.
- 版本重要更新:【v6.2 】添加了分类模型,【v7 】添加了分割模型.
- 当前系列教程解析的代码版本:【v6.1】.
在线推理
运行下面的代码,模型 【yolov5s.pt】自动从 【“ultralytics/yolov5”】下载,测试图片也是从网上下载。完整代码如下:
import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # or yolov5n - yolov5x6, custom
# Images
img = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, PIL, OpenCV, numpy, list
# Inference
results = model(img)
# Results
results.print() # or .show(), .save(), .crop(), .pandas(), etc.
运行后,如果出现如下的错误,表示无法访问 Github,
离线推理
如果无法访问 【Github】,也可以把模型和测试图片通过其它方式下载到本地,进行离线推理,具体代码修改如下,
import torch
# Model
# model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # or yolov5n - yolov5x6, custom
model = torch.hub.load('./', 'yolov5s', source='local') # or yolov5n - yolov5x6, custom
# Images
# img = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, PIL, OpenCV, numpy, list
img = 'data/images/zidane.jpg' # or file, Path, PIL, OpenCV, numpy, list
# Inference
results = model(img)
# Results
results.show() # or .show(), .save(), .crop(), .pandas(), etc.
【detect.py
】
官方教程如下图所示,可以是多种输入形式,
调用摄像头进行预测,命令如下,
python detect.py --weights yolov5s.pt --source 0
推理单张图,命令如下,
python detect.py --weights yolov5s.pt --source D:\\DM2\\2DDetection\\datasets\\coco128\\images\\train2017\\000000000030.jpg
推理路径下的所有图片,运行命令和结果如下,
python detect.py --weights yolov5s.pt --source D:\\DM2\\2DDetection\\datasets\\coco128\\images\\train2017\\
输入参数为网站视频链接,并进行推理,同时还会输出目标的数量(见终端打印内容),
python detect.py --weights yolov5s.pt --source https://youtu.be/Zgi9g1ksQHc
为了快速验证工程的效果,只重新训练其中一个模型【yolo-v5s】。该模型大小适中,不至于消耗太长时间。当前教程的目的仅仅是为了重新训练一遍,【快速】验证工程的有效性。具体流程如下,
官方教程
单卡、单机训练教程如下图所示,可以快速开始训练,验证项目是否可以正常训练。根据自己显卡的显存,需调整批次的大小,避免显存溢出。运行命令以及训练的可视化结果如下,
运行【trian.py
】
本机配置:Ubuntu18,batch=32(64,显存不够),2022-12-07,09:30AM 开始训练,2022-12-11,08:30AM 结束训练(大概4天训练完成)。运行后,终端打印内容如下,比如显卡型号,显存,训练集和测试集图片数量等内容,
YOLOv5 v6.1-0-g3752807 torch 1.10.0 CUDA:1 (NVIDIA GeForce RTX 2080 Ti, 11019MiB)
train:Scanning ‘/home/slam/kxh-1/2DDection/datasets/coco/train2017. cache’ images and labels … 117266 found, 1021 missing, 0 empty, 0 corrupt: 100%|████████████████████████████████████████████████████████████████████████████████|
118287/118287 [00:00, ?it/s]
val: Scanning’/home/slam/kxh-1/2DDection/datasets/coco/val2017. cache’ images and labels… 4952 found, 48 missing, 0 empty, 0 corrupt: 100%|████████████████████████████████████████████████████████████████████████████████████████████|
5000/5000 [00:00, ?it/s]
训练的命令如下,包含第一次开启训练,以及训练中断后,恢复训练,
# 开启训练
python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5s.yaml --batch-size 32
# 训练中断,重新恢复训练,resume:添加模型的路径
python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5s.yaml --batch-size 32
--resume runs/train/exp/weights/last.pt
训练开始后的部分截图如下图所示,
如上图所示,训练【epoch=300】后,【yolo-v5s】训练完成。最终打印结果如下,
Class Images Labels P R mAP@.5 mAP@.5:.95: 100%|██████████| 79/79 [00:34<00:00, 2.29it/s]
all 5000 36335 0.688 0.508 0.559 0.36
person 5000 10777 0.768 0.683 0.759 0.477
bicycle 5000 314 0.693 0.438 0.521 0.27
car 5000 1918 0.681 0.572 0.62 0.373
motorcycle 5000 367 0.759 0.586 0.658 0.361
airplane 5000 143 0.884 0.755 0.846 0.575
bus 5000 283 0.826 0.707 0.777 0.602
train 5000 190 0.903 0.768 0.839 0.585
truck 5000 414 0.615 0.403 0.487 0.289
boat 5000 424 0.634 0.37 0.452 0.21
traffic light 5000 634 0.638 0.476 0.507 0.237
fire hydrant 5000 101 0.825 0.713 0.814 0.601
stop sign 5000 75 0.785 0.68 0.722 0.601
parking meter 5000 60 0.73 0.533 0.603 0.419
bench 5000 411 0.632 0.268 0.331 0.204
bird 5000 427 0.657 0.431 0.48 0.285
cat 5000 202 0.827 0.748 0.814 0.557
dog 5000 218 0.786 0.674 0.729 0.546
horse 5000 272 0.766 0.695 0.748 0.525
sheep 5000 354 0.629 0.681 0.698 0.459
cow 5000 372 0.712 0.675 0.726 0.478
elephant 5000 252 0.743 0.813 0.809 0.556
bear 5000 71 0.852 0.817 0.857 0.63
zebra 5000 266 0.886 0.815 0.898 0.623
giraffe 5000 232 0.915 0.831 0.901 0.658
backpack 5000 371 0.579 0.259 0.259 0.124
umbrella 5000 407 0.693 0.55 0.596 0.348
handbag 5000 540 0.615 0.181 0.223 0.111
tie 5000 252 0.691 0.417 0.486 0.262
suitcase 5000 299 0.603 0.472 0.544 0.337
frisbee 5000 115 0.786 0.783 0.813 0.601
skis 5000 241 0.691 0.357 0.413 0.169
snowboard 5000 69 0.579 0.406 0.427 0.233
sports ball 5000 260 0.691 0.588 0.599 0.387
kite 5000 327 0.615 0.581 0.601 0.377
baseball bat 5000 145 0.651 0.469 0.505 0.23
baseballglove 5000 148 0.734 0.534 0.582 0.327
skateboard 5000 179 0.825 0.686 0.699 0.456
surfboard 5000 267 0.65 0.487 0.522 0.284
tennis racket 5000 225 0.771 0.687 0.739 0.419
bottle 5000 1013 0.65 0.449 0.516 0.313
wine glass 5000 341 0.691 0.428 0.513 0.296
cup 5000 895 0.641 0.509 0.551 0.37
fork 5000 215 0.586 0.344 0.429 0.247
knife 5000 325 0.551 0.191 0.244 0.122
spoon 5000 253 0.494 0.178 0.212 0.112
bowl 5000 623 0.663 0.478 0.534 0.364
banana 5000 370 0.537 0.335 0.338 0.182
apple 5000 236 0.477 0.286 0.252 0.165
sandwich 5000 177 0.643 0.469 0.501 0.325
orange 5000 285 0.474 0.361 0.368 0.276
broccoli 5000 312 0.551 0.359 0.379 0.178
carrot 5000 365 0.415 0.301 0.293 0.17
hot dog 5000 125 0.659 0.464 0.501 0.317
pizza 5000 284 0.754 0.644 0.683 0.475
donut 5000 328 0.632 0.509 0.548 0.401
cake 5000 310 0.627 0.487 0.519 0.323
chair 5000 1771 0.624 0.385 0.455 0.256
couch 5000 261 0.749 0.479 0.616 0.419
potted plant 5000 342 0.586 0.435 0.446 0.231
bed 5000 163 0.804 0.454 0.609 0.376
dining table 5000 695 0.627 0.332 0.386 0.233
toilet 5000 179 0.741 0.693 0.785 0.603
tv 5000 288 0.742 0.66 0.734 0.51
laptop 5000 231 0.772 0.628 0.692 0.52
mouse 5000 106 0.777 0.764 0.804 0.585
remote 5000 283 0.525 0.36 0.402 0.206
keyboard 5000 153 0.69 0.608 0.679 0.455
cell phone 5000 262 0.598 0.458 0.488 0.293
microwave 5000 55 0.708 0.636 0.726 0.519
oven 5000 143 0.657 0.448 0.512 0.299
toaster 5000 9 0.998 0.444 0.587 0.399
sink 5000 225 0.659 0.493 0.535 0.34
refrigerator 5000 126 0.789 0.595 0.683 0.464
book 5000 1129 0.486 0.157 0.215 0.084
clock 5000 267 0.778 0.689 0.712 0.477
vase 5000 274 0.596 0.496 0.531 0.34
scissors 5000 36 0.477 0.25 0.269 0.18
teddy bear 5000 190 0.715 0.529 0.63 0.409
hair drier 5000 11 1 0 0.017 0.0113
toothbrush 5000 57 0.54 0.281 0.259 0.156
Evaluating pycocotools mAP... saving runs/train/exp/_predictions.json...
loading annotations into memory...
Done (t=0.33s)
creating index...
index created!
Loading and preparing results...
DONE (t=6.45s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=60.67s).
Accumulating evaluation results...
DONE (t=12.01s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.373
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.566
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.405
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.219
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.423
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.482
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.307
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.514
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.569
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.381
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.628
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.722
Results saved to runs/train/exp
运行【val.py
】
python val.py --weights runs/train/exp/weights/best.pt --data coco.yaml --img 640
运行评估脚本,结果如下:
val: data=/home/slam/kxh-1/2DDection/test/yolov5/data/coco.yaml,
weights=['runs/train/exp/weights/best.pt'],
batch_size=32, imgsz=640, conf_thres=0.001, iou_thres=0.6,
task=val, device=, workers=8, single_cls=False, augment=False,
verbose=False, save_txt=False, save_hybrid=False, save_conf=False,
save_json=True, project=runs/val, name=exp, exist_ok=False,half=False, dnn=False
YOLOv5 v6.1-0-g3752807 torch 1.10.0 CUDA:0 (NVIDIA GeForce RTX 2080 Ti, 11019MiB)
Fusing layers...
Model Summary: 213 layers, 7225885 parameters, 0 gradients, 16.4 GFLOPs
val: Scanning '/home/slam/kxh-1/2DDection/datasets/coco/val2017.cache' images and labels...
4952 found, 48 missing, 0 empty, 0 corrupt: 100%|█████████████████████████████████| 5000/5000
[00:00<?, ?it/s]
Class Images Labels P R mAP@.5 mAP@.5:.95: 100%|██████████| 157/157 [00:40<00:00, 3.85it/s]
all 5000 36335 0.668 0.523 0.563 0.363
Speed: 0.1ms pre-process, 2.5ms inference, 1.5ms NMS per image at shape (32, 3, 640, 640)
Evaluating pycocotools mAP... saving runs/val/exp3/best_predictions.json...
loading annotations into memory...
Done (t=0.84s)
creating index...
index created!
Loading and preparing results...
DONE (t=5.40s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=58.44s).
Accumulating evaluation results...
DONE (t=11.82s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.373
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.570
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.403
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.218
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.423
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.482
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.308
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.511
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.562
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.376
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.620
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.715
Results saved to runs/val/exp3
训练结果可视化
借助 tensorboard,训练可视化结果如下所示,
结果评估
重新训练【yolo-v5s】后,得到如下表的结果,很容易可以达到官方训练的水平。表明模型具有极好的稳定性,可复现性,非常适合实际应用,
yolo-v5s | image size | mAP(val) 0.5:0.95 |
mAP(val) 0.5 |
训练时间 | batch size |
---|---|---|---|---|---|
官方发布 | 640 | 37.4 | 56.8 | 2 days V100 | 64 |
重新训练 | 640 | 37.3 | 57.0 | 4 days 2080Ti | 32 |