代碼摘要:
1.讀取數據
2.搭建鑑別網絡和生成網絡
3.初始化網絡的權重值
4.訓練鑑別網絡和生成網絡(時間大概需要幾個小時)
root路徑可以自己設置
"分批讀取CIFAR-10圖片並將部分批次保存爲圖片文件"
from torch.utils.data import DataLoader
from torchvision.datasets import CIFAR10
import torchvision.transforms as transforms
from torchvision.utils import save_image
dataset = CIFAR10(root='./data', download=True,
transform=transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64, shuffle=True)#喂入大小是把原來數據集中的多少圖片組合成一張圖片
batch_size=64
for batch_idx, data in enumerate(dataloader):
if batch_idx==len(dataloader)-1:
continue
real_images, _ = data
print ('#{} has {} images.'.format(batch_idx, batch_size))
if batch_idx % 100 == 0:
path = './data/CIFAR10_shuffled_batch{:03d}.png'.format(batch_idx)
save_image(real_images, path, normalize=True)
運行結果部分展示:
"搭建生成網絡和鑑別網絡"
"隱藏的卷積層(即除了最後的輸出卷積層外)的輸出都需要經過規範化操作"
import torch.nn as nn
# 搭建生成網絡
latent_size = 64 # 潛在大小
n_channel = 3 # 輸出通道數
n_g_feature = 64 # 生成網絡隱藏層大小
"生成網絡採用了四層轉置卷積操作"
gnet = nn.Sequential(
# 輸入大小 = (64, 1, 1)
#有點像互相關的反操作,(x-4)/1=1-->x=4
nn.ConvTranspose2d(latent_size, 4 * n_g_feature, kernel_size=4,
bias=False),
nn.BatchNorm2d(4 * n_g_feature),
nn.ReLU(),
# 大小 = (256, 4, 4)
#{x+2(填充)-4(核尺寸)+2(步長)}/2=4-->x=8
nn.ConvTranspose2d(4 * n_g_feature, 2 * n_g_feature, kernel_size=4,
stride=2, padding=1, bias=False),
nn.BatchNorm2d(2 * n_g_feature),
nn.ReLU(),
# 大小 = (128, 8, 8)
nn.ConvTranspose2d(2 * n_g_feature, n_g_feature, kernel_size=4,
stride=2, padding=1, bias=False),
nn.BatchNorm2d(n_g_feature),
nn.ReLU(),
# 大小 = (64, 16, 16)
nn.ConvTranspose2d(n_g_feature, n_channel, kernel_size=4,
stride=2, padding=1),
nn.Sigmoid(),
# 圖片大小 = (3, 32, 32)
)
print (gnet)
# 搭建鑑別網絡
n_d_feature = 64 # 鑑別網絡隱藏層大小
"鑑別網絡採用了4層互相關操作"
dnet = nn.Sequential(
# 圖片大小 = (3, 32, 32)
nn.Conv2d(n_channel, n_d_feature, kernel_size=4,
stride=2, padding=1),
nn.LeakyReLU(0.2),
# 大小 = (64, 16, 16)
nn.Conv2d(n_d_feature, 2 * n_d_feature, kernel_size=4,
stride=2, padding=1, bias=False),
nn.BatchNorm2d(2 * n_d_feature),
nn.LeakyReLU(0.2),
# 大小 = (128, 8, 8)
nn.Conv2d(2 * n_d_feature, 4 * n_d_feature, kernel_size=4,
stride=2, padding=1, bias=False),
nn.BatchNorm2d(4 * n_d_feature),
nn.LeakyReLU(0.2),
# 大小 = (256, 4, 4)
nn.Conv2d(4 * n_d_feature, 1, kernel_size=4),
# 對數賠率張量大小 = (1, 1, 1)
)
print(dnet)
代碼運行的部分結果:
"初始化權重值"
import torch.nn.init as init
def weights_init(m): # 用於初始化權重值的函數
if type(m) in [nn.ConvTranspose2d, nn.Conv2d]:
init.xavier_normal_(m.weight)
elif type(m) == nn.BatchNorm2d:
init.normal_(m.weight, 1.0, 0.02)
init.constant_(m.bias, 0)
#調用apply()函數,torch.nn.Module類實例會遞歸地讓自己成爲weights_init()裏面函數的m
gnet.apply(weights_init)
dnet.apply(weights_init)
"訓練生成網絡和鑑別網絡並輸出圖片"
import torch
import torch.optim
# 損失
criterion = nn.BCEWithLogitsLoss()
# 優化器
#Adam優化器的默認學習率n=0.01,過高,應減小爲0.002,動量參數默認0.9,會造成震盪,減小爲0.5
goptimizer = torch.optim.Adam(gnet.parameters(),
lr=0.0002, betas=(0.5, 0.999))
doptimizer = torch.optim.Adam(dnet.parameters(),
lr=0.0002, betas=(0.5, 0.999))
# 用於測試的固定噪聲,用來查看相同的潛在張量在訓練過程中生成圖片的變換
batch_size = 64
fixed_noises = torch.randn(batch_size, latent_size, 1, 1)
# 訓練過程
epoch_num = 10
for epoch in range(epoch_num):
for batch_idx, data in enumerate(dataloader):
if batch_idx==len(dataloader)-1: #剔除最後一張是(16,3,32,32)
continue
# 載入本批次數據
real_images, _ = data#real_images(64,3,32,32)
# 訓練鑑別網絡
labels = torch.ones(batch_size) # 真實數據對應標籤爲1(64,)
preds = dnet(real_images) # 對真實數據進行判別(64,1,1,1)
outputs = preds.reshape(-1)#(64,)
dloss_real = criterion(outputs, labels) # 真實數據的鑑別器損失
dmean_real = outputs.sigmoid().mean() # 計算鑑別器將多少比例的真數據判定爲真,僅用於輸出顯示
noises = torch.randn(batch_size, latent_size, 1, 1) # 潛在噪聲(64,64,1,1)
fake_images = gnet(noises) # 生成假數據(64,3,32,32)
labels = torch.zeros(batch_size) # 假數據對應標籤爲0
fake = fake_images.detach()# 使得梯度的計算不回溯到生成網絡,可用於加快訓練速度.刪去此步結果不變
preds = dnet(fake) # 對假數據進行鑑別
outputs = preds.view(-1)
dloss_fake = criterion(outputs, labels) # 假數據的鑑別器損失
dmean_fake = outputs.sigmoid().mean()
# 計算鑑別器將多少比例的假數據判定爲真,僅用於輸出顯示
dloss = dloss_real + dloss_fake # 總的鑑別器損失
dnet.zero_grad()
dloss.backward()
doptimizer.step()
# 訓練生成網絡
labels = torch.ones(batch_size)
# 生成網絡希望所有生成的數據都被認爲是真數據
preds = dnet(fake_images) # 把假數據通過鑑別網絡
outputs = preds.view(-1)
gloss = criterion(outputs, labels) # 真數據看到的損失
gmean_fake = outputs.sigmoid().mean()
# 計算鑑別器將多少比例的假數據判定爲真,僅用於輸出顯示
gnet.zero_grad()
gloss.backward()
goptimizer.step()
# 輸出本步訓練結果
if batch_idx % 100 == 0:
print('[{}/{}]'.format(epoch, epoch_num) +
'[{}/{}]'.format(batch_idx, len(dataloader)) +
'鑑別網絡損失:{:g} 生成網絡損失:{:g}'.format(dloss, gloss) +
'真數據判真比例:{:g} 假數據判真比例:{:g}/{:g}'.format(
dmean_real, dmean_fake, gmean_fake))
fake = gnet(fixed_noises) # 由固定潛在張量生成假數據
save_image(fake, # 保存假數據
'./data/images_epoch{:02d}_batch{:03d}.png'.format(
epoch, batch_idx))
代碼運行結果:人工停止的結果
生成的假圖像:
依次是第0論的0,300,600,第1輪的100