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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.utils.data as data
|
| 3 |
+
import torchvision.transforms as transforms
|
| 4 |
+
|
| 5 |
+
from PIL import Image, ImageDraw
|
| 6 |
+
|
| 7 |
+
import os.path as osp
|
| 8 |
+
import numpy as np
|
| 9 |
+
import json
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class CPDatasetTest(data.Dataset):
|
| 13 |
+
"""
|
| 14 |
+
Test Dataset for CP-VTON.
|
| 15 |
+
"""
|
| 16 |
+
def __init__(self, opt):
|
| 17 |
+
super(CPDatasetTest, self).__init__()
|
| 18 |
+
# base setting
|
| 19 |
+
self.opt = opt
|
| 20 |
+
self.root = opt.dataroot
|
| 21 |
+
self.datamode = opt.datamode # train or test or self-defined
|
| 22 |
+
self.data_list = opt.data_list
|
| 23 |
+
self.fine_height = opt.fine_height
|
| 24 |
+
self.fine_width = opt.fine_width
|
| 25 |
+
self.semantic_nc = opt.semantic_nc
|
| 26 |
+
self.data_path = osp.join(opt.dataroot, opt.datamode)
|
| 27 |
+
self.transform = transforms.Compose([ \
|
| 28 |
+
transforms.ToTensor(), \
|
| 29 |
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
|
| 30 |
+
|
| 31 |
+
# load data list
|
| 32 |
+
im_names = []
|
| 33 |
+
c_names = []
|
| 34 |
+
with open(osp.join(opt.dataroot, opt.data_list), 'r') as f:
|
| 35 |
+
for line in f.readlines():
|
| 36 |
+
im_name, c_name = line.strip().split()
|
| 37 |
+
im_names.append(im_name)
|
| 38 |
+
c_names.append(c_name)
|
| 39 |
+
|
| 40 |
+
self.im_names = im_names
|
| 41 |
+
self.c_names = dict()
|
| 42 |
+
self.c_names['paired'] = im_names
|
| 43 |
+
self.c_names['unpaired'] = c_names
|
| 44 |
+
|
| 45 |
+
def name(self):
|
| 46 |
+
return "CPDataset"
|
| 47 |
+
|
| 48 |
+
def get_agnostic(self, im, im_parse, pose_data):
|
| 49 |
+
parse_array = np.array(im_parse)
|
| 50 |
+
parse_head = ((parse_array == 4).astype(np.float32) +
|
| 51 |
+
(parse_array == 13).astype(np.float32))
|
| 52 |
+
parse_lower = ((parse_array == 9).astype(np.float32) +
|
| 53 |
+
(parse_array == 12).astype(np.float32) +
|
| 54 |
+
(parse_array == 16).astype(np.float32) +
|
| 55 |
+
(parse_array == 17).astype(np.float32) +
|
| 56 |
+
(parse_array == 18).astype(np.float32) +
|
| 57 |
+
(parse_array == 19).astype(np.float32))
|
| 58 |
+
|
| 59 |
+
agnostic = im.copy()
|
| 60 |
+
agnostic_draw = ImageDraw.Draw(agnostic)
|
| 61 |
+
|
| 62 |
+
length_a = np.linalg.norm(pose_data[5] - pose_data[2])
|
| 63 |
+
length_b = np.linalg.norm(pose_data[12] - pose_data[9])
|
| 64 |
+
point = (pose_data[9] + pose_data[12]) / 2
|
| 65 |
+
pose_data[9] = point + (pose_data[9] - point) / length_b * length_a
|
| 66 |
+
pose_data[12] = point + (pose_data[12] - point) / length_b * length_a
|
| 67 |
+
|
| 68 |
+
r = int(length_a / 16) + 1
|
| 69 |
+
|
| 70 |
+
# mask torso
|
| 71 |
+
for i in [9, 12]:
|
| 72 |
+
pointx, pointy = pose_data[i]
|
| 73 |
+
agnostic_draw.ellipse((pointx-r*3, pointy-r*6, pointx+r*3, pointy+r*6), 'gray', 'gray')
|
| 74 |
+
agnostic_draw.line([tuple(pose_data[i]) for i in [2, 9]], 'gray', width=r*6)
|
| 75 |
+
agnostic_draw.line([tuple(pose_data[i]) for i in [5, 12]], 'gray', width=r*6)
|
| 76 |
+
agnostic_draw.line([tuple(pose_data[i]) for i in [9, 12]], 'gray', width=r*12)
|
| 77 |
+
agnostic_draw.polygon([tuple(pose_data[i]) for i in [2, 5, 12, 9]], 'gray', 'gray')
|
| 78 |
+
|
| 79 |
+
# mask neck
|
| 80 |
+
pointx, pointy = pose_data[1]
|
| 81 |
+
agnostic_draw.rectangle((pointx-r*5, pointy-r*9, pointx+r*5, pointy), 'gray', 'gray')
|
| 82 |
+
|
| 83 |
+
# mask arms
|
| 84 |
+
agnostic_draw.line([tuple(pose_data[i]) for i in [2, 5]], 'gray', width=r*12)
|
| 85 |
+
for i in [2, 5]:
|
| 86 |
+
pointx, pointy = pose_data[i]
|
| 87 |
+
agnostic_draw.ellipse((pointx-r*5, pointy-r*6, pointx+r*5, pointy+r*6), 'gray', 'gray')
|
| 88 |
+
for i in [3, 4, 6, 7]:
|
| 89 |
+
if (pose_data[i-1, 0] == 0.0 and pose_data[i-1, 1] == 0.0) or (pose_data[i, 0] == 0.0 and pose_data[i, 1] == 0.0):
|
| 90 |
+
continue
|
| 91 |
+
agnostic_draw.line([tuple(pose_data[j]) for j in [i - 1, i]], 'gray', width=r*10)
|
| 92 |
+
pointx, pointy = pose_data[i]
|
| 93 |
+
agnostic_draw.ellipse((pointx-r*5, pointy-r*5, pointx+r*5, pointy+r*5), 'gray', 'gray')
|
| 94 |
+
|
| 95 |
+
for parse_id, pose_ids in [(14, [5, 6, 7]), (15, [2, 3, 4])]:
|
| 96 |
+
mask_arm = Image.new('L', (768, 1024), 'white')
|
| 97 |
+
mask_arm_draw = ImageDraw.Draw(mask_arm)
|
| 98 |
+
pointx, pointy = pose_data[pose_ids[0]]
|
| 99 |
+
mask_arm_draw.ellipse((pointx-r*5, pointy-r*6, pointx+r*5, pointy+r*6), 'black', 'black')
|
| 100 |
+
for i in pose_ids[1:]:
|
| 101 |
+
if (pose_data[i-1, 0] == 0.0 and pose_data[i-1, 1] == 0.0) or (pose_data[i, 0] == 0.0 and pose_data[i, 1] == 0.0):
|
| 102 |
+
continue
|
| 103 |
+
mask_arm_draw.line([tuple(pose_data[j]) for j in [i - 1, i]], 'black', width=r*10)
|
| 104 |
+
pointx, pointy = pose_data[i]
|
| 105 |
+
if i != pose_ids[-1]:
|
| 106 |
+
mask_arm_draw.ellipse((pointx-r*5, pointy-r*5, pointx+r*5, pointy+r*5), 'black', 'black')
|
| 107 |
+
mask_arm_draw.ellipse((pointx-r*4, pointy-r*4, pointx+r*4, pointy+r*4), 'black', 'black')
|
| 108 |
+
|
| 109 |
+
parse_arm = (np.array(mask_arm) / 255) * (parse_array == parse_id).astype(np.float32)
|
| 110 |
+
agnostic.paste(im, None, Image.fromarray(np.uint8(parse_arm * 255), 'L'))
|
| 111 |
+
|
| 112 |
+
agnostic.paste(im, None, Image.fromarray(np.uint8(parse_head * 255), 'L'))
|
| 113 |
+
agnostic.paste(im, None, Image.fromarray(np.uint8(parse_lower * 255), 'L'))
|
| 114 |
+
return agnostic
|
| 115 |
+
|
| 116 |
+
def __getitem__(self, index):
|
| 117 |
+
im_name = self.im_names[index]
|
| 118 |
+
c_name = {}
|
| 119 |
+
c = {}
|
| 120 |
+
cm = {}
|
| 121 |
+
for key in self.c_names:
|
| 122 |
+
c_name[key] = self.c_names[key][index]
|
| 123 |
+
c[key] = Image.open(osp.join(self.data_path, 'cloth', c_name[key])).convert('RGB')
|
| 124 |
+
c[key] = transforms.Resize(self.fine_width, interpolation=2)(c[key])
|
| 125 |
+
cm[key] = Image.open(osp.join(self.data_path, 'cloth-mask', c_name[key]))
|
| 126 |
+
cm[key] = transforms.Resize(self.fine_width, interpolation=0)(cm[key])
|
| 127 |
+
|
| 128 |
+
c[key] = self.transform(c[key]) # [-1,1]
|
| 129 |
+
cm_array = np.array(cm[key])
|
| 130 |
+
cm_array = (cm_array >= 128).astype(np.float32)
|
| 131 |
+
cm[key] = torch.from_numpy(cm_array) # [0,1]
|
| 132 |
+
cm[key].unsqueeze_(0)
|
| 133 |
+
|
| 134 |
+
# person image
|
| 135 |
+
im_pil_big = Image.open(osp.join(self.data_path, 'image', im_name))
|
| 136 |
+
im_pil = transforms.Resize(self.fine_width, interpolation=2)(im_pil_big)
|
| 137 |
+
|
| 138 |
+
im = self.transform(im_pil)
|
| 139 |
+
|
| 140 |
+
# load parsing image
|
| 141 |
+
parse_name = im_name.replace('.jpg', '.png')
|
| 142 |
+
im_parse_pil_big = Image.open(osp.join(self.data_path, 'image-parse-v3', parse_name))
|
| 143 |
+
im_parse_pil = transforms.Resize(self.fine_width, interpolation=0)(im_parse_pil_big)
|
| 144 |
+
parse = torch.from_numpy(np.array(im_parse_pil)[None]).long()
|
| 145 |
+
im_parse = self.transform(im_parse_pil.convert('RGB'))
|
| 146 |
+
|
| 147 |
+
labels = {
|
| 148 |
+
0: ['background', [0, 10]],
|
| 149 |
+
1: ['hair', [1, 2]],
|
| 150 |
+
2: ['face', [4, 13]],
|
| 151 |
+
3: ['upper', [5, 6, 7]],
|
| 152 |
+
4: ['bottom', [9, 12]],
|
| 153 |
+
5: ['left_arm', [14]],
|
| 154 |
+
6: ['right_arm', [15]],
|
| 155 |
+
7: ['left_leg', [16]],
|
| 156 |
+
8: ['right_leg', [17]],
|
| 157 |
+
9: ['left_shoe', [18]],
|
| 158 |
+
10: ['right_shoe', [19]],
|
| 159 |
+
11: ['socks', [8]],
|
| 160 |
+
12: ['noise', [3, 11]]
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
parse_map = torch.FloatTensor(20, self.fine_height, self.fine_width).zero_()
|
| 164 |
+
parse_map = parse_map.scatter_(0, parse, 1.0)
|
| 165 |
+
new_parse_map = torch.FloatTensor(self.semantic_nc, self.fine_height, self.fine_width).zero_()
|
| 166 |
+
|
| 167 |
+
for i in range(len(labels)):
|
| 168 |
+
for label in labels[i][1]:
|
| 169 |
+
new_parse_map[i] += parse_map[label]
|
| 170 |
+
|
| 171 |
+
parse_onehot = torch.FloatTensor(1, self.fine_height, self.fine_width).zero_()
|
| 172 |
+
for i in range(len(labels)):
|
| 173 |
+
for label in labels[i][1]:
|
| 174 |
+
parse_onehot[0] += parse_map[label] * i
|
| 175 |
+
|
| 176 |
+
# load image-parse-agnostic
|
| 177 |
+
image_parse_agnostic = Image.open(osp.join(self.data_path, 'image-parse-agnostic-v3.2', parse_name))
|
| 178 |
+
image_parse_agnostic = transforms.Resize(self.fine_width, interpolation=0)(image_parse_agnostic)
|
| 179 |
+
parse_agnostic = torch.from_numpy(np.array(image_parse_agnostic)[None]).long()
|
| 180 |
+
image_parse_agnostic = self.transform(image_parse_agnostic.convert('RGB'))
|
| 181 |
+
|
| 182 |
+
parse_agnostic_map = torch.FloatTensor(20, self.fine_height, self.fine_width).zero_()
|
| 183 |
+
parse_agnostic_map = parse_agnostic_map.scatter_(0, parse_agnostic, 1.0)
|
| 184 |
+
new_parse_agnostic_map = torch.FloatTensor(self.semantic_nc, self.fine_height, self.fine_width).zero_()
|
| 185 |
+
for i in range(len(labels)):
|
| 186 |
+
for label in labels[i][1]:
|
| 187 |
+
new_parse_agnostic_map[i] += parse_agnostic_map[label]
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
# parse cloth & parse cloth mask
|
| 191 |
+
pcm = new_parse_map[3:4]
|
| 192 |
+
im_c = im * pcm + (1 - pcm)
|
| 193 |
+
|
| 194 |
+
# load pose points
|
| 195 |
+
pose_name = im_name.replace('.jpg', '_rendered.png')
|
| 196 |
+
pose_map = Image.open(osp.join(self.data_path, 'openpose_img', pose_name))
|
| 197 |
+
pose_map = transforms.Resize(self.fine_width, interpolation=2)(pose_map)
|
| 198 |
+
pose_map = self.transform(pose_map) # [-1,1]
|
| 199 |
+
|
| 200 |
+
pose_name = im_name.replace('.jpg', '_keypoints.json')
|
| 201 |
+
with open(osp.join(self.data_path, 'openpose_json', pose_name), 'r') as f:
|
| 202 |
+
pose_label = json.load(f)
|
| 203 |
+
pose_data = pose_label['people'][0]['pose_keypoints_2d']
|
| 204 |
+
pose_data = np.array(pose_data)
|
| 205 |
+
pose_data = pose_data.reshape((-1, 3))[:, :2]
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
# load densepose
|
| 209 |
+
densepose_name = im_name.replace('image', 'image-densepose')
|
| 210 |
+
densepose_map = Image.open(osp.join(self.data_path, 'image-densepose', densepose_name))
|
| 211 |
+
densepose_map = transforms.Resize(self.fine_width, interpolation=2)(densepose_map)
|
| 212 |
+
densepose_map = self.transform(densepose_map) # [-1,1]
|
| 213 |
+
agnostic = self.get_agnostic(im_pil_big, im_parse_pil_big, pose_data)
|
| 214 |
+
agnostic = transforms.Resize(self.fine_width, interpolation=2)(agnostic)
|
| 215 |
+
agnostic = self.transform(agnostic)
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
result = {
|
| 220 |
+
'c_name': c_name, # for visualization
|
| 221 |
+
'im_name': im_name, # for visualization or ground truth
|
| 222 |
+
# intput 1 (clothfloww)
|
| 223 |
+
'cloth': c, # for input
|
| 224 |
+
'cloth_mask': cm, # for input
|
| 225 |
+
# intput 2 (segnet)
|
| 226 |
+
'parse_agnostic': new_parse_agnostic_map,
|
| 227 |
+
'densepose': densepose_map,
|
| 228 |
+
'pose': pose_map, # for conditioning
|
| 229 |
+
# GT
|
| 230 |
+
'parse_onehot' : parse_onehot, # Cross Entropy
|
| 231 |
+
'parse': new_parse_map, # GAN Loss real
|
| 232 |
+
'pcm': pcm, # L1 Loss & vis
|
| 233 |
+
'parse_cloth': im_c, # VGG Loss & vis
|
| 234 |
+
# visualization
|
| 235 |
+
'image': im, # for visualization
|
| 236 |
+
'agnostic' : agnostic
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
return result
|
| 240 |
+
|
| 241 |
+
def __len__(self):
|
| 242 |
+
return len(self.im_names)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
class CPDataLoader(object):
|
| 246 |
+
def __init__(self, opt, dataset):
|
| 247 |
+
super(CPDataLoader, self).__init__()
|
| 248 |
+
if opt.shuffle :
|
| 249 |
+
train_sampler = torch.utils.data.sampler.RandomSampler(dataset)
|
| 250 |
+
else:
|
| 251 |
+
train_sampler = None
|
| 252 |
+
|
| 253 |
+
self.data_loader = torch.utils.data.DataLoader(
|
| 254 |
+
dataset, batch_size=opt.batch_size, shuffle=(train_sampler is None),
|
| 255 |
+
num_workers=opt.workers, pin_memory=True, drop_last=True, sampler=train_sampler)
|
| 256 |
+
self.dataset = dataset
|
| 257 |
+
self.data_iter = self.data_loader.__iter__()
|
| 258 |
+
|
| 259 |
+
def next_batch(self):
|
| 260 |
+
try:
|
| 261 |
+
batch = self.data_iter.__next__()
|
| 262 |
+
except StopIteration:
|
| 263 |
+
self.data_iter = self.data_loader.__iter__()
|
| 264 |
+
batch = self.data_iter.__next__()
|
| 265 |
+
|
| 266 |
+
return batch
|