📌本周任务:模型改进📌
注:对yolov5l.yaml 文件中的backbone 模块和head模块进行改进。
任务结构图:
YOLOv5s网络结构图:
原始模型代码:
python
# YOLOv5 v6.0 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 6, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 3, C3, [1024]],
[-1, 1, SPPF, [1024, 5]], # 9
]
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, False]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
改进代码:
python
# YOLOv5 v6.0 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 6, C2, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 3, C3, [512]],
#[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
#[-1, 3, C3, [1024]],
[-1, 1, SPPF, [512, 5]], # 9
]
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [512, 3, 2]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, False]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 12], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 8], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
[[15, 18, 21], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
运行模型:
python train.py --img 640 --batch 8 --epoch 1 --data data/ab.yaml --cfg models/yolov5s.yaml
(venv) D:\Out\yolov5-master>python train.py --img 640 --batch 8 --epoch 1 --data data/ab.yaml --cfg models/yolov5s.yaml
train: weights=yolov5s.pt, cfg=models/yolov5s.yaml, data=data/ab.yaml, hyp=data\hyps\hyp.scratch-low.yaml, epochs=1, batch_size=8, imgsz=640, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, bucket=, cache=None, 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, seed=0, local_rank=-1, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest
github: skipping check (not a git repository), for updates see https://github.com/ultralytics/yolov5
YOLOv5 2023-6-27 Python-3.10.3 torch-2.0.1+cpu CPU
hyperparameters: 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
Comet: run 'pip install comet_ml' to automatically track and visualize YOLOv5 runs in Comet
TensorBoard: Start with 'tensorboard --logdir runs\train', view at http://localhost:6006/
Overriding model.yaml nc=80 with nc=4
from n params module arguments
0 -1 1 3520 models.common.Conv [3, 32, 6, 2, 2]
1 -1 1 18560 models.common.Conv [32, 64, 3, 2]
2 -1 1 18816 models.common.C3 [64, 64, 1]
3 -1 1 14592 models.common.C2 [64, 64, 1]
4 -1 1 73984 models.common.Conv [64, 128, 3, 2]
5 -1 2 115712 models.common.C3 [128, 128, 2]
6 -1 1 295424 models.common.Conv [128, 256, 3, 2]
7 -1 3 625152 models.common.C3 [256, 256, 3]
8 -1 1 1180672 models.common.Conv [256, 512, 3, 2]
9 -1 1 1182720 models.common.C3 [512, 512, 1]
10 -1 1 656896 models.common.SPPF [512, 512, 5]
11 -1 1 131584 models.common.Conv [512, 256, 1, 1]
12 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
13 [-1, 6] 1 0 models.common.Concat [1]
14 -1 1 361984 models.common.C3 [512, 256, 1, False]
15 -1 1 33024 models.common.Conv [256, 128, 1, 1]
16 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
17 [-1, 4] 1 0 models.common.Concat [1]
18 -1 1 90880 models.common.C3 [256, 128, 1, False]
19 -1 1 147712 models.common.Conv [128, 128, 3, 2]
20 [-1, 14] 1 0 models.common.Concat [1]
21 -1 1 329216 models.common.C3 [384, 256, 1, False]
22 -1 1 590336 models.common.Conv [256, 256, 3, 2]
23 [-1, 10] 1 0 models.common.Concat [1]
24 -1 1 1313792 models.common.C3 [768, 512, 1, False]
25 [17, 20, 23] 1 38097 models.yolo.Detect [4, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [256, 384, 768]]
YOLOv5s summary: 229 layers, 7222673 parameters, 7222673 gradients, 17.0 GFLOPs
Transferred 49/373 items from yolov5s.pt
optimizer: SGD(lr=0.01) with parameter groups 61 weight(decay=0.0), 64 weight(decay=0.0005), 64 bias
train: Scanning D:\Out\yolov5-master\paper_data\train.cache... 160 images, 0 backgrounds, 0 corrupt: 100%|██████████| 1
val: Scanning D:\Out\yolov5-master\paper_data\val.cache... 20 images, 0 backgrounds, 0 corrupt: 100%|██████████| 20/20
AutoAnchor: 5.35 anchors/target, 1.000 Best Possible Recall (BPR). Current anchors are a good fit to dataset
Plotting labels to runs\train\exp3\labels.jpg...
Image sizes 640 train, 640 val
Using 4 dataloader workers
Logging results to runs\train\exp3
Starting training for 1 epochs...
Epoch GPU_mem box_loss obj_loss cls_loss Instances Size
0/0 0G 0.1101 0.04563 0.0454 49 640: 100%|██████████| 20/20 [02:44<00:00, 8.
Class Images Instances P R mAP50 mAP50-95: 100%|██████████| 2/2 [00:05<0
all 20 60 0.000542 0.25 0.000682 0.000268
1 epochs completed in 0.048 hours.
Optimizer stripped from runs\train\exp3\weights\last.pt, 14.8MB
Optimizer stripped from runs\train\exp3\weights\best.pt, 14.8MB
Validating runs\train\exp3\weights\best.pt...
Fusing layers...
YOLOv5s summary: 168 layers, 7213041 parameters, 0 gradients, 16.8 GFLOPs
Class Images Instances P R mAP50 mAP50-95: 100%|██████████| 2/2 [00:05<0
all 20 60 0.000542 0.25 0.000685 0.000268
banana 20 12 0 0 0 0
snake fruit 20 20 0 0 0 0
dragon fruit 20 13 0.00217 1 0.00274 0.00107
pineapple 20 15 0 0 0 0
Results saved to runs\train\exp3