-
Notifications
You must be signed in to change notification settings - Fork 5
/
datasets.py
375 lines (299 loc) · 13.1 KB
/
datasets.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
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
from collections import defaultdict
import json
import os
import io
import pickle
import zipfile
import numpy as np
import braceexpand
import base64
from PIL import Image, ImageFile
import torch
from torchvision import transforms
from torchvision import datasets as t_datasets
from torchvision.transforms import functional as F
import utils
ImageFile.LOAD_TRUNCATED_IMAGES = True
def pil_loader(path):
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
if path[-1] == 'p':
path = path + 'g'
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def yfcc_loader(root, index):
index = format(index, "0>8d")
repo = index[:2]
z = index[2: 5]
file_img = index[5:] + '.jpg'
path_zip = os.path.join(root, 'images', repo, z) + '.zip'
with zipfile.ZipFile(path_zip, 'r') as myzip:
img = Image.open(myzip.open(file_img))
return img.convert('RGB')
class ZipDataset(torch.utils.data.Dataset):
def __init__(self,zip_path,txt_path,transform=None):
self.zip_path = zip_path
self.transforms = transform
self.zip_handle = None
self.txtlines = []
with open(txt_path,'r') as f:
self.txtlines = f.readlines()
def __getitem__(self,idx):
line = self.txtlines[idx]
name,label = line.split()
label = int(label)
if self.zip_handle is None:
self.zip_handle = zipfile.ZipFile(self.zip_path,'r')
image = Image.open(io.BytesIO(self.zip_handle.read(name)))
image = image.convert('RGB')
if self.transforms is not None:
image = self.transforms(image)
return image,label
def __len__(self):
return len(self.txtlines)
class ImageCaptionDatasetBase(torch.utils.data.Dataset):
def __init__(self, dataset, root, metadata):
self.dataset = dataset
self.root = root
if self.dataset == 'yfcc15m':
#For cluster
input_filename = os.path.join(self.root, 'train-image-{00..012}.tsv')
# input_filename = os.path.join(self.root, 'train-image-{00..001}.tsv')
img_data_plist = list(braceexpand.braceexpand(input_filename))
caption_list = [img_data_p.replace('tsv', 'json').replace('image', 'annos-2caps') for img_data_p in img_data_plist]
samples = []
for cpath in caption_list:
with open(cpath,'r') as f:
js_list = json.loads(f.readlines()[0])
samples.extend(js_list)
self.samples = samples
self.img_data_plist = img_data_plist
# with open(metadata, 'rb') as f:
# self.samples = pickle.load(f)
elif self.dataset == 'coco':
samples = defaultdict(list)
with open(metadata) as f:
annotations = json.load(f)['annotations']
for ann in annotations:
samples[ann['image_id']].append(ann['caption'])
self.samples = [(k, v) for k, v in samples.items()]
elif self.dataset == 'cc12m' or self.dataset == 'cc3m':
self.samples = np.load(metadata, allow_pickle=True)
elif self.dataset == 'redcaps':
with open(metadata) as f:
annotations = json.load(f)
self.samples = [(ann['image_id'], ann['subreddit'], ann['caption']) for ann in annotations]
def get_raw_item(self, i):
if self.dataset == 'yfcc15m':
# for cluster
#--------------------------------
dic = self.samples[i]
shard_idx = dic['img_location']
lineidx_ptr = int(dic['lineidx_ptr'])
img_path = self.img_data_plist[shard_idx]
with open(img_path,'r') as f:
f.seek(lineidx_ptr,0)
img_line = f.readline()
_, img_str = img_line.split('\t')
captions = dic['caption']
np.random.shuffle(captions)
if captions[0]:
caption = captions[0]
else:
caption = captions[1]
image = base64.b64decode(str(img_str))
img = Image.open(io.BytesIO(image)).convert('RGB')
#--------------------------------
# index, title, desc = self.samples[i]
# caption = np.random.choice([title, desc])
# img = yfcc_loader(self.root, index)
elif self.dataset == 'coco':
index, captions = self.samples[i]
path = os.path.join(self.root, 'train2017', '{:012d}.jpg'.format(index))
img = pil_loader(path)
caption = np.random.choice(captions)
elif self.dataset == 'cc3m':
ann = self.samples[i]
filename, captions = ann['image_id'], ann['captions']
path = os.path.join(self.root, str(filename))
img = pil_loader(path)
caption = np.random.choice(captions)
elif self.dataset == 'cc12m':
ann = self.samples[i]
filename, captions = ann['image_name'], ann['captions']
path = os.path.join(self.root, filename)
img = pil_loader(path)
caption = np.random.choice(captions)
elif self.dataset == 'redcaps':
image_id, subreddit, caption = self.samples[i]
path = os.path.join(self.root, subreddit, f"{image_id}.jpg")
img = pil_loader(path)
return img, caption
def __getitem__(self, i):
raise NotImplementedError
def __len__(self):
return len(self.samples)
class ImageCaptionDatasetCLIP(ImageCaptionDatasetBase):
def __init__(self, dataset, root, metadata, transform=None, tokenizer=None):
super().__init__(dataset, root, metadata)
self.transform = transform
self.tokenizer = tokenizer
def __getitem__(self, i):
img, caption = self.get_raw_item(i)
# apply transformation
if self.transform is not None:
image = self.transform(img)
# tokenize caption
if self.tokenizer is not None:
caption = self.tokenizer(caption)
return image, caption
class ImageCaptionDatasetSLIP(ImageCaptionDatasetBase):
def __init__(self, dataset, root, metadata, transform, augment, tokenizer=None):
super().__init__(dataset, root, metadata)
self.transform = transform
self.augment = augment
self.tokenizer = tokenizer
def __getitem__(self, i):
img, caption = self.get_raw_item(i)
image = self.transform(img)
aug1 = self.augment(img)
aug2 = self.augment(img)
# tokenize caption
if self.tokenizer is not None:
caption = self.tokenizer(caption)
return image, caption, aug1, aug2
class ImageCaptionDatasetACLIP(ImageCaptionDatasetBase):
def __init__(self, dataset, root, metadata, transform, ema_transform, tokenizer=None):
super().__init__(dataset, root, metadata)
self.transform = transform
self.ema_transform = ema_transform
self.tokenizer = tokenizer
self.get_three_crop = GetThreeRandomResizedCrop(224, scale=(0.5, 1.0))
def __getitem__(self, i):
img, caption = self.get_raw_item(i)
res = self.get_three_crop(img)
im1, ret1 = res[0]
im2, ret2 = res[1]
im3, ret3 = res[2]
im1 = self.transform(im1)
im2 = self.transform(im2)
im3 = self.ema_transform(im3)
pos = np.array([ret1,ret2,ret3])
if self.tokenizer is not None:
caption = self.tokenizer(caption)
return [im1, im2, im3], pos, caption
class ImageCaptionDatasetSSL(ImageCaptionDatasetBase):
def __init__(self, dataset, root, metadata, augment):
super().__init__(dataset, root, metadata)
self.augment = augment
def __getitem__(self, i):
img, _ = self.get_raw_item(i)
aug1 = self.augment(img)
aug2 = self.augment(img)
return aug1, aug2
class FileListDataset(torch.utils.data.Dataset):
def __init__(self, images, labels, transform=None, target_transform=None):
self.transform = transform
self.target_transform = target_transform
self.images = np.load(images)
self.labels = np.load(labels)
def __getitem__(self, index):
img = pil_loader(self.images[index])
target = self.labels[index]
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.images)
def get_downstream_dataset(catalog, name, is_train, transform):
entry = catalog[name]
root = entry['path']
if entry['type'] == 'imagefolder':
dataset = t_datasets.ImageFolder(os.path.join(root, entry['train'] if is_train else entry['test']),
transform=transform)
elif entry['type'] == 'special':
if name == 'cifar10':
dataset = t_datasets.CIFAR10(root, train=is_train,
transform=transform, download=True)
elif name == 'cifar100':
dataset = t_datasets.CIFAR100(root, train=is_train,
transform=transform, download=True)
elif name == 'stl10':
dataset = t_datasets.STL10(root, split='train' if is_train else 'test',
transform=transform, download=True)
elif name == 'mnist':
dataset = t_datasets.MNIST(root, train=is_train,
transform=transform, download=True)
elif entry['type'] == 'filelist':
path = entry['train'] if is_train else entry['test']
val_images = os.path.join(root, path + '_images.npy')
val_labels = os.path.join(root, path + '_labels.npy')
if name == 'clevr_counts':
target_transform = lambda x: ['count_10', 'count_3', 'count_4', 'count_5', 'count_6', 'count_7', 'count_8', 'count_9'].index(x)
else:
target_transform = None
dataset = FileListDataset(val_images, val_labels, transform, target_transform)
else:
raise Exception('Unknown dataset')
return dataset
class GetThreeRandomResizedCrop(transforms.RandomResizedCrop):
def forward(self, img):
"""
Args:
img (PIL Image or Tensor): Image to be cropped and resized.
Returns:
List[(cropped image, ret)] *3.
The scale of the last image is larger than the first two.
"""
ret1 = self.get_params(img, self.scale, self.ratio)
ret2 = self.get_params(img, self.scale, self.ratio)
try:
_, height, width = F.get_dimensions(img)
except:
width, height = F.get_image_size(img)
im1 = F.resized_crop(img, *ret1, self.size, self.interpolation)
im2 = F.resized_crop(img, *ret2, self.size, self.interpolation)
# zoom out
ret3 = [0, 0, 0, 0]
ret3[0], ret3[1], = min(ret1[0], ret2[0]), min(ret1[1], ret2[2])
rh = max(ret1[0] + ret1[2], ret2[0] + ret2[2])
rw = max(ret1[1] + ret1[3], ret2[1] + ret2[3])
ret3[2], ret3[3] = rh - ret3[0], rw - ret3[1]
ret3[0] = torch.randint(0, ret3[0] + 1, size=(1,)).item() if ret3[0] > 0 else ret3[0]
ret3[1] = torch.randint(0, ret3[1] + 1, size=(1,)).item() if ret3[1] > 0 else ret3[1]
ret3[2] = torch.randint(ret3[2], height - ret3[0] + 1, size=(1,)).item() if ret3[2] < height else ret3[2]
ret3[3] = torch.randint(ret3[3], width - ret3[1] + 1, size=(1,)).item() if ret3[3] < width else ret3[3]
im3 = F.resized_crop(img, *ret3, self.size, self.interpolation)
return [(im1, ret1), (im2, ret2), (im3, ret3)]
def get_dataset(train_transform, tokenizer, args):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
augment = transforms.Compose([
transforms.RandomResizedCrop(224, scale=(0.08, 1.)),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.4, 0.1) # not strengthened
], p=0.8),
transforms.RandomGrayscale(p=0.2),
transforms.RandomApply([utils.GaussianBlur([.1, 2.])], p=0.5),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
if args.model.startswith('SIMCLR'):
return ImageCaptionDatasetSSL(args.dataset, args.root, args.metadata, augment)
elif args.model.startswith('CLIP'):
return ImageCaptionDatasetCLIP(args.dataset, args.root, args.metadata, train_transform, tokenizer)
elif args.model.startswith('SLIP'):
return ImageCaptionDatasetSLIP(args.dataset, args.root, args.metadata, train_transform, augment, tokenizer)
elif args.model.startswith('ACLIP'):
ema_transform = transforms.Compose([
transforms.ToTensor(),
normalize
])
return ImageCaptionDatasetACLIP(args.dataset, args.root, args.metadata, train_transform, ema_transform, tokenizer)