forked from facebookresearch/msn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_manager.py
324 lines (276 loc) · 9.89 KB
/
data_manager.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
# Copyright (c) Facebook, Inc. and its 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.
#
import os
import subprocess
import time
from logging import getLogger
import torch
import torchvision
import torchvision.transforms as transforms
from PIL import ImageFilter
_GLOBAL_SEED = 0
logger = getLogger()
def init_data(
transform,
batch_size,
pin_mem=True,
num_workers=8,
world_size=1,
rank=0,
root_path=None,
image_folder=None,
training=True,
copy_data=False,
drop_last=True,
subset_file=None,
with_path=False,
):
if with_path:
imagenet_class = ImageNetWithPath
imagenet_subset_class = ImageNetSubsetWithPath
else:
imagenet_class = ImageNet
imagenet_subset_class = ImageNetSubset
dataset = imagenet_class(
root=root_path,
image_folder=image_folder,
transform=transform,
train=training,
copy_data=copy_data,
)
if subset_file is not None:
dataset = imagenet_subset_class(dataset, subset_file)
logger.info("ImageNet dataset created")
dist_sampler = torch.utils.data.distributed.DistributedSampler(
dataset=dataset, num_replicas=world_size, rank=rank
)
data_loader = torch.utils.data.DataLoader(
dataset,
sampler=dist_sampler,
batch_size=batch_size,
drop_last=drop_last,
pin_memory=pin_mem,
num_workers=num_workers,
)
logger.info("ImageNet unsupervised data loader created")
return (data_loader, dist_sampler)
def make_transforms(
rand_size=224,
focal_size=96,
rand_crop_scale=(0.3, 1.0),
focal_crop_scale=(0.05, 0.3),
color_jitter=1.0,
rand_views=2,
focal_views=10,
):
logger.info("making imagenet data transforms")
def get_color_distortion(s=1.0):
# s is the strength of color distortion.
color_jitter = transforms.ColorJitter(0.8 * s, 0.8 * s, 0.8 * s, 0.2 * s)
rnd_color_jitter = transforms.RandomApply([color_jitter], p=0.8)
rnd_gray = transforms.RandomGrayscale(p=0.2)
color_distort = transforms.Compose([rnd_color_jitter, rnd_gray])
return color_distort
rand_transform = transforms.Compose(
[
transforms.RandomResizedCrop(rand_size, scale=rand_crop_scale),
transforms.RandomHorizontalFlip(),
get_color_distortion(s=color_jitter),
GaussianBlur(p=0.5),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]
)
focal_transform = transforms.Compose(
[
transforms.RandomResizedCrop(focal_size, scale=focal_crop_scale),
transforms.RandomHorizontalFlip(),
get_color_distortion(s=color_jitter),
GaussianBlur(p=0.5),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]
)
transform = MultiViewTransform(
rand_transform=rand_transform,
focal_transform=focal_transform,
rand_views=rand_views,
focal_views=focal_views,
)
return transform
class MultiViewTransform(object):
def __init__(
self,
rand_transform=None,
focal_transform=None,
rand_views=1,
focal_views=1,
):
self.rand_views = rand_views
self.focal_views = focal_views
self.rand_transform = rand_transform
self.focal_transform = focal_transform
def __call__(self, img):
img_views = []
# -- generate random views
if self.rand_views > 0:
img_views += [self.rand_transform(img) for i in range(self.rand_views)]
# -- generate focal views
if self.focal_views > 0:
img_views += [self.focal_transform(img) for i in range(self.focal_views)]
return img_views
class ImageNet(torchvision.datasets.ImageFolder):
def __init__(
self,
root,
image_folder="imagenet_full_size/061417/",
tar_folder="imagenet_full_size/",
tar_file="imagenet_full_size-061417.tar",
transform=None,
train=True,
job_id=None,
local_rank=None,
copy_data=True,
):
"""
ImageNet
Dataset wrapper (can copy data locally to machine)
:param root: root network directory for ImageNet data
:param image_folder: path to images inside root network directory
:param tar_file: zipped image_folder inside root network directory
:param train: whether to load train data (or validation)
:param job_id: scheduler job-id used to create dir on local machine
:param copy_data: whether to copy data from network file locally
"""
suffix = "train/" if train else "val/"
data_path = None
if copy_data:
logger.info("copying data locally")
data_path = copy_imgnt_locally(
root=root,
suffix=suffix,
image_folder=image_folder,
tar_folder=tar_folder,
tar_file=tar_file,
job_id=job_id,
local_rank=local_rank,
)
if (not copy_data) or (data_path is None):
data_path = os.path.join(root, image_folder, suffix)
logger.info(f"data-path {data_path}")
super(ImageNet, self).__init__(root=data_path, transform=transform)
logger.info("Initialized ImageNet")
class ImageNetSubset(object):
def __init__(self, dataset, subset_file):
"""
ImageNetSubset
:param dataset: ImageNet dataset object
:param subset_file: '.txt' file containing IDs of IN1K images to keep
"""
self.dataset = dataset
self.subset_file = subset_file
self.filter_dataset_(subset_file)
def filter_dataset_(self, subset_file):
"""Filter self.dataset to a subset"""
root = self.dataset.root
class_to_idx = self.dataset.class_to_idx
# -- update samples to subset of IN1k targets/samples
new_samples = []
logger.info(f"Using {subset_file}")
with open(subset_file, "r") as rfile:
for line in rfile:
class_name = line.split("_")[0]
target = class_to_idx[class_name]
img = line.split("\n")[0]
new_samples.append((os.path.join(root, class_name, img), target))
self.samples = new_samples
@property
def classes(self):
return self.dataset.classes
def __len__(self):
return len(self.samples)
def __getitem__(self, index):
path, target = self.samples[index]
img = self.dataset.loader(path)
if self.dataset.transform is not None:
img = self.dataset.transform(img)
if self.dataset.target_transform is not None:
target = self.dataset.target_transform(target)
return img, target
class ImageNetWithPath(ImageNet):
def __getitem__(self, index):
"""
Adapted from https://github.com/pytorch/vision/blob/main/torchvision/datasets/folder.py.
"""
path, target = self.samples[index]
sample = self.loader(path)
if self.transform is not None:
sample = self.transform(sample)
if self.target_transform is not None:
target = self.target_transform(target)
return path, sample, target
class ImageNetSubsetWithPath(ImageNetSubset):
def __getitem__(self, index):
path, target = self.samples[index]
img = self.dataset.loader(path)
if self.dataset.transform is not None:
img = self.dataset.transform(img)
if self.dataset.target_transform is not None:
target = self.dataset.target_transform(target)
return path, img, target
class GaussianBlur(object):
def __init__(self, p=0.5, radius_min=0.1, radius_max=2.0):
self.prob = p
self.radius_min = radius_min
self.radius_max = radius_max
def __call__(self, img):
if torch.bernoulli(torch.tensor(self.prob)) == 0:
return img
radius = self.radius_min + torch.rand(1) * (self.radius_max - self.radius_min)
return img.filter(ImageFilter.GaussianBlur(radius=radius))
def copy_imgnt_locally(
root,
suffix,
image_folder="imagenet_full_size/061417/",
tar_folder="imagenet_full_size/",
tar_file="imagenet_full_size-061417.tar",
job_id=None,
local_rank=None,
):
if job_id is None:
try:
job_id = os.environ["SLURM_JOBID"]
except Exception:
logger.info("No job-id, will load directly from network file")
return None
if local_rank is None:
try:
local_rank = int(os.environ["SLURM_LOCALID"])
except Exception:
logger.info("No job-id, will load directly from network file")
return None
source_file = os.path.join(root, tar_folder, tar_file)
target = f"/scratch/slurm_tmpdir/{job_id}/"
target_file = os.path.join(target, tar_file)
data_path = os.path.join(target, image_folder, suffix)
logger.info(f"{source_file}\n{target}\n{target_file}\n{data_path}")
tmp_sgnl_file = os.path.join(target, "copy_signal.txt")
if not os.path.exists(data_path):
if local_rank == 0:
commands = [["tar", "-xf", source_file, "-C", target]]
for cmnd in commands:
start_time = time.time()
logger.info(f"Executing {cmnd}")
subprocess.run(cmnd)
logger.info(f"Cmnd took {(time.time()-start_time)/60.} min.")
with open(tmp_sgnl_file, "+w") as f:
print("Done copying locally.", file=f)
else:
while not os.path.exists(tmp_sgnl_file):
time.sleep(60)
logger.info(f"{local_rank}: Checking {tmp_sgnl_file}")
return data_path