-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
285 lines (233 loc) · 9.53 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
"""
Train a diffusion model on images.
"""
import random
import blobfile as bf
from absl import logging
import numpy as np
from PIL import Image, ImageOps, ImageFilter
import torch
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms
def seed_worker(worker_id):
worker_seed = torch.initial_seed() % 2**32
np.random.seed(worker_seed)
random.seed(worker_seed)
def _list_image_files_recursively(data_dir):
results = []
for entry in sorted(bf.listdir(data_dir)):
full_path = bf.join(data_dir, entry)
ext = entry.split(".")[-1]
if "." in entry and ext.lower() in ["jpg", "jpeg", "png", "gif"]:
results.append(full_path)
elif bf.isdir(full_path):
results.extend(_list_image_files_recursively(full_path))
return results
class Solarize(object):
"""Solarize augmentation from BYOL: https://arxiv.org/abs/2006.07733"""
def __call__(self, x):
return ImageOps.solarize(x)
class GaussianBlur(object):
"""Gaussian blur augmentation from SimCLR: https://arxiv.org/abs/2002.05709"""
def __init__(self, sigma=[.1, 2.]):
self.sigma = sigma
def __call__(self, x):
sigma = random.uniform(self.sigma[0], self.sigma[1])
x = x.filter(ImageFilter.GaussianBlur(radius=sigma))
return x
class GaussianNoise(object):
def __init__(self, coeff=2, sigma_min=0.002, sigma_max=80, sigma_data=1, rho=7, scales=20):
self.coeff = coeff
self.sigma_min = sigma_min
self.sigma_max = sigma_max
self.sigma_data = sigma_data
self.rho = rho
self.scales = scales
def sample_sigmas(self):
# index = torch.randint(0, min(self.scales // self.coeff + 1, self.scales-1) , (1,))
index = 4
sigmas = ( self.sigma_max**(1/self.rho) + ( index/(self.scales-1) )*( self.sigma_min**(1/self.rho) - self.sigma_max**(1/self.rho) ) )**self.rho
return sigmas
def __call__(self, x):
noise = torch.randn_like(x)
sigmas = self.sample_sigmas()
x = x + sigmas*noise
return x
def center_crop_arr(pil_image, image_size):
# We are not on a new enough PIL to support the `reducing_gap`
# argument, which uses BOX downsampling at powers of two first.
# Thus, we do it by hand to improve downsample quality.
if pil_image.size[0] == image_size and pil_image.size[1] == image_size:
return np.array(pil_image)
while min(*pil_image.size) >= 2 * image_size:
pil_image = pil_image.resize(
tuple(x // 2 for x in pil_image.size), resample=Image.BOX
)
scale = image_size / min(*pil_image.size)
pil_image = pil_image.resize(
tuple(round(x * scale) for x in pil_image.size), resample=Image.BICUBIC
)
arr = np.array(pil_image)
crop_y = (arr.shape[0] - image_size) // 2
crop_x = (arr.shape[1] - image_size) // 2
return arr[crop_y : crop_y + image_size, crop_x : crop_x + image_size]
class TwoCropsTransform:
"""Take two random crops of one image"""
def __init__(self, base_transform1, base_transform2):
self.base_transform1 = base_transform1
self.base_transform2 = base_transform2
def __call__(self, x):
im1 = self.base_transform1(x)
im2 = self.base_transform2(x)
return [im1, im2]
class ImageDataset(Dataset):
def __init__(
self,
name,
resolution,
image_paths,
augmentation_type = "strong",
value_range="-1,1" # the range of pixel value after normalization, by default is [-1,1]
):
super().__init__()
self.name = name
self.resolution = resolution
self.local_images = image_paths
if value_range == "0,1":
mean = [0, 0, 0]
std = [1,1,1]
elif value_range == "0.5,0.5":
mean = [0.5, 0.5, 0.5]
std=[0.5, 0.5, 0.5]
else:
mean = [0.49139968, 0.48215827, 0.44653124]
std=[0.24703233, 0.24348505, 0.26158768]
normalize = transforms.Normalize(mean = mean, std = std)
logging.info(f"mean: {mean}, std:{std}")
if name == "cifar10":
augmentation = transforms.Compose([
transforms.RandomResizedCrop(self.resolution, scale=(0.2, 1.)),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.4, 0.2)
], p=0.8),
transforms.RandomGrayscale(p=0.2),
transforms.RandomApply([GaussianBlur([.1, 2.])], p=0.5),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
augmentation1 = transforms.Compose([
transforms.RandomResizedCrop(32, scale=(0.08, 1.)),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.2, 0.1) # not strengthened
], p=0.8),
transforms.RandomGrayscale(p=0.2),
transforms.RandomApply([GaussianBlur([.1, 2.])], p=1.0),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
augmentation2 = transforms.Compose([
transforms.RandomResizedCrop(32, scale=(0.08, 1.)),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.2, 0.1) # not strengthened
], p=0.8),
transforms.RandomGrayscale(p=0.2),
transforms.RandomApply([GaussianBlur([.1, 2.])], p=0.1),
transforms.RandomApply([Solarize()], p=0.2),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
if augmentation_type == "strong":
self.augmentation = TwoCropsTransform(augmentation1, augmentation2)
else:
self.augmentation = TwoCropsTransform(augmentation, augmentation)
elif name == "imagenet":
augmentation1 = transforms.Compose([
transforms.RandomResizedCrop(self.resolution, scale=(0.08, 1.)),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.2, 0.1)
], p=0.8),
transforms.RandomGrayscale(p=0.2),
transforms.RandomApply([GaussianBlur([.1, 2.])], p=1.0),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
augmentation2 = transforms.Compose([
transforms.RandomResizedCrop(self.resolution, scale=(0.08, 1.)),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.2, 0.1)
], p=0.8),
transforms.RandomGrayscale(p=0.2),
transforms.RandomApply([GaussianBlur([.1, 2.])], p=0.1),
transforms.RandomApply([Solarize()], p=0.2),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
self.augmentation = TwoCropsTransform(augmentation1, augmentation2)
self.totensor = transforms.Compose([
transforms.ToTensor(),
normalize,
])
self.simple_augmentation = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
def __len__(self):
return len(self.local_images)
def __getitem__(self, idx):
path = self.local_images[idx]
with bf.BlobFile(path, "rb") as f:
pil_image = Image.open(f)
pil_image.load()
pil_image = pil_image.convert("RGB")
x_aug, x_aug2 = self.augmentation(pil_image)
cropped_pil= Image.fromarray(center_crop_arr(pil_image, self.resolution))
x = self.simple_augmentation(cropped_pil)
return x, x_aug, x_aug2
def load_data(
*,
name,
data_dir,
batch_size,
image_size,
deterministic=False,
num_workers=8,
**kwargs,
):
"""
For a dataset, create a generator over (images, kwargs) pairs.
Each images is an NCHW float tensor, and the kwargs dict contains zero or
more keys, each of which map to a batched Tensor of their own.
The kwargs dict can be used for class labels, in which case the key is "y"
and the values are integer tensors of class labels.
:param data_dir: a dataset directory.
:param batch_size: the batch size of each returned pair.
:param image_size: the size to which images are resized.
:param class_cond: if True, include a "y" key in returned dicts for class
label. If classes are not available and this is true, an
exception will be raised.
:param deterministic: if True, yield results in a deterministic order.
:param random_crop: if True, randomly crop the images for augmentation.
:param random_flip: if True, randomly flip the images for augmentation.
"""
if not data_dir:
raise ValueError("unspecified data directory")
all_files = _list_image_files_recursively(data_dir)
logging.info(f"total training samples: {len(all_files)}")
dataset = ImageDataset(
name,
image_size,
all_files,
**kwargs,
)
logging.info(f"batch size per process:{batch_size}")
loader = DataLoader(
dataset, batch_size=batch_size, shuffle=(not deterministic), num_workers=num_workers, drop_last=True,
pin_memory=True, persistent_workers=True,
)
return loader