DOTA数据集应用于Yolo-v4(-Tiny)系列2——使用Pytorch框架的Yolov4(-Tiny)训练与推测

文章目录

  • 前言
  • 一、Pytorch框架源码
  • 二、调整数据集存放位置
    • 2.1 调整./dota_data
    • 2.2 补充说明
  • 三、使用Yolov4训练与推断
    • 3.1 调整yolov4.yaml与训练
    • 3.2 测试结果
  • 四、使用Yolov4-Tiny训练与推断
    • 4.1 调整yolov4-tiny.yaml与训练
    • 4.2 测试结果
  • 总结


前言

因为项目需要在Zynq开发板上实现深度网络的部署,采用Yolo-v4(-Tiny)两种目标检测模型,并使用武汉大学开源的DOTA数据集来训练和推断。因为此前使用计算机视觉相关的代码都是直接用已经处理好的数据集比如Pascal VOC、COCO、ImageNet等,DOTA数据集由于图片分辨率比较高,(2K-3K)*(2K-3K)分辨率,如果直接用于训练,效果不如将图片分割为多张图片,这样反而可以增加一张图片的目标框数量,对于提高模型精度十分有帮助。其次,DOTA数据集的标签格式不同于YOLO,所以需要在两种之间实现转换。此外,之前从未处理过raw的图片来制作数据集,所以也趁着这个机会对raw图做VOC或者YOLO格式的标签。另外在Darknet框架和Pytorch框架下都实现了Yolov4和Yolov4-Tiny框架的训练和推断。

趁着在部署到板子上之前,记录总结这一周多的工作。

由于篇幅较长,所以将分为几篇文章来记录。

本文在系列1数据集准备的基础上,使用Pytorch框架源码训练和推断,本文是系列文章中的第2篇。仅依赖系列文章1和2就可以实现基于Pytorch框架下Yolov4Yolov4-TinyDOTA数据集的训练和推断。后续的系列文章3和系列文章1实现基于Darknet框架下Yolov4Yolov4-TinyDOTA数据集的训练和推断。


一、Pytorch框架源码

环境:autodl PyTorch-1.8.1 Python-3.8(ubuntu18.04) Cuda-11.1
源码:https://blog.csdn.net/hukyfjghkl/article/details/107863266

从该博客下载的源码,我比对了ultralytics提供的源码,在train.py上仍有诸多行需要调整,请比对下方我修改后的源码。

    # 将模型参数分为三组(weights、biases、bn)来进行分组优化
    g = [], [], []  # optimizer parameter groups
    # 这里应该是把nn模型中层名集合k、参数值集合v做成一个dict,如果k里面'Norm',则对应v存储到tuple中
    bn = tuple(v for k, v in nn.__dict__.items() if 'Norm' in k)  # normalization layers, i.e. BatchNorm2d()
    # FIXME: module.modules()方法
    # model.modules()和model.children()均为迭代器,
    # model.modules()会遍历model中所有的子层,
    # 而model.children()仅会遍历当前层。
    for v in model.modules():
        if hasattr(v, 'bias') and isinstance(v.bias, nn.Parameter):  # bias  对应pg2
            g[2].append(v.bias)
        if isinstance(v, bn):  # weight (no decay) 对应pg0
            g[1].append(v.weight)
        elif hasattr(v, 'weight') and isinstance(v.weight, nn.Parameter):  # weight (with decay) 对应pg1
            g[0].append(v.weight)

    ### TODO: 这个地方感觉有问题! 下面应该不是g[2],而是g[1]
    if opt.optimizer == 'Adam':
        optimizer = Adam(g[2], lr=hyp['lr0'], betas=(hyp['momentum'], 0.999))  # adjust beta1 to momentum
    elif opt.optimizer == 'AdamW':
        optimizer = AdamW(g[2], lr=hyp['lr0'], betas=(hyp['momentum'], 0.999))  # adjust beta1 to momentum
    else:
        optimizer = SGD(g[2], lr=hyp['lr0'], momentum=hyp['momentum'], nesterov=True)

    ### TODO: 这个地方感觉有问题!下面的g[1]应该是g[2]
    # 设置pg1(weights)的优化方式
    optimizer.add_param_group({'params': g[0], 'weight_decay': hyp['weight_decay']})  # add g0 with weight_decay
    # 设置pg2(biases)的优化方式
    optimizer.add_param_group({'params': g[1]})  # add g1 (BatchNorm2d weights)
    LOGGER.info(f"{colorstr('optimizer:')} {type(optimizer).__name__} with parameter groups "
                f"{len(g[1])} weight (no decay), {len(g[0])} weight, {len(g[2])} bias")
    # 删除三个变量 优化代码
    del g

