
LinkNet模型用于语义分割,

整体结构所需模块:
卷积模块:卷积con2+BN层+激发
反卷积模块:反卷积+BN+激发
解码器:4*卷积层
编码器:编码器
模块代码理解:
卷积模块:
初始化内容:输入channel,输出channel,kernel_size(卷积核),stride(步长),pad(填充)
使用Sequential打包:conv2d(卷积),BN层,nn.ReLU(inplace=True)
inplace = True ,会改变输入数据的值,节省反复申请与释放内存的空间与时间,只是将原来的地址传递,效率更好
forward:可以不进行判断
反卷积模块
初始化内容:输入channel,输出channel,kernel_size(卷积核),stride(步长),pad(填充)output_padding(对反卷积后的边缘进行填充)
conv2d(卷积)
BN层
forward:进行判断是否为中间层,中间层激活BN
编码器模块:
除图中四个卷积层外,还有一个residue/shortcut卷积层,共进行五次Convblock调用,其中第一次卷积/2:进行一次缩放
class EncodeBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(EncodeBlock, self).__init__()
self.conv1 = ConvBlock(in_channels, out_channels, stride=2)
self.conv2 = ConvBlock(out_channels, out_channels)
self.conv3 = ConvBlock(out_channels, out_channels)
self.conv4 = ConvBlock(out_channels, out_channels)
self.shortcut = ConvBlock(in_channels, out_channels, stride=2)
forwrd:进行两次卷积
def forward(self, x):
out1 = self.conv1(x)
out1 = self.conv2(out1)
带有缩放
residue = self.shortcut(x)
对输入进行加和再进行两次卷积
out2 = self.conv3(out1 + residue)
out2 = self.conv4(out2)
输出和
return out2 + out1

解码器模块:
卷积【输入in_channels(m), out_channels输出(m/4)】+反卷积【输入m/4,输出m/4】+卷积【输入m/4,输出mn】
class DecodeBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(DecodeBlock, self).__init__()
self.conv1 = ConvBlock(in_channels, in_channels//4,
kernel=1, pad=0)
self.deconv = DeconvBlock(in_channels//4, in_channels//4)
self.conv2 = ConvBlock(in_channels//4, out_channels,
kernel=1, pad=0)
反卷积模块默认kernel_size=3,stride=2,pad=1,output_padding=1 :
class DeconvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel=3, stride=2, pad=1,padding=1):

整体结构:

左半部分:
初始卷积层+maxpool层
编码器1
编码器2
编码器3
编码器4
右半部分:
解码器1
解码器2
解码器3
解码器4
反卷积层+卷积层+反卷积层
forward:
初始卷积——初始maxpool——编码1l1——编码2l2——编码3l3——编码4l4——r4=对l4进行解码+l3——r3=对r4进行解码+l2——r2=对r3进行解码+l1——r1=对r2进行解码——最后反卷积1——最后卷积——最后反卷积2。
def forward(self, x):
x = self.init_conv(x)
x = self.init_maxpool(x)
l1 = self.encode1(x)
l2 = self.encode2(l1)
l3 = self.encode3(l2)
l4 = self.encode4(l3)
r4 = self.decode4(l4) + l3
r3 = self.decode3(r4) + l2
r2 = self.decode2(r3) + l1
r1 = self.decode1(r2)
last1 = self.deconv_last1(r1)
last2 = self.conv_last(last1)
last3 = self.deconv_last2(last2, is_act=False)
return last3