YOLOv5 hyperparameters, migration training settings

Table of contents

1. Hyperparameter settings

1. Define the adaptive function

2. Define model depth and width

2. Migration training settings

1. Set up frozen layers for transfer training


1. Hyperparameter settings

1. Define the adaptive function

The fitness in the genetic algorithm is the main index to describe the performance of the individual, which directly affects the convergence speed of the algorithm and whether the optimal solution can be found. Fitness is a value that is sought to be maximized during training. The default fitness function of YOLOv5 is a weighted combination of indicators: mAP_0.5 accounts for 10% of the weight; mAP_0.5:0.95 accounts for 90% of the weight, and there is no Precision and Recall.

The defined location is in ./utils/metric.py:

def fitness(x):
    # Model fitness as a weighted combination of metrics
    w = [0.0, 0.0, 0.1, 0.9]  # weights for [P, R, [email protected], [email protected]:0.95]
    return (x[:, :4] * w).sum(1)

2. Define model depth and width

YOLOv5 introduces depth_multiple and width_multiple coefficients to get models of different sizes:

nc: 1  # number of classes
depth_multiple: 0.33  # model depth multiple
width_multiple: 0.50  # layer channel multiple

depth_multiple indicates the zoom factor of the channel, which is to multiply the channel settings of the backbone and head parts in the configuration by this factor. And width_multiple represents the layer scaling factor of the BottleneckCSP module, and the number coefficient of all the BottleneckCSP modules is multiplied by this parameter to obtain the final number of layers. It can be found that through these two parameters, model designs of different sizes and complexities can be realized.

The backbone and head under the same file record the network structure:

# 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)
  ]

from: the first column; represents which layer the input comes from, -1 represents the previous layer, 4 represents the fourth layer

number: the second column; the number of convolution kernels, the final number needs to be multiplied by width

module: the third column; module name, including: Conv Focus BottleneckCSP SPP #

args: the fourth column; the parameters of the module

2. Migration training settings

1. Set up frozen layers for transfer training

Migration training by freezing some layers enables fast retraining on the new model to save training resources. The freezing of all layers of YOLOv5 is achieved by setting its gradient to zero, and the execution location is in train.py:

# Freeze
    freeze = [f'model.{x}.' for x in (freeze if len(freeze) > 1 else range(freeze[0]))]  # layers to freeze
    for k, v in model.named_parameters():
        v.requires_grad = True  # train all layers
        if any(x in k for x in freeze):
            LOGGER.info(f'freezing {k}')
            v.requires_grad = False

You can view the network layer in ./model/yolov5x.yaml:

The backbone is 0-9 layers

head is layer 13-23

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)
  ]

pass:

python train.py --freeze 10

Freeze the head for training.

pass:

python train.py --freeze 24

Freeze all layers except the output layer for training.

Guess you like

Origin blog.csdn.net/WZT725/article/details/124244108