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

WinCLIP improvements #1985

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Changed

- WinCLIP: set device in text embedding collection and apply forward pass with no grad, by @djdameln in https://github.com/openvinotoolkit/anomalib/pull/1984
- πŸ”¨Rename OptimalF1 to F1Max for consistency with the literature, by @samet-akcay in https://github.com/openvinotoolkit/anomalib/pull/1980
- 🐞Update OptimalF1 score to use BinaryPrecisionRecallCurve and remove num_classes by @ashwinvaidya17 in https://github.com/openvinotoolkit/anomalib/pull/1972

Expand Down
7 changes: 5 additions & 2 deletions src/anomalib/models/image/winclip/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def _get_window_embeddings(self, feature_map: torch.Tensor, masks: torch.Tensor)

return pooled.reshape((n_masks, batch_size, -1)).permute(1, 0, 2)

@torch.no_grad
def forward(self, batch: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
"""Forward-pass through the model to obtain image and pixel scores.

Expand Down Expand Up @@ -325,15 +326,17 @@ def _collect_text_embeddings(self, class_name: str) -> None:
Args:
class_name (str): The name of the object class used in the prompt ensemble.
"""
# get the device, this is to ensure that we move the text embeddings to the same device as the model
device = next(self.parameters()).device
# collect prompt ensemble
normal_prompts, anomalous_prompts = create_prompt_ensemble(class_name)
# tokenize prompts
normal_tokens = tokenize(normal_prompts)
anomalous_tokens = tokenize(anomalous_prompts)
# encode tokens to obtain prompt embeddings
with torch.no_grad():
normal_embeddings = self.clip.encode_text(normal_tokens)
anomalous_embeddings = self.clip.encode_text(anomalous_tokens)
normal_embeddings = self.clip.encode_text(normal_tokens.to(device))
anomalous_embeddings = self.clip.encode_text(anomalous_tokens.to(device))
djdameln marked this conversation as resolved.
Show resolved Hide resolved
# average prompt embeddings
normal_embeddings = torch.mean(normal_embeddings, dim=0, keepdim=True)
anomalous_embeddings = torch.mean(anomalous_embeddings, dim=0, keepdim=True)
Expand Down
Loading