-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfer.py
149 lines (116 loc) · 5.58 KB
/
infer.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import argparse
import os
from collections import namedtuple
import matplotlib.pyplot as plt
from models.gmm import GMM
from models.unet import UnetGenerator
from utilities import load_checkpoint
from datasets import CPDataset
def get_model_opt(use_cuda=False):
options = namedtuple('options', ['fine_width', 'fine_height', 'radius', 'grid_size', 'use_cuda'])
return options(fine_width=192, fine_height=256, radius=5, grid_size=5, use_cuda=use_cuda)
def parse_opt():
parser = argparse.ArgumentParser()
parser.add_argument("--dataroot", default = "data")
parser.add_argument("--datamode", default = "test")
parser.add_argument("--data_list", default = "test_pairs.txt")
parser.add_argument("--result_dir", type=str, default="predictions", help="save inference result")
parser.add_argument("--gpu_ids", default = "")
parser.add_argument("--use_cuda", action=argparse.BooleanOptionalAction, default = False)
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 = 3)
parser.add_argument("--stage", default = "GMM")
opt = parser.parse_args()
return opt
def main():
opt = parse_opt()
model_opts = get_model_opt(use_cuda=opt.use_cuda)
pretrained_gmm_path = os.path.join("checkpoints", "train_gmm_200K", "gmm_final.pth")
pretrained_tom_path = os.path.join("checkpoints", "train_tom_200K", "tom_final.pth")
data_loader = CPDataset(opt)
while True:
idx = int(input(f"\n\nChoose an index (0, {len(data_loader)}]: "))
inputs = data_loader[idx]
print(f"Selected: \n\tcloth_file: {inputs['c_name']} \n\timg_file: {inputs['im_name']}")
_, ax = plt.subplots(4, 3, figsize=(15, 8), num="VITON Inference Results")
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1, wspace=0.2, hspace=0.2)
ax[0, 0].imshow( ( inputs['cloth'].detach().permute(1, 2, 0) * 0.5 ) + 0.5 )
ax[0, 0].axis("off")
ax[0, 0].set_title("Cloth")
ax[0, 1].imshow( ( inputs['image'].detach().permute(1, 2, 0) * 0.5 ) + 0.5 )
ax[0, 1].axis("off")
ax[0, 1].set_title("Image")
ax[0, 2].imshow( inputs["parse_image"] )
ax[0, 2].axis("off")
ax[0, 2].set_title("Parsed Image")
ax[1, 0].imshow( ( inputs['head'].detach().permute(1, 2, 0) * 0.5 ) + 0.5 )
ax[1, 0].axis("off")
ax[1, 0].set_title("Head")
ax[1, 1].imshow( ( inputs['shape'].detach().permute(1, 2, 0) * 0.5 ) + 0.5, cmap="gray" )
ax[1, 1].axis("off")
ax[1, 1].set_title("Shape")
ax[1, 2].imshow( ( inputs["pose_image"].detach().permute(1, 2, 0) * 0.5 ) + 0.5, cmap="gray" )
ax[1, 2].axis("off")
ax[1, 2].set_title("Pose Keypoints")
if opt.use_cuda:
agnostic = inputs['agnostic'].cuda()
c = inputs['cloth'].cuda()
cm = inputs['cloth_mask'].cuda()
im_g = inputs['grid_image'].cuda()
else:
agnostic = inputs['agnostic']
c = inputs['cloth']
cm = inputs['cloth_mask']
im_g = inputs['grid_image']
im = inputs['image']
# make batch=1
agnostic.unsqueeze_(0)
c.unsqueeze_(0)
cm.unsqueeze_(0)
im_g.unsqueeze_(0)
# GMM predictions
model = GMM(model_opts)
load_checkpoint(model, pretrained_gmm_path, opt.use_cuda)
with torch.no_grad():
grid, _ = model(agnostic, c)
warped_cloth = F.grid_sample(c, grid, padding_mode='border', align_corners=False)
warped_mask = F.grid_sample(cm, grid, padding_mode='zeros', align_corners=False)
warped_grid = F.grid_sample(im_g, grid, padding_mode='zeros', align_corners=False)
ax[2, 0].imshow( ( warped_cloth.squeeze(0).cpu().detach().permute(1, 2, 0) * 0.5 ) + 0.5 )
ax[2, 0].axis("off")
ax[2, 0].set_title("Warp Cloth")
ax[2, 1].imshow( ( warped_mask.squeeze(0).cpu().detach().permute(1, 2, 0) * 0.5 ) + 0.5, cmap="gray" )
ax[2, 1].axis("off")
ax[2, 1].set_title("Warp Mask")
ax[2, 2].imshow( ( warped_grid.squeeze(0).cpu().detach().permute(1, 2, 0) * 0.5 ) + 0.5 )
ax[2, 2].axis("off")
ax[2, 2].set_title("Warp Grid")
# TOM predictions
model_tom = UnetGenerator(25, 4, 6, ngf=64, norm_layer=nn.InstanceNorm2d)
load_checkpoint(model_tom, pretrained_tom_path, opt.use_cuda)
with torch.no_grad():
outputs = model_tom(torch.cat([agnostic, warped_cloth], 1))
p_rendered, m_composite = torch.split(outputs, 3, 1)
p_rendered = F.tanh(p_rendered)
m_composite = F.sigmoid(m_composite)
p_tryon = warped_cloth * m_composite + p_rendered * (1 - m_composite)
gmm_overlay = warped_cloth + im
gmm_overlay = gmm_overlay / torch.max(gmm_overlay)
ax[3, 0].imshow( ( gmm_overlay.squeeze(0).cpu().detach().permute(1, 2, 0) * 0.5 ) + 0.5 )
ax[3, 0].axis("off")
ax[3, 0].set_title("GMM Overlay")
ax[3, 1].imshow( ( p_rendered.squeeze(0).cpu().detach().permute(1, 2, 0) * 0.5 ) + 0.5 )
ax[3, 1].axis("off")
ax[3, 1].set_title("Render Image")
ax[3, 2].imshow( ( p_tryon.squeeze(0).cpu().detach().permute(1, 2, 0) * 0.5 ) + 0.5 )
ax[3, 2].axis("off")
ax[3, 2].set_title("Try On")
plt.show()
proceed = bool(input("want to continue? "))
if not proceed: break
if __name__ == '__main__':
main()