การสร้าง U-Net: เมธอด forward
เมื่อกำหนด encoder และ decoder layer เรียบร้อยแล้ว ก็สามารถ implement เมธอด forward() ของ U-Net ได้ โดย input ถูกส่งผ่าน encoder ให้แล้ว แต่ยังต้องกำหนด decoder block สุดท้ายด้วยตัวเอง
เป้าหมายของ decoder คือการ upsample feature map ให้ผลลัพธ์มีความสูงและความกว้างเท่ากับภาพ input ของ U-Net ซึ่งจะช่วยให้ได้ semantic mask ในระดับ pixel
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Deep Learning สำหรับภาพด้วย PyTorch
คำแนะนำการฝึกหัด
- กำหนด decoder block สุดท้าย โดยใช้
torch.cat()เพื่อสร้าง skip connection
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
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)