This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
data.py
446 lines (380 loc) · 16.3 KB
/
data.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
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
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pathlib
from typing import Any, Callable, List, Optional, Sequence, Tuple, Union
import torch
from PIL import Image
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from torchvision import transforms as T
from torchvision.datasets import VisionDataset
from torchvision.datasets.folder import has_file_allowed_extension, IMG_EXTENSIONS, make_dataset
from flash.core.classification import ClassificationDataPipeline
from flash.core.data.datamodule import DataModule
from flash.core.data.utils import _contains_any_tensor
def _pil_loader(path) -> Image:
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, "rb") as f, Image.open(f) as img:
return img.convert("RGB")
class FilepathDataset(torch.utils.data.Dataset):
"""Dataset that takes in filepaths and labels."""
def __init__(
self,
filepaths: Optional[Sequence[Union[str, pathlib.Path]]],
labels: Optional[Sequence],
loader: Callable,
transform: Optional[Callable] = None,
):
"""
Args:
filepaths: file paths to load with :attr:`loader`
labels: the labels corresponding to the :attr:`filepaths`.
Each unique value will get a class index by sorting them.
loader: the function to load an image from a given file path
transform: the transforms to apply to the loaded images
"""
self.fnames = filepaths or []
self.labels = labels or []
self.transform = transform
self.loader = loader
if self.has_labels:
self.label_to_class_mapping = {v: k for k, v in enumerate(list(sorted(list(set(self.fnames)))))}
@property
def has_labels(self) -> bool:
return self.labels is not None
def __len__(self) -> int:
return len(self.fnames)
def __getitem__(self, index: int) -> Tuple[Any, Optional[int]]:
filename = self.fnames[index]
img = self.loader(filename)
label = None
if self.has_labels:
label = self.label_to_class_mapping[filename]
return img, label
class FlashDatasetFolder(VisionDataset):
"""A generic data loader where the samples are arranged in this way: ::
root/class_x/xxx.ext
root/class_x/xxy.ext
root/class_x/xxz.ext
root/class_y/123.ext
root/class_y/nsdf3.ext
root/class_y/asd932_.ext
Args:
root: Root directory path.
loader: A function to load a sample given its path.
extensions: A list of allowed extensions. both extensions
and is_valid_file should not be passed.
transform: A function/transform that takes in
a sample and returns a transformed version.
E.g, ``transforms.RandomCrop`` for images.
target_transform: A function/transform that takes
in the target and transforms it.
is_valid_file: A function that takes path of a file
and check if the file is a valid file (used to check of corrupt files)
both extensions and is_valid_file should not be passed.
with_targets: Whether to include targets
img_paths: List of image paths to load. Only used when ``with_targets=False``
Attributes:
classes (list): List of the class names sorted alphabetically.
class_to_idx (dict): Dict with items (class_name, class_index).
samples (list): List of (sample path, class_index) tuples
targets (list): The class_index value for each image in the dataset
"""
def __init__(
self,
root: str,
loader: Callable,
extensions: Tuple[str] = IMG_EXTENSIONS,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
is_valid_file: Optional[Callable] = None,
with_targets: bool = True,
img_paths: Optional[List[str]] = None,
):
super(FlashDatasetFolder, self).__init__(root, transform=transform, target_transform=target_transform)
self.loader = loader
self.extensions = extensions
self.with_targets = with_targets
if with_targets:
classes, class_to_idx = self._find_classes(self.root)
samples = make_dataset(self.root, class_to_idx, extensions, is_valid_file)
if len(samples) == 0:
msg = "Found 0 files in subfolders of: {}\n".format(self.root)
if extensions is not None:
msg += "Supported extensions are: {}".format(",".join(extensions))
raise RuntimeError(msg)
self.classes = classes
self.class_to_idx = class_to_idx
self.samples = samples
self.targets = [s[1] for s in samples]
else:
if not img_paths:
raise MisconfigurationException(
"`FlashDatasetFolder(with_target=False)` but no `img_paths` were provided"
)
self.samples = img_paths
def _find_classes(self, dir):
"""
Finds the class folders in a dataset.
Args:
dir (string): Root directory path.
Returns:
tuple: (classes, class_to_idx) where classes are relative to (dir), and class_to_idx is a dictionary.
Ensures:
No class is a subdirectory of another.
"""
classes = [d.name for d in os.scandir(dir) if d.is_dir()]
classes.sort()
class_to_idx = {cls_name: i for i, cls_name in enumerate(classes)}
return classes, class_to_idx
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (sample, target) where target is class_index of the target class.
"""
if self.with_targets:
path, target = self.samples[index]
if self.target_transform is not None:
target = self.target_transform(target)
else:
path = self.samples[index]
sample = self.loader(path)
if self.transform is not None:
sample = self.transform(sample)
return (sample, target) if self.with_targets else sample
def __len__(self) -> int:
return len(self.samples)
_default_train_transforms = T.Compose([
T.RandomResizedCrop(224),
T.RandomHorizontalFlip(),
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
_default_valid_transforms = T.Compose([
T.Resize(256),
T.CenterCrop(224),
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
# todo: torch.nn.modules.module.ModuleAttributeError: 'Resize' object has no attribute '_forward_hooks'
# Find better fix and raised issue on torchvision.
_default_valid_transforms.transforms[0]._forward_hooks = {}
class ImageClassificationDataPipeline(ClassificationDataPipeline):
def __init__(
self,
train_transform: Optional[Callable] = _default_train_transforms,
valid_transform: Optional[Callable] = _default_valid_transforms,
use_valid_transform: bool = True,
loader: Callable = _pil_loader
):
self._train_transform = train_transform
self._valid_transform = valid_transform
self._use_valid_transform = use_valid_transform
self._loader = loader
def before_collate(self, samples: Any) -> Any:
if _contains_any_tensor(samples):
return samples
if isinstance(samples, str):
samples = [samples]
if isinstance(samples, (list, tuple)) and all(isinstance(p, str) for p in samples):
outputs = []
for sample in samples:
output = self._loader(sample)
transform = self._valid_transform if self._use_valid_transform else self._train_transform
outputs.append(transform(output))
return outputs
raise MisconfigurationException("The samples should either be a tensor or a list of paths.")
class ImageClassificationData(DataModule):
"""Data module for image classification tasks."""
@classmethod
def from_filepaths(
cls,
train_filepaths: Optional[Sequence[Union[str, pathlib.Path]]] = None,
train_labels: Optional[Sequence] = None,
train_transform: Optional[Callable] = _default_train_transforms,
valid_filepaths: Optional[Sequence[Union[str, pathlib.Path]]] = None,
valid_labels: Optional[Sequence] = None,
valid_transform: Optional[Callable] = _default_valid_transforms,
test_filepaths: Optional[Sequence[Union[str, pathlib.Path]]] = None,
test_labels: Optional[Sequence] = None,
loader: Callable = _pil_loader,
batch_size: int = 64,
num_workers: Optional[int] = None,
**kwargs
):
"""Creates a ImageClassificationData object from lists of image filepaths and labels
Args:
train_filepaths: sequence of file paths for training dataset. Defaults to None.
train_labels: sequence of labels for training dataset. Defaults to None.
train_transform: transforms for training dataset. Defaults to None.
valid_filepaths: sequence of file paths for validation dataset. Defaults to None.
valid_labels: sequence of labels for validation dataset. Defaults to None.
valid_transform: transforms for validation and testing dataset. Defaults to None.
test_filepaths: sequence of file paths for test dataset. Defaults to None.
test_labels: sequence of labels for test dataset. Defaults to None.
loader: function to load an image file. Defaults to None.
batch_size: the batchsize to use for parallel loading. Defaults to 64.
num_workers: The number of workers to use for parallelized loading.
Defaults to None which equals the number of available CPU threads.
Returns:
ImageClassificationData: The constructed data module.
Examples:
>>> img_data = ImageClassificationData.from_filepaths(["a.png", "b.png"], [0, 1]) # doctest: +SKIP
"""
train_ds = FilepathDataset(
filepaths=train_filepaths,
labels=train_labels,
loader=loader,
transform=train_transform,
)
valid_ds = (
FilepathDataset(
filepaths=valid_filepaths,
labels=valid_labels,
loader=loader,
transform=valid_transform,
) if valid_filepaths is not None else None
)
test_ds = (
FilepathDataset(
filepaths=test_filepaths,
labels=test_labels,
loader=loader,
transform=valid_transform,
) if test_filepaths is not None else None
)
return cls(
train_ds=train_ds,
valid_ds=valid_ds,
test_ds=test_ds,
batch_size=batch_size,
num_workers=num_workers,
)
@classmethod
def from_folders(
cls,
train_folder: Optional[Union[str, pathlib.Path]],
train_transform: Optional[Callable] = _default_train_transforms,
valid_folder: Optional[Union[str, pathlib.Path]] = None,
valid_transform: Optional[Callable] = _default_valid_transforms,
test_folder: Optional[Union[str, pathlib.Path]] = None,
loader: Callable = _pil_loader,
batch_size: int = 4,
num_workers: Optional[int] = None,
**kwargs
):
"""
Creates a ImageClassificationData object from folders of images arranged in this way: ::
train/dog/xxx.png
train/dog/xxy.png
train/dog/xxz.png
train/cat/123.png
train/cat/nsdf3.png
train/cat/asd932.png
Args:
train_folder: Path to training folder.
train_transform: Image transform to use for training set.
valid_folder: Path to validation folder.
valid_transform: Image transform to use for validation and test set.
test_folder: Path to test folder.
loader: A function to load an image given its path.
batch_size: Batch size for data loading.
num_workers: The number of workers to use for parallelized loading.
Defaults to None which equals the number of available CPU threads.
Returns:
ImageClassificationData: the constructed data module
Examples:
>>> img_data = ImageClassificationData.from_folders("train/") # doctest: +SKIP
"""
train_ds = FlashDatasetFolder(train_folder, transform=train_transform, loader=loader)
valid_ds = (
FlashDatasetFolder(valid_folder, transform=valid_transform, loader=loader)
if valid_folder is not None else None
)
test_ds = (
FlashDatasetFolder(test_folder, transform=valid_transform, loader=loader)
if test_folder is not None else None
)
datamodule = cls(
train_ds=train_ds,
valid_ds=valid_ds,
test_ds=test_ds,
batch_size=batch_size,
num_workers=num_workers,
)
datamodule.num_classes = len(train_ds.classes)
datamodule.data_pipeline = ImageClassificationDataPipeline(
train_transform=train_transform, valid_transform=valid_transform, loader=loader
)
return datamodule
@classmethod
def from_folder(
cls,
folder: Union[str, pathlib.Path],
transform: Optional[Callable] = _default_valid_transforms,
loader: Callable = _pil_loader,
batch_size: int = 64,
num_workers: Optional[int] = None,
**kwargs
):
"""
Creates a ImageClassificationData object from folders of images arranged in this way: ::
folder/dog_xxx.png
folder/dog_xxy.png
folder/dog_xxz.png
folder/cat_123.png
folder/cat_nsdf3.png
folder/cat_asd932_.png
Args:
folder: Path to the data folder.
transform: Image transform to apply to the data.
loader: A function to load an image given its path.
batch_size: Batch size for data loading.
num_workers: The number of workers to use for parallelized loading.
Defaults to None which equals the number of available CPU threads.
Returns:
ImageClassificationData: the constructed data module
Examples:
>>> img_data = ImageClassificationData.from_folder("my_folder/") # doctest: +SKIP
"""
if not os.path.isdir(folder):
raise MisconfigurationException("folder should be a directory")
filenames = os.listdir(folder)
if any(not has_file_allowed_extension(f, IMG_EXTENSIONS) for f in filenames):
raise MisconfigurationException(
"No images with allowed extensions {IMG_EXTENSIONS} where found in {folder}"
)
test_ds = (
FlashDatasetFolder(
folder,
transform=transform,
loader=loader,
with_targets=False,
img_paths=[os.path.join(folder, f) for f in filenames]
)
)
datamodule = cls(
test_ds=test_ds,
batch_size=batch_size,
num_workers=num_workers,
)
datamodule.data_pipeline = ImageClassificationDataPipeline(valid_transform=transform, loader=loader)
return datamodule
@staticmethod
def default_pipeline() -> ImageClassificationDataPipeline:
return ImageClassificationDataPipeline(
train_transform=_default_train_transforms, valid_transform=_default_valid_transforms, loader=_pil_loader
)