YOLOv5复现(论文复现)

文章目录

    • YOLOv5复现(论文复现)
        • 概述
        • 模型结构
        • 正负样本匹配策略
        • 损失计算
        • 数据增强
        • 使用方式
          • 训练
          • 测试
          • 验证
          • Demo
概述
模型结构

YOLOv5复现(论文复现)-LMLPHP

# CSPDarkNet
class CSPDarkNet(nn.Module):
    def __init__(self, depth=1.0, width=1.0, act_type='silu', norm_type='BN', depthwise=False):
        super(CSPDarkNet, self).__init__()
        self.feat_dims = [round(64 * width), round(128 * width), round(256 * width), round(512 * width), round(1024 * width)]
        # P1/2
        self.layer_1 = Conv(3, self.feat_dims[0], k=6, p=2, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
        # P2/4
        self.layer_2 = nn.Sequential(
            Conv(self.feat_dims[0], self.feat_dims[1], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise),
            CSPBlock(in_dim       = self.feat_dims[1],
                     out_dim      = self.feat_dims[1],
                     expand_ratio = 0.5,
                     nblocks      = round(3*depth),
                     shortcut     = True,
                     act_type     = act_type,
                     norm_type    = norm_type,
                     depthwise    = depthwise)
        )
        # P3/8
        self.layer_3 = nn.Sequential(
            Conv(self.feat_dims[1], self.feat_dims[2], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise),
            CSPBlock(in_dim       = self.feat_dims[2],
                     out_dim      = self.feat_dims[2],
                     expand_ratio = 0.5,
                     nblocks      = round(9*depth),
                     shortcut     = True,
                     act_type     = act_type,
                     norm_type    = norm_type,
                     depthwise    = depthwise)
        )
        # P4/16
        self.layer_4 = nn.Sequential(
            Conv(self.feat_dims[2], self.feat_dims[3], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise),
            CSPBlock(in_dim       = self.feat_dims[3],
                     out_dim      = self.feat_dims[3],
                     expand_ratio = 0.5,
                     nblocks      = round(9*depth),
                     shortcut     = True,
                     act_type     = act_type,
                     norm_type    = norm_type,
                     depthwise    = depthwise)
        )
        # P5/32
        self.layer_5 = nn.Sequential(
            Conv(self.feat_dims[3], self.feat_dims[4], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise),
            SPPF(self.feat_dims[4], self.feat_dims[4], expand_ratio=0.5),
            CSPBlock(in_dim       = self.feat_dims[4],
                     out_dim      = self.feat_dims[4],
                     expand_ratio = 0.5,
                     nblocks      = round(3*depth),
                     shortcut     = True,
                     act_type     = act_type,
                     norm_type    = norm_type,
                     depthwise    = depthwise)
        )

    def forward(self, x):
        c1 = self.layer_1(x)
        c2 = self.layer_2(c1)
        c3 = self.layer_3(c2)
        c4 = self.layer_4(c3)
        c5 = self.layer_5(c4)

        outputs = [c3, c4, c5]

        return outputs
正负样本匹配策略
损失计算
数据增强
使用方式
conda create -n yolov5 python=3.8
conda activate yolov5
pip install -r requirents.txt

YOLOv5复现(论文复现)-LMLPHP

F:\datasets
|___COCO2017
	|___annotations
		|____instances_train2017.json
		|____instances_val2017.json
		...
	|___train2017
		|____000000000009.jpg
		...
	|___val2017
		|____000000000139.jpg
		...
cd tools/
python clean_coco.py --root path/to/coco --image_set train
python clean_coco.py --root path/to/coco --image_set val
python dataset/coco.py
训练
python train.py --cuda -d coco --root F:\datasets\ -m yolov5_s -bs 1 --max_epoch 300 --wp_epoch 1 --eval_epoch 10 --fp16 --ema --multi_scale
测试
python test.py -d coco --cuda -m yolov5_s --img_size 640 --weight yolov5_s_coco_adamw.pth --root F:\datasets\ --no_multi_labels --show
验证
python eval.py -d coco --cuda -m yolov5_s --img_size 640 --weight yolov5_s_coco_adamw.pth --root F:\datasets\
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.444
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.519
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.324
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.544
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.612
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.432
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.685
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.766
ap50_95 : 0.3912800741053746
ap50 : 0.5693696831091651
Demo
python demo.py --mode image --path_to_img dataset\demo\images --cuda --img_size 640 --model yolov5_s --weight yolov5_s_coco_adamw.pth --dataset coco --num_classes 80 --show
python demo.py --mode video --path_to_vid dataset\demo\videos\01.mp4 --cuda --img_size 640 -m yolov5_s --weight yolov5_s_coco_adamw.pth --show --gif
10-08 21:34