最近学习别人复现的deeplab的代码,看到这样写的:
class DeepLabV2(nn.Sequential):
"""
DeepLab v2: Dilated ResNet + ASPP
Output stride is fixed at 8
"""
def __init__(self, n_classes, n_blocks, atrous_rates):
super(DeepLabV2, self).__init__()
ch = [64 * 2 ** p for p in range(6)]
self.add_module("layer1", _Stem(ch[0]))
self.add_module("layer2", _ResLayer(n_blocks[0], ch[0], ch[2], 1, 1))
self.add_module("layer3", _ResLayer(n_blocks[1], ch[2], ch[3], 2, 1))
self.add_module("layer4", _ResLayer(n_blocks[2], ch[3], ch[4], 1, 2))
self.add_module("layer5", _ResLayer(n_blocks[3], ch[4], ch[5], 1, 4))
self.add_module("aspp", _ASPP(ch[5], n_classes, atrous_rates))
def freeze_bn(self):
for m in self.modules():
if isinstance(m, _ConvBnReLU.BATCH_NORM):
m.eval()
我找了半天没看到forward函数,仔细一看这里不是常见的继承nn.Module的定义模型的方法,继承的是nn.Sequential。
网上查了一下nn.Sequential方法大概就是按照类定义的顺序去执行模型,并且不需要写forward函数。
博客:https://blog.csdn.net/qq_41488595/article/details/123305865中举了几种集成nn.Sequential模型的写法,代码如下:
class conv3x3(nn.Sequential):
def __init__(self, in_channel, stride, bias=False):
super(conv3x3, self).__init__(
nn.Conv2d(in_channels=in_channel, out_channels=in_channel,
kernel_size=3, stride=stride, padding=1, groups=in_channel, bias=bias),
nn.BatchNorm2d(in_channel)
)
class conv3x3(nn.Sequential):
def __init__(self, in_channel, stride, bias=False):
super(conv3x3, self).__init__()
self.conv3x3 = nn.Sequential(
nn.Conv2d(in_channels=in_channel, out_channels=in_channel,
kernel_size=3, stride=stride, padding=1, groups=in_channel, bias=bias),
nn.BatchNorm2d(in_channel)
)
class conv3x3(nn.Module):
# 3x3卷积中的输入通道和输出通道一致,且使用dw卷积,也就是group=channel,都不使用Relu,stride有两种取值,2只在每个stage的第一个block
def __init__(self, in_channel, stride, bias=False):
super(conv3x3, self).__init__()
self.conv3x3 = nn.Sequential(
nn.Conv2d(in_channels=in_channel, out_channels=in_channel,
kernel_size=3, stride=stride, padding=1, groups=in_channel, bias=bias),
nn.BatchNorm2d(in_channel)
)
def forward(self, x):
return self.conv3x3(x)
这样写确实可以,但是我有个疑问,这个nn.Sequential不写forward的话,那么debug的时候,我该咋知道我的模型跑到哪里了?望哪位大佬能解答一下疑惑。