forked from laoreja/face-identity-transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_visual.py
128 lines (105 loc) · 4.59 KB
/
save_visual.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
import os, sys
import os.path as osp
import numpy as np
from PIL import Image
import gc
from tqdm import tqdm
import torch
import torchvision.transforms as transforms
from torchvision.utils import save_image
# libraries within this package
from cmd_args import parse_args
from utils.tools import *
from utils.util import generate_code
import datasets
import models
# -------------------- load & set args --------------------
args = parse_args(sys.argv[1])
args.during_training = False
args.old_ckpt_dir = osp.dirname(sys.argv[1])
args.resume = osp.join(args.old_ckpt_dir, args.ckpt_name)
args.gpu_ids = list(range(len(os.environ['CUDA_VISIBLE_DEVICES'].split(','))))
args.device = torch.device('cuda:0')
args.batch_size = args.batch_size // 4 * len(args.gpu_ids)
# -------------------- dataset & loader --------------------
test_dataset = datasets.__dict__[args.dataset](
train=False,
transform=transforms.Compose([
transforms.Resize((args.imageSize, args.imageSize), Image.BICUBIC),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))
]),
args=args
)
print('test_dataset: ' + str(test_dataset))
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
pin_memory=True,
worker_init_fn=lambda x: np.random.seed((torch.initial_seed()) % (2 ** 32))
)
model_dict = {}
G_input_nc = args.input_nc + args.passwd_length
model_dict['G'] = models.define_G(G_input_nc, args.output_nc,
args.ngf, args.which_model_netG, args.n_downsample_G,
args.normG, args.dropout,
args.init_type, args.init_gain,
args.passwd_length,
use_leaky=args.use_leakyG,
use_resize_conv=args.use_resize_conv,
padding_type=args.padding_type)
model_dict['G_nets'] = [model_dict['G']]
if args.resume:
if osp.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume, map_location='cpu')
args.start_epoch = checkpoint['epoch'] + 1
name = 'G'
net = model_dict[name]
if isinstance(net, torch.nn.DataParallel):
net = net.module
net.load_state_dict(checkpoint['state_dict_' + name])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
gc.collect()
torch.cuda.empty_cache()
torch.backends.cudnn.benchmark = True
model_dict['G'].train()
save_dir = osp.join('qualitative_results', args.dataset, *args.resume.split('.')[0].split('/')[-2:])
os.makedirs(save_dir, exist_ok=True)
with torch.no_grad():
for i, (img, label, landmarks, img_path) in enumerate(tqdm(test_loader)):
if img.size(0) != args.batch_size:
continue
start = i * args.batch_size
end = start + args.batch_size
img_cuda = img.cuda()
z, dis_target, \
rand_z, rand_dis_target, \
inv_z, inv_dis_target, \
rand_inv_z, rand_inv_dis_target, _, _ = generate_code(args.passwd_length,
args.batch_size,
args.device,
inv=True,
use_minus_one=args.use_minus_one,
gen_random_WR=False)
fake = model_dict['G'](img, z.cpu())
rand_fake = model_dict['G'](img, rand_z.cpu())
recon = model_dict['G'](fake, inv_z)
wrong_recon = model_dict['G'](fake, rand_inv_z)
# batchsize*5, 3, H, W
for save_idx in range(args.batch_size):
save_image((torch.cat((img[save_idx:save_idx + 1, ...].cpu(),
fake[save_idx:save_idx + 1, ...].cpu(),
rand_fake[save_idx:save_idx + 1, ...].cpu(),
wrong_recon[save_idx:save_idx + 1, ...].cpu(),
recon[save_idx:save_idx + 1, ...].cpu(),), dim=0) + 1.) / 2.,
filename=osp.join(save_dir, '_'.join(img_path[save_idx].split('.')[0].split('/')[-2:]) + '.png'),
nrow=5,
padding=2,
pad_value=1)