/
solver_dosgan.py
684 lines (524 loc) · 28.7 KB
/
solver_dosgan.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
from model import *
from torch.autograd import Variable
from torchvision.utils import save_image
import torch
import torch.nn.functional as F
import numpy as np
import os
import time
import datetime
import itertools
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
if len(output[0]) < topk[1]:
topk = (1, len(output[0]))
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
class Solver(object):
def __init__(self, data_loader, data_loader_test, config):
# Data loader.
self.data_loader = data_loader
self.data_loader_test = data_loader_test
# Model configurations and loss weights.
self.ft_num = config.ft_num
self.c_dim = config.c_dim
self.d_conv_dim = config.d_conv_dim
self.d_repeat_num = config.d_repeat_num
self.n_blocks = config.n_blocks
self.lambda_rec = config.lambda_rec
self.lambda_rec2 = config.lambda_rec2
self.lambda_gp = config.lambda_gp
self.lambda_fs = config.lambda_fs
# Training configurations.
self.batch_size = config.batch_size
self.num_iters = config.num_iters
self.num_iters_decay = config.num_iters_decay
self.g_lr = config.g_lr
self.d_lr = config.d_lr
self.n_critic = config.n_critic
self.beta1 = config.beta1
self.beta2 = config.beta2
self.resume_iters = config.resume_iters
self.image_size = config.image_size
# Test configurations.
self.test_iters = config.test_iters
self.non_conditional = config.non_conditional
# Miscellaneous.
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Directories.
self.log_dir = config.log_dir
self.sample_dir = config.sample_dir
self.model_save_dir = config.model_save_dir
self.cls_save_dir = config.cls_save_dir
self.result_dir = config.result_dir
# Step size.
self.log_step = config.log_step
self.sample_step = config.sample_step
self.model_save_step = config.model_save_step
self.lr_update_step = config.lr_update_step
# Build the model.
self.build_model()
def build_model(self):
"""Initializing networks."""
self.encoder = ResnetEncoder()
self.decoder = ResnetDecoder(ft_num=self.ft_num,image_size=self.image_size)
self.D = Discriminator(self.image_size, self.d_conv_dim, self.c_dim, self.d_repeat_num, ft_num = self.ft_num)
self.C = Classifier(image_size=self.image_size, c_dim = self.c_dim, ft_num = self.ft_num, n_blocks = self.n_blocks)
self.g_optimizer = torch.optim.Adam(itertools.chain(self.encoder.parameters(), self.decoder.parameters()), self.g_lr, [self.beta1, self.beta2])
self.d_optimizer = torch.optim.Adam(self.D.parameters(), self.d_lr, [self.beta1, self.beta2])
self.c_optimizer = torch.optim.Adam(self.C.parameters(), self.d_lr, [self.beta1, self.beta2])
self.encoder.to(self.device)
self.decoder.to(self.device)
self.D.to(self.device)
self.C.to(self.device)
def restore_model(self, resume_iters):
"""Restore the trained networks."""
print('Loading the trained models from step {}...'.format(resume_iters))
encoder_path = os.path.join(self.model_save_dir, '{}-encoder.ckpt'.format(resume_iters))
decoder_path = os.path.join(self.model_save_dir, '{}-decoder.ckpt'.format(resume_iters))
D_path = os.path.join(self.model_save_dir, '{}-D.ckpt'.format(resume_iters))
self.encoder.load_state_dict(torch.load(encoder_path, map_location=lambda storage, loc: storage))
self.decoder.load_state_dict(torch.load(decoder_path, map_location=lambda storage, loc: storage))
self.D.load_state_dict(torch.load(D_path, map_location=lambda storage, loc: storage))
def update_lr(self, g_lr, d_lr):
"""Decay learning rates."""
for param_group in self.g_optimizer.param_groups:
param_group['lr'] = g_lr
for param_group in self.d_optimizer.param_groups:
param_group['lr'] = d_lr
def reset_grad(self):
"""Reset the gradient buffers."""
self.g_optimizer.zero_grad()
self.d_optimizer.zero_grad()
def denorm(self, x):
"""Convert the range from [-1, 1] to [0, 1]."""
out = (x + 1) / 2
return out.clamp_(0, 1)
def gradient_penalty(self, y, x):
"""Compute gradient penalty: (L2_norm(dy/dx) - 1)**2."""
weight = torch.ones(y.size()).to(self.device)
dydx = torch.autograd.grad(outputs=y,
inputs=x,
grad_outputs=weight,
retain_graph=True,
create_graph=True,
only_inputs=True)[0]
dydx = dydx.view(dydx.size(0), -1)
dydx_l2norm = torch.sqrt(torch.sum(dydx**2, dim=1))
return torch.mean((dydx_l2norm-1)**2)
def classification_loss(self, logit, target):
return F.cross_entropy(logit, target)
def train(self):
# Load pre-trained classification network
cls_iter = 160000
C_path = os.path.join(self.cls_save_dir, '{}-C.ckpt'.format(cls_iter))
self.C.load_state_dict(torch.load(C_path, map_location=lambda storage, loc: storage))
# Set data loader.
data_loader = self.data_loader
# Set learning rate
g_lr = self.g_lr
d_lr = self.d_lr
# Start training from scratch or resume training.
start_iters = 0
if self.resume_iters:
start_iters = self.resume_iters
self.restore_model(self.resume_iters)
empty = torch.FloatTensor(1, 3, self.image_size, self.image_size).to(self.device)
empty.fill_(1)
# Calculate domain feature centroid of each domain
domain_sf_num = torch.FloatTensor(self.c_dim, 1).to(self.device)
domain_sf_num.fill_(0.00000001)
domain_sf = torch.FloatTensor(self.c_dim, self.ft_num).to(self.device)
domain_sf.fill_(0)
with torch.no_grad():
for indx, (x_real, label_org) in enumerate(data_loader):
x_real = x_real.to(self.device)
label_org = label_org.to(self.device)
x_ds, x_cls = self.C(x_real)
for j in range(label_org.size(0)):
domain_sf[label_org[j], :] = (domain_sf[label_org[j], :] + x_ds[j] / domain_sf_num[label_org[j], :]) * (
domain_sf_num[label_org[j], :] / (domain_sf_num[label_org[j], :] + 1))
domain_sf_num[label_org[j], :] += 1
start_time = time.time()
# Start training.
for i in range(start_iters, self.num_iters):
# Fetch real images and labels.
try:
x_real, label_org = next(data_iter)
except:
data_iter = iter(data_loader)
x_real, label_org = next(data_iter)
x_real = x_real.to(self.device)
label_org = label_org.to(self.device)
x_ds, x_cls = self.C(x_real) #obtain domain feature for each real image
#obtain domain feature centroid for each real image
x_ds_mean = torch.FloatTensor(label_org.size(0), self.ft_num).to(self.device)
for j in range(label_org.size(0)):
x_ds_mean[j] = domain_sf[label_org[j]:label_org[j] + 1, :]
# random target
rand_idx = torch.randperm(label_org.size(0))
trg_dst = x_ds_mean[rand_idx]
trg_ds = trg_dst.clone()
# =================================================================================== #
# 2. Train the discriminator #
# =================================================================================== #
# Compute loss with real images.
out_src, out_cls = self.D(x_real)
d_loss_real = - torch.mean(out_src)
d_loss_dsrec = torch.mean(
torch.abs(x_ds.detach() - out_cls))
# Compute loss with fake images.
x_fake = self.decoder(self.encoder(x_real), trg_ds)
out_src, out_cls = self.D(x_fake.detach())
d_loss_fake = torch.mean(out_src)
# Compute loss for gradient penalty.
alpha = torch.rand(x_real.size(0), 1, 1, 1).to(self.device)
x_hat = (alpha * x_real.data + (1 - alpha) * x_fake.data).requires_grad_(True)
out_src, _ = self.D(x_hat)
d_loss_gp = self.gradient_penalty(out_src, x_hat)
# Backward and optimize.
d_loss = d_loss_real + d_loss_fake + self.lambda_fs * d_loss_dsrec + self.lambda_gp * d_loss_gp
self.reset_grad()
d_loss.backward()
self.d_optimizer.step()
# Logging.
loss = {}
loss['D/loss_real'] = d_loss_real.item()
loss['D/loss_fake'] = d_loss_fake.item()
loss['D/loss_dsrec'] = d_loss_dsrec.item()
loss['D/loss_gp'] = d_loss_gp.item()
# =================================================================================== #
# 3. Train the encoder and decoder #
# =================================================================================== #
if (i + 1) % self.n_critic == 0:
# Original-to-target domain.
x_di = self.encoder(x_real)
x_fake = self.decoder(x_di, trg_ds)
x_reconst1 = self.decoder(x_di, x_ds)
out_src, out_cls = self.D(x_fake)
g_loss_fake = - torch.mean(out_src)
g_loss_dsrec = torch.mean(
torch.abs(trg_ds.detach() - out_cls))
# Target-to-original domain.
x_fake_di = self.encoder(x_fake)
x_reconst2 = self.decoder(x_fake_di, x_ds)
g_loss_rec = torch.mean(torch.abs(x_real - x_reconst1))
g_loss_rec2 = torch.mean(torch.abs(x_real - x_reconst2))
# Backward and optimize.
g_loss = g_loss_fake + self.lambda_rec * g_loss_rec + self.lambda_rec2 * g_loss_rec2 + self.lambda_fs * g_loss_dsrec
self.reset_grad()
g_loss.backward()
self.g_optimizer.step()
# Logging.
loss['G/loss_fake'] = g_loss_fake.item()
loss['G/loss_rec'] = g_loss_rec.item()
loss['G/loss_rec2'] = g_loss_rec2.item()
loss['G/loss_dsrec'] = g_loss_dsrec.item()
# =================================================================================== #
# 4. Miscellaneous #
# =================================================================================== #
# Print out training information.
if (i + 1) % self.log_step == 0:
et = time.time() - start_time
et = str(datetime.timedelta(seconds=et))[:-7]
log = "Elapsed [{}], Iteration [{}/{}]".format(et, i + 1, self.num_iters)
for tag, value in loss.items():
log += ", {}: {:.4f}".format(tag, value)
print(log)
# Translate fixed images for debugging.
if (i) % self.sample_step == 0:
with torch.no_grad():
out_A2B_results = [empty]
for idx1 in range(label_org.size(0)):
out_A2B_results.append(x_real[idx1:idx1 + 1])
for idx2 in range(label_org.size(0)):
out_A2B_results.append(x_real[idx2:idx2 + 1])
for idx1 in range(label_org.size(0)):
x_fake = self.decoder(self.encoder(x_real[idx2:idx2 + 1]), x_ds_mean[idx1:idx1 + 1])
out_A2B_results.append(x_fake)
results_concat = torch.cat(out_A2B_results)
x_AB_results_path = os.path.join(self.sample_dir, '{}_x_AB_results.jpg'.format(i + 1))
save_image(self.denorm(results_concat.data.cpu()), x_AB_results_path, nrow=label_org.size(0) + 1,
padding=0)
print('Saved real and fake images into {}...'.format(x_AB_results_path))
# Save model checkpoints.
if (i + 1) % self.model_save_step == 0:
encoder_path = os.path.join(self.model_save_dir, '{}-encoder.ckpt'.format(i + 1))
decoder_path = os.path.join(self.model_save_dir, '{}-decoder.ckpt'.format(i + 1))
D_path = os.path.join(self.model_save_dir, '{}-D.ckpt'.format(i + 1))
torch.save(self.encoder.state_dict(), encoder_path)
torch.save(self.decoder.state_dict(), decoder_path)
torch.save(self.D.state_dict(), D_path)
print('Saved model checkpoints into {}...'.format(self.model_save_dir))
# Decay learning rates.
if (i + 1) % self.lr_update_step == 0 and (i + 1) > (self.num_iters - self.num_iters_decay):
g_lr -= (self.g_lr / float(self.num_iters_decay))
d_lr -= (self.d_lr / float(self.num_iters_decay))
self.update_lr(g_lr, d_lr)
print('Decayed learning rates, g_lr: {}, d_lr: {}.'.format(g_lr, d_lr))
def train_conditional(self):
# Load pre-trained classification network
cls_iter = 160000
C_path = os.path.join(self.cls_save_dir, '{}-C.ckpt'.format(cls_iter))
self.C.load_state_dict(torch.load(C_path, map_location=lambda storage, loc: storage))
# Set data loader.
data_loader = self.data_loader
# Set learning rate
g_lr = self.g_lr
d_lr = self.d_lr
# Start training from scratch or resume training.
start_iters = 0
if self.resume_iters:
start_iters = self.resume_iters
self.restore_model(self.resume_iters)
empty = torch.FloatTensor(1, 3, self.image_size, self.image_size).to(self.device)
empty.fill_(1)
start_time = time.time()
# Start training.
for i in range(start_iters, self.num_iters):
# Fetch real images and labels.
try:
x_real, label_org = next(data_iter)
except:
data_iter = iter(data_loader)
x_real, label_org = next(data_iter)
x_real = x_real.to(self.device)
label_org = label_org.to(self.device)
x_ds, x_cls = self.C(x_real) # obtain domain feature for each real image
# random target
rand_idx = torch.randperm(label_org.size(0))
trg_dst = x_ds[rand_idx]
trg_ds = trg_dst.clone()
# =================================================================================== #
# 2. Train the discriminator #
# =================================================================================== #
# Compute loss with real images.
out_src, out_cls = self.D(x_real)
d_loss_real = - torch.mean(out_src)
d_loss_dsrec = torch.mean(torch.abs(x_ds.detach() - out_cls))
# Compute loss with fake images.
x_fake = self.decoder(self.encoder(x_real), trg_ds)
out_src, out_cls = self.D(x_fake.detach())
d_loss_fake = torch.mean(out_src)
# Compute loss for gradient penalty.
alpha = torch.rand(x_real.size(0), 1, 1, 1).to(self.device)
x_hat = (alpha * x_real.data + (1 - alpha) * x_fake.data).requires_grad_(True)
out_src, _ = self.D(x_hat)
d_loss_gp = self.gradient_penalty(out_src, x_hat)
# Backward and optimize.
d_loss = d_loss_real + d_loss_fake + self.lambda_fs * d_loss_dsrec + self.lambda_gp * d_loss_gp
self.reset_grad()
d_loss.backward()
self.d_optimizer.step()
# Logging.
loss = {}
loss['D/loss_real'] = d_loss_real.item()
loss['D/loss_fake'] = d_loss_fake.item()
loss['D/loss_dsrec'] = d_loss_dsrec.item()
loss['D/loss_gp'] = d_loss_gp.item()
# =================================================================================== #
# 3. Train the encoder and decoder #
# =================================================================================== #
if (i + 1) % self.n_critic == 0:
# Original-to-target domain.
x_di = self.encoder(x_real)
x_fake = self.decoder(x_di, trg_ds)
x_reconst1 = self.decoder(x_di, x_ds)
out_src, out_cls = self.D(x_fake)
g_loss_fake = - torch.mean(out_src)
g_loss_dsrec = torch.mean(torch.abs(trg_ds.detach() - out_cls))
# Target-to-original domain.
x_fake_di = self.encoder(x_fake)
x_reconst2 = self.decoder(x_fake_di, x_ds)
g_loss_rec = torch.mean(torch.abs(x_real - x_reconst1))
g_loss_rec2 = torch.mean(torch.abs(x_real - x_reconst2))
# Backward and optimize.
g_loss = g_loss_fake + self.lambda_rec * g_loss_rec + self.lambda_rec2 * g_loss_rec2 + self.lambda_fs * g_loss_dsrec
self.reset_grad()
g_loss.backward()
self.g_optimizer.step()
# Logging.
loss['G/loss_fake'] = g_loss_fake.item()
loss['G/loss_rec'] = g_loss_rec.item()
loss['G/loss_rec2'] = g_loss_rec2.item()
loss['G/loss_dsrec'] = g_loss_dsrec.item()
# =================================================================================== #
# 4. Miscellaneous #
# =================================================================================== #
# Print out training information.
if (i + 1) % self.log_step == 0:
et = time.time() - start_time
et = str(datetime.timedelta(seconds=et))[:-7]
log = "Elapsed [{}], Iteration [{}/{}]".format(et, i + 1, self.num_iters)
for tag, value in loss.items():
log += ", {}: {:.4f}".format(tag, value)
print(log)
# Translate fixed images for debugging.
if (i) % self.sample_step == 0:
with torch.no_grad():
out_A2B_results = [empty]
for idx1 in range(label_org.size(0)):
out_A2B_results.append(x_real[idx1:idx1 + 1])
for idx2 in range(label_org.size(0)):
out_A2B_results.append(x_real[idx2:idx2 + 1])
for idx1 in range(label_org.size(0)):
x_fake = self.decoder(self.encoder(x_real[idx2:idx2 + 1]), x_ds[idx1:idx1 + 1])
out_A2B_results.append(x_fake)
results_concat = torch.cat(out_A2B_results)
x_AB_results_path = os.path.join(self.sample_dir, '{}_x_AB_results.jpg'.format(i + 1))
save_image(self.denorm(results_concat.data.cpu()), x_AB_results_path, nrow=label_org.size(0) + 1,
padding=0)
print('Saved real and fake images into {}...'.format(x_AB_results_path))
# Save model checkpoints.
if (i + 1) % self.model_save_step == 0:
encoder_path = os.path.join(self.model_save_dir, '{}-encoder.ckpt'.format(i + 1))
decoder_path = os.path.join(self.model_save_dir, '{}-decoder.ckpt'.format(i + 1))
D_path = os.path.join(self.model_save_dir, '{}-D.ckpt'.format(i + 1))
torch.save(self.encoder.state_dict(), encoder_path)
torch.save(self.decoder.state_dict(), decoder_path)
torch.save(self.D.state_dict(), D_path)
print('Saved model checkpoints into {}...'.format(self.model_save_dir))
# Decay learning rates.
if (i + 1) % self.lr_update_step == 0 and (i + 1) > (self.num_iters - self.num_iters_decay):
g_lr -= (self.g_lr / float(self.num_iters_decay))
d_lr -= (self.d_lr / float(self.num_iters_decay))
self.update_lr(g_lr, d_lr)
print('Decayed learning rates, g_lr: {}, d_lr: {}.'.format(g_lr, d_lr))
def cls(self):
"""Train a domain classifier"""
# Set data loader.
data_loader = self.data_loader
# Start training from scratch or resume training.
start_iters = 0
# Start training.
start_time = time.time()
for i in range(start_iters, self.num_iters):
try:
x_real, label_org = next(data_iter)
except:
data_iter = iter(data_loader)
x_real, label_org = next(data_iter)
x_real = x_real.to(self.device) # Input images.
label_org = label_org.to(self.device) # Labels for computing classification loss.
# =================================================================================== #
# Train the classifier #
# =================================================================================== #
out_src, out_cls = self.C(x_real)
d_loss_cls = self.classification_loss(out_cls, label_org)
# Backward and optimize.
d_loss = d_loss_cls
self.c_optimizer.zero_grad()
d_loss.backward()
self.c_optimizer.step()
# Logging.
loss = {}
loss['D/loss_cls'] = d_loss_cls.item()
# Print out training information.
if (i+1) % self.log_step == 0:
et = time.time() - start_time
et = str(datetime.timedelta(seconds=et))[:-7]
prec1, prec5 = accuracy(out_cls.data, label_org.data, topk=(1, 5))
loss['prec1'] = prec1
loss['prec5'] = prec5
log = "Elapsed [{}], Iteration [{}/{}]".format(et, i+1, self.num_iters)
for tag, value in loss.items():
log += ", {}: {:.4f}".format(tag, value)
print(log)
# Save model checkpoints.
if (i+1) % self.model_save_step == 0:
C_path = os.path.join(self.model_save_dir, '{}-C.ckpt'.format(i+1))
torch.save(self.C.state_dict(), C_path)
print('Saved model checkpoints into {}...'.format(self.model_save_dir))
def label2onehot(self, labels, dim):
"""Convert label indices to one-hot vectors."""
batch_size = labels.size(0)
out = torch.zeros(batch_size, dim)
out[np.arange(batch_size), labels.long()] = 1
return out
def create_labels(self, c_org, c_dim=5):
"""Generate target domain labels for debugging and testing."""
# Get hair color indices.
hair_color_indices = []
for i in range(c_dim):
hair_color_indices.append(i)
c_trg_list = []
for i in range(c_dim):
c_trg = c_org.clone()
if i in hair_color_indices: # Set one hair color to 1 and the rest to 0.
c_trg[:, i] = 1
for j in hair_color_indices:
if j != i:
c_trg[:, j] = 0
c_trg_list.append(c_trg.to(self.device))
return c_trg_list
def test(self):
"""Translate images with trained DosGAN."""
# Load the trained networks.
cls_iter = 160000
C_path = os.path.join(self.cls_save_dir, '{}-C.ckpt'.format(cls_iter))
self.C.load_state_dict(torch.load(C_path, map_location=lambda storage, loc: storage))
self.restore_model(self.test_iters)
# Set data loader.
data_loader = self.data_loader
data_loader_test = self.data_loader_test
step = 0
empty = torch.FloatTensor(1, 3,self.image_size,self.image_size).to(self.device)
empty.fill_(1)
domain_sf_num = torch.FloatTensor(self.c_dim, 1).to(self.device)
domain_sf_num.fill_(0.00000001)
domain_sf = torch.FloatTensor(self.c_dim, self.ft_num).to(self.device)
domain_sf.fill_(0)
with torch.no_grad():
if self.non_conditional: # non_conditional testing
for indx, (x_real, label_org) in enumerate(data_loader):
x_real = x_real.to(self.device) # Input images.
label_org = label_org.to(self.device)
x_ds, x_cls = self.C(x_real)
for j in range(label_org.size(0)):
domain_sf[label_org[j],:] = (domain_sf[label_org[j],:] + x_ds[j]/domain_sf_num[label_org[j],:])*(domain_sf_num[label_org[j],:]/(domain_sf_num[label_org[j],:]+1))
domain_sf_num[label_org[j],:] += 1
step = step +1
for indx, (x_real, label_org) in enumerate(data_loader_test):
x_real = x_real.to(self.device) # Input images.
x_ds, x_cls = self.C(x_real)
c_org = self.label2onehot(label_org, self.c_dim)
c_org = c_org.to(self.device)
label_org = label_org.to(self.device)
c_fixed_list = self.create_labels(c_org, self.c_dim)
x_fake_list = [x_real]
for c_fixed in c_fixed_list:
_, out_pred_fixed = torch.max(c_fixed.data, 1)
x_ds_m = x_ds.clone()
for k in range(label_org.size(0)):
x_ds_m[k,:] = domain_sf[out_pred_fixed[k],:]
x_fake = self.decoder(self.encoder(x_real), x_ds_m)
x_fake_list.append(x_fake)
x_concat = torch.cat(x_fake_list, dim=3)
sample_path = os.path.join(self.result_dir, '{}-images.jpg'.format(indx+1))
save_image(self.denorm(x_concat.data.cpu()), sample_path, nrow=1, padding=0)
print('Saved real and fake images into {}...'.format(sample_path))
else: # conditional image translation testing
for indx, (x_real, label_org) in enumerate(data_loader_test):
x_real = x_real.to(self.device) # Input images.
label_org = label_org.to(self.device)
x_ds, x_cls = self.C(x_real)
out_A2B_results = [empty]
for j in range(label_org.size(0)):
out_A2B_results.append(x_real[j:j+1])
for i in range(label_org.size(0)):
out_A2B_results.append(x_real[i:i+1])
for j in range(label_org.size(0)):
x_fake = self.decoder(self.encoder(x_real[i:i+1]), x_ds[j:j+1])
out_A2B_results.append(x_fake)
results_concat = torch.cat(out_A2B_results)
x_AB_results_path = os.path.join(self.result_dir, '{}_x_AB_results.jpg'.format(indx+1))
save_image(self.denorm(results_concat.data.cpu()), x_AB_results_path, nrow=label_org.size(0)+1,padding=0)
print('Saved real and fake images into {}...'.format(x_AB_results_path))