Skip to content

Commit

Permalink
Denomalize and visualize the ImageNet-normalized batch.
Browse files Browse the repository at this point in the history
  • Loading branch information
mzweilin committed May 15, 2024
1 parent 395c5f6 commit c2420fc
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/anomalib/utils/visualization/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from skimage.segmentation import mark_boundaries

from anomalib import TaskType
from anomalib.data.utils import read_image
from anomalib.utils.post_processing import add_anomalous_label, add_normal_label, draw_boxes, superimpose_anomaly_map

from .base import BaseVisualizer, GeneratorResult, VisualizationStep
Expand Down Expand Up @@ -127,6 +126,21 @@ def generate(self, **kwargs) -> Iterator[GeneratorResult]:
raise ValueError(msg)
return self._visualize_batch(outputs)

def denormalize_imagenet_to_uint8(self, image_normalized: np.ndarray) -> np.ndarray:
"""Convert the NumPy array image from the ImageNet-normalized scale to uint8 [0, 255].
Args:
image_normalized (np.ndarray): A NumPy array of image(s) that are normalized with ImageNet statistics.
Returns:
np.ndarray: Image(s) in the uint8 format.
"""
std = np.array([0.229, 0.224, 0.225]) * 255
mean = np.array([0.485, 0.456, 0.406]) * 255

# We do not clip pixel values here, in case of hiding the problematic input.
return (image_normalized * std + mean).astype(np.uint8)

def _visualize_batch(self, batch: dict) -> Iterator[GeneratorResult]:
"""Yield a visualization result for each item in the batch.
Expand All @@ -139,9 +153,8 @@ def _visualize_batch(self, batch: dict) -> Iterator[GeneratorResult]:
batch_size = batch["image"].shape[0]
for i in range(batch_size):
if "image_path" in batch:
height, width = batch["image"].shape[-2:]
image = (read_image(path=batch["image_path"][i]) * 255).astype(np.uint8)
image = cv2.resize(image, dsize=(width, height), interpolation=cv2.INTER_AREA)
image = batch["image"][i].cpu().numpy().transpose(1, 2, 0) # HWC, RGB
image = self.denormalize_imagenet_to_uint8(image)
elif "video_path" in batch:
height, width = batch["image"].shape[-2:]
image = batch["original_image"][i].squeeze().cpu().numpy()
Expand Down

0 comments on commit c2420fc

Please sign in to comment.