@@ -50,6 +50,7 @@ def make_dataset(
50
50
class_to_idx : Optional [Dict [str , int ]] = None ,
51
51
extensions : Optional [Union [str , Tuple [str , ...]]] = None ,
52
52
is_valid_file : Optional [Callable [[str ], bool ]] = None ,
53
+ allow_empty : bool = False ,
53
54
) -> List [Tuple [str , int ]]:
54
55
"""Generates a list of samples of a form (path_to_sample, class).
55
56
@@ -95,7 +96,7 @@ def is_valid_file(x: str) -> bool:
95
96
available_classes .add (target_class )
96
97
97
98
empty_classes = set (class_to_idx .keys ()) - available_classes
98
- if empty_classes :
99
+ if empty_classes and not allow_empty :
99
100
msg = f"Found no valid file for the classes { ', ' .join (sorted (empty_classes ))} . "
100
101
if extensions is not None :
101
102
msg += f"Supported extensions are: { extensions if isinstance (extensions , str ) else ', ' .join (extensions )} "
@@ -123,6 +124,8 @@ class DatasetFolder(VisionDataset):
123
124
is_valid_file (callable, optional): A function that takes path of a file
124
125
and check if the file is a valid file (used to check of corrupt files)
125
126
both extensions and is_valid_file should not be passed.
127
+ allow_empty(bool, optional): If True, empty folders are considered to be valid classes.
128
+ An error is raised on empty folders if False (default).
126
129
127
130
Attributes:
128
131
classes (list): List of the class names sorted alphabetically.
@@ -139,10 +142,17 @@ def __init__(
139
142
transform : Optional [Callable ] = None ,
140
143
target_transform : Optional [Callable ] = None ,
141
144
is_valid_file : Optional [Callable [[str ], bool ]] = None ,
145
+ allow_empty : bool = False ,
142
146
) -> None :
143
147
super ().__init__ (root , transform = transform , target_transform = target_transform )
144
148
classes , class_to_idx = self .find_classes (self .root )
145
- samples = self .make_dataset (self .root , class_to_idx , extensions , is_valid_file )
149
+ samples = self .make_dataset (
150
+ self .root ,
151
+ class_to_idx = class_to_idx ,
152
+ extensions = extensions ,
153
+ is_valid_file = is_valid_file ,
154
+ allow_empty = allow_empty ,
155
+ )
146
156
147
157
self .loader = loader
148
158
self .extensions = extensions
@@ -158,6 +168,7 @@ def make_dataset(
158
168
class_to_idx : Dict [str , int ],
159
169
extensions : Optional [Tuple [str , ...]] = None ,
160
170
is_valid_file : Optional [Callable [[str ], bool ]] = None ,
171
+ allow_empty : bool = False ,
161
172
) -> List [Tuple [str , int ]]:
162
173
"""Generates a list of samples of a form (path_to_sample, class).
163
174
@@ -172,6 +183,8 @@ def make_dataset(
172
183
and checks if the file is a valid file
173
184
(used to check of corrupt files) both extensions and
174
185
is_valid_file should not be passed. Defaults to None.
186
+ allow_empty(bool, optional): If True, empty folders are considered to be valid classes.
187
+ An error is raised on empty folders if False (default).
175
188
176
189
Raises:
177
190
ValueError: In case ``class_to_idx`` is empty.
@@ -186,7 +199,9 @@ def make_dataset(
186
199
# find_classes() function, instead of using that of the find_classes() method, which
187
200
# is potentially overridden and thus could have a different logic.
188
201
raise ValueError ("The class_to_idx parameter cannot be None." )
189
- return make_dataset (directory , class_to_idx , extensions = extensions , is_valid_file = is_valid_file )
202
+ return make_dataset (
203
+ directory , class_to_idx , extensions = extensions , is_valid_file = is_valid_file , allow_empty = allow_empty
204
+ )
190
205
191
206
def find_classes (self , directory : str ) -> Tuple [List [str ], Dict [str , int ]]:
192
207
"""Find the class folders in a dataset structured as follows::
@@ -291,6 +306,8 @@ class ImageFolder(DatasetFolder):
291
306
loader (callable, optional): A function to load an image given its path.
292
307
is_valid_file (callable, optional): A function that takes path of an Image file
293
308
and check if the file is a valid file (used to check of corrupt files)
309
+ allow_empty(bool, optional): If True, empty folders are considered to be valid classes.
310
+ An error is raised on empty folders if False (default).
294
311
295
312
Attributes:
296
313
classes (list): List of the class names sorted alphabetically.
@@ -305,6 +322,7 @@ def __init__(
305
322
target_transform : Optional [Callable ] = None ,
306
323
loader : Callable [[str ], Any ] = default_loader ,
307
324
is_valid_file : Optional [Callable [[str ], bool ]] = None ,
325
+ allow_empty : bool = False ,
308
326
):
309
327
super ().__init__ (
310
328
root ,
@@ -313,5 +331,6 @@ def __init__(
313
331
transform = transform ,
314
332
target_transform = target_transform ,
315
333
is_valid_file = is_valid_file ,
334
+ allow_empty = allow_empty ,
316
335
)
317
336
self .imgs = self .samples
0 commit comments