-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathgan.py
206 lines (166 loc) · 5.96 KB
/
gan.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import argparse
import os
import random
from pathlib import Path
from multiprocessing import cpu_count
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
RANDOM_STATE = 1
random.seed(RANDOM_STATE)
torch.manual_seed(RANDOM_STATE)
data_root = Path.home()/'data'/'celeba'
workers = cpu_count()
def main():
batch_size = 128
image_size = 64
nz = 100
nc = 3
n_gen_feat = 64
n_dis_feat = 64
epochs = 5
lr = 1e-5
beta1 = 0.5
device = torch.device('cuda:0')
dataset = dset.ImageFolder(
root=str(data_root),
transform=transforms.Compose([
transforms.Resize(image_size),
transforms.CenterCrop(image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
]))
data_loader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=True, num_workers=workers)
# real_batch = next(iter(data_loader))
# plt.figure(figsize=(8, 8))
# plt.axis('off')
# plt.imshow(
# np.transpose(
# vutils.make_grid(
# real_batch[0].to(device)[:64],
# padding=2, normalize=True).cpu(),
# (1, 2, 0)))
# plt.pause(0.0001)
crit = nn.BCELoss()
fixed_noise = torch.randn(64, nz, 1, 1, device=device)
real_label = 1
fake_label = 0
net_g = Generator(nz, n_gen_feat, nc).to(device)
net_d = Discriminator(n_dis_feat, nc).to(device)
opt_g = optim.Adam(net_g.parameters(), lr=lr, betas=(beta1, 0.999))
opt_d = optim.Adam(net_d.parameters(), lr=lr, betas=(beta1, 0.999))
g_losses, d_losses = [], []
img_list = []
iters = 0
print('Starting training loop...')
for epoch in range(epochs):
for i, data in enumerate(data_loader, 0):
net_d.zero_grad()
real = data[0].to(device)
b_size = real.size(0)
label = torch.full((b_size,), real_label, device=device)
output = net_d(real).view(-1)
err_d_real = crit(output, label)
err_d_real.backward()
d_x = output.mean().item()
noise = torch.randn(b_size, nz, 1, 1, device=device)
fake = net_g(noise)
label.fill_(fake_label)
output = net_d(fake.detach()).view(-1)
err_d_fake = crit(output, label)
err_d_fake.backward()
d_g_z1 = output.mean().item()
err_d = err_d_real + err_d_fake
opt_d.step()
net_g.zero_grad()
label.fill_(real_label)
output = net_d(fake).view(-1)
err_g = crit(output, label)
err_g.backward()
d_g_z2 = output.mean().item()
opt_g.step()
if i % 50 == 0:
print('[%d/%d][%d/%d]\t'
'Loss_D: %.4f\t'
'Loss_G: %.4f\t'
'D(x): %.4f\t'
'D(G(z)): %.4f / %.4f'
% (epoch, epochs, i, len(data_loader),
err_d.item(), err_g.item(), d_x, d_g_z1, d_g_z2))
g_losses.append(err_g.item())
d_losses.append(err_d.item())
if (iters % 500 == 0) or (
(epoch == epochs - 1) and (i == len(data_loader) - 1)):
with torch.no_grad():
fake = net_g(fixed_noise).detach().cpu()
img_list.append(vutils.make_grid(
fake, padding=2, normalize=True))
iters += 1
class GenBlock(nn.Module):
def __init__(self, ni, no, kernel, stride, pad, bias=False):
super().__init__()
self.conv = nn.ConvTranspose2d(ni, no, kernel, stride, pad, bias=bias)
self.bn = nn.BatchNorm2d(no)
self.relu = nn.ReLU(True)
def forward(self, x):
return self.relu(self.bn(self.conv(x)))
class Generator(nn.Module):
def __init__(self, nz, nf, nc):
super().__init__()
self.main = nn.Sequential(
GenBlock(nz, nf * 8, 4, 1, 0),
GenBlock(nf * 8, nf * 4, 4, 2, 1),
GenBlock(nf * 4, nf * 2, 4, 2, 1),
GenBlock(nf * 2, nf, 4, 2, 1),
nn.ConvTranspose2d(nf, nc, 4, 2, 1, bias=False),
nn.Tanh()
)
self.apply(init_weights)
def forward(self, x):
return self.main(x)
class ConvBlock(nn.Module):
def __init__(self, ni, no, kernel, stride, pad, alpha=0.2, bias=False):
super().__init__()
self.conv = nn.Conv2d(ni, no, kernel, stride, pad, bias=bias)
self.bn = nn.BatchNorm2d(no)
self.leaky_relu = nn.LeakyReLU(alpha, True)
def forward(self, x):
return self.leaky_relu(self.bn(self.conv(x)))
class Discriminator(nn.Module):
def __init__(self, nf, nc):
super().__init__()
self.main = nn.Sequential(
nn.Conv2d(nc, nf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
ConvBlock(nf, nf * 2, 4, 2, 1),
ConvBlock(nf * 2, nf * 4, 4, 2, 1),
ConvBlock(nf * 4, nf * 8, 4, 2, 1),
nn.Conv2d(nf * 8, 1, 4, 1, 0),
nn.Sigmoid()
)
self.apply(init_weights)
def forward(self, x):
return self.main(x)
def init_weights(m):
class_name = m.__class__.__name__
if class_name in ('ConvBlock', 'GenBlock'):
for child in m.children():
init_weights(child)
elif class_name.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif class_name.find('BatchNorm') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
if __name__ == '__main__':
main()