I am trying to implement detnet backbone using code from a book, but I am bit confused with how the convolution blocks are arranged for each bottleneck…
The random number generator output tensor (input) is of size (1, 1024), not sure what the parameters value 14 are for.
I am also confused with how the input is used to interface with bottleneck_b
977×888
detnet.py
import torch
from detnet_bottleneck import DetBottleneck

# 完成一个Stage 5,即B-A-A的结构,Stage 4输出通道数为1024
bottleneck_b = DetBottleneck(1024, 256, 1, True).cuda()

bottleneck_a1 = DetBottleneck(256, 256).cuda()
bottleneck_a2 = DetBottleneck(256, 256).cuda()

input = torch.randn(1, 1024, 14, 14).cuda()

# 将input作为某一层的特征图,依次传入Bottleneck B、A1与A2三个模块
output1 = bottleneck_b(input)
output2 = bottleneck_a1(output1)
output3 = bottleneck_a2(output2)

print(output1.shape)
print(output2.shape)
print(output3.shape)

detnet_bottleneck.py
from torch import nn
class DetBottleneck(nn.Module):
    # 初始化时extra为False时为Bottleneck A,为True时则为Bottleneck B
    def __init__(self, inplanes, planes, stride=1, extra=False):
        super(DetBottleneck, self).__init__()
        # 构建连续3个卷积层的Bottleneck
        self.bottleneck = nn.Sequential
        (
            nn.Conv2d(inplanes, planes, 1, bias=False),
            nn.BatchNorm2d(planes),
            nn.ReLU(inplace=True),
            nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=2, dilation=2, bias=False),
            nn.BatchNorm2d(planes),
            nn.ReLU(inplace=True),
            nn.Conv2d(planes, planes, 1, bias=False),
            nn.BatchNorm2d(planes),
        )

        self.relu = nn.ReLU(inplace=True)
        self.extra = extra
        # Bottleneck B的1×1卷积
        if self.extra:
            self.extra_conv = nn.Sequential(
                nn.Conv2d(inplanes, planes, 1, bias=False),
                nn.BatchNorm2d(planes)
            )

    def forward(self, x):
        # 对于Bottleneck B来讲,需要对恒等映射增加卷积处理,与ResNet类似
        if self.extra:
            identity = self.extra_conv(x)
        else:
            identity = x
        out = self.bottleneck(x)
        out += identity
        out = self.relu(out)
        return out