Skip to content

Commit

Permalink
Fix KeyError:'label' in classification folder dataset (#175)
Browse files Browse the repository at this point in the history
* Fix classification label bug in folder dataset

* bump black to 22.3.0

* bump black to 22.3.0

* format with new black
  • Loading branch information
samet-akcay committed Mar 30, 2022
1 parent 8c1a04f commit c6fc4dd
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repos:

# python code formatting
- repo: https://github.com/psf/black
rev: 20.8b1
rev: 22.3.0
hooks:
- id: black
args: [--line-length, "120"]
Expand Down
8 changes: 4 additions & 4 deletions anomalib/data/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ def __getitem__(self, index: int) -> Dict[str, Union[str, Tensor]]:
image_path = self.samples.image_path[index]
image = read_image(image_path)

if self.split == "train" or self.task == "classification":
pre_processed = self.pre_process(image=image)
item = {"image": pre_processed["image"]}
elif self.split in ["val", "test"]:
pre_processed = self.pre_process(image=image)
item = {"image": pre_processed["image"]}

if self.split in ["val", "test"]:
label_index = self.samples.label_index[index]

item["image_path"] = image_path
Expand Down
2 changes: 1 addition & 1 deletion anomalib/models/cflow/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_logp(dim_feature_vector: int, p_u: torch.Tensor, logdet_j: torch.Tensor)
torch.Tensor: Log probability
"""
ln_sqrt_2pi = -np.log(np.sqrt(2 * np.pi)) # ln(sqrt(2*pi))
logp = dim_feature_vector * ln_sqrt_2pi - 0.5 * torch.sum(p_u ** 2, 1) + logdet_j
logp = dim_feature_vector * ln_sqrt_2pi - 0.5 * torch.sum(p_u**2, 1) + logdet_j
return logp


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def johnson_lindenstrauss_min_dim(self, n_samples: int, eps: float = 0.1):
eps (float, optional): Minimum distortion rate. Defaults to 0.1.
"""

denominator = (eps ** 2 / 2) - (eps ** 3 / 3)
denominator = (eps**2 / 2) - (eps**3 / 3)
return (4 * np.log(n_samples) / denominator).astype(np.int64)

def fit(self, embedding: Tensor) -> "SparseRandomProjection":
Expand Down
2 changes: 1 addition & 1 deletion anomalib/models/components/stats/kde.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def fit(self, dataset: Tensor) -> None:

cov_mat = self.cov(dataset.T)
inv_cov_mat = torch.linalg.inv(cov_mat)
inv_cov = inv_cov_mat / factor ** 2
inv_cov = inv_cov_mat / factor**2

# transform data to account for bandwidth
bw_transform = torch.linalg.cholesky(inv_cov)
Expand Down
2 changes: 1 addition & 1 deletion anomalib/models/ganomaly/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __init__(

# Calculate input channel size to recreate inverse pyramid
exp_factor = int(math.log(input_size // 4, 2)) - 1
n_input_features = n_features * (2 ** exp_factor)
n_input_features = n_features * (2**exp_factor)

# CNN layer for latent vector input
self.latent_input.add_module(
Expand Down
39 changes: 20 additions & 19 deletions anomalib/utils/callbacks/visualizer_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,26 @@ def on_test_batch_end(
normalize = True # raw anomaly maps. Still need to normalize
threshold = pl_module.pixel_metrics.F1.threshold

for (filename, image, true_mask, anomaly_map) in zip(
outputs["image_path"], outputs["image"], outputs["mask"], outputs["anomaly_maps"]
):
image = Denormalize()(image.cpu())
true_mask = true_mask.cpu().numpy()
anomaly_map = anomaly_map.cpu().numpy()

heat_map = superimpose_anomaly_map(anomaly_map, image, normalize=normalize)
pred_mask = compute_mask(anomaly_map, threshold)
vis_img = mark_boundaries(image, pred_mask, color=(1, 0, 0), mode="thick")

visualizer = Visualizer(num_rows=1, num_cols=5, figure_size=(12, 3))
visualizer.add_image(image=image, title="Image")
visualizer.add_image(image=true_mask, color_map="gray", title="Ground Truth")
visualizer.add_image(image=heat_map, title="Predicted Heat Map")
visualizer.add_image(image=pred_mask, color_map="gray", title="Predicted Mask")
visualizer.add_image(image=vis_img, title="Segmentation Result")
self._add_images(visualizer, pl_module, Path(filename))
visualizer.close()
if isinstance(outputs, dict) and "mask" in outputs.keys():
for (filename, image, true_mask, anomaly_map) in zip(
outputs["image_path"], outputs["image"], outputs["mask"], outputs["anomaly_maps"]
):
image = Denormalize()(image.cpu())
true_mask = true_mask.cpu().numpy()
anomaly_map = anomaly_map.cpu().numpy()

heat_map = superimpose_anomaly_map(anomaly_map, image, normalize=normalize)
pred_mask = compute_mask(anomaly_map, threshold)
vis_img = mark_boundaries(image, pred_mask, color=(1, 0, 0), mode="thick")

visualizer = Visualizer(num_rows=1, num_cols=5, figure_size=(12, 3))
visualizer.add_image(image=image, title="Image")
visualizer.add_image(image=true_mask, color_map="gray", title="Ground Truth")
visualizer.add_image(image=heat_map, title="Predicted Heat Map")
visualizer.add_image(image=pred_mask, color_map="gray", title="Predicted Mask")
visualizer.add_image(image=vis_img, title="Segmentation Result")
self._add_images(visualizer, pl_module, Path(filename))
visualizer.close()

def on_test_end(self, _trainer: pl.Trainer, pl_module: pl.LightningModule) -> None:
"""Sync logs.
Expand Down
4 changes: 2 additions & 2 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
black
black==22.3.0
coverage
flake8
flaky
isort
isort==5.10.1
mypy
pytest
pylint
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ envlist =

[testenv:black]
basepython = python3
deps = black==20.8b1
deps = black==22.3.0
commands = black --check --diff anomalib -l 120

[testenv:isort]
Expand Down

0 comments on commit c6fc4dd

Please sign in to comment.