ONE is preparing: 01 / INITIALIZING MODEL ROUTER. Evaluating routing across providers.
Current state
01 / INITIALIZING MODEL ROUTER.

Evaluating routing across providers.

Start learning
← Kiến trúc tiên tiến

AI Researcher

Kiến trúc U-Net

Vietnamese onlyThe learning content is available in Vietnamese only. The surrounding interface follows your language.

Kiến trúc encoder-decoder hình chữ U với skip connections cho phân đoạn ảnh

lesson115 minKiến trúc tiên tiếnAI Researcher

Dự đoán

Bạn cần gán nhãn MỖI pixel trong ảnh (ví dụ: pixel này là đường, pixel kia là xe). Autoencoder nén ảnh rồi giải nén, nhưng chi tiết pixel bị mất. Làm sao khắc phục?

U-Net! Encoder nén để hiểu 'bức tranh tổng thể' (đâu là đường, đâu là nhà). Decoder phóng to lại, nhưng nhận thêm chi tiết pixel từ encoder qua skip connections. Kết quả: phân đoạn chính xác đến từng pixel!

Khám phá kiến trúc chữ U

Hãy tưởng tượng bạn vẽ bản đồ từ ảnh vệ tinh. Bước 1: bạn zoom out dần để thấy bức tranh tổng thể (encoder). Bước 2: bạn zoom in lại, vẽ chi tiết ranh giới (decoder). Ghi chú từ mỗi mức zoom (skip connections) giúp bạn không quên chi tiết.

Khoảnh khắc Aha

U-Net = Encoder (nén) + Decoder (phóng to) + Skip connections (giữ chi tiết). Hình chữ U! Encoder nắm ngữ cảnh ("đây là con đường"), decoder vẽ chính xác ranh giới pixel, skip connections truyền chi tiết không bị mất khi nén.

U-Net trong Diffusion

Thử thách

Encoder U-Net: 256×256→128→64→32→16 (bottleneck). Mỗi lần max pool 2×2. Skip connection ở cấp 64×64 truyền feature map 64×64. Decoder nhận concat: kích thước?

Decoder upsample 32→64, rồi concat với skip 64×64 từ encoder. Kích thước không gian vẫn 64×64 nhưng số channel gấp đôi (256 + 256 = 512). Conv tiếp theo giảm channel xuống lại.

Giải thích chi tiết

U-Net (Ronneberger et al., 2015) là kiến trúc encoder-decoder hình chữ U dựa trên CNN, ban đầu cho phân đoạn ảnh y tế, nay là backbone chính của diffusion models như Stable Diffusion.

unet_simple.py

import torch.nn as nn

class UNet(nn.Module):
    def __init__(self, in_ch=3, out_ch=1):
        super().__init__()
        # Encoder
        self.enc1 = self.conv_block(in_ch, 64)
        self.enc2 = self.conv_block(64, 128)
        self.pool = nn.MaxPool2d(2)
        # Bottleneck
        self.bottleneck = self.conv_block(128, 256)
        # Decoder
        self.up2 = nn.ConvTranspose2d(256, 128, 2, stride=2)
        self.dec2 = self.conv_block(256, 128)  # 128+128=256 input (concat!)
        self.up1 = nn.ConvTranspose2d(128, 64, 2, stride=2)
        self.dec1 = self.conv_block(128, 64)   # 64+64=128 input
        self.out = nn.Conv2d(64, out_ch, 1)

    def conv_block(self, in_ch, out_ch):
        return nn.Sequential(
            nn.Conv2d(in_ch, out_ch, 3, padding=1), nn.ReLU(),
            nn.Conv2d(out_ch, out_ch, 3, padding=1), nn.ReLU(),
        )

    def forward(self, x):
        e1 = self.enc1(x)                      # Skip 1
        e2 = self.enc2(self.pool(e1))           # Skip 2
        b = self.bottleneck(self.pool(e2))      # Bottleneck
        d2 = self.dec2(torch.cat([self.up2(b), e2], 1))  # Concat skip!
        d1 = self.dec1(torch.cat([self.up1(d2), e1], 1))
        return self.out(d1)

Tóm tắt

  • U-Net = Encoder (nén) + Bottleneck + Decoder (phóng to) + Skip connections (concat chi tiết).
  • Skip connections concat (nối) feature map encoder với decoder cùng cấp → giữ chi tiết pixel-level.
  • Khác ResNet skip (cộng): U-Net skip nối → decoder nhận gấp đôi channel → phong phú thông tin hơn.
  • Ứng dụng: phân đoạn ảnh y tế, vệ tinh, tự lái. Là backbone của Stable Diffusion (diffusion models).
  • Trong Diffusion: U-Net + timestep embedding + cross-attention (text) → sinh ảnh theo mô tả.

Continue the module

Lesson 11 of 12