构建 U-Net:forward 方法
在已经定义编码器和解码器层的基础上,您现在可以实现 U-Net 的 forward() 方法。输入已经为您经过编码器处理。不过,您需要定义最后一个解码器块。
解码器的目标是对特征图进行上采样,使其输出的高度和宽度与 U-Net 的输入图像一致。这样您就可以获得像素级的语义掩码。
本练习是课程的一部分
使用 PyTorch 进行图像深度学习
练习说明
- 定义最后一个解码器块,使用
torch.cat()构建跳跃连接。
交互式实操练习
通过完成这段示例代码来试试这个练习。
def forward(self, x):
x1 = self.enc1(x)
x2 = self.enc2(self.pool(x1))
x3 = self.enc3(self.pool(x2))
x4 = self.enc4(self.pool(x3))
x = self.upconv3(x4)
x = torch.cat([x, x3], dim=1)
x = self.dec1(x)
x = self.upconv2(x)
x = torch.cat([x, x2], dim=1)
x = self.dec2(x)
# Define the last decoder block with skip connections
x = ____
x = ____
x = ____
return self.out(x)