-
Notifications
You must be signed in to change notification settings - Fork 70
/
trainer.py
242 lines (209 loc) · 9.25 KB
/
trainer.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
import os
import time
from collections import OrderedDict
import torch
import torch.distributed as dist
import torch.optim
import torchvision.utils as vutils
from torch.utils.data import DataLoader
import models
import utils
from dataset import ImageDataset
class Trainer(object):
def __init__(self, config):
self.rank, self.world_size = 0, 1
if config['dist']:
self.rank = dist.get_rank()
self.world_size = dist.get_world_size()
self.mode = config['dgp_mode']
assert self.mode in [
'reconstruct', 'colorization', 'SR', 'hybrid', 'inpainting',
'morphing', 'defence', 'jitter'
]
if self.rank == 0:
# mkdir path
if not os.path.exists('{}/images'.format(config['exp_path'])):
os.makedirs('{}/images'.format(config['exp_path']))
if not os.path.exists('{}/images_sheet'.format(
config['exp_path'])):
os.makedirs('{}/images_sheet'.format(config['exp_path']))
if not os.path.exists('{}/logs'.format(config['exp_path'])):
os.makedirs('{}/logs'.format(config['exp_path']))
# prepare logger
if not config['no_tb']:
try:
from tensorboardX import SummaryWriter
except ImportError:
raise Exception("Please switch off \"tensorboard\" "
"in your config file if you do not "
"want to use it, otherwise install it.")
self.tb_logger = SummaryWriter('{}'.format(config['exp_path']))
else:
self.tb_logger = None
self.logger = utils.create_logger(
'global_logger',
'{}/logs/log_train.txt'.format(config['exp_path']))
self.model = models.DGP(config)
if self.mode == 'morphing':
self.model2 = models.DGP(config)
self.model_interp = models.DGP(config)
# Data loader
train_dataset = ImageDataset(
config['root_dir'],
config['list_file'],
image_size=config['resolution'],
normalize=True)
sampler = utils.DistributedSampler(
train_dataset) if config['dist'] else None
self.train_loader = DataLoader(
train_dataset,
batch_size=1,
shuffle=False,
sampler=sampler,
num_workers=1,
pin_memory=False)
self.config = config
def run(self):
# train
if self.mode == 'morphing':
self.train_morphing()
else:
self.train()
def train(self):
btime_rec = utils.AverageMeter()
dtime_rec = utils.AverageMeter()
recorder = {}
end = time.time()
for i, (image, category, img_path) in enumerate(self.train_loader):
# measure data loading time
dtime_rec.update(time.time() - end)
torch.cuda.empty_cache()
image = image.cuda()
category = category.cuda()
img_path = img_path[0]
self.model.reset_G()
self.model.set_target(image, category, img_path)
# when category is unkonwn (category=-1), it would be selected from samples
self.model.select_z(select_y=True if category.item() < 0 else False)
loss_dict = self.model.run(save_interval=self.config['save_interval'])
# average loss if distributed
if self.config['dist']:
for k, v in loss_dict.items():
reduced = v.data.clone() / self.world_size
dist.all_reduce_multigpu([reduced])
loss_dict[k] = reduced
if len(recorder) == 0:
for k in loss_dict.keys():
recorder[k] = utils.AverageMeter()
for k in loss_dict.keys():
recorder[k].update(loss_dict[k].item())
btime_rec.update(time.time() - end)
end = time.time()
# logging
loss_str = ""
if self.rank == 0:
for k in recorder.keys():
if self.tb_logger is not None:
self.tb_logger.add_scalar('train_{}'.format(k),
recorder[k].avg, i + 1)
loss_str += '{}: {loss.val:.4g} ({loss.avg:.4g}) '.format(
k, loss=recorder[k])
self.logger.info(
'Iter: [{0}/{1}] '.format(i + 1, len(self.train_loader)) +
'Time {batch_time.val:.3f} ({batch_time.avg:.3f}) '.
format(batch_time=btime_rec) +
'Data {data_time.val:.3f} ({data_time.avg:.3f}) '.format(
data_time=dtime_rec) + 'Image {} '.format(img_path) +
loss_str)
def train_morphing(self):
btime_rec = utils.AverageMeter()
dtime_rec = utils.AverageMeter()
recorder = {}
last_category = -1
end = time.time()
for i, (image, category, img_path) in enumerate(self.train_loader):
# measure data loading time
dtime_rec.update(time.time() - end)
assert image.shape[0] > 0
image = image.cuda()
category = category.cuda()
img_path = img_path[0]
self.model.reset_G()
self.model.set_target(image, category, img_path)
self.model.select_z()
loss_dict = self.model.run(
save_interval=self.config['save_interval'])
# apply image morphing within the same category
if category == last_category:
self.morphing()
torch.cuda.empty_cache()
with torch.no_grad():
self.model2.G.load_state_dict(self.model.G.state_dict())
self.model2.z.copy_(self.model.z)
self.model2.img_name = self.model.img_name
self.model2.target = self.model.target
self.model2.category = self.model.category
if category == last_category:
# average loss if distributed
if self.config['dist']:
for k, v in loss_dict.items():
reduced = v.data.clone() / self.world_size
dist.all_reduce_multigpu([reduced])
loss_dict[k] = reduced
if len(recorder) < len(loss_dict):
for k in loss_dict.keys():
recorder[k] = utils.AverageMeter()
for k in loss_dict.keys():
recorder[k].update(loss_dict[k].item())
btime_rec.update(time.time() - end)
end = time.time()
# logging
loss_str = ""
if self.rank == 0:
for k in recorder.keys():
if self.tb_logger is not None:
self.tb_logger.add_scalar('train_{}'.format(k),
recorder[k].avg, i + 1)
loss_str += '{}: {loss.val:.4g} ({loss.avg:.4g}) '.format(
k, loss=recorder[k])
self.logger.info(
'Iter: [{0}/{1}] '.format(i, len(self.train_loader)) +
'Time {batch_time.val:.3f} ({batch_time.avg:.3f}) '.
format(batch_time=btime_rec) +
'Data {data_time.val:.3f} ({data_time.avg:.3f}) '.
format(data_time=dtime_rec) +
'Image {} '.format(img_path) + loss_str)
last_category = category
def morphing(self):
weight1 = self.model.G.state_dict()
weight2 = self.model2.G.state_dict()
weight_interp = OrderedDict()
imgs = []
with torch.no_grad():
for i in range(11):
alpha = i / 10
# interpolate between both latent vector and generator weight
z_interp = alpha * self.model.z + (1 - alpha) * self.model2.z
for k, w1 in weight1.items():
w2 = weight2[k]
weight_interp[k] = alpha * w1 + (1 - alpha) * w2
self.model_interp.G.load_state_dict(weight_interp)
x_interp = self.model_interp.G(
z_interp, self.model_interp.G.shared(self.model.y))
imgs.append(x_interp.cpu())
# save image
save_path = '%s/images/%s_%s' % (self.config['exp_path'],
self.model.img_name,
self.model2.img_name)
if not os.path.exists(save_path):
os.makedirs(save_path)
utils.save_img(x_interp[0], '%s/%03d.jpg' % (save_path, i + 1))
imgs = torch.cat(imgs, 0)
vutils.save_image(
imgs,
'%s/images_sheet/morphing_class%d_%s_%s.jpg' %
(self.config['exp_path'], self.model.category, self.model.img_name,
self.model2.img_name),
nrow=int(imgs.size(0)**0.5),
normalize=True)
del weight_interp