-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
164 lines (132 loc) · 5.9 KB
/
test.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
#coding=utf-8
import torch
import torch.nn as nn
import torch.nn.functional as F
import argparse
import os
import time
from cp_dataset import CPDataset, CPDataLoader
from networks import GMM, UnetGenerator, load_checkpoint
from tensorboardX import SummaryWriter
from visualization import board_add_image, board_add_images, save_images
def get_opt():
parser = argparse.ArgumentParser()
parser.add_argument("--name", default = "GMM")
parser.add_argument("--gpu_ids", default = "")
parser.add_argument('-j', '--workers', type=int, default=1)
parser.add_argument('-b', '--batch-size', type=int, default=4)
parser.add_argument("--dataroot", default = "data")
parser.add_argument("--datamode", default = "train")
parser.add_argument("--stage", default = "GMM")
parser.add_argument("--data_list", default = "train_pairs.txt")
parser.add_argument("--fine_width", type=int, default = 192)
parser.add_argument("--fine_height", type=int, default = 256)
parser.add_argument("--radius", type=int, default = 5)
parser.add_argument("--grid_size", type=int, default = 5)
parser.add_argument('--tensorboard_dir', type=str, default='tensorboard', help='save tensorboard infos')
parser.add_argument('--result_dir', type=str, default='result', help='save result infos')
parser.add_argument('--checkpoint', type=str, default='', help='model checkpoint for test')
parser.add_argument("--display_count", type=int, default = 1)
parser.add_argument("--shuffle", action='store_true', help='shuffle input data')
opt = parser.parse_args()
return opt
def test_gmm(opt, test_loader, model, board):
#model.cuda()
model.eval()
base_name = os.path.basename(opt.checkpoint)
save_dir = os.path.join(opt.result_dir, base_name, opt.datamode)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
warp_cloth_dir = os.path.join(save_dir, 'warp-cloth')
if not os.path.exists(warp_cloth_dir):
os.makedirs(warp_cloth_dir)
warp_mask_dir = os.path.join(save_dir, 'warp-mask')
if not os.path.exists(warp_mask_dir):
os.makedirs(warp_mask_dir)
for step, inputs in enumerate(test_loader.data_loader):
iter_start_time = time.time()
c_names = inputs['c_name']
im = inputs['image']#.cuda()
im_pose = inputs['pose_image']#.cuda()
im_h = inputs['head']#.cuda()
shape = inputs['shape']#.cuda()
agnostic = inputs['agnostic']#.cuda()
c = inputs['cloth']#.cuda()
cm = inputs['cloth_mask']#.cuda()
im_c = inputs['parse_cloth']#.cuda()
im_g = inputs['grid_image']#.cuda()
grid, theta = model(agnostic, c)
warped_cloth = F.grid_sample(c, grid, padding_mode='border')
warped_mask = F.grid_sample(cm, grid, padding_mode='zeros')
warped_grid = F.grid_sample(im_g, grid, padding_mode='zeros')
visuals = [ [im_h, shape, im_pose],
[c, warped_cloth, im_c],
[warped_grid, (warped_cloth+im)*0.5, im]]
save_images(warped_cloth, c_names, warp_cloth_dir)
save_images(warped_mask*2-1, c_names, warp_mask_dir)
if (step+1) % opt.display_count == 0:
board_add_images(board, 'combine', visuals, step+1)
t = time.time() - iter_start_time
print('step: %8d, time: %.3f' % (step+1, t), flush=True)
def test_tom(opt, test_loader, model, board):
#model.cuda()
model.eval()
base_name = os.path.basename(opt.checkpoint)
save_dir = os.path.join(opt.result_dir, base_name, opt.datamode)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
try_on_dir = os.path.join(save_dir, 'try-on')
if not os.path.exists(try_on_dir):
os.makedirs(try_on_dir)
print('Dataset size: %05d!' % (len(test_loader.dataset)), flush=True)
for step, inputs in enumerate(test_loader.data_loader):
iter_start_time = time.time()
im_names = inputs['im_name']
im = inputs['image']#.cuda()
im_pose = inputs['pose_image']
im_h = inputs['head']
shape = inputs['shape']
agnostic = inputs['agnostic']#.cuda()
c = inputs['cloth']#.cuda()
cm = inputs['cloth_mask']#.cuda()
outputs = model(torch.cat([agnostic, c],1))
p_rendered, m_composite = torch.split(outputs, 3,1)
p_rendered = F.tanh(p_rendered)
m_composite = F.sigmoid(m_composite)
p_tryon = c * m_composite + p_rendered * (1 - m_composite)
visuals = [ [im_h, shape, im_pose],
[c, 2*cm-1, m_composite],
[p_rendered, p_tryon, im]]
save_images(p_tryon, im_names, try_on_dir)
if (step+1) % opt.display_count == 0:
board_add_images(board, 'combine', visuals, step+1)
t = time.time() - iter_start_time
print('step: %8d, time: %.3f' % (step+1, t), flush=True)
def main():
opt = get_opt()
print(opt)
print("Start to test stage: %s, named: %s!" % (opt.stage, opt.name))
# create dataset
train_dataset = CPDataset(opt)
# create dataloader
train_loader = CPDataLoader(opt, train_dataset)
# visualization
if not os.path.exists(opt.tensorboard_dir):
os.makedirs(opt.tensorboard_dir)
board = SummaryWriter(log_dir = os.path.join(opt.tensorboard_dir, opt.name))
# create model & train
if opt.stage == 'GMM':
model = GMM(opt)
load_checkpoint(model, opt.checkpoint)
with torch.no_grad():
test_gmm(opt, train_loader, model, board)
elif opt.stage == 'TOM':
model = UnetGenerator(25, 4, 6, ngf=64, norm_layer=nn.InstanceNorm2d)
load_checkpoint(model, opt.checkpoint)
with torch.no_grad():
test_tom(opt, train_loader, model, board)
else:
raise NotImplementedError('Model [%s] is not implemented' % opt.stage)
print('Finished test %s, named: %s!' % (opt.stage, opt.name))
if __name__ == "__main__":
main()