在前述给定的autodl的环境下,使用下述命令配置环境

pip install -r requirements.txt

随后补充安装

pip install opencv-python install "opencv-python-headless<4.3"

二、调整数据集存放位置

2.1 调整./dota_data

./dota_datadota_name.yaml规定了trainval的图片路径来源,nc指的是DOTA数据集中的分类数,值为15names指的是分类list,如下

names: ['small-vehicle', 'large-vehicle', 'plane', 'storage-tank', 'ship', 'harbor', 'ground-track-field', 'soccer-ball-field', 'tennis-court', 'swimming-pool', 'baseball-diamond', 'roundabout', 'basketball-court', 'bridge', 'helicopter']

./dota_data中的imageslabels下各自都有trainval,分别存放图像和标签文件。在系列文章1中我们已经制备了符合Yolo格式的图片和标签,因此根据上述已经存放的风格进行更替。
另外注意在修改.yaml文件时:后面必须有一个空格以后才能接着写内容。
所以完整的dota_name.yaml文件如下:

train: dota_data/images/train/   # 图片
val: dota_data/images/val/  

# number of classes
nc: 15

# class names
names: ['small-vehicle', 'large-vehicle', 'plane', 'storage-tank', 'ship', 'harbor', 'ground-track-field', 'soccer-ball-field', 'tennis-court', 'swimming-pool', 'baseball-diamond', 'roundabout', 'basketball-court', 'bridge', 'helicopter']

以上names直接用在该博主提供的数据集。

2.2 补充说明

由于笔者后来在切割图片和标签风格转换时,采用了另一种names,所以在调整.yaml文件时,需要注意names的改变,如下:

names: ['plane', 'baseball-diamond', 'bridge', 'ground-track-field', 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court', 'basketball-court',  'storage-tank', 'soccer-ball-field', 'roundabout', 'harbor', 'swimming-pool', 'helicopter']

这个风格我将在基于Darknet框架的Yolov4中使用。

三、使用Yolov4训练与推断

3.1 调整yolov4.yaml与训练

我们从0开始训练权重,但在这之前,我们需要将yolov4.yaml文件放到./dota_data下,其中yolov4.yaml的内容修改如下:

# parameters
nc: 15  # number of classes
depth_multiple: 1.0  # model depth multiple
width_multiple: 1.0  # layer channel multiple

# anchors
anchors:
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

# CSPDarknet53-SPP backbone
backbone:
  # [from, number, module, args]
  [[-1, 1, Conv, [32, 3, 1]],  # 0
   [-1, 1, Conv, [64, 3, 2]],  # 1-P1/2
   [-1, 1, BottleneckCSP, [64]],
   [-1, 1, Conv, [64, 1, 1]],
   [-1, 1, Conv, [128, 3, 2]],  # 4-P2/4
   [-1, 2, BottleneckCSP, [128]],
   [-1, 1, Conv, [128, 1, 1]],
   [-1, 1, Conv, [256, 3, 2]],  # 7-P3/8
   [-1, 8, BottleneckCSP, [256]],
   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, Conv, [512, 3, 2]],  # 10-P4/16
   [-1, 8, BottleneckCSP, [512]],
   [-1, 1, Conv, [512, 1, 1]],
   [-1, 1, Conv, [1024, 3, 2]],  # 13-P5/32
   [-1, 4, BottleneckCSP, [1024]],
   [-1, 1, Conv, [1024, 1, 1]],  # 15
  ]

