一、開篇:創造力的算法革命
從昨天的Transformer到今天的生成模型,我們正從"理解"世界邁向"創造"世界。生成對抗網絡(GAN)和擴散模型(Diffusion Model)代表了當前生成式AI的兩大主流范式,它們讓機器能夠生成逼真的圖像、音樂甚至視頻。今天我們將深入這兩種技術的核心原理,并親自動手實現圖像生成的神奇過程。
二、上午攻堅:GAN原理與實戰
2.1 GAN核心架構解析
最小最大博弈公式:
min_G max_D V(D,G) = E_{x~p_data}[log D(x)] + E_{z~p_z}[log(1-D(G(z)))]
DCGAN關鍵實現:
# 生成器網絡
class Generator(nn.Module):
def __init__(self, latent_dim=100):
super().__init__()
self.main = nn.Sequential(
# 輸入: (latent_dim, 1, 1)
nn.ConvTranspose2d(latent_dim, 512, 4, 1, 0, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(True),
# 輸出: (512, 4, 4)
nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(True),
# 輸出: (256, 8, 8)
nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(True),
# 輸出: (128, 16, 16)
nn.ConvTranspose2d(128, 1, 4, 2, 1, bias=False),
nn.Tanh() ?# 輸出范圍[-1,1]
# 最終輸出: (1, 28, 28)
)
? ? def forward(self, input):
return self.main(input)
# 判別器網絡
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.main = nn.Sequential(
# 輸入: (1, 28, 28)
nn.Conv2d(1, 128, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# 輸出: (128, 14, 14)
nn.Conv2d(128, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True),
# 輸出: (256, 7, 7)
nn.Conv2d(256, 512, 4, 2, 1, bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True),
# 輸出: (512, 3, 3)
nn.Conv2d(512, 1, 3, 1, 0, bias=False),
nn.Sigmoid() ?# 輸出概率
)
? ? def forward(self, input):
return self.main(input).view(-1)
2.2 GAN訓練技巧與可視化
訓練循環關鍵代碼:
for epoch in range(epochs):
for i, (real_imgs, _) in enumerate(dataloader):
# 訓練判別器
optimizer_D.zero_grad()
# 真實圖像損失
real_loss = criterion(D(real_imgs), real_labels)
# 生成假圖像
z = torch.randn(batch_size, latent_dim, 1, 1)
fake_imgs = G(z)
fake_loss = criterion(D(fake_imgs.detach()), fake_labels)
d_loss = real_loss + fake_loss
d_loss.backward()
optimizer_D.step()
# 訓練生成器
optimizer_G.zero_grad()
g_loss = criterion(D(fake_imgs), real_labels) ?# 騙過判別器
g_loss.backward()
optimizer_G.step()
模式坍塌診斷與解決:
- 現象:生成器只產生少量模式樣本
- 解決方案:
- 使用Wasserstein GAN (WGAN)
- 添加多樣性懲罰項
- 嘗試小批量判別(Minibatch Discrimination)
生成過程可視化:
# 固定潛在向量觀察生成演變
fixed_z = torch.randn(64, latent_dim, 1, 1)
sample_imgs = G(fixed_z).detach()
grid = torchvision.utils.make_grid(sample_imgs, nrow=8)
plt.imshow(grid.permute(1, 2, 0))
plt.show()
三、下午探索:擴散模型原理與實踐
3.1 擴散過程數學描述
前向擴散(加噪):
q(x_t|x_{t-1}) = N(x_t; √(1-β_t)x_{t-1}, β_tI)
其中β_t是噪聲調度
反向去噪(生成):
p_θ(x_{t-1}|x_t) = N(x_{t-1}; μ_θ(x_t,t), Σ_θ(x_t,t))
DDPM簡化訓練目標:
def diffusion_loss(model, x0, t):
# 隨機時間步
t = torch.randint(0, T, (x0.size(0),)
# 計算加噪后的樣本
sqrt_alpha_bar = extract(sqrt_alpha_bar_t, t, x0.shape)
sqrt_one_minus_alpha_bar = extract(sqrt_one_minus_alpha_bar_t, t, x0.shape)
noise = torch.randn_like(x0)
xt = sqrt_alpha_bar * x0 + sqrt_one_minus_alpha_bar * noise
# 預測噪聲
predicted_noise = model(xt, t)
# 計算損失
return F.mse_loss(predicted_noise, noise)
3.2 擴散模型實踐
使用Diffusers庫生成圖像:
from diffusers import DDPMPipeline, DDPMScheduler
# 加載預訓練模型
pipe = DDPMPipeline.from_pretrained("google/ddpm-cifar10-32")
# 生成圖像
image = pipe().images[0]
image.save("generated_image.png")
自定義采樣過程:
def sample_ddpm(model, shape, steps=50):
x = torch.randn(shape)
for t in reversed(range(steps)):
t_tensor = torch.full((shape[0],), t, dtype=torch.long)
with torch.no_grad():
pred_noise = model(x, t_tensor)
alpha_t = alpha[t]
alpha_bar_t = alpha_bar[t]
beta_t = beta[t]
if t > 0:
noise = torch.randn_like(x)
else:
noise = 0
x = (x - (1-alpha_t)/torch.sqrt(1-alpha_bar_t)*pred_noise)/torch.sqrt(alpha_t)
x += torch.sqrt(beta_t) * noise
return x
四、生成模型應用全景
4.1 圖像超分辨率實現
# 使用ESRGAN (Enhanced Super-Resolution GAN)
from basicsr.archs.rrdbnet_arch import RRDBNet
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23)
4.2 藝術風格遷移
# 基于擴散模型的風格遷移
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
image = pipe("Van Gogh style landscape").images[0]
4.3 醫學圖像合成
# 使用條件GAN生成CT掃描圖像
class MedGAN(nn.Module):
def __init__(self):
super().__init__()
self.encoder = ... ?# 編碼臨床參數
self.generator = ... ?# 生成圖像
self.discriminator = ... ?# 判別真實/生成
五、學習總結與明日計劃
5.1 今日核心成果
? 實現DCGAN并生成MNIST/Fashion-MNIST圖像 ?
? 理解擴散模型的前向/反向過程 ?
? 使用Diffusers庫完成圖像生成 ?
? 探索生成模型在超分辨率等場景的應用 ?
5.2 關鍵問題記錄
? GAN訓練不穩定的根本原因 ?
? 擴散模型采樣加速方法 ?
? 生成結果的評估指標選擇 ?
5.3 明日學習重點
- 圖神經網絡(GNN)基礎概念
- 圖卷積網絡(GCN)實現
- 節點分類與圖分類任務
- 圖注意力網絡(GAT)初探
六、資源推薦與延伸閱讀
1. GAN Zoo:各類GAN變體集合 ?
2. Diffusion Models Beat GANs:擴散模型里程碑論文 ?
3. Stable Diffusion WebUI:最強開源圖像生成工具 ?
4. 生成模型可視化:交互式理解GAN訓練 ?
七、實踐心得與倫理思考
1. 生成模型調試技巧:
- GAN:監控D_loss和G_loss的平衡
- 擴散:可視化中間去噪過程
- 通用:使用固定隨機種子復現問題
2. 倫理邊界警示:
# 人臉生成倫理檢查
if task == "face_generation":
assert has_ethical_approval, "需要倫理審查"
add_watermark(output_image)
3. 實用代碼片段:
# 潛在空間插值
z1 = torch.randn(1, latent_dim)
z2 = torch.randn(1, latent_dim)
for alpha in torch.linspace(0, 1, 10):
z = alpha*z1 + (1-alpha)*z2
generated = G(z)
下篇預告:《Day9:圖神經網絡入門—非歐空間的數據智慧》 ?
將探索社交網絡、分子結構等圖數據的深度學習處理方法!