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

Added extension as an option when saving the result images. #162

Merged
merged 6 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
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
31 changes: 24 additions & 7 deletions anomalib/data/btech.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from torch.utils.data import DataLoader
from torch.utils.data.dataset import Dataset
from torchvision.datasets.folder import VisionDataset
from tqdm import tqdm

from anomalib.data.inference import InferenceDataset
from anomalib.data.utils import DownloadProgressBar, read_image
Expand Down Expand Up @@ -94,7 +95,9 @@ def make_btech_dataset(
Returns:
DataFrame: an output dataframe containing samples for the requested split (ie., train or test)
"""
samples_list = [(str(path),) + filename.parts[-3:] for filename in path.glob("**/*.bmp")]
samples_list = [
(str(path),) + filename.parts[-3:] for filename in path.glob("**/*") if filename.suffix in (".bmp", ".png")
]
if len(samples_list) == 0:
raise RuntimeError(f"Found 0 images in {path}")

Expand All @@ -107,7 +110,7 @@ def make_btech_dataset(
+ "/ground_truth/"
+ samples.label
+ "/"
+ samples.image_path.str.rstrip("bmp").str.rstrip(".")
+ samples.image_path.str.rstrip("png").str.rstrip(".")
+ ".png"
)

Expand Down Expand Up @@ -337,10 +340,9 @@ def prepare_data(self) -> None:
if (self.root / self.category).is_dir():
logging.info("Found the dataset.")
else:
self.root.mkdir(parents=True, exist_ok=True)
zip_filename = self.root / "btad.zip"
zip_filename = self.root.parent / "btad.zip"

logging.info("Downloading the dataset.")
logging.info("Downloading the BTech dataset.")
with DownloadProgressBar(unit="B", unit_scale=True, miniters=1, desc="BTech") as progress_bar:
urlretrieve(
url="https://avires.dimi.uniud.it/papers/btad/btad.zip",
Expand All @@ -350,10 +352,25 @@ def prepare_data(self) -> None:

logging.info("Extracting the dataset.")
with zipfile.ZipFile(zip_filename, "r") as zip_file:
zip_file.extractall(self.root)
zip_file.extractall(self.root.parent)

logging.info("Renaming the dataset directory")
shutil.move(src=str(self.root / "BTech_Dataset_transformed"), dst=str(self.root / "BTech"))
shutil.move(src=str(self.root.parent / "BTech_Dataset_transformed"), dst=str(self.root))

# NOTE: Each BTech category has different image extension as follows
# | Category | Image | Mask |
# |----------|-------|------|
# | 01 | bmp | png |
# | 02 | png | png |
# | 03 | bmp | bmp |
# To avoid any conflict, the following script converts all the extensions to png.
# This solution works fine, but it's also possible to properly ready the bmp and
# png filenames from categories in `make_btech_dataset` function.
logging.info("Convert the bmp formats to png to have consistent image extensions")
for filename in tqdm(self.root.glob("**/*.bmp"), desc="Converting bmp to png"):
image = cv2.imread(str(filename))
cv2.imwrite(str(filename.with_suffix(".png")), image)
filename.unlink()

logging.info("Cleaning the tar file")
zip_filename.unlink()
Expand Down
2 changes: 1 addition & 1 deletion anomalib/models/dfkde/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(self, hparams: Union[DictConfig, ListConfig]):
self.embeddings: List[Tensor] = []

@staticmethod
def configure_optimizers():
def configure_optimizers(): # pylint: disable=arguments-differ
"""DFKDE doesn't require optimization, therefore returns no optimizers."""
return None

Expand Down
2 changes: 1 addition & 1 deletion anomalib/models/dfm/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, hparams: Union[DictConfig, ListConfig]):
self.embeddings: List[Tensor] = []

@staticmethod
def configure_optimizers() -> None:
def configure_optimizers() -> None: # pylint: disable=arguments-differ
"""DFM doesn't require optimization, therefore returns no optimizers."""
return None

Expand Down
2 changes: 1 addition & 1 deletion anomalib/models/padim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def __init__(self, hparams: Union[DictConfig, ListConfig]):
self.embeddings: List[Tensor] = []

@staticmethod
def configure_optimizers():
def configure_optimizers(): # pylint: disable=arguments-differ
"""PADIM doesn't require optimization, therefore returns no optimizers."""
return None

Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ ignore = E203,W503
extension-pkg-whitelist = cv2
ignored-modules = cv2
disable = duplicate-code,
arguments-differ,
fixme,
import-error,
no-self-use,
Expand Down