# YOLOv5 head
head:
  [[-1, 1, Conv, [512, 1, 1]],
   [-1, 1, Conv, [1024, 3, 1]],
   [-1, 1, Conv, [512, 1, 1]],
   [-1, 1, SPP, [1024, [5, 9, 13]]],
   [-1, 1, Conv, [512, 1, 1]],
   [-1, 1, Conv, [1024, 3, 1]],
   [-1, 1, Conv, [512, 1, 1]],  # 22

   [-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, "nearest"]],
   [[-1, 12], 1, Concat, [1]],  # concat backbone P4
   [-1, 3, BottleneckCSP, [512, False]],  # 26

   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, "nearest"]],
   [[-1, 9], 1, Concat, [1]],  # concat backbone P3
   [-1, 3, BottleneckCSP, [256, False]],  # 30

   [-1, 1, Conv, [256, 3, 2]],
   [[-1, 27], 1, Concat, [1]],  # concat head P4
   [-1, 3, BottleneckCSP, [512, False]],  # 33

   [-1, 1, Conv, [512, 3, 2]],
   [[-1, 23], 1, Concat, [1]],  # concat head P5
   [-1, 3, BottleneckCSP, [1024, False]],  # 36

   [[30, 33, 36], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)
  ]

主要是注意nc这个变量名。
随后我们使用下述命令开始训练:

python train.py --data ./dota_data/dota_name.yaml --weights '' --cfg ./dota_data/yolov4.yaml --img 128

我训练了574个epochs以后停止训练,我这边顺带提供训练后的命令行输出结果:



     Epoch   gpu_mem       box       obj       cls    labels  img_size
  560/2999     9.06G    0.1022   0.02479  0.004797       823       256:  43%|████▎     | 143/329 [02:13<02:53,  1.07it/s]                  wandb: Network error (ConnectTimeout), entering retry loop.
  560/2999     9.06G    0.1014   0.02474  0.004767        19       256: 100%|██████████| 329/329 [05:06<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:49<00:00,  1.14it/s]    
                 all       5297      59807       0.71      0.573      0.586      0.338

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  561/2999     9.06G    0.1012   0.02473  0.004774       113       256: 100%|██████████| 329/329 [05:07<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:49<00:00,  1.14it/s]    
                 all       5297      59807       0.71      0.572      0.586      0.338

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  562/2999     9.06G    0.1007   0.02462   0.00482        85       256: 100%|██████████| 329/329 [05:07<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:49<00:00,  1.14it/s]    
                 all       5297      59807      0.711      0.572      0.586      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  563/2999     9.06G    0.1009     0.025  0.004669        79       256: 100%|██████████| 329/329 [05:07<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:48<00:00,  1.15it/s]    
                 all       5297      59808      0.712      0.571      0.586      0.338

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  564/2999     9.06G    0.1017   0.02497  0.005007        70       256: 100%|██████████| 329/329 [05:06<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:48<00:00,  1.15it/s]    
                 all       5297      59808      0.712      0.571      0.586      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  565/2999     9.06G    0.1019    0.0249    0.0047       115       256: 100%|██████████| 329/329 [05:07<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:51<00:00,  1.09it/s]    
                 all       5297      59808      0.713       0.57      0.586      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  566/2999     9.06G    0.1022   0.02518  0.005203        63       256: 100%|██████████| 329/329 [05:08<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:49<00:00,  1.14it/s]    
                 all       5297      59808      0.715      0.567      0.585      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  567/2999     9.06G    0.1015   0.02489  0.005049        49       256: 100%|██████████| 329/329 [05:08<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:49<00:00,  1.13it/s]    
                 all       5297      59809      0.714      0.569      0.586      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  568/2999     9.06G    0.1023   0.02582  0.006269         7       256: 100%|██████████| 329/329 [05:08<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95:  55%|█████▌    | 31/56 [00:27<00:22,  1.09it/s]    wandb: Network error (ConnectTimeout), entering retry loop.
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:48<00:00,  1.15it/s]    
                 all       5297      59809      0.707      0.572      0.586      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  569/2999     9.06G    0.1012    0.0249  0.005227       714       256:  39%|███▉      | 128/329 [01:59<03:07,  1.07it/s]                  wandb: Network error (ConnectTimeout), entering retry loop.
  569/2999     9.06G    0.1016   0.02509  0.005097      1200       256:  63%|██████▎   | 207/329 [03:13<01:54,  1.07it/s]                  wandb: Network error (ConnectTimeout), entering retry loop.
  569/2999     9.06G    0.1019   0.02506  0.005005       136       256: 100%|██████████| 329/329 [05:07<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:48<00:00,  1.15it/s]    
                 all       5297      59811       0.71       0.57      0.585      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  570/2999     9.06G    0.1014   0.02463  0.004804        57       256: 100%|██████████| 329/329 [05:07<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:49<00:00,  1.13it/s]    
                 all       5297      59811      0.717      0.565      0.585      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  571/2999     9.06G    0.1015   0.02446  0.004885        15       256: 100%|██████████| 329/329 [05:07<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:48<00:00,  1.14it/s]    
                 all       5297      59811      0.717      0.565      0.585      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  572/2999     9.06G    0.1009   0.02462  0.004638       591       256:  10%|| 34/329 [00:31<04:35,  1.07it/s]                   wandb: Network error (ConnectTimeout), entering retry loop.
  572/2999     9.06G    0.1012   0.02497  0.004651        56       256: 100%|██████████| 329/329 [05:07<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:48<00:00,  1.14it/s]    
                 all       5297      59811      0.718      0.565      0.585      0.339

     Epoch   gpu_mem       box       obj       cls    labels  img_size
  573/2999     9.06G    0.1013   0.02484  0.004554        49       256: 100%|██████████| 329/329 [05:08<00:00,  1.07it/s]                  
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:49<00:00,  1.14it/s]    
                 all       5297      59811      0.717      0.565      0.585      0.339
