-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathwgan_train.py
95 lines (75 loc) · 2.75 KB
/
wgan_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import torch
import torch.nn as nn
import torchvision.datasets as dataset
import torch.optim as optim
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
from dcgan import Discriminator, Generator, weights_init
from preprocessing import Dataset
n_critic = 5
clip_value = 0.01
lr = 1e-4
epoch_num = 64
batch_size = 8
nz = 100 # length of noise
ngpu = 0
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def main():
# load training data
trainset = Dataset('./data/brilliant_blue')
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=batch_size, shuffle=True
)
# init netD and netG
netD = Discriminator().to(device)
netD.apply(weights_init)
netG = Generator(nz).to(device)
netG.apply(weights_init)
# used for visualizing training process
fixed_noise = torch.randn(16, nz, 1, device=device)
# optimizers
optimizerD = optim.RMSprop(netD.parameters(), lr=lr)
optimizerG = optim.RMSprop(netG.parameters(), lr=lr)
for epoch in range(epoch_num):
for step, (data, _) in enumerate(trainloader):
# training netD
real_cpu = data.to(device)
b_size = real_cpu.size(0)
netD.zero_grad()
noise = torch.randn(b_size, nz, 1, device=device)
fake = netG(noise)
loss_D = -torch.mean(netD(real_cpu)) + torch.mean(netD(fake))
loss_D.backward()
optimizerD.step()
for p in netD.parameters():
p.data.clamp_(-clip_value, clip_value)
if step % n_critic == 0:
# training netG
noise = torch.randn(b_size, nz, 1, device=device)
netG.zero_grad()
fake = netG(noise)
loss_G = -torch.mean(netD(fake))
netD.zero_grad()
netG.zero_grad()
loss_G.backward()
optimizerG.step()
if step % 5 == 0:
print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f'
% (epoch, epoch_num, step, len(trainloader), loss_D.item(), loss_G.item()))
# save training process
with torch.no_grad():
fake = netG(fixed_noise).detach().cpu()
f, a = plt.subplots(4, 4, figsize=(8, 8))
for i in range(4):
for j in range(4):
a[i][j].plot(fake[i * 4 + j].view(-1))
a[i][j].set_xticks(())
a[i][j].set_yticks(())
plt.savefig('./img/wgan_epoch_%d.png' % epoch)
plt.close()
# save model
torch.save(netG, './nets/wgan_netG.pkl')
torch.save(netD, './nets/wgan_netD.pkl')
if __name__ == '__main__':
main()