Fix index_directory directory structure ignoring when passing labels explicitly
@@ -496,10 +496,13 @@ def index_directory(
seed=None,
follow_links=False,
):
- """Make list of all files in the subdirs of `directory`, with their labels.
+ """Make list of all files in `directory`, with their labels.
Args:
- directory: The target directory (string).
+ directory: Directory where the data is located.
+ If `labels` is "inferred", it should contain
+ subdirectories, each containing files for a class.
+ Otherwise, the directory structure is ignored.
labels: Either "inferred"
(labels are generated from the directory structure),
None (no labels),
@@ -524,8 +527,8 @@ def index_directory(
class_names: names of the classes corresponding to these labels, in
order.
"""
- if labels is None:
- # in the no-label case, index from the parent directory down.
+ if labels != "inferred":
+ # in the explicit/no-label cases, index from the parent directory down.
subdirs = [""]
class_names = subdirs
else:
@@ -572,6 +575,7 @@ def index_directory(
f"{len(labels)} while we found {len(filenames)} files "
f"in directory {directory}."
)
+ class_names = sorted(set(labels))
else:
i = 0
labels = np.zeros((len(filenames),), dtype="int32")
@@ -641,7 +645,9 @@ def index_subdirectory(directory, class_indices, follow_links, formats):
return filenames, labels
-def get_training_or_validation_split(samples, labels, validation_split, subset):
+def get_training_or_validation_split(
+ samples, labels, validation_split, subset
+):
"""Potentially restict samples & labels to a training or validation split.
Args:
