-
Notifications
You must be signed in to change notification settings - Fork 39
/
dcgan.py
203 lines (172 loc) · 7.47 KB
/
dcgan.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
import os
import torch
import torch.optim as optim
from absl import flags, app
from torchvision import datasets, transforms
from torchvision.utils import make_grid, save_image
from tensorboardX import SummaryWriter
from tqdm import trange
from pytorch_gan_metrics import get_inception_score_and_fid
import source.models.dcgan as models
import source.losses as losses
from source.utils import generate_imgs, infiniteloop, set_seed
net_G_models = {
'cnn32': models.Generator32,
}
net_D_models = {
'cnn32': models.Discriminator32,
}
loss_fns = {
'bce': losses.BCEWithLogits,
'hinge': losses.Hinge,
'was': losses.Wasserstein,
'softplus': losses.Softplus
}
FLAGS = flags.FLAGS
# model and training
flags.DEFINE_enum('dataset', 'cifar10', ['cifar10', 'stl10'], "dataset")
flags.DEFINE_enum('arch', 'cnn32', net_G_models.keys(), "architecture")
flags.DEFINE_integer('total_steps', 50000, "total number of training steps")
flags.DEFINE_integer('batch_size', 128, "batch size")
flags.DEFINE_float('lr_G', 2e-4, "Generator learning rate")
flags.DEFINE_float('lr_D', 2e-4, "Discriminator learning rate")
flags.DEFINE_multi_float('betas', [0.5, 0.9], "for Adam")
flags.DEFINE_integer('n_dis', 1, "update Generator every this steps")
flags.DEFINE_integer('z_dim', 100, "latent space dimension")
flags.DEFINE_enum('loss', 'bce', loss_fns.keys(), "loss function")
flags.DEFINE_integer('seed', 0, "random seed")
# logging
flags.DEFINE_integer('eval_step', 5000, "evaluate FID and Inception Score")
flags.DEFINE_integer('sample_step', 500, "sample image every this steps")
flags.DEFINE_integer('sample_size', 64, "sampling size of images")
flags.DEFINE_string('logdir', './logs/DCGAN_CIFAR10', 'logging folder')
flags.DEFINE_bool('record', True, "record inception score and FID")
flags.DEFINE_string('fid_cache', './stats/cifar10.train.npz', 'FID cache')
# generate
flags.DEFINE_bool('generate', False, 'generate images')
flags.DEFINE_string('pretrain', None, 'path to test model')
flags.DEFINE_string('output', './outputs', 'path to output dir')
flags.DEFINE_integer('num_images', 50000, 'the number of generated images')
device = torch.device('cuda:0')
def generate():
assert FLAGS.pretrain is not None, "set model weight by --pretrain [model]"
net_G = net_G_models[FLAGS.arch](FLAGS.z_dim).to(device)
net_G.load_state_dict(torch.load(FLAGS.pretrain)['net_G'])
net_G.eval()
counter = 0
os.makedirs(FLAGS.output)
with torch.no_grad():
for start in trange(
0, FLAGS.num_images, FLAGS.batch_size, dynamic_ncols=True):
batch_size = min(FLAGS.batch_size, FLAGS.num_images - start)
z = torch.randn(batch_size, FLAGS.z_dim).to(device)
x = net_G(z).cpu()
x = (x + 1) / 2
for image in x:
save_image(
image, os.path.join(FLAGS.output, '%d.png' % counter))
counter += 1
def train():
if FLAGS.dataset == 'cifar10':
dataset = datasets.CIFAR10(
'./data', train=True, download=True,
transform=transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
if FLAGS.dataset == 'stl10':
dataset = datasets.STL10(
'./data', split='unlabeled', download=True,
transform=transforms.Compose([
transforms.Resize((48, 48)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
dataloader = torch.utils.data.DataLoader(
dataset, batch_size=FLAGS.batch_size, shuffle=True, num_workers=4,
drop_last=True)
net_G = net_G_models[FLAGS.arch](FLAGS.z_dim).to(device)
net_D = net_D_models[FLAGS.arch]().to(device)
loss_fn = loss_fns[FLAGS.loss]()
optim_G = optim.Adam(net_G.parameters(), lr=FLAGS.lr_G, betas=FLAGS.betas)
optim_D = optim.Adam(net_D.parameters(), lr=FLAGS.lr_D, betas=FLAGS.betas)
sched_G = optim.lr_scheduler.LambdaLR(
optim_G, lambda step: 1 - step / FLAGS.total_steps)
sched_D = optim.lr_scheduler.LambdaLR(
optim_D, lambda step: 1 - step / FLAGS.total_steps)
os.makedirs(os.path.join(FLAGS.logdir, 'sample'))
writer = SummaryWriter(os.path.join(FLAGS.logdir))
sample_z = torch.randn(FLAGS.sample_size, FLAGS.z_dim).to(device)
with open(os.path.join(FLAGS.logdir, "flagfile.txt"), 'w') as f:
f.write(FLAGS.flags_into_string())
writer.add_text(
"flagfile", FLAGS.flags_into_string().replace('\n', ' \n'))
real, _ = next(iter(dataloader))
grid = (make_grid(real[:FLAGS.sample_size]) + 1) / 2
writer.add_image('real_sample', grid)
looper = infiniteloop(dataloader)
with trange(1, FLAGS.total_steps + 1, desc='Training', ncols=0) as pbar:
for step in pbar:
# Discriminator
for _ in range(FLAGS.n_dis):
with torch.no_grad():
z = torch.randn(FLAGS.batch_size, FLAGS.z_dim).to(device)
fake = net_G(z).detach()
real = next(looper).to(device)
net_D_real = net_D(real)
net_D_fake = net_D(fake)
loss = loss_fn(net_D_real, net_D_fake)
optim_D.zero_grad()
loss.backward()
optim_D.step()
if FLAGS.loss == 'was':
loss = -loss
pbar.set_postfix(loss='%.4f' % loss)
writer.add_scalar('loss', loss, step)
# Generator
z = torch.randn(FLAGS.batch_size * 2, FLAGS.z_dim).to(device)
loss = loss_fn(net_D(net_G(z)))
optim_G.zero_grad()
loss.backward()
optim_G.step()
sched_G.step()
sched_D.step()
if step == 1 or step % FLAGS.sample_step == 0:
fake = net_G(sample_z).cpu()
grid = (make_grid(fake) + 1) / 2
writer.add_image('sample', grid, step)
save_image(grid, os.path.join(
FLAGS.logdir, 'sample', '%d.png' % step))
if step == 1 or step % FLAGS.eval_step == 0:
torch.save({
'net_G': net_G.state_dict(),
'net_D': net_D.state_dict(),
'optim_G': optim_G.state_dict(),
'optim_D': optim_D.state_dict(),
'sched_G': sched_G.state_dict(),
'sched_D': sched_D.state_dict(),
}, os.path.join(FLAGS.logdir, 'model.pt'))
if FLAGS.record:
imgs = generate_imgs(
net_G, device, FLAGS.z_dim,
FLAGS.num_images, FLAGS.batch_size)
IS, FID = get_inception_score_and_fid(
imgs, FLAGS.fid_cache, verbose=True)
pbar.write(
"%s/%s Inception Score: %.3f(%.5f), "
"FID: %6.3f" % (
step, FLAGS.total_steps, IS[0], IS[1], FID))
writer.add_scalar('Inception_Score', IS[0], step)
writer.add_scalar('Inception_Score_std', IS[1], step)
writer.add_scalar('FID', FID, step)
writer.close()
def main(argv):
set_seed(FLAGS.seed)
if FLAGS.generate:
generate()
else:
train()
if __name__ == '__main__':
app.run(main)