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

Adding noise perturbation detector with Gaussian noise #52

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
72 changes: 72 additions & 0 deletions giskard_vision/core/dataloaders/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,78 @@ def get_image(self, idx: int) -> np.ndarray:
return cv2.GaussianBlur(image, self._kernel_size, *self._sigma)


class NoisyDataLoader(DataLoaderWrapper):
"""Wrapper class for a DataIteratorBase, providing noisy images.

Args:
dataloader (DataIteratorBase): The data loader to be wrapped.
sigma (float): Standard deviation of the Gaussian noise.

Returns:
NoisyDataLoader: Noisy data loader instance.
"""

def __init__(
self,
dataloader: DataIteratorBase,
sigma: float = 0.1,
) -> None:
"""
Initializes the BlurredDataLoader.

Args:
dataloader (DataIteratorBase): The data loader to be wrapped.
sigma (float): Standard deviation of the Gaussian noise.
"""
super().__init__(dataloader)
self._sigma = sigma

@property
def name(self):
"""
Gets the name of the blurred data loader.

Returns:
str: The name of the blurred data loader.
"""
return "noisy"

def get_image(self, idx: int) -> np.ndarray:
"""
Gets a blurred image using Gaussian blur.

Args:
idx (int): Index of the data.

Returns:
np.ndarray: Blurred image data.
"""
image = super().get_image(idx)
return self.add_gaussian_noise(image, self._sigma * 255)

def add_gaussian_noise(self, image, std_dev):
"""
Add Gaussian noise to the image

Args:
image (np.ndarray): Image
std_dev (float): Standard deviation of the Gaussian noise.

Returns:
np.ndarray: Noisy image
"""
# Generate Gaussian noise
noise = np.random.normal(0, std_dev, image.shape).astype(np.float32)

# Add the noise to the image
noisy_image = cv2.add(image.astype(np.float32), noise)

# Clip the values to stay within valid range (0-255 for uint8)
noisy_image = np.clip(noisy_image, 0, 255).astype(np.uint8)

return noisy_image


class ColoredDataLoader(DataLoaderWrapper):
"""Wrapper class for a DataIteratorBase, providing color-altered images using OpenCV color conversion.

Expand Down
23 changes: 23 additions & 0 deletions giskard_vision/core/detectors/transformation_noise_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from giskard_vision.core.dataloaders.wrappers import NoisyDataLoader

from ...core.detectors.decorator import maybe_detector
from .perturbation import PerturbationBaseDetector, Robustness


@maybe_detector("noise", tags=["vision", "robustness", "image_classification", "landmark", "object_detection"])
class TransformationNoiseDetectorLandmark(PerturbationBaseDetector):
"""
Detector that evaluates models performance on noisy images
"""

issue_group = Robustness

def __init__(self, sigma=0.1):
self.sigma = sigma

def get_dataloaders(self, dataset):
dl = NoisyDataLoader(dataset, self.sigma)

dls = [dl]

return dls
Loading