-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_utils.py
More file actions
427 lines (355 loc) · 17.6 KB
/
data_utils.py
File metadata and controls
427 lines (355 loc) · 17.6 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import json
from pathlib import Path
from typing_extensions import Literal
from typing import Union, List, Dict
import PIL
import PIL.Image
import torchvision.transforms.functional as F
from torch.utils.data import Dataset
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize
import torch
base_path = Path(__file__).absolute().parents[1].absolute()
def collate_fn(batch):
'''
function which discard None images in a batch when using torch DataLoader
:param batch: input_batch
:return: output_batch = input_batch - None_values
'''
batch = list(filter(lambda x: x is not None, batch))
return torch.utils.data.dataloader.default_collate(batch)
def _convert_image_to_rgb(image):
return image.convert("RGB")
class SquarePad:
"""
Square pad the input image with zero padding
"""
def __init__(self, size: int):
"""
For having a consistent preprocess pipeline with CLIP we need to have the preprocessing output dimension as
a parameter
:param size: preprocessing output dimension
"""
self.size = size
def __call__(self, image):
w, h = image.size
max_wh = max(w, h)
hp = int((max_wh - w) / 2)
vp = int((max_wh - h) / 2)
padding = [hp, vp, hp, vp]
return F.pad(image, padding, 0, 'constant')
class TargetPad:
"""
Pad the image if its aspect ratio is above a target ratio.
Pad the image to match such target ratio
"""
def __init__(self, target_ratio: float, size: int):
"""
:param target_ratio: target ratio
:param size: preprocessing output dimension
"""
self.size = size
self.target_ratio = target_ratio
def __call__(self, image):
w, h = image.size
actual_ratio = max(w, h) / min(w, h)
if actual_ratio < self.target_ratio: # check if the ratio is above or below the target ratio
return image
scaled_max_wh = max(w, h) / self.target_ratio # rescale the pad to match the target ratio
hp = max(int((scaled_max_wh - w) / 2), 0)
vp = max(int((scaled_max_wh - h) / 2), 0)
padding = [hp, vp, hp, vp]
return F.pad(image, padding, 0, 'constant')
def squarepad_transform(dim: int):
"""
CLIP-like preprocessing transform on a square padded image
:param dim: image output dimension
:return: CLIP-like torchvision Compose transform
"""
return Compose([
SquarePad(dim),
Resize(dim, interpolation=PIL.Image.BICUBIC),
CenterCrop(dim),
_convert_image_to_rgb,
ToTensor(),
Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
])
def targetpad_transform(target_ratio: float, dim: int):
"""
CLIP-like preprocessing transform computed after using TargetPad pad
:param target_ratio: target ratio for TargetPad
:param dim: image output dimension
:return: CLIP-like torchvision Compose transform
"""
return Compose([
TargetPad(target_ratio, dim),
Resize(dim, interpolation=PIL.Image.BICUBIC),
CenterCrop(dim),
_convert_image_to_rgb,
ToTensor(),
Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
])
class FashionIQDataset(Dataset):
"""
FashionIQ dataset class which manage FashionIQ data.
The dataset can be used in 'relative' or 'classic' mode:
- In 'classic' mode the dataset yield tuples made of (image_name, image)
- In 'relative' mode the dataset yield tuples made of:
- (reference_image, target_image, image_captions) when split == train
- (reference_name, target_name, image_captions) when split == val
- (reference_name, reference_image, image_captions) when split == test
The dataset manage an arbitrary numbers of FashionIQ category, e.g. only dress, dress+toptee+shirt, dress+shirt...
"""
def __init__(self, split: str, dress_types: List[str], mode: str, preprocess: callable):
"""
:param split: dataset split, should be in ['test', 'train', 'val']
:param dress_types: list of fashionIQ category
:param mode: dataset mode, should be in ['relative', 'classic']:
- In 'classic' mode the dataset yield tuples made of (image_name, image)
- In 'relative' mode the dataset yield tuples made of:
- (reference_image, target_image, image_captions) when split == train
- (reference_name, target_name, image_captions) when split == val
- (reference_name, reference_image, image_captions) when split == test
:param preprocess: function which preprocesses the image
"""
self.mode = mode
self.dress_types = dress_types
self.split = split
if mode not in ['relative', 'classic']:
raise ValueError("mode should be in ['relative', 'classic']")
if split not in ['test', 'train', 'val']:
raise ValueError("split should be in ['test', 'train', 'val']")
for dress_type in dress_types:
if dress_type not in ['dress', 'shirt', 'toptee']:
raise ValueError("dress_type should be in ['dress', 'shirt', 'toptee']")
self.preprocess = preprocess
# get triplets made by (reference_image, target_image, a pair of relative captions)
self.triplets: List[dict] = []
for dress_type in dress_types:
with open(base_path / 'fashionIQ_dataset' / 'captions' / f'cap.{dress_type}.{split}.json') as f:
self.triplets.extend(json.load(f))
# get the image names
self.image_names: list = []
for dress_type in dress_types:
with open(base_path / 'fashionIQ_dataset' / 'image_splits' / f'split.{dress_type}.{split}.json') as f:
self.image_names.extend(json.load(f))
print(f"FashionIQ {split} - {dress_types} dataset in {mode} mode initialized")
def __getitem__(self, index):
try:
if self.mode == 'relative':
image_captions = self.triplets[index]['captions']
reference_name = self.triplets[index]['candidate']
if self.split == 'train':
reference_image_path = base_path / 'fashionIQ_dataset' / 'images' / f"{reference_name}.jpg"
reference_image = self.preprocess(PIL.Image.open(reference_image_path))
target_name = self.triplets[index]['target']
target_image_path = base_path / 'fashionIQ_dataset' / 'images' / f"{target_name}.jpg"
target_image = self.preprocess(PIL.Image.open(target_image_path))
return reference_image, target_image, image_captions
elif self.split == 'val':
target_name = self.triplets[index]['target']
return reference_name, target_name, image_captions
elif self.split == 'test':
reference_image_path = base_path / 'fashionIQ_dataset' / 'images' / f"{reference_name}.jpg"
reference_image = self.preprocess(PIL.Image.open(reference_image_path))
return reference_name, reference_image, image_captions
elif self.mode == 'classic':
image_name = self.image_names[index]
image_path = base_path / 'fashionIQ_dataset' / 'images' / f"{image_name}.jpg"
image = self.preprocess(PIL.Image.open(image_path))
return image_name, image
else:
raise ValueError("mode should be in ['relative', 'classic']")
except Exception as e:
print(f"Exception: {e}")
def __len__(self):
if self.mode == 'relative':
return len(self.triplets)
elif self.mode == 'classic':
return len(self.image_names)
else:
raise ValueError("mode should be in ['relative', 'classic']")
class CIRRDataset(Dataset):
"""
CIRR dataset class which manage CIRR data
The dataset can be used in 'relative' or 'classic' mode:
- In 'classic' mode the dataset yield tuples made of (image_name, image)
- In 'relative' mode the dataset yield tuples made of:
- (reference_image, target_image, rel_caption) when split == train
- (reference_name, target_name, rel_caption, group_members) when split == val
- (pair_id, reference_name, rel_caption, group_members) when split == test1
"""
def __init__(self, split: str, mode: str, preprocess: callable):
"""
:param split: dataset split, should be in ['test', 'train', 'val']
:param mode: dataset mode, should be in ['relative', 'classic']:
- In 'classic' mode the dataset yield tuples made of (image_name, image)
- In 'relative' mode the dataset yield tuples made of:
- (reference_image, target_image, rel_caption) when split == train
- (reference_name, target_name, rel_caption, group_members) when split == val
- (pair_id, reference_name, rel_caption, group_members) when split == test1
:param preprocess: function which preprocesses the image
"""
self.preprocess = preprocess
self.mode = mode
self.split = split
if split not in ['test1', 'train', 'val']:
raise ValueError("split should be in ['test1', 'train', 'val']")
if mode not in ['relative', 'classic']:
raise ValueError("mode should be in ['relative', 'classic']")
# get triplets made by (reference_image, target_image, relative caption)
with open(base_path / 'cirr_dataset' / 'cirr' / 'captions' / f'cap.rc2.{split}.json') as f:
self.triplets = json.load(f)
# get a mapping from image name to relative path
with open(base_path / 'cirr_dataset' / 'cirr' / 'image_splits' / f'split.rc2.{split}.json') as f:
self.name_to_relpath = json.load(f)
print(f"CIRR {split} dataset in {mode} mode initialized")
def __getitem__(self, index):
try:
if self.mode == 'relative':
group_members = self.triplets[index]['img_set']['members']
reference_name = self.triplets[index]['reference']
rel_caption = self.triplets[index]['caption']
if self.split == 'train':
reference_image_path = base_path / 'cirr_dataset' / self.name_to_relpath[reference_name]
reference_image = self.preprocess(PIL.Image.open(reference_image_path))
target_hard_name = self.triplets[index]['target_hard']
target_image_path = base_path / 'cirr_dataset' / self.name_to_relpath[target_hard_name]
target_image = self.preprocess(PIL.Image.open(target_image_path))
return reference_image, target_image, rel_caption
elif self.split == 'val':
target_hard_name = self.triplets[index]['target_hard']
return reference_name, target_hard_name, rel_caption, group_members
elif self.split == 'test1':
pair_id = self.triplets[index]['pairid']
return pair_id, reference_name, rel_caption, group_members
elif self.mode == 'classic':
image_name = list(self.name_to_relpath.keys())[index]
image_path = base_path / 'cirr_dataset' / self.name_to_relpath[image_name]
im = PIL.Image.open(image_path)
image = self.preprocess(im)
return image_name, image
else:
raise ValueError("mode should be in ['relative', 'classic']")
except Exception as e:
print(f"Exception: {e}")
def __len__(self):
if self.mode == 'relative':
return len(self.triplets)
elif self.mode == 'classic':
return len(self.name_to_relpath)
else:
raise ValueError("mode should be in ['relative', 'classic']")
class CIRCODataset(Dataset):
"""
CIRCO dataset
"""
def __init__(self, data_path: Union[str, Path], split: Literal['val', 'test'],
mode: Literal['relative', 'classic'], preprocess: callable):
"""
Args:
data_path (Union[str, Path]): path to CIRCO dataset
split (str): dataset split, should be in ['test', 'val']
mode (str): dataset mode, should be in ['relative', 'classic']
preprocess (callable): function which preprocesses the image
"""
# Set dataset paths and configurations
data_path = Path(data_path)
self.mode = mode
self.split = split
self.preprocess = preprocess
self.data_path = data_path
# Ensure input arguments are valid
if mode not in ['relative', 'classic']:
raise ValueError("mode should be in ['relative', 'classic']")
if split not in ['test', 'val']:
raise ValueError("split should be in ['test', 'val']")
# Load COCO images information
with open(data_path / 'COCO2017_unlabeled' / "annotations" / "image_info_unlabeled2017.json", "r") as f:
imgs_info = json.load(f)
self.img_paths = [data_path / 'COCO2017_unlabeled' / "unlabeled2017" / img_info["file_name"] for img_info in
imgs_info["images"]]
self.img_ids = [img_info["id"] for img_info in imgs_info["images"]]
self.img_ids_indexes_map = {str(img_id): i for i, img_id in enumerate(self.img_ids)}
# get CIRCO annotations
with open(data_path / 'annotations' / f'{split}.json', "r") as f:
self.annotations: List[dict] = json.load(f)
# Get maximum number of ground truth images (for padding when loading the images)
self.max_num_gts = 23 # Maximum number of ground truth images
print(f"CIRCODataset {split} dataset in {mode} mode initialized")
def get_target_img_ids(self, index) -> Dict[str, int]:
"""
Returns the id of the target image and ground truth images for a given query
Args:
index (int): id of the query
Returns:
Dict[str, int]: dictionary containing target image id and a list of ground truth image ids
"""
return {
'target_img_id': self.annotations[index]['target_img_id'],
'gt_img_ids': self.annotations[index]['gt_img_ids']
}
def __getitem__(self, index) -> dict:
"""
Returns a specific item from the dataset based on the index.
In 'classic' mode, the dataset yields a dictionary with the following keys: [img, img_id]
In 'relative' mode, the dataset yields dictionaries with the following keys:
- [reference_img, reference_img_id, target_img, target_img_id, relative_caption, shared_concept, gt_img_ids,
query_id] if split == val
- [reference_img, reference_img_id, relative_caption, shared_concept, query_id] if split == test
"""
if self.mode == 'relative':
# Get the query id
query_id = str(self.annotations[index]['id'])
# Get relative caption and shared concept
relative_caption = self.annotations[index]['relative_caption']
shared_concept = self.annotations[index]['shared_concept']
# Get the reference image
reference_img_id = str(self.annotations[index]['reference_img_id'])
reference_img_path = self.img_paths[self.img_ids_indexes_map[reference_img_id]]
reference_img = self.preprocess(PIL.Image.open(reference_img_path))
if self.split == 'val':
# Get the target image and ground truth images
target_img_id = str(self.annotations[index]['target_img_id'])
gt_img_ids = [str(x) for x in self.annotations[index]['gt_img_ids']]
target_img_path = self.img_paths[self.img_ids_indexes_map[target_img_id]]
target_img = self.preprocess(PIL.Image.open(target_img_path))
# Pad ground truth image IDs with zeros for collate_fn
gt_img_ids += [''] * (self.max_num_gts - len(gt_img_ids))
return {
'reference_img': reference_img,
'reference_imd_id': reference_img_id,
'target_img': target_img,
'target_img_id': target_img_id,
'relative_caption': relative_caption,
'shared_concept': shared_concept,
'gt_img_ids': gt_img_ids,
'query_id': query_id,
}
elif self.split == 'test':
return {
'reference_img': reference_img,
'reference_imd_id': reference_img_id,
'relative_caption': relative_caption,
'shared_concept': shared_concept,
'query_id': query_id,
}
elif self.mode == 'classic':
# Get image ID and image path
img_id = str(self.img_ids[index])
img_path = self.img_paths[index]
# Preprocess image and return
img = self.preprocess(PIL.Image.open(img_path))
return {
'img': img,
'img_id': img_id
}
def __len__(self):
"""
Returns the length of the dataset.
"""
if self.mode == 'relative':
return len(self.annotations)
elif self.mode == 'classic':
return len(self.img_ids)
else:
raise ValueError("mode should be in ['relative', 'classic']")