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 patchcore interpolation #1335

Merged
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
7 changes: 5 additions & 2 deletions src/anomalib/models/patchcore/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def generate_embedding(self, features: dict[str, Tensor]) -> Tensor:
embeddings = features[self.layers[0]]
for layer in self.layers[1:]:
layer_embedding = features[layer]
layer_embedding = F.interpolate(layer_embedding, size=embeddings.shape[-2:], mode="nearest")
layer_embedding = F.interpolate(layer_embedding, size=embeddings.shape[-2:], mode="bilinear")
embeddings = torch.cat((embeddings, layer_embedding), 1)

return embeddings
Expand Down Expand Up @@ -208,7 +208,10 @@ def compute_anomaly_score(self, patch_scores: Tensor, locations: Tensor, embeddi
# 3. Find the support samples of the nearest neighbor in the membank
nn_sample = self.memory_bank[nn_index, :] # m^* in the paper
# indices of N_b(m^*) in the paper
_, support_samples = self.nearest_neighbors(nn_sample, n_neighbors=self.num_neighbors)
memory_bank_effective_size = self.memory_bank.shape[0] # edge case when memory bank is too small
_, support_samples = self.nearest_neighbors(
nn_sample, n_neighbors=min(self.num_neighbors, memory_bank_effective_size)
)
# 4. Find the distance of the patch features to each of the support samples
distances = self.euclidean_dist(max_patches_features.unsqueeze(1), self.memory_bank[support_samples])
# 5. Apply softmax to find the weights
Expand Down