残差网络连接,使得输入与输出的尺寸一样
def forward(self, x):out = self.layer1(x)out = self.layer2(out)# 使用插值将输入x上采样至与layer2输出相同的尺寸x = F.interpolate(x, size=(out.size(2), out.size(3)), mode='bilinear', align_corners=False)# 确保x的通道数与out匹配x = x[:, :out.size(1), :, :] # 选择前out.size(1)个通道out += x # 残差连接,将输入直接添加到layer2的输出上out = out.reshape(out.size(0), -1)out = self.dropout(out)out = self.fc(out)return out
这里采用双线性插值法