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/efficient ad normalize before every validation #1441

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
7 changes: 3 additions & 4 deletions src/anomalib/models/efficient_ad/lightning_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def map_norm_quantiles(self, dataloader: DataLoader) -> dict[str, Tensor]:
for batch in tqdm.tqdm(dataloader, desc="Calculate Validation Dataset Quantiles", position=0, leave=True):
for img, label in zip(batch["image"], batch["label"]):
if label == 0: # only use good images of validation set!
output = self.model(img.to(self.device))
output = self.model(img.to(self.device), normalize=False)
map_st = output["map_st"]
map_ae = output["map_ae"]
maps_st.append(map_st)
Expand Down Expand Up @@ -256,9 +256,8 @@ def on_validation_start(self) -> None:
"""
Calculate the feature map quantiles of the validation dataset and push to the model.
"""
if (self.current_epoch + 1) == self.trainer.max_epochs:
map_norm_quantiles = self.map_norm_quantiles(self.trainer.datamodule.val_dataloader())
self.model.quantiles.update(map_norm_quantiles)
map_norm_quantiles = self.map_norm_quantiles(self.trainer.datamodule.val_dataloader())
self.model.quantiles.update(map_norm_quantiles)

def validation_step(self, batch: dict[str, str | Tensor], *args, **kwargs) -> STEP_OUTPUT:
"""Validation Step of EfficientAd returns anomaly maps for the input image batch
Expand Down
5 changes: 3 additions & 2 deletions src/anomalib/models/efficient_ad/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,11 @@ def choose_random_aug_image(self, image: Tensor) -> Tensor:
transform_function = random.choice(transform_functions) # nosec: B311
return transform_function(image, coefficient)

def forward(self, batch: Tensor, batch_imagenet: Tensor = None) -> Tensor | dict:
def forward(self, batch: Tensor, batch_imagenet: Tensor = None, normalize: bool = True) -> Tensor | dict:
"""Prediction by EfficientAd models.

Args:
normalize: Normalize anomaly maps or not.
batch (Tensor): Input images.

Returns:
Expand Down Expand Up @@ -346,7 +347,7 @@ def forward(self, batch: Tensor, batch_imagenet: Tensor = None) -> Tensor | dict
map_st = F.interpolate(map_st, size=(self.input_size[0], self.input_size[1]), mode="bilinear")
map_stae = F.interpolate(map_stae, size=(self.input_size[0], self.input_size[1]), mode="bilinear")

if self.is_set(self.quantiles):
if self.is_set(self.quantiles) and normalize:
map_st = 0.1 * (map_st - self.quantiles["qa_st"]) / (self.quantiles["qb_st"] - self.quantiles["qa_st"])
map_stae = (
0.1 * (map_stae - self.quantiles["qa_ae"]) / (self.quantiles["qb_ae"] - self.quantiles["qa_ae"])
Expand Down
Loading