-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bpgm.py
287 lines (216 loc) · 11.7 KB
/
test_bpgm.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
import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import sys
sys.path.append('../')
import multiprocessing as mp
import os
import argparse
import numpy as np
import torch
from torch.nn import functional as F
from PIL import Image
from bpgm.model.models import BPGM
from bpgm.model.utils import load_checkpoint, save_checkpoint
from bpgm.dataset import DataLoader, VitonDataset
def get_opt():
parser = argparse.ArgumentParser()
# Name of the GMM or TOM model
parser.add_argument("--name", default="GMM")
# parser.add_argument("--name", default="TOM")
# Add multiple workers support
parser.add_argument("--workers", type=int, default=mp.cpu_count() // 2)
# GPU IDs to use
# parser.add_argument("--gpu_ids", default="")
# Number of workers for dataloader (default: 1)
#parser.add_argument('-j', '--workers', type=int, default=1)
# Batch size for training (default: 32)
# Batch size defines the number of images that are processed at the same time
parser.add_argument('-b', '--batch-size', type=int, default=32)
# Path to the data folder
parser.add_argument("--dataroot", default="/scratch/c.c1984628/my_diss/bpgm/data")
# Training mode or testing mode
parser.add_argument("--datamode", default="train")
# What are we training/testing here
parser.add_argument("--stage", default="GMM")
# parser.add_argument("--stage", default="TOM")
# Path to the list of training/testing images
parser.add_argument("--data_list", default="/scratch/c.c1984628/my_diss/bpgm/data/train_pairs.txt")
# choose dataset
parser.add_argument("--dataset", default="viton")
# fine_width, fine_height: size of the input image to the network
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)
# lr = learning rate
parser.add_argument('--lr', type=float, default=0.0001,
help='initial learning rate for adam')
# tensorboard_dir: path to the folder where tensorboard files are saved
parser.add_argument('--tensorboard_dir', type=str,
default='tensorboard', help='save tensorboard infos')
parser.add_argument('--checkpoint_dir', type=str,
default='checkpoints', help='save checkpoint infos')
parser.add_argument('--checkpoint', type=str, default='',
help='model checkpoint for initialization')
# display_count: how often to display the training results defaulted to every 20 steps
parser.add_argument("--display_count", type=int, default=20)
# save_count: how often to save the model defaulted to every 5000 steps
parser.add_argument("--save_count", type=int, default=5000)
# keep_step: how many steps to train the model for
parser.add_argument("--keep_step", type=int, default=100000)
# decay_step: how many steps to decay the learning rate for
parser.add_argument("--decay_step", type=int, default=100000)
# shuffle: shuffle the input data
parser.add_argument("--shuffle", action='store_true',
help='shuffle input data')
opt = parser.parse_args()
return opt
def main():
opt = get_opt()
opt.train_size = 0.9
opt.val_size = 0.1
opt.img_size = 256
print(opt)
print("Start to train stage: %s, named: %s!" % (opt.stage, opt.name))
# create dataset
# if opt.dataset == "mpv":
# dataset = MPVDataset(opt)
# el
if opt.dataset == "viton":
dataset = VitonDataset(opt)
else:
raise NotImplementedError
model = BPGM(opt)
if not opt.checkpoint == '' and os.path.exists(opt.checkpoint):
load_checkpoint(model, opt.checkpoint)
else:
raise NotImplementedError
model.cuda()
model.eval()
for i in range(len(dataset.filepath_df)):
print("Processing image: ", i)
# images = dataset[i]
# images_swap = dataset[i]
# if images['im_name'] != "013418_0.jpg":
# continue
images = dataset[8855]
images_swap = dataset[12546]
print("Name of the source image: ", images_swap['im_name'])
print("Name of the target image: ", images['im_name'])
for key, im in images.items():
if isinstance(im, torch.Tensor) and im.shape[0] in {1, 3}:
im = im / 2 + 0.5
im = im.permute(1, 2, 0).numpy()
im = (im * 255).astype(np.uint8)
if im.shape[-1] == 1:
im = np.repeat(im, 3, axis=-1)
im = Image.fromarray(im)
# im.save(os.path.join("sample", "bpgm_warp", key + ".png"))
# DEAL WITH ORIGINAL
tc = images['target_cloth'].unsqueeze(0).cuda()
tcm = images['target_cloth_mask'].unsqueeze(0).cuda()
im_bm = images['body_mask'].unsqueeze(0).cuda()
im_label = images['body_label'].unsqueeze(0).cuda()
# agnostic = images['agnostic'].unsqueeze(0).cuda()
grid = model(im_label, tc)
# grid = model(agnostic, tc)
warped_cloth = F.grid_sample(tc, grid, padding_mode='border', align_corners=True)
warped_cloth_masked = warped_cloth * im_bm
warped_mask = F.grid_sample(tcm, grid, padding_mode='border', align_corners=True)
warped_cloth = warped_cloth.squeeze(0).permute(1, 2, 0).detach().cpu().numpy() / 2 + 0.5
warped_cloth = (warped_cloth * 255).astype(np.uint8)
warped_cloth_masked = warped_cloth_masked.squeeze(0).permute(1, 2, 0).detach().cpu().numpy() / 2 + 0.5
warped_cloth_masked = (warped_cloth_masked * 255).astype(np.uint8)
warped_mask = warped_mask.squeeze(0).permute(1, 2, 0).detach().cpu().numpy() / 2 + 0.5
warped_mask = np.repeat(warped_mask, 3, axis=-1)
warped_mask = (warped_mask * 255).astype(np.uint8)
# Load the original image
im = Image.open(os.path.join("/scratch/c.c1984628/my_diss/bpgm/data/image", images_swap['im_name']))
# save the original image to the sample folder
im.save(os.path.join("/scratch/c.c1984628/my_diss/bpgm/results/sample", "original.png"))
im = Image.fromarray(warped_cloth).save("/scratch/c.c1984628/my_diss/bpgm/results/sample/warped_cloth.png")
#im = Image.fromarray(warped_cloth_masked).save("/scratch/c.c1984628/my_diss/bpgm/results/sample/warped_cloth_masked.png")
im = Image.fromarray(warped_mask).save("/scratch/c.c1984628/my_diss/bpgm/results/sample/warped_mask.png")
# DEAL WITH SWAP
tc = images_swap['target_cloth'].unsqueeze(0).cuda()
tcm = images_swap['target_cloth_mask'].unsqueeze(0).cuda()
im_bm = images['body_mask'].unsqueeze(0).cuda()
im_label = images['body_label'].unsqueeze(0).cuda()
# agnostic = images['agnostic'].unsqueeze(0).cuda()
grid = model(im_label, tc)
# grid = model(agnostic, tc)
warped_cloth_swap = F.grid_sample(tc, grid, padding_mode='border', align_corners=True)
warped_cloth_masked_swap = warped_cloth_swap * im_bm
warped_mask_swap = F.grid_sample(tcm, grid, padding_mode='border', align_corners=True)
warped_cloth_swap = warped_cloth_swap.squeeze(0).permute(1, 2, 0).detach().cpu().numpy() / 2 + 0.5
warped_cloth_swap = (warped_cloth_swap * 255).astype(np.uint8)
warped_cloth_masked_swap = warped_cloth_masked_swap.squeeze(0).permute(1, 2, 0).detach().cpu().numpy() / 2 + 0.5
warped_cloth_masked_swap = (warped_cloth_masked_swap * 255).astype(np.uint8)
warped_mask_swap = warped_mask_swap.squeeze(0).permute(1, 2, 0).detach().cpu().numpy() / 2 + 0.5
warped_mask_swap = np.repeat(warped_mask_swap, 3, axis=-1)
warped_mask_swap = (warped_mask_swap * 255).astype(np.uint8)
# load the target image
im = Image.open(os.path.join("/scratch/c.c1984628/my_diss/bpgm/data/image", images['im_name']))
# save the target image to the sample folder
im.save(os.path.join("/scratch/c.c1984628/my_diss/bpgm/results/sample", "target.png"))
im = Image.fromarray(warped_cloth).save("/scratch/c.c1984628/my_diss/warped_cloth.png")
im = Image.fromarray(warped_cloth_swap).save("/scratch/c.c1984628/my_diss/bpgm/results/sample/viton_bpgm_warp.png")
#im = Image.fromarray(warped_cloth_swap).save(os.path.join("tmp.jpg"))
break
# im = Image.fromarray(warped_cloth_masked_swap).save(os.path.join("sample", "bpgm_warp", "warped_cloth_masked_swap.png"))
# im = Image.fromarray(warped_mask_swap).save(os.path.join("sample", "bpgm_warp", "warped_mask_swap.png"))
def get_opt():
parser = argparse.ArgumentParser()
# Name of the GMM or TOM model
parser.add_argument("--name", default="GMM")
# parser.add_argument("--name", default="TOM")
# Add multiple workers support
parser.add_argument("--workers", type=int, default=mp.cpu_count() // 2)
# GPU IDs to use
# parser.add_argument("--gpu_ids", default="")
# Number of workers for dataloader (default: 1)
#parser.add_argument('-j', '--workers', type=int, default=1)
# Batch size for training (default: 32)
# Batch size defines the number of images that are processed at the same time
parser.add_argument('-b', '--batch-size', type=int, default=32)
# Path to the data folder
parser.add_argument("--dataroot", default="/scratch/c.c1984628/my_diss/bpgm/data")
# Training mode or testing mode
parser.add_argument("--datamode", default="train")
# What are we training/testing here
parser.add_argument("--stage", default="GMM")
# parser.add_argument("--stage", default="TOM")
# Path to the list of training/testing images
parser.add_argument("--data_list", default="/scratch/c.c1984628/my_diss/bpgm/data/train_pairs.txt")
# choose dataset
parser.add_argument("--dataset", default="viton")
# fine_width, fine_height: size of the input image to the network
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)
# lr = learning rate
parser.add_argument('--lr', type=float, default=0.0001,
help='initial learning rate for adam')
# tensorboard_dir: path to the folder where tensorboard files are saved
parser.add_argument('--tensorboard_dir', type=str,
default='tensorboard', help='save tensorboard infos')
parser.add_argument('--checkpoint_dir', type=str,
default='checkpoints', help='save checkpoint infos')
parser.add_argument('--checkpoint', type=str, default='',
help='model checkpoint for initialization')
# display_count: how often to display the training results defaulted to every 20 steps
parser.add_argument("--display_count", type=int, default=20)
# save_count: how often to save the model defaulted to every 5000 steps
parser.add_argument("--save_count", type=int, default=5000)
# keep_step: how many steps to train the model for
parser.add_argument("--keep_step", type=int, default=100000)
# decay_step: how many steps to decay the learning rate for
parser.add_argument("--decay_step", type=int, default=100000)
# shuffle: shuffle the input data
parser.add_argument("--shuffle", action='store_true',
help='shuffle input data')
opt = parser.parse_args()
return opt
if __name__ == "__main__":
main()