-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfoGAN.py
166 lines (135 loc) · 5.07 KB
/
infoGAN.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
import os
import time
from tqdm import tqdm
import torch
import numpy as np
import random
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
from models.dcgan import Discriminator, Generator, QHead, DHead
from info_utils import NoiseGenerator, InfoGANLoss
from opt import get_options, choose_dataset
from utils.misc import json_dump, save_opt, TensorImageUtils
from utils.eval import FIDEvaluator
opt = get_options()
save_path = opt.save_path
os.makedirs(save_path, exist_ok=True)
save_opt(save_path, opt)
# ------------------ configuratoin -------------------------
# fix seed
seed = opt.seed
torch.random.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
# infoGAN related configuration
num_clsses = opt.ndlist
num_categorical_variables = len(num_clsses)
num_continuous_variables = opt.ncz
# training related configuration
use_cuda = opt.cuda
device = torch.device("cuda" if use_cuda and torch.cuda.is_available() else "cpu")
lr_D = opt.lrD
lr_G = opt.lrG
epochs = opt.epochs
save_epoch_interval = opt.save_epoch_interval
train_D_iter = opt.train_D_iter
# persistence related parameters
writer = SummaryWriter(save_path)
utiler = TensorImageUtils(save_path)
nrow = opt.nrow
# dataset
data = choose_dataset(opt)
in_channels = opt.in_channels
# models
dim_z = opt.dim_z
netD = Discriminator(in_channels, dim_z)
netG = Generator(in_channels, dim_z)
headD = DHead(512)
headQ = QHead(512, num_clsses, num_continuous_variables)
if use_cuda:
netD.cuda()
netG.cuda()
headD.cuda()
headQ.cuda()
# training config
optimizer_D = optim.Adam([
{"params": netD.parameters()},
{"params": headD.parameters()}
], lr=lr_D, betas=opt.adam_betas)
optimizer_G = optim.Adam([
{"params": netG.parameters()},
{"params": headQ.parameters()}
], lr=lr_G, betas=opt.adam_betas)
beta = 1
infoGAN_criterion = InfoGANLoss(beta)
noiseG = NoiseGenerator(dim_z, num_clsses, num_continuous_variables, device=device)
# evaluator
tmp_eval_path = os.path.join(save_path, "eval")
evaluator = FIDEvaluator(tmp_path=tmp_eval_path)
eval_res = []
#------------------ Training -------------------------
tensor_true = torch.tensor([1], dtype=torch.float, device=device)
tensor_fake = torch.tensor([0], dtype=torch.float, device=device)
Dstep = 1
Gstep = 1
print("Start Training, using {}".format(device))
starttime = time.process_time()
for epoch in range(epochs):
for i, batch in enumerate(tqdm(data)):
images, _ = batch
if use_cuda:
images = images.cuda()
batch_size = images.size(0)
# ======================== Update Discriminator ===========================
for i in range(train_D_iter):
optimizer_D.zero_grad()
# noise
zu, zc, zd, zd_labels = noiseG.random_get(batch_size)
z = torch.cat([zu, zc, zd], dim=1)
fake = netG(z)
out_fake = headD(netD(fake))
labels_fake = tensor_fake.expand_as(out_fake)
lossD_fake = infoGAN_criterion.get_adv_loss(out_fake, labels_fake)
out_true = headD(netD(images))
labels_true = tensor_true.expand_as(out_true)
lossD_true = infoGAN_criterion.get_adv_loss(out_true, labels_true)
lossD = lossD_fake + lossD_true
lossD.backward()
optimizer_D.step()
writer.add_scalar("lossD", lossD.item(), Dstep)
Dstep += 1
# ======================== Update Generator ===========================
optimizer_G.zero_grad()
zu, zc, zd, zd_labels = noiseG.random_get(batch_size)
z = torch.cat([zu, zc, zd], dim=1)
fake = netG(z)
out_feature = netD(fake)
out_adv = headD(out_feature)
out_mi = headQ(out_feature)
labels_true = tensor_true.expand_as(out_adv)
lossG_adv = infoGAN_criterion.get_adv_loss(out_adv, labels_true)
lossG_mi = infoGAN_criterion.get_mi_loss(out_mi, zd_labels, zc)
lossG = lossG_adv + lossG_mi
lossG.backward()
optimizer_G.step()
writer.add_scalar("lossG", lossG.item(), Gstep)
writer.add_scalar("lossG_adv", lossG_adv.item(), Gstep)
writer.add_scalar("lossG_mi", lossG_mi.item(), Gstep)
Gstep += 1
if (epoch + 1) % save_epoch_interval == 0 or epoch == epochs - 1:
torch.save(netG.state_dict(), os.path.join(save_path, "netG.pt"))
torch.save(netD.state_dict(), os.path.join(save_path, "netD.pt"))
utiler.save_images(images, "real_{}.png".format(epoch+1), nrow=nrow)
utiler.save_images(fake, "fake_{}.png".format(epoch+1), nrow=nrow)
# evaluation
fid_score = evaluator.evaluate(netG, noiseG, data, device)
writer.add_scalar("eval/fid", fid_score, epoch)
epoch_eval_res = {
f"{epoch}": dict(fid=fid_score)
}
eval_res.append(epoch_eval_res)
json_dump(eval_res, os.path.join(save_path, "evaluation.json"))
endtime = time.process_time()
consume_time = endtime - starttime
print("Training Complete, Using %d min %d s" %(consume_time // 60,consume_time % 60))