Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🛠 Fix mask filenames in folder dataset #249

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions anomalib/data/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ def _prepare_files_labels(
if extensions is None:
extensions = IMG_EXTENSIONS

filenames = [f for f in path.glob(r"**/*") if f.suffix in extensions]
if isinstance(extensions, str):
extensions = (extensions,)

filenames = [f for f in path.glob(r"**/*") if f.suffix in extensions and not f.is_dir()]
if len(filenames) == 0:
raise RuntimeError(f"Found 0 {path_type} images in {path}")

Expand Down Expand Up @@ -141,11 +144,10 @@ def make_dataset(
# If a path to mask is provided, add it to the sample dataframe.
if mask_dir is not None:
mask_dir = _check_and_convert_path(mask_dir)
normal_gt = ["" for f in samples.loc[samples.label_index == 0]["image_path"]]
abnormal_gt = [str(mask_dir / f.name) for f in samples.loc[samples.label_index == 1]["image_path"]]
gt_filenames = normal_gt + abnormal_gt

samples["mask_path"] = gt_filenames
samples["mask_path"] = ""
for index, row in samples.iterrows():
if row.label_index == 1:
samples["mask_path"][index] = str(mask_dir / row.image_path.name)

# Ensure the pathlib objects are converted to str.
# This is because torch dataloader doesn't like pathlib.
Expand Down Expand Up @@ -463,6 +465,7 @@ def setup(self, stage: Optional[str] = None) -> None:
self.train_data = FolderDataset(
normal_dir=self.normal_dir,
abnormal_dir=self.abnormal_dir,
normal_test_dir=self.normal_test,
split="train",
split_ratio=self.split_ratio,
mask_dir=self.mask_dir,
Expand All @@ -477,6 +480,7 @@ def setup(self, stage: Optional[str] = None) -> None:
self.val_data = FolderDataset(
normal_dir=self.normal_dir,
abnormal_dir=self.abnormal_dir,
normal_test_dir=self.normal_test,
split="val",
split_ratio=self.split_ratio,
mask_dir=self.mask_dir,
Expand Down