Stopping training early as no improvement observed in last 100 epochs. Best results observed at epoch 473, best model saved as best.pt.
To update EarlyStopping(patience=100) pass a new patience value, i.e. `python train.py --patience 300` or use `--patience 0` to disable EarlyStopping.

574 epochs completed in 57.592 hours.
Optimizer stripped from runs/exp/weights/last.pt, 121.5MB
Optimizer stripped from runs/exp/weights/best.pt, 121.5MB

Validating runs/exp/weights/best.pt...
Fusing layers... 
yolov4 summary: 440 layers, 60466580 parameters, 0 gradients, 130.9 GFLOPs
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95:  93%|█████████▎| 52/56 [01:16<00:04,  1.24s/it]    wandb: Network error (ConnectTimeout), entering retry loop.
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [01:19<00:00,  1.43s/it]    
                 all       5297      59804      0.719      0.571       0.59      0.339
       small-vehicle       5297      10828      0.595      0.432       0.41      0.202
       large-vehicle       5297       9256      0.753      0.653      0.687      0.406
               plane       5297       5298      0.871      0.849      0.872      0.604
        storage-tank       5297       5012      0.888      0.417      0.481      0.277
                ship       5297      19571      0.786      0.552      0.644      0.332
              harbor       5297       4788      0.786      0.784       0.78       0.38
  ground-track-field       5297        316      0.791      0.424      0.519      0.257
   soccer-ball-field       5297        347      0.703      0.473      0.473      0.278
        tennis-court       5297       1602      0.798      0.878      0.893      0.736
       swimming-pool       5297        763      0.685      0.611       0.55      0.232
    baseball-diamond       5297        403      0.764      0.695      0.726      0.388
          roundabout       5297        293      0.771      0.468      0.516      0.266
    basketball-court       5297        297      0.542      0.401      0.439        0.3
              bridge       5297        896      0.579      0.455      0.417      0.166
          helicopter       5297        134      0.469      0.467      0.448      0.263
wandb: Waiting for W&B process to finish... (success).
wandb:                                                                                
wandb: 
wandb: Run history:
wandb:      metrics/mAP_0.5 ▁▃▆▆▇▇▇▇▇▇▇█████████████████████████████
wandb: metrics/mAP_0.5:0.95 ▁▃▅▆▆▆▆▆▇▇▇▇▇▇▇▇████████████████████████
wandb:    metrics/precision ▇▁▅▅▆▇▇▆▇▇██▇▇▇▇██▇▇▇▇▇▇█▇▇█▇██▇▇▇█▇▇▇▇▇
wandb:       metrics/recall ▁▃▅▆▆▆▆▇▇▇▇▇▇▇██▇███████████████████████
wandb:       train/box_loss █▆▅▄▄▃▃▃▃▃▃▃▂▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▂▂▂▁▁
wandb:       train/cls_loss █▅▄▃▃▃▂▃▂▂▂▂▂▃▂▂▂▂▂▂▂▁▂▁▁▁▁▂▁▁▂▂▂▁▁▂▁▁▁▁
wandb:       train/obj_loss █▇▆▆▆▅▅▄▄▄▄▅▄▆▃▃▃▃▃▄▂▃▃▂▂▂▂▃▂▂▃▂▃▂▁▃▂▁▁▁
wandb:         val/box_loss █▅▃▃▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
wandb:         val/cls_loss █▄▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
wandb:         val/obj_loss █▇▆▅▄▃▁▂▃▃▃▂▂▂▂▂▂▃▃▄▅▅▅▅▅▅▆▆▇▆▆▇▇███▇█▇█
wandb:                x/lr0 ███▇▇▇▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▄▄▃▃▃▃▃▃▂▂▂▂▂▁▁▁
wandb:                x/lr1 ███▇▇▇▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▄▄▃▃▃▃▃▃▂▂▂▂▂▁▁▁
wandb:                x/lr2 ███▇▇▇▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▄▄▃▃▃▃▃▃▂▂▂▂▂▁▁▁
wandb: 
wandb: Run summary:
wandb:           best/epoch 473
wandb:         best/mAP_0.5 0.59052
wandb:    best/mAP_0.5:0.95 0.33932
wandb:       best/precision 0.719
wandb:          best/recall 0.57074
wandb:      metrics/mAP_0.5 0.59042
wandb: metrics/mAP_0.5:0.95 0.33913
wandb:    metrics/precision 0.71865
wandb:       metrics/recall 0.5706
wandb:       train/box_loss 0.10126
wandb:       train/cls_loss 0.00455
wandb:       train/obj_loss 0.02484
wandb:         val/box_loss 0.08645
wandb:         val/cls_loss 0.00941
wandb:         val/obj_loss 0.01825
wandb:                x/lr0 0.00811
wandb:                x/lr1 0.00811
wandb:                x/lr2 0.00811
wandb: 
wandb: Synced copper-gorge-5: https://wandb.ai/dention/runs/runs/3ixp7thm
wandb: Synced 5 W&B file(s), 49 media file(s), 1 artifact file(s) and 0 other file(s)
wandb: Find logs at: ./wandb/run-20220825_141538-3ixp7thm/logs
Results saved to runs/exp

3.2 测试结果

使用如下命令运行推断:

python ./detect.py --weights <path>/best.pt --source <path>/<to>/<images>

选取了test中一张图片


顺带提供分割后子图的识别情况,如下

四、使用Yolov4-Tiny训练与推断

4.1 调整yolov4-tiny.yaml与训练

如果直接使用开源的yolov4-tiny.yaml将会出现如下的error:

Traceback (most recent call last):
  File "train.py", line 673, in <module>
    main(opt)
  File "train.py", line 568, in main
    train(opt.hyp, opt, device, callbacks)
  File "train.py", line 129, in train
    model = Model(cfg, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device)  # create
  File "/root/autodl-tmp/yolo-master/models/yolo.py", line 112, in __init__
    self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch])  # model, savelist
  File "/root/autodl-tmp/yolo-master/models/yolo.py", line 260, in parse_model
    m = eval(m) if isinstance(m, str) else m  # eval strings
  File "", line 1, in <module>
NameError: name 'Maxpool' is not defined

这是因为库中没有定义Maxpool,当然不用着急,因为nn自带最大池化的函数,所以我们需要调整yolov4-tiny.yaml文件,如下:

# parameters
nc: 15  # number of classes
depth_multiple: 1.0  # model depth multiple
width_multiple: 1.0  # layer channel multiple

# anchors
anchors:
  - [10,14, 23,27, 37,58]  # P4/16
  - [81,82, 135,169, 344,319]  # P5/32

# CSPDarknet-19 backbone
backbone:
  # [from, number, module, args]
  [[-1, 1, Conv, [32, 3, 2]],  # 0-p1/2
   [-1, 1, Conv, [64, 3, 2]],  # 1-P2/4
   [-1, 1, BottleneckCSP, [64]],
   [-1, 1, nn.MaxPool2d, [2, 2]],   # 3-P3/8
   [-1, 1, BottleneckCSP, [128]],
   [-1, 1, nn.MaxPool2d, [2, 2]],   # 5-P4/16
   [-1, 1, BottleneckCSP, [256]],
   [-1, 1, nn.MaxPool2d, [2, 2]],   # 7-P5/32
   [-1, 1, Conv, [512, 3, 1]],
  ]

