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

Calculate feature map shape patchcore #148

Merged
merged 9 commits into from
Mar 22, 2022
18 changes: 11 additions & 7 deletions anomalib/models/patchcore/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ def __init__(
self.input_size = input_size
self.sigma = sigma

def compute_anomaly_map(self, patch_scores: torch.Tensor) -> torch.Tensor:
def compute_anomaly_map(self, patch_scores: torch.Tensor, feature_map_shape: torch.Tensor) -> torch.Tensor:
alexriedel1 marked this conversation as resolved.
Show resolved Hide resolved
"""Pixel Level Anomaly Heatmap.

Args:
patch_scores (torch.Tensor): Patch-level anomaly scores
Returns:
torch.Tensor: Map of the pixel-level anomaly scores
"""
# TODO: https://github.com/openvinotoolkit/anomalib/issues/40
batch_size = len(patch_scores) // (28 * 28)
w, h = feature_map_shape
batch_size = len(patch_scores) // (w * h)

anomaly_map = patch_scores[:, 0].reshape((batch_size, 1, 28, 28))
anomaly_map = patch_scores[:, 0].reshape((batch_size, 1, w, h))
anomaly_map = F.interpolate(anomaly_map, size=(self.input_size[0], self.input_size[1]))

kernel_size = 2 * int(4.0 * self.sigma + 0.5) + 1
Expand Down Expand Up @@ -99,7 +99,9 @@ def __call__(self, **kwargs: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
raise ValueError(f"Expected key `patch_scores`. Found {kwargs.keys()}")

patch_scores = kwargs["patch_scores"]
anomaly_map = self.compute_anomaly_map(patch_scores)
feature_map_shape = kwargs["feature_map_shape"]

anomaly_map = self.compute_anomaly_map(patch_scores, feature_map_shape)
anomaly_score = self.compute_anomaly_score(patch_scores)
return anomaly_map, anomaly_score

Expand Down Expand Up @@ -161,14 +163,16 @@ def forward(self, input_tensor: Tensor) -> Union[torch.Tensor, Tuple[torch.Tenso

if self.apply_tiling:
embedding = self.tiler.untile(embedding)


feature_map_shape = embedding.shape[-2:]
alexriedel1 marked this conversation as resolved.
Show resolved Hide resolved
embedding = self.reshape_embedding(embedding)


if self.training:
output = embedding
else:
patch_scores = self.nearest_neighbors(embedding=embedding, n_neighbors=9)
anomaly_map, anomaly_score = self.anomaly_map_generator(patch_scores=patch_scores)
anomaly_map, anomaly_score = self.anomaly_map_generator(patch_scores=patch_scores, feature_map_shape=feature_map_shape)
output = (anomaly_map, anomaly_score)

return output
Expand Down