content
foreword
This blog will explain the implementation source code of MMDetection about Darknet53, the Backbone. In fact, I have learned many versions of yolov3 code before, but they are all based on the source code of various configuration files, and there is no complete network construction code, so I want to change it during experiments. When yolov3's network is used, you will find that you have no way to start, change the configuration file, and don't know where to start. Just recently learning MMDetection, so I plan to start from this simplest and most classic one-stage target detection network to unravel the mystery of MMDetection (a little bit of writing a small composition...).
[Darknet53 network structure (both are stolen pictures, I am too lazy to draw them myself)]
Well, the above has a basic understanding of Darkent53, let's start building Backbone.
1. Configuration file
The backbone related configuration files are as follows:
Model name_backbone name_dataset name
yolov3_d53_mstrain-608_273e_pest.py
model = dict(
type='YOLOV3',
backbone=dict(
type='Darknet', # backbone类型
depth=53, # 网络层数
out_indices=(3, 4, 5), # 输出的stage的序号
init_cfg=dict(type='Pretrained', checkpoint='open-mmlab://darknet53')), # 预训练模型:open-mmlab://darknet53
二、Darknet
Here I don't plan to start from the configuration file and how to read the configuration and then register to find the class Darknet (BaseModule). Our learning center here will be placed on the construction of darknet53. As for configuration file interpretation and registry operations such as MMCV , you can see my other source code interpretation blog: [MMDetection target detection framework learning] .
2.1, Darknet class introduction and global parameter settings
@BACKBONES.register_module()
class Darknet(BaseModule):
"""Darknet backbone.
Args:
depth (int): Depth of Darknet. Currently only support 53.
out_indices (Sequence[int]): Output from which stages.
frozen_stages (int): Stages to be frozen (stop grad and set eval mode).
-1 means not freezing any parameters. Default: -1.
conv_cfg (dict): Config dict for convolution layer. Default: None.
norm_cfg (dict): Dictionary to construct and config norm layer.
Default: dict(type='BN', requires_grad=True)
act_cfg (dict): Config dict for activation layer.
Default: dict(type='LeakyReLU', negative_slope=0.1).
norm_eval (bool): Whether to set norm layers to eval mode, namely,
freeze running stats (mean and var). Note: Effect on Batch Norm
and its variants only.
pretrained (str, optional): model pretrained path. Default: None
init_cfg (dict or list[dict], optional): Initialization config dict.
Default: None
Example:
>>> from mmdet.models import Darknet
>>> import torch
>>> self = Darknet(depth=53)
>>> self.eval()
>>> inputs = torch.rand(1, 3, 416, 416)
>>> level_outputs = self.forward(inputs)
>>> for level_out in level_outputs:
... print(tuple(level_out.shape))
...
(1, 256, 52, 52)
(1, 512, 26, 26)
(1, 1024, 13, 13)
"""
# Dict(depth: (layers, channels))
arch_settings = {
# 深度 5个stage的重复个数 5个stage的输入channel和输出channel
53: ((1, 2, 8, 8, 4), ((32, 64), (64, 128), (128, 256), (256, 512),
(512, 1024)))
}
2.2, __init__ initialization
def __init__(self,
depth=53, # backbone深度
out_indices=(3, 4, 5), # backbone输出的stage的序号(输出向Neck)
frozen_stages=-1, # 哪些层需要冻结权重训练
conv_cfg=None, # 卷积层配置
norm_cfg=dict(type='BN', requires_grad=True), # norm层配置
act_cfg=dict(type='LeakyReLU', negative_slope=0.1), # 激活函数配置
norm_eval=True, # 是否将norm layer设置为eval mode 相应的需要冻结mean and var参数
pretrained=None, # 预训练配置
init_cfg=None): # 初始化配置
super(Darknet, self).__init__(init_cfg)
if depth not in self.arch_settings:
raise KeyError(f'invalid depth {
depth} for darknet')
self.depth = depth # backbone深度
self.out_indices = out_indices # backbone输出的stage的序号(输出向Neck)
self.frozen_stages = frozen_stages # 哪些层需要冻结权重训练
# 5个stage的重复个数 5个stage的输入channel和输出channel
self.layers, self.channels = self.arch_settings[depth]
# 卷积的配置文件 一般conv_cfg=None norm_cfg=BN act_cfg=LeakyReLU
cfg = dict(conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)
# backbone的第一层
self.conv1 = ConvModule(3, 32, 3, padding=1, **cfg)
# self.cr_blocks:存放所有的卷积名即通过name可以直接用self.name找到
self.cr_blocks = ['conv1']
# 依次遍历搭建其他的5个stage [1,2,8,8,4]
for i, n_layers in enumerate(self.layers):
layer_name = f'conv_res_block{
i + 1}' # 每一个stage的name
in_c, out_c = self.channels[i] # 每一个stage的输入输出channel
self.add_module( # 调用make_conv_res_block函数,搭建当前stage
layer_name,
self.make_conv_res_block(in_c, out_c, n_layers, **cfg))
self.cr_blocks.append(layer_name) # 更新self.cr_blocks
# 是否将norm layer设置为eval mode 相应的需要冻结mean and var参数
# Note: Effect on Batch Norm and its variants only.
self.norm_eval = norm_eval
assert not (init_cfg and pretrained), \
'init_cfg and pretrained cannot be setting at the same time'
# pretrained=str self.init_cfg 导入预训练初始化配置
if isinstance(pretrained, str):
warnings.warn('DeprecationWarning: pretrained is deprecated, '
'please use "init_cfg" instead')
self.init_cfg = dict(type='Pretrained', checkpoint=pretrained)
# pretrained=None and init_cfg=None 设置为Kaiming初始化配置
elif pretrained is None:
if init_cfg is None:
self.init_cfg = [
dict(type='Kaiming', layer='Conv2d'),
dict(
type='Constant',
val=1,
layer=['_BatchNorm', 'GroupNorm'])
]
else:
raise TypeError('pretrained must be a str or None')
After initialization, you can see that there are these variables in self (Darknet):
2.3. Forward reasoning
def forward(self, x):
outs = [] # 存放backbone的3个输出feature map
for i, layer_name in enumerate(self.cr_blocks): # 遍历每一个操作名称
# getattr:返回对象self的一个name=layer_name的属性(就是__init__搭建的一个层结构)
cr_block = getattr(self, layer_name)
x = cr_block(x) # 前向推理
# 如果i in self.out_indices 就将当前层的输出feature map保存到outs
# 值得注意的是i=0就是backbone的第一个卷积层 i=1~5就是后面需要在make_conv_res_block中搭建的5个模块
# 其中第3、4、5三个模块输出到Neck中
if i in self.out_indices:
outs.append(x)
# 返回outs 3个输出feature map会在YOLOV3的forward中传入YOLOv3Neck
return tuple(outs)
2.4, build stage1-5
At this point, combined with the two figures at the beginning of the article, you should be very familiar with the structure of the entire backbone in general. The rest is a question: how is this stage1-stage5 built, which involves two functions: make_conv_res_block and ResBlock.
make_conv_res_block function:
@staticmethod
def make_conv_res_block(in_channels, # stage的输入channel
out_channels, # stage的输出channel out_channels=2*in_channels
res_repeat, # 这个stage的ResBlock重复个数
conv_cfg=None, # 卷积配置 一般是None
norm_cfg=dict(type='BN', requires_grad=True), # norm layer配置 一般是BN
act_cfg=dict(type='LeakyReLU', # 激活函数配置 一般是LeakyReLU
negative_slope=0.1)):
"""In Darknet backbone, ConvLayer is usually followed by ResBlock. This
function will make that. The Conv layers always have 3x3 filters with
stride=2. The number of the filters in Conv layer is the same as the
out channels of the ResBlock.
Args:
in_channels (int): The number of input channels.
out_channels (int): The number of output channels.
res_repeat (int): The number of ResBlocks.
conv_cfg (dict): Config dict for convolution layer. Default: None.
norm_cfg (dict): Dictionary to construct and config norm layer.
Default: dict(type='BN', requires_grad=True)
act_cfg (dict): Config dict for activation layer.
Default: dict(type='LeakyReLU', negative_slope=0.1).
"""
# 注意:
# 1、一个stage = Conv(k=3,s=2,p=1) + ResBlock x n
# 2、整个stage在第一个Conv就将feature map的channel上升为了当前stage输入channel的2倍(即当前stage的输出channel)
# wh下采样为输入feature map的一般半 且之后的所有ResBlock部分的feature map wh不变
# {'conv_cfg': None, 'norm_cfg': {'type': 'BN', 'requires_grad': True}, 'act_cfg': {'type': 'LeakyReLU', 'negative_slope': 0.1}}
cfg = dict(conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)
model = nn.Sequential()
model.add_module( # 搭建当前stage的第一个3x3Conv 下采样卷积
'conv',
ConvModule( # mmcv内置的卷积搭建模块 类似Pytorch中的Conv2d
in_channels, out_channels, 3, stride=2, padding=1, **cfg))
# 依次搭建res_repeat个ResBlock
# 注意这res_repeat个ResBlock中所有的卷积输出channel都是out_channels
for idx in range(res_repeat):
model.add_module('res{}'.format(idx),
ResBlock(out_channels, **cfg))
return model
ResBlock function:
class ResBlock(BaseModule):
"""The basic residual block used in Darknet. Each ResBlock consists of two
ConvModules and the input is added to the final output. Each ConvModule is
composed of Conv, BN, and LeakyReLU. In YoloV3 paper, the first convLayer
has half of the number of the filters as much as the second convLayer. The
first convLayer has filter size of 1x1 and the second one has the filter
size of 3x3.
Args:
in_channels (int): The input channels. Must be even.
conv_cfg (dict): Config dict for convolution layer. Default: None.
norm_cfg (dict): Dictionary to construct and config norm layer.
Default: dict(type='BN', requires_grad=True)
act_cfg (dict): Config dict for activation layer.
Default: dict(type='LeakyReLU', negative_slope=0.1).
init_cfg (dict or list[dict], optional): Initialization config dict.
Default: None
"""
def __init__(self,
in_channels, # ResBlock的输入channel=输出channel
conv_cfg=None, # conv配置 一般为None
norm_cfg=dict(type='BN', requires_grad=True), # norm layer配置 一般为BN
act_cfg=dict(type='LeakyReLU', negative_slope=0.1), # 激活函数配置 一般为LeakyReLU
init_cfg=None): # 初始化配置 一般为None
super(ResBlock, self).__init__(init_cfg)
# 注意:ResBlock = 1x1Conv+BN+LeakyReLU + 3x3Conv+BN+LeakyReLU
# 第一个卷积将channel下降为输入channel一半 第二个卷积将channel恢复到输入channel大小
# 所以整个ResBlock的输入channel和输出channel相等 且整个ResBlock所有的特征的wh都相等
assert in_channels % 2 == 0 # ensure the in_channels is even
half_in_channels = in_channels // 2 # 第一个卷积的输出channel 要下降为输入的一半
# shortcut
cfg = dict(conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)
self.conv1 = ConvModule(in_channels, half_in_channels, 1, **cfg) # 1x1conv
self.conv2 = ConvModule( # 3x3conv
half_in_channels, in_channels, 3, padding=1, **cfg)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.conv2(out)
out = out + residual
return out
[Output from Backbone]
Returns three feature maps in tensor format with shapes: [bs, 256, 64, 64], [bs, 512, 32, 32], [bs, 1024, 16, 16], which will be passed to Neck (FPN) layer for feature fusion.
Summarize
Because I have been learning the code of the yolo series for a long time, I have seen many configuration file versions of yolov3, yolov4, and yolov5, so this part is relatively simple as a whole. Basically, I looked at the two pictures at the beginning, according to the source code. Comments, and then debug can understand.
But in fact, there are some codes about MMCV that I selectively ignore, such as BaseModule and ConvModule. One is that there is not enough time, so I can only take a look at the main code, and the other is that I am lazy (hahaha). I hope that I can make up for this pit in the source code of MMCV in the future!