forked from rlawjdghek/StableVITON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
301 lines (268 loc) · 11.6 KB
/
dataset.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
from os.path import join as opj
import cv2
import numpy as np
import albumentations as A
from torch.utils.data import Dataset
def imread(
p, h, w,
is_mask=False,
in_inverse_mask=False,
img=None
):
if img is None:
img = cv2.imread(p)
if not is_mask:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (w,h))
img = (img.astype(np.float32) / 127.5) - 1.0 # [-1, 1]
else:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (w,h))
img = (img >= 128).astype(np.float32) # 0 or 1
img = img[:,:,None]
if in_inverse_mask:
img = 1-img
return img
def imread_for_albu(
p,
is_mask=False,
in_inverse_mask=False,
cloth_mask_check=False,
use_resize=False,
height=512,
width=384,
):
img = cv2.imread(p)
if use_resize:
img = cv2.resize(img, (width, height))
if not is_mask:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
else:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = (img>=128).astype(np.float32)
if cloth_mask_check:
if img.sum() < 30720*4:
img = np.ones_like(img).astype(np.float32)
if in_inverse_mask:
img = 1 - img
img = np.uint8(img*255.0)
return img
def norm_for_albu(img, is_mask=False):
if not is_mask:
img = (img.astype(np.float32)/127.5) - 1.0
else:
img = img.astype(np.float32) / 255.0
img = img[:,:,None]
return img
class VITONHDDataset(Dataset):
def __init__(
self,
data_root_dir,
img_H,
img_W,
is_paired=True,
is_test=False,
is_sorted=False,
transform_size=None,
transform_color=None,
**kwargs
):
self.drd = data_root_dir
self.img_H = img_H
self.img_W = img_W
self.pair_key = "paired" if is_paired else "unpaired"
self.data_type = "train" if not is_test else "test"
self.is_test = is_test
self.resize_ratio_H = 1.0
self.resize_ratio_W = 1.0
self.resize_transform = A.Resize(img_H, img_W)
self.transform_size = None
self.transform_crop_person = None
self.transform_crop_cloth = None
self.transform_color = None
#### spatial aug >>>>
transform_crop_person_lst = []
transform_crop_cloth_lst = []
transform_size_lst = [A.Resize(int(img_H*self.resize_ratio_H), int(img_W*self.resize_ratio_W))]
if transform_size is not None:
if "hflip" in transform_size:
transform_size_lst.append(A.HorizontalFlip(p=0.5))
if "shiftscale" in transform_size:
transform_crop_person_lst.append(A.ShiftScaleRotate(rotate_limit=0, shift_limit=0.2, scale_limit=(-0.2, 0.2), border_mode=cv2.BORDER_CONSTANT, p=0.5, value=0))
transform_crop_cloth_lst.append(A.ShiftScaleRotate(rotate_limit=0, shift_limit=0.2, scale_limit=(-0.2, 0.2), border_mode=cv2.BORDER_CONSTANT, p=0.5, value=0))
self.transform_crop_person = A.Compose(
transform_crop_person_lst,
additional_targets={"agn":"image",
"agn_mask":"image",
"cloth_mask_warped":"image",
"cloth_warped":"image",
"image_densepose":"image",
"image_parse":"image",
"gt_cloth_warped_mask":"image",
}
)
self.transform_crop_cloth = A.Compose(
transform_crop_cloth_lst,
additional_targets={"cloth_mask":"image"}
)
self.transform_size = A.Compose(
transform_size_lst,
additional_targets={"agn":"image",
"agn_mask":"image",
"cloth":"image",
"cloth_mask":"image",
"cloth_mask_warped":"image",
"cloth_warped":"image",
"image_densepose":"image",
"image_parse":"image",
"gt_cloth_warped_mask":"image",
}
)
#### spatial aug <<<<
#### non-spatial aug >>>>
if transform_color is not None:
transform_color_lst = []
for t in transform_color:
if t == "hsv":
transform_color_lst.append(A.HueSaturationValue(5,5,5,p=0.5))
elif t == "bright_contrast":
transform_color_lst.append(A.RandomBrightnessContrast(brightness_limit=(-0.1, 0.02), contrast_limit=(-0.3, 0.3), p=0.5))
self.transform_color = A.Compose(
transform_color_lst,
additional_targets={"agn":"image",
"cloth":"image",
"cloth_warped":"image",
}
)
#### non-spatial aug <<<<
assert not (self.data_type == "train" and self.pair_key == "unpaired"), f"train must use paired dataset"
im_names = []
c_names = []
with open(opj(self.drd, f"{self.data_type}_pairs.txt"), "r") as f:
for line in f.readlines():
im_name, c_name = line.strip().split()
im_names.append(im_name)
c_names.append(c_name)
if is_sorted:
im_names, c_names = zip(*sorted(zip(im_names, c_names)))
self.im_names = im_names
self.c_names = dict()
self.c_names["paired"] = im_names
self.c_names["unpaired"] = c_names
def __len__(self):
return len(self.im_names)
def __getitem__(self, idx):
img_fn = self.im_names[idx]
cloth_fn = self.c_names[self.pair_key][idx]
if self.transform_size is None and self.transform_color is None:
agn = imread(
opj(self.drd, self.data_type, "agnostic-v3.2", self.im_names[idx]),
self.img_H,
self.img_W
)
agn_mask = imread(
opj(self.drd, self.data_type, "agnostic-mask", self.im_names[idx].replace(".jpg", "_mask.png")),
self.img_H,
self.img_W,
is_mask=True,
in_inverse_mask=True
)
cloth = imread(
opj(self.drd, self.data_type, "cloth", self.c_names[self.pair_key][idx]),
self.img_H,
self.img_W
)
cloth_mask = imread(
opj(self.drd, self.data_type, "cloth-mask", self.c_names[self.pair_key][idx]),
self.img_H,
self.img_W,
is_mask=True,
cloth_mask_check=True
)
gt_cloth_warped_mask = imread(
opj(self.drd, self.data_type, "gt_cloth_warped_mask", self.im_names[idx]),
self.img_H,
self.img_W,
is_mask=True
) if not self.is_test else np.zeros_like(agn_mask)
image = imread(opj(self.drd, self.data_type, "image", self.im_names[idx]), self.img_H, self.img_W)
image_densepose = imread(opj(self.drd, self.data_type, "image-densepose", self.im_names[idx]), self.img_H, self.img_W)
else:
agn = imread_for_albu(opj(self.drd, self.data_type, "agnostic-v3.2", self.im_names[idx]))
agn_mask = imread_for_albu(opj(self.drd, self.data_type, "agnostic-mask", self.im_names[idx].replace(".jpg", "_mask.png")), is_mask=True)
cloth = imread_for_albu(opj(self.drd, self.data_type, "cloth", self.c_names[self.pair_key][idx]))
cloth_mask = imread_for_albu(opj(self.drd, self.data_type, "cloth-mask", self.c_names[self.pair_key][idx]), is_mask=True, cloth_mask_check=True)
gt_cloth_warped_mask = imread_for_albu(
opj(self.drd, self.data_type, "gt_cloth_warped_mask", self.im_names[idx]),
is_mask=True
) if not self.is_test else np.zeros_like(agn_mask)
image = imread_for_albu(opj(self.drd, self.data_type, "image", self.im_names[idx]))
image_densepose = imread_for_albu(opj(self.drd, self.data_type, "image-densepose", self.im_names[idx]))
if self.transform_size is not None:
transformed = self.transform_size(
image=image,
agn=agn,
agn_mask=agn_mask,
cloth=cloth,
cloth_mask=cloth_mask,
image_densepose=image_densepose,
gt_cloth_warped_mask=gt_cloth_warped_mask,
)
image=transformed["image"]
agn=transformed["agn"]
agn_mask=transformed["agn_mask"]
image_densepose=transformed["image_densepose"]
gt_cloth_warped_mask=transformed["gt_cloth_warped_mask"]
cloth=transformed["cloth"]
cloth_mask=transformed["cloth_mask"]
if self.transform_crop_person is not None:
transformed_image = self.transform_crop_person(
image=image,
agn=agn,
agn_mask=agn_mask,
image_densepose=image_densepose,
gt_cloth_warped_mask=gt_cloth_warped_mask,
)
image=transformed_image["image"]
agn=transformed_image["agn"]
agn_mask=transformed_image["agn_mask"]
image_densepose=transformed_image["image_densepose"]
gt_cloth_warped_mask=transformed["gt_cloth_warped_mask"]
if self.transform_crop_cloth is not None:
transformed_cloth = self.transform_crop_cloth(
image=cloth,
cloth_mask=cloth_mask
)
cloth=transformed_cloth["image"]
cloth_mask=transformed_cloth["cloth_mask"]
agn_mask = 255 - agn_mask
if self.transform_color is not None:
transformed = self.transform_color(
image=image,
agn=agn,
cloth=cloth,
)
image=transformed["image"]
agn=transformed["agn"]
cloth=transformed["cloth"]
agn = agn * agn_mask[:,:,None].astype(np.float32)/255.0 + 128 * (1 - agn_mask[:,:,None].astype(np.float32)/255.0)
agn = norm_for_albu(agn)
agn_mask = norm_for_albu(agn_mask, is_mask=True)
cloth = norm_for_albu(cloth)
cloth_mask = norm_for_albu(cloth_mask, is_mask=True)
image = norm_for_albu(image)
image_densepose = norm_for_albu(image_densepose)
gt_cloth_warped_mask = norm_for_albu(gt_cloth_warped_mask, is_mask=True)
return dict(
agn=agn,
agn_mask=agn_mask,
cloth=cloth,
cloth_mask=cloth_mask,
image=image,
image_densepose=image_densepose,
gt_cloth_warped_mask=gt_cloth_warped_mask,
txt="",
img_fn=img_fn,
cloth_fn=cloth_fn,
)