-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatasets.py
More file actions
616 lines (504 loc) · 20.9 KB
/
datasets.py
File metadata and controls
616 lines (504 loc) · 20.9 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# Standard libraries
from pathlib import Path
import glob
import csv
import os
import json
import io
import pickle
import PIL.Image
import zipfile
import tarfile
# For opening packed data formats
import h5py
import lmdb
# Libraries for fast image reading [OPTIONAL]
try:
import pyspng
except ImportError:
pyspng = None
try:
import turbojpeg
except ImportError:
turbojpeg = None
if turbojpeg:
try:
turbojpeg_path = "/opt/TurboVNC/java/libturbojpeg.so"
# Test
turbojpeg_decoder = turbojpeg.TurboJPEG(turbojpeg_path)
except OSError:
print("turboJPEG was installed but not found. Continuing without")
# Deep Learning
import numpy as np
import torch
from torchvision.transforms.functional import to_tensor
class ImageDataset(torch.utils.data.Dataset):
"""Dataset for PNG/JPEG to pass to a PyTorch dataloader
Args:
path (str): location where the images are stored on disk
transform (obj): torchvision.transforms object or None
prefix (str): only for ImageNet we need custom prefix ILSVRC2012_val_
offset_index (int): only for ImageNet we need offset (000000001.jpeg => offset_index is 8)
Returns:
torch Dataset: to pass to a dataloader
"""
def __init__(self, path, cache=False, transform=None, prefix="", offset_index=0):
print("Initializing Image dataset with path: ", path)
self.img_dir = Path(path)
self.labels = []
self.cached_images = []
self.cache = cache
self.transform = transform
# For ImageNet we need a custom prefix (ILSVRC2012_val_)
self.prefix = prefix
# For ImageNet: we have format of 00000001.jpeg (fill to 8)
self.zfill_len = 0 if prefix == "" else 8
self.offset_index = offset_index
# Find image extension
index = offset_index
img_path_example = os.path.join(
self.img_dir, self.prefix + str(index).zfill(self.zfill_len)
)
img_path = list(glob.glob(img_path_example + "*"))
assert len(img_path) == 1
self.file_ext = os.path.splitext(img_path[0])[1]
# Read all labels and store them in the class instance
self.read_label_file()
if cache:
self.cache_images()
def read_label_file(self):
"""
Read label function
Assumes a single label file containing the ground-truth labels for all samples in txt or csv
"""
label_file = list(self.img_dir.glob("*.csv")) + list(self.img_dir.glob("*.txt"))
assert len(label_file) == 1
label_file = label_file[0]
with open(label_file, "r") as csvfile:
reader = csv.reader(
csvfile, delimiter=" ", quotechar="|", quoting=csv.QUOTE_MINIMAL
)
for row in reader:
self.labels.append(int(row[0]))
def read_image(self, img_path):
"""
Read image and return image
img_path: path where image is located on disk
"""
fname = open(img_path, "rb")
jpgs = (".jpg", ".jpeg")
if self.file_ext == ".png" and pyspng and not self.encoder_info:
image = pyspng.load(fname.read())
elif self.file_ext.lower() in jpgs and turbojpeg:
try:
image = turbojpeg_decoder.decode(fname.read(), pixel_format=0)
except IOError: # Catch jpgs which are actually encoded as PNG
image = PIL.Image.open(fname).convert("RGB")
else:
image = PIL.Image.open(img_path).convert("RGB")
return image
def cache_images(self):
for index in range(len(self.labels)):
index += self.offset_index
img_path = os.path.join(
self.img_dir,
self.prefix + str(index).zfill(self.zfill_len) + self.file_ext,
)
image = self.read_image(img_path)
self.cached_images.append(image)
def __getitem__(self, index):
label = self.labels[index]
if self.cache:
image = self.cached_images[index]
if self.transform:
image = self.transform(image)
else:
image = to_tensor(image)
return image, label
index += self.offset_index
img_path = os.path.join(
self.img_dir, self.prefix + str(index).zfill(self.zfill_len) + self.file_ext
)
image = self.read_image(img_path)
if self.transform:
image = self.transform(image)
else:
image = to_tensor(image)
return image, label
def __len__(self):
return len(self.labels)
# TODO:
# - Multiple .h5 files of data
# - (Partially) cached data
class H5Dataset(torch.utils.data.Dataset):
"""Dataset for packed HDF5/H5 files to pass to a PyTorch dataloader
Args:
path (str): location where the images are stored on disk
transform (obj): torchvision.transforms object or None
load_encoded (bool): whether the images within the .h5 file are encoded or saved as bytes directly
Returns:
torch Dataset: to pass to a dataloader
"""
def __init__(self, path, cache=False, transform=None, load_encoded=False):
print("Initializing HDF5 dataset with path: ", path)
self.file_path = path
self.cache = cache
self.transform = transform
self.load_encoded = load_encoded
# Hardcoded key, value pair within the .h5 files
self.h5_key_samples = "images"
self.h5_key_labels = "labels"
self.labels = []
self.dataset = None
# Initial check (or for caching)
with h5py.File(self.file_path, "r") as file:
self.dataset_len = len(file["labels"])
if cache:
self.cached_images = list(file["images"])
self.cached_labels = list(file["labels"])
def __getitem__(self, index):
# https://discuss.pytorch.org/t/dataloader-when-num-worker-0-there-is-bug/25643/16?fbclid=IwAR2jFrRkKXv4PL9urrZeiHT_a3eEn7eZDWjUaQ-zcLP6BRtMO7e0nMgwlKU
# Why fill dataset at getitem rather than init?
# Creates dataset first time getitem is called
if self.cache:
image = self.cached_images[index]
label = self.cached_labels[index]
if self.load_encoded:
image = PIL.Image.open(io.BytesIO(image))
if self.transform:
image = self.transform(image)
else:
image = to_tensor(image)
return image, label
# Each worker (which are forked after the init) need to have their own file handle
if self.dataset is None:
data = h5py.File(self.file_path, "r")
self.dataset = data.get(self.h5_key_samples)
self.labels = data.get(self.h5_key_labels)
image = self.dataset[index]
if self.load_encoded:
image = PIL.Image.open(io.BytesIO(image))
label = self.labels[index]
if self.transform:
image = self.transform(image)
else:
image = to_tensor(image)
return image, label
def __len__(self):
return self.dataset_len
class LMDBDataset(torch.utils.data.Dataset):
"""Dataset for packed LMDB files to pass to a PyTorch dataloader
Args:
path (str): location where the images are stored on disk
transform (obj): torchvision.transforms object or None
load_encoded (bool): whether the images within the .h5 file are encoded or saved as bytes directly
Returns:
torch Dataset: to pass to a dataloader
"""
def __init__(self, path, cache=False, transform=None, load_encoded=False):
print("Initializing LMDB dataset with path: ", path)
self.path = path
self.cache = cache
self.transform = transform
self.load_encoded = load_encoded
self.cached_images = []
self.cached_labels = []
self.env = None
self.txn = None
# Is this necessary?
self.env = lmdb.open(
self.path,
subdir=False,
readonly=True,
lock=False,
readahead=False,
meminit=False,
)
# Extract all keys
with self.env.begin(write=False) as txn:
self.length = txn.stat()["entries"]
if cache:
for key in txn.cursor().iternext(keys=True, values=False):
image, label = pickle.loads(txn.get(key))
self.cached_images.append(image)
self.cached_labels.append(label)
self.keys = [
k for k in txn.cursor().iternext(keys=True, values=False)
] # https://github.com/jnwatson/py-lmdb/issues/195
def __getitem__(self, index):
if self.cache:
image = self.cached_images[index]
label = self.cached_labels[index]
if self.load_encoded:
image = PIL.Image.open(io.BytesIO(image))
if self.transform:
image = self.transform(image)
else:
image = to_tensor(image)
return image, label
# Each worker (which are forked after the init) need to have their own file handle
if self.txn is None:
self.txn = self.env.begin()
# Load from LMDB
image, label = pickle.loads(self.txn.get(self.keys[index]))
if self.load_encoded:
image = PIL.Image.open(io.BytesIO(image))
if self.transform:
image = self.transform(image)
else:
image = torch.tensor(image)
return image, label
def __len__(self):
return self.length
class ZIPDataset(torch.utils.data.Dataset):
"""Dataset for packed ZIP to pass to a PyTorch dataloader
Args:
path (str): location where the images are stored on disk
transform (obj): torchvision.transforms object or None
load_encoded (bool): whether the images within the .zip file are encoded or saved as bytes directly
Returns:
torch Dataset: to pass to a dataloader
"""
def __init__(self, path, cache=False, transform=None, load_encoded=False):
print(f"Initializing ZIP dataset for: {path}")
self.path = path
self.transform = transform
self.load_encoded = load_encoded
# Each worker needs to get the keys of the ZIP file
worker = torch.utils.data.get_worker_info()
worker = worker.id if worker else None
self.zip_handle = {worker: zipfile.ZipFile(path)}
self.members = sorted(self.zip_handle[worker].namelist())
# Retrieve samples in list and label files in list
self._get_all_samples()
label_fname = "" # Or None. If None then no label file is there
if len(self.label_fname) >= 1 and label_fname is not None:
self._parse_label_file(worker)
else:
self._get_filler_labels()
def _get_all_samples(self, label_exts=(".json")):
"""Sort labels and images from the names
Args:
label_exts: the file extensions which will be categorized as labels
Returns:
None: filled lists of self.samples and self.label_fname
"""
self.samples = []
self.label_fname = []
PIL.Image.init()
for m in self.members:
if m.lower().endswith(tuple(PIL.Image.EXTENSION.keys())):
self.samples.append(m)
elif m.lower().endswith(label_exts):
self.label_fname.append(m)
self.file_ext = os.path.splitext(self.samples[0])[1]
self.length = len(self.samples)
def _parse_label_file(self, worker, label_fname=""):
"""Sort labels and images from the names
Args:
worker (int): worker id
label_fname (str): hardcoded label file name
Returns:
None: parsed labels
"""
# If no hardcoded label filename is given, use the first one from the list by _get_all_samples()
if label_fname == "":
if len(self.label_fname) != 1:
print(
f"WARNING: found {len(self.label_fname)} label files - using only the first"
)
label_fname = self.label_fname[0]
# Parse labels from json file
label_file = self.zip_handle[worker].open(label_fname, "r")
labels = json.load(label_file)["labels"]
labels = dict(sorted(labels, key=lambda item: item[1]))
labels = [labels[fname.replace("\\", "/")] for fname in self.samples]
self.labels = np.array(labels, dtype=np.uint8)
def _get_filler_labels(self):
self.labels = [0] * self.length
def _get_file(self, fname):
"""Retrieve file handle for a given file name within the ZIP file"""
worker = torch.utils.data.get_worker_info()
worker = worker.id if worker else None
if worker not in self.zip_handle:
self.zip_handle[worker] = zipfile.ZipFile(self.path)
return self.zip_handle[worker].open(fname, "r")
def _get_image(self, fname):
fname = self._get_file(fname)
jpgs = (".jpg", ".jpeg")
if not self.load_encoded:
# In case of having the image saved as bytes:
image = np.frombuffer(fname.read(), dtype=np.uint8).reshape(256, 256, 3)
else:
if self.file_ext.lower() == ".png" and pyspng:
image = pyspng.load(fname.read())
elif self.file_ext.lower() in jpgs and turbojpeg:
try:
image = turbojpeg_decoder.decode(fname.read(), pixel_format=0)
except IOError: # Catch jpgs which are actually encoded as PNG
image = PIL.Image.open(fname).convert("RGB")
else:
image = PIL.Image.open(fname.read()).convert("RGB")
return image
def _get_label(self, index):
return self.labels[index]
def __getitem__(self, index):
fname_image = self.samples[index]
image = self._get_image(fname_image)
label = self._get_label(index)
if self.transform:
image = self.transform(image)
else:
image = to_tensor(image)
return image, label
def __len__(self):
return self.length
def __del__(self):
"""Clean all file handles of the workers on exit"""
for o in self.zip_handle.values():
o.close()
def __getstate__(self):
"""Serialize without the ZipFile references, for multiprocessing compatibility"""
state = dict(self.__dict__)
state["zip_handle"] = {}
return state
class TARDataset(torch.utils.data.Dataset):
"""Dataset for packed ZIP to pass to a PyTorch dataloader
Args:
path (str): location where the images are stored on disk
transform (obj): torchvision.transforms object or None
load_encoded (bool): whether the images within the .tar file are encoded or saved as bytes directly
label_file (str): path to save the cached getmembers() output as this may take a while for larger dataset
Returns:
torch Dataset: to pass to a dataloader
"""
def __init__(self, path, transform=None, load_encoded=False, label_file=None):
print(f"Initializing TAR dataset for: {path}")
self.path = path
self.transform = transform
self.load_encoded = load_encoded
# First uncompress because .gz cannot be read in parallel
if self.path.endswith(".tar.gz"):
print(
"WARNING: tar file is compressed which drastically impacts performance -> gunzip -k <file>"
)
# TAR is not thread-safe so give file handle to each worker
worker = torch.utils.data.get_worker_info()
worker = worker.id if worker else None
self.tar_handle = {worker: tarfile.open(path)}
# Store headers of all files and folders by name
# self.members = sorted(self.tar_handle[worker].getmembers(), key=lambda m: m.name)
# store headers of all files and folders by name
if label_file:
with open(label_file, "rb") as fp:
self.members_by_name = pickle.load(fp)
else:
# get.members() takes very long for larger TAR archives so cache the members in a byte file
self.members_by_name = {
m.name: m
for m in sorted(
self.tar_handle[worker].getmembers(), key=lambda m: m.name
)
}
with open(os.path.join(Path(path).parent, "members"), "wb") as fp:
pickle.dump(self.members_by_name, fp)
print(
f"Finished create a members file. Please add the following path to the init as label_file next time: ",
Path(path).parent + "members",
)
self._get_all_samples()
label_fname = None # or "String"
if len(self.label_fname) >= 1 and label_fname is not None:
self._parse_label_file(worker, label_fname)
else:
self._get_filler_labels()
def _get_all_samples(self, label_exts=(".json")):
"""Sort labels and images from the names
Args:
label_exts: the file extensions which will be categorized as labels
Returns:
None: filled lists of self.samples and self.label_fname
"""
self.members_by_name = {m.name: m for m in self.members}
self.samples = []
self.label_fname = []
PIL.Image.init()
for m in self.members_by_name.values():
m_name = m.name
if m_name.lower().endswith(tuple(PIL.Image.EXTENSION.keys())):
self.samples.append(m_name)
elif m_name.lower().endswith(label_exts):
self.label_fname.append(m_name)
self.file_ext = os.path.splitext(self.samples[0])[1]
self.length = len(self.samples)
def _parse_label_file(self, worker, label_fname=""):
"""Sort labels and images from the names
Args:
worker (int): worker id
label_fname (str): hardcoded label file name
Returns:
None: parsed labels
"""
# If no hardcoded label filename is given, use the first one from the list by _get_all_samples()
if label_fname == "":
if len(self.label_fname) != 1:
print(
f"WARNING: found {len(self.label_fname)} label files - using only the first"
)
label_fname = self.label_fname[0]
# Parse labels from json file
label_file = self.tar_handle[worker].extractfile(label_fname)
labels = json.load(label_file)["labels"]
labels = dict(sorted(labels, key=lambda item: item[1]))
labels = [labels[fname.replace("\\", "/")] for fname in self.samples]
self.labels = np.array(labels, dtype=np.uint8)
def _get_filler_labels(self):
# Placeholder for datasets with no labels like FFHQ
self.labels = [0] * self.length
def _get_file(self, fname):
"""Retrieve file handle for a given file name within the ZIP file"""
worker = torch.utils.data.get_worker_info()
worker = worker.id if worker else None
if worker not in self.tar_handle:
self.tar_handle[worker] = tarfile.open(self.path)
return self.tar_handle[worker].extractfile(self.members_by_name[fname])
def _get_image(self, fname):
fname = self._get_file(fname)
jpgs = (".jpg", ".jpeg")
if not self.load_encoded:
# In case of having the image saved as bytes:
image = np.frombuffer(fname.read(), dtype=np.uint8).reshape(256, 256, 3)
else:
if self.file_ext.lower() == ".png" and pyspng:
image = pyspng.load(fname.read())
elif self.file_ext.lower() in jpgs and turbojpeg:
try:
image = turbojpeg_decoder.decode(fname.read(), pixel_format=0)
except IOError: # Catch jpgs which are actually encoded as PNG
image = PIL.Image.open(fname).convert("RGB")
else:
image = PIL.Image.open(fname.read()).convert("RGB")
return image
def _get_label(self, index):
return self.labels[index]
def __getitem__(self, index):
fname_image = self.samples[index]
image = self._get_image(fname_image)
label = self._get_label(index)
if self.transform:
image = self.transform(image)
else:
image = to_tensor(image)
return image, label
def __len__(self):
return self.length
def __del__(self):
"""Clean all file handles of the workers on exit"""
if hasattr(self, "tar_handle"):
for o in self.tar_handle.values():
o.close()
def __getstate__(self):
"""Serialize without the ZipFile references, for multiprocessing compatibility"""
state = dict(self.__dict__)
state["tar_handle"] = {}
return state