0

我指的是这段代码。用于设计 YOLOv4。我有一个关于制作backbone().(=CSPDarkNet53= CSPNet + DarkNet53)

代码在 (yolov4 > models.py)

class Backbone(nn.Module):
    def __init__(self, yolov4conv137weight=None, inference=False):
        self.down1 = DownSample1()
        self.down2 = DownSample2()
        self.down3 = DownSample3()
        self.down4 = DownSample4()
        self.down5 = DownSample5()
        self.end = TransferClassify()

        # neck
        self.neek = Neck(inference)
        # yolov4conv137
        if yolov4conv137weight:
            _model = nn.Sequential(self.down1, self.down2, self.down3, self.down4, self.down5, self.neek)
            pretrained_dict = torch.load(yolov4conv137weight)

            model_dict = _model.state_dict()
            # 1. filter out unnecessary keys
            pretrained_dict = {k1: v for (k, v), k1 in zip(pretrained_dict.items(), model_dict)}
            # 2. overwrite entries in the existing state dict
            model_dict.update(pretrained_dict)
            _model.load_state_dict(model_dict)

        self._model = nn.Sequential(self.down1, self.down2, self.down3, self.down4, self.down5, self.end)

    def forward(self, x):
        return self._model(x)

问题是 DownSample2()

class ResBlock(nn.Module):
    """
    Sequential residual blocks each of which consists of \
    two convolution layers.
    Args:
        ch (int): number of input and output channels.
        nblocks (int): number of residual blocks.
        shortcut (bool): if True, residual tensor addition is enabled.
    """

    def __init__(self, ch, nblocks=1, shortcut=True):
        super().__init__()
        self.shortcut = shortcut
        self.module_list = nn.ModuleList()
        for i in range(nblocks):
            resblock_one = nn.ModuleList()
            resblock_one.append(Conv_Bn_Activation(ch, ch, 1, 1, 'mish'))
            resblock_one.append(Conv_Bn_Activation(ch, ch, 3, 1, 'mish'))
            self.module_list.append(resblock_one)

    def forward(self, x):
        for module in self.module_list:
            h = x
            for res in module:
                h = res(h)
            x = x + h if self.shortcut else h
        return x

class DownSample2(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = Conv_Bn_Activation(64, 128, 3, 2, 'mish')
        self.conv2 = Conv_Bn_Activation(128, 64, 1, 1, 'mish')
        # r -2
        self.conv3 = Conv_Bn_Activation(128, 64, 1, 1, 'mish')

        self.resblock = ResBlock(ch=64, nblocks=2)

        # s -3
        self.conv4 = Conv_Bn_Activation(64, 64, 1, 1, 'mish')
        # r -1 -10
        self.conv5 = Conv_Bn_Activation(128, 128, 1, 1, 'mish')

    def forward(self, input):
        x1 = self.conv1(input)
        x2 = self.conv2(x1)
        x3 = self.conv3(x1)

        r = self.resblock(x3)
        x4 = self.conv4(r)

        x4 = torch.cat([x4, x2], dim=1)
        x5 = self.conv5(x4)
        return x5

他们做了一个ResBlock()类,用来做一个DownSample2()图层。

ResBlock()中,有一个for这样的循环:

    self.module_list = nn.ModuleList()
    for i in range(nblocks):
        resblock_one = nn.ModuleList()
        resblock_one.append(Conv_Bn_Activation(ch, ch, 1, 1, 'mish'))
        resblock_one.append(Conv_Bn_Activation(ch, ch, 3, 1, 'mish'))
        self.module_list.append(resblock_one)

他们正在尝试使用Conv_Bn_Activation具有不同步幅 1 和 3的层来重复这些层,并附nblocks加到nn.ModuleList()

它似乎直接附加到“resblock_one's ModuleList()”,最后进入“self.module_list's ModuleList()”,对吧?那么,为什么他们不直接附加到 self.moudle_list 呢?

它会产生一些不同的效果,或者我的方式是错误的。我是说:

   self.module_list = nn.ModuleList()
    for i in range(nblocks):
        self.module_list.append(Conv_Bn_Act_layer(ch, ch, 1, 1, 'mish'))
        self.module_list.append(Conv_Bn_Act_layer(ch, ch, 3, 1, 'mish'))

这段代码可以代替上面的吗?

现在,我明白了……在“前进”中,他们想部分使用对吗?

4

0 回答 0