This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathdatasets.py
235 lines (186 loc) · 8.05 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
# 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 pickle
import zipfile
import numpy as np
from PIL import Image, ImageFile
import torch
from torchvision import transforms
from torchvision import datasets as t_datasets
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)
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 ImageCaptionDatasetBase(torch.utils.data.Dataset):
def __init__(self, dataset, root, metadata):
self.dataset = dataset
self.root = root
if self.dataset == 'yfcc15m':
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':
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 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
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)