# YOLOv3-tiny head
head:
  [[-1, 1, Bottleneck, [512, False]],  # 9

   [-1, 1, Conv, [128, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, "nearest"]],
   [[-1, 6], 1, Concat, [1]],  # concat backbone P4
   [-1, 1, Conv, [256, 1, 1]],  # 13

   [[13, 9], 1, Detect, [nc, anchors]],   # Detect(P4, P5)
  ]

使用下述命令从0开始训练:

python train.py --data ./dota_data/dota_name.yaml --weights '' --cfg ./dota_data/yolov4.yaml --img 128

运行了999个epochs,终端输出结果如下:

Optimizer stripped from runs/exp5/weights/last.pt, 121.5MB
Optimizer stripped from runs/exp5/weights/best.pt, 121.5MB

Validating runs/exp5/weights/best.pt...
Fusing layers... 
yolov4 summary: 440 layers, 60466580 parameters, 0 gradients, 130.9 GFLOPs
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 56/56 [00:36<00:00,  1.53it/s]                                       
                 all       5297      59814      0.608    0.00951    0.00289   0.000565
       small-vehicle       5297      10828    0.00134    0.00803   0.000257   5.39e-05
       large-vehicle       5297       9256     0.0901     0.0931     0.0242    0.00453
               plane       5297       5298    0.00335   0.000189    0.00373   0.000839
        storage-tank       5297       5012          1          0   0.000907   0.000233
                ship       5297      19571     0.0077     0.0162    0.00105   0.000212
              harbor       5297       4788    0.00998     0.0251    0.00452   0.000878
  ground-track-field       5297        316          1          0    0.00863    0.00173
   soccer-ball-field       5297        351          1          0          0          0
        tennis-court       5297       1602          1          0          0          0
       swimming-pool       5297        763          1          0          0          0
    baseball-diamond       5297        403          1          0          0          0
          roundabout       5297        299          0          0          0          0
    basketball-court       5297        297          1          0          0          0
              bridge       5297        896          1          0          0          0
          helicopter       5297        134          1          0          0          0
wandb: Waiting for W&B process to finish... (success).
wandb:                                                                                
wandb: 
wandb: Run history:
wandb:      metrics/mAP_0.5 ▁██
wandb: metrics/mAP_0.5:0.95 ▁██
wandb:    metrics/precision ▁██
wandb:       metrics/recall ▁██
wandb:       train/box_loss █▁
wandb:       train/cls_loss █▁
wandb:       train/obj_loss ▁█
wandb:         val/box_loss █▁▁
wandb:         val/cls_loss █▁▁
wandb:         val/obj_loss ▁██
wandb:                x/lr0 ▁█
wandb:                x/lr1 ▁█
wandb:                x/lr2 █▁
wandb: 
wandb: Run summary:
wandb:           best/epoch 1
wandb:         best/mAP_0.5 0.00291
wandb:    best/mAP_0.5:0.95 0.00057
wandb:       best/precision 0.6075
wandb:          best/recall 0.00951
wandb:      metrics/mAP_0.5 0.00289
wandb: metrics/mAP_0.5:0.95 0.00056
wandb:    metrics/precision 0.6075
wandb:       metrics/recall 0.00951
wandb:       train/box_loss 0.14971
wandb:       train/cls_loss 0.04377
wandb:       train/obj_loss 0.00704
wandb:         val/box_loss 0.14975
wandb:         val/cls_loss 0.05936
wandb:         val/obj_loss 0.00455
wandb:                x/lr0 0.00336
wandb:                x/lr1 0.00336
wandb:                x/lr2 0.0368
wandb: 
wandb: Synced earthy-cosmos-10: https://wandb.ai/dention/runs/runs/28tsv5zq
wandb: Synced 5 W&B file(s), 81 media file(s), 1 artifact file(s) and 0 other file(s)
wandb: Find logs at: ./wandb/run-20220901_091249-28tsv5zq/logs
Results saved to runs/exp5

4.2 测试结果

使用如下命令运行推断:

python ./detect.py --weights <path>/best.pt --source <path>/<to>/<images>

与Yolov4采用相同的图像,

分割后的图像经过目标检测如下:


总结

本文对Yolov4和Yolov4-Tiny都做了从0开始训练权重文件。

你可能感兴趣的:(pytorch,深度学习,计算机视觉)