-
Notifications
You must be signed in to change notification settings - Fork 0
/
cGAN.py
243 lines (198 loc) · 8.32 KB
/
cGAN.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from torch import optim
import os
import torchvision.utils as vutils
import numpy as np
from torchvision import datasets
from torchvision import transforms
import torch
import torch.nn as nn
import torch.nn.functional as F
# Arguments
BATCH_SIZE = 256
Z_DIM = 32
LABEL_EMBED_SIZE = 5
NUM_CLASSES = 10
IMGS_TO_DISPLAY_PER_CLASS = 20
LOAD_MODEL = False
DB = 'CIFAR10' # SVHN | MNIST | FashionMNIST | USPS
if DB == 'MNIST' or DB == 'FashionMNIST':
CHANNELS = 1
EPOCHS = 50
elif DB == 'USPS':
CHANNELS = 1
EPOCHS = 100
elif DB == 'SVHN':
CHANNELS = 3
EPOCHS = 100
elif DB == 'CIFAR10':
CHANNELS = 3
EPOCHS = 1000
else:
print("Incorrect dataset")
exit(0)
# Directories for storing data, model and output samples
db_path = os.path.join('./data', DB)
os.makedirs(db_path, exist_ok=True)
model_path = os.path.join('./model', DB)
os.makedirs(model_path, exist_ok=True)
samples_path = os.path.join('./samples', DB)
os.makedirs(samples_path, exist_ok=True)
# Data loader
transform = transforms.Compose([transforms.Resize([32, 32]),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])])
if DB == 'MNIST':
dataset = datasets.MNIST(db_path, train=True, download=True, transform=transform)
elif DB == 'FashionMNIST':
dataset = datasets.FashionMNIST(db_path, train=True, download=True, transform=transform)
elif DB == 'USPS':
dataset = datasets.USPS(db_path, train=True, download=True, transform=transform)
elif DB == 'SVHN':
dataset = datasets.SVHN(db_path, split='train', download=True, transform=transform)
elif DB == 'CIFAR10':
dataset = datasets.CIFAR10(db_path, train=True, download=True, transform=transform)
else:
print("Incorrect DB")
exit(0)
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=BATCH_SIZE,
shuffle=True,
num_workers=4,
drop_last=True)
# Method for storing generated images
def generate_imgs(z, fixed_label, epoch=0):
gen.eval()
fake_imgs = gen(z, fixed_label)
fake_imgs = (fake_imgs + 1) / 2
fake_imgs_ = vutils.make_grid(fake_imgs, normalize=False, nrow=IMGS_TO_DISPLAY_PER_CLASS)
vutils.save_image(fake_imgs_, os.path.join(samples_path, 'sample_' + str(epoch) + '.png'))
# Networks
def conv_block(c_in, c_out, k_size=4, stride=2, pad=1, use_bn=True, transpose=False):
module = []
if transpose:
module.append(nn.ConvTranspose2d(c_in, c_out, k_size, stride, pad, bias=not use_bn))
else:
module.append(nn.Conv2d(c_in, c_out, k_size, stride, pad, bias=not use_bn))
if use_bn:
module.append(nn.BatchNorm2d(c_out))
return nn.Sequential(*module)
class Generator(nn.Module):
def __init__(self, z_dim=10, num_classes=10, label_embed_size=5, channels=3, conv_dim=64):
super(Generator, self).__init__()
self.label_embedding = nn.Embedding(num_classes, label_embed_size)
self.tconv1 = conv_block(z_dim + label_embed_size, conv_dim * 4, pad=0, transpose=True)
self.tconv2 = conv_block(conv_dim * 4, conv_dim * 2, transpose=True)
self.tconv3 = conv_block(conv_dim * 2, conv_dim, transpose=True)
self.tconv4 = conv_block(conv_dim, channels, transpose=True, use_bn=False)
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
nn.init.normal_(m.weight, 0.0, 0.02)
if isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x, label):
x = x.reshape([x.shape[0], -1, 1, 1])
label_embed = self.label_embedding(label)
label_embed = label_embed.reshape([label_embed.shape[0], -1, 1, 1])
x = torch.cat((x, label_embed), dim=1)
x = F.relu(self.tconv1(x))
x = F.relu(self.tconv2(x))
x = F.relu(self.tconv3(x))
x = torch.tanh(self.tconv4(x))
return x
class Discriminator(nn.Module):
def __init__(self, num_classes=10, channels=3, conv_dim=64):
super(Discriminator, self).__init__()
self.image_size = 32
self.label_embedding = nn.Embedding(num_classes, self.image_size*self.image_size)
self.conv1 = conv_block(channels + 1, conv_dim, use_bn=False)
self.conv2 = conv_block(conv_dim, conv_dim * 2)
self.conv3 = conv_block(conv_dim * 2, conv_dim * 4)
self.conv4 = conv_block(conv_dim * 4, 1, k_size=4, stride=1, pad=0, use_bn=False)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal_(m.weight, 0.0, 0.02)
if isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x, label):
alpha = 0.2
label_embed = self.label_embedding(label)
label_embed = label_embed.reshape([label_embed.shape[0], 1, self.image_size, self.image_size])
x = torch.cat((x, label_embed), dim=1)
x = F.leaky_relu(self.conv1(x), alpha)
x = F.leaky_relu(self.conv2(x), alpha)
x = F.leaky_relu(self.conv3(x), alpha)
x = torch.sigmoid(self.conv4(x))
return x.squeeze()
gen = Generator(z_dim=Z_DIM, num_classes=NUM_CLASSES, label_embed_size=LABEL_EMBED_SIZE, channels=CHANNELS)
dis = Discriminator(num_classes=NUM_CLASSES, channels=CHANNELS)
# Load previous model
if LOAD_MODEL:
gen.load_state_dict(torch.load(os.path.join(model_path, 'gen.pkl')))
dis.load_state_dict(torch.load(os.path.join(model_path, 'dis.pkl')))
# Model Summary
print("------------------Generator------------------")
print(gen)
print("------------------Discriminator------------------")
print(dis)
# Define Optimizers
g_opt = optim.Adam(gen.parameters(), lr=0.0002, betas=(0.5, 0.999), weight_decay=2e-5)
d_opt = optim.Adam(dis.parameters(), lr=0.0002, betas=(0.5, 0.999), weight_decay=2e-5)
# Loss functions
loss_fn = nn.BCELoss()
# Fix images for viz
fixed_z = torch.randn(IMGS_TO_DISPLAY_PER_CLASS*NUM_CLASSES, Z_DIM)
fixed_label = torch.arange(0, NUM_CLASSES)
fixed_label = torch.repeat_interleave(fixed_label, IMGS_TO_DISPLAY_PER_CLASS)
# Labels
real_label = torch.ones(BATCH_SIZE)
fake_label = torch.zeros(BATCH_SIZE)
# GPU Compatibility
is_cuda = torch.cuda.is_available()
if is_cuda:
gen, dis = gen.cuda(), dis.cuda()
real_label, fake_label = real_label.cuda(), fake_label.cuda()
fixed_z, fixed_label = fixed_z.cuda(), fixed_label.cuda()
total_iters = 0
max_iter = len(data_loader)
# Training
for epoch in range(EPOCHS):
gen.train()
dis.train()
for i, data in enumerate(data_loader):
total_iters += 1
# Loading data
x_real, x_label = data
z_fake = torch.randn(BATCH_SIZE, Z_DIM)
if is_cuda:
x_real = x_real.cuda()
x_label = x_label.cuda()
z_fake = z_fake.cuda()
# Generate fake data
x_fake = gen(z_fake, x_label)
# Train Discriminator
fake_out = dis(x_fake.detach(), x_label)
real_out = dis(x_real.detach(), x_label)
d_loss = (loss_fn(fake_out, fake_label) + loss_fn(real_out, real_label)) / 2
d_opt.zero_grad()
d_loss.backward()
d_opt.step()
# Train Generator
fake_out = dis(x_fake, x_label)
g_loss = loss_fn(fake_out, real_label)
g_opt.zero_grad()
g_loss.backward()
g_opt.step()
if i % 50 == 0:
print("Epoch: " + str(epoch + 1) + "/" + str(EPOCHS)
+ "\titer: " + str(i) + "/" + str(max_iter)
+ "\ttotal_iters: " + str(total_iters)
+ "\td_loss:" + str(round(d_loss.item(), 4))
+ "\tg_loss:" + str(round(g_loss.item(), 4))
)
if (epoch + 1) % 5 == 0:
torch.save(gen.state_dict(), os.path.join(model_path, 'gen.pkl'))
torch.save(dis.state_dict(), os.path.join(model_path, 'dis.pkl'))
generate_imgs(fixed_z, fixed_label, epoch=epoch + 1)
generate_imgs(fixed_z, fixed_label)