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

Add additive noise #2149

Merged
merged 5 commits into from
Nov 18, 2024
Merged

Add additive noise #2149

merged 5 commits into from
Nov 18, 2024

Conversation

ternaus
Copy link
Collaborator

@ternaus ternaus commented Nov 18, 2024

Summary by Sourcery

Add the AdditiveNoise transform to apply random noise using various distributions and refactor RGBShift to utilize this new transform. Update tests to cover the new functionality.

New Features:

  • Introduce the AdditiveNoise transform, which applies random noise to image channels using various noise distributions such as uniform, gaussian, laplace, beta, and poisson. It supports different spatial modes and allows for configurable noise parameters.

Enhancements:

  • Refactor the RGBShift transform to be a specialized version of the new AdditiveNoise transform, focusing on applying constant uniform shifts to RGB channels.

Tests:

  • Add tests for the new AdditiveNoise transform to ensure its functionality and integration with existing augmentation pipelines.

Copy link
Contributor

sourcery-ai bot commented Nov 18, 2024

Reviewer's Guide by Sourcery

This PR introduces a new AdditiveNoise transform and refactors the existing RGBShift transform to use it as a base. The implementation provides flexible noise generation capabilities with various distribution types (uniform, gaussian, laplace, beta, poisson) and spatial modes (constant, per-pixel, shared).

Class diagram for AdditiveNoise and related classes

classDiagram
    class AdditiveNoise {
        - noise_type: Literal["uniform", "gaussian", "laplace", "beta", "poisson"]
        - spatial_mode: Literal["constant", "per_pixel", "shared"]
        - noise_params: dict[str, Any] | None
        - approximation: float
        - always_apply: bool | None
        - p: float
        + apply(img: np.ndarray, noise_map: np.ndarray, **params: Any) np.ndarray
        + get_params_dependent_on_data(params: dict[str, Any], data: dict[str, Any]) dict[str, Any]
        + get_transform_init_args_names() tuple[str, ...]
    }

    class RGBShift {
        - r_shift_limit: ScaleFloatType
        - g_shift_limit: ScaleFloatType
        - b_shift_limit: ScaleFloatType
        + get_transform_init_args_names() tuple[str, ...]
    }

    class NoiseParamsBase {
        - noise_type: str
    }

    class UniformParams {
        - noise_type: Literal["uniform"]
        - ranges: list[tuple[float, float]]
    }

    class GaussianParams {
        - noise_type: Literal["gaussian"]
        - mean_range: tuple[float, float]
        - std_range: tuple[float, float]
    }

    class LaplaceParams {
        - noise_type: Literal["laplace"]
        - mean_range: tuple[float, float]
        - scale_range: tuple[float, float]
    }

    class BetaParams {
        - noise_type: Literal["beta"]
        - alpha_range: tuple[float, float]
        - beta_range: tuple[float, float]
        - scale_range: tuple[float, float]
    }

    class PoissonParams {
        - noise_type: Literal["poisson"]
        - lambda_range: tuple[float, float]
    }

    AdditiveNoise <|-- RGBShift
    NoiseParamsBase <|-- UniformParams
    NoiseParamsBase <|-- GaussianParams
    NoiseParamsBase <|-- LaplaceParams
    NoiseParamsBase <|-- BetaParams
    NoiseParamsBase <|-- PoissonParams
    AdditiveNoise --> NoiseParamsBase
    RGBShift --> AdditiveNoise
    note for AdditiveNoise "This class applies random noise to image channels using various noise distributions."
    note for RGBShift "This class is a specialized version of AdditiveNoise for RGB channel shifts."
Loading

File-Level Changes

Change Details Files
Added new AdditiveNoise transform with comprehensive noise generation capabilities
  • Implemented support for multiple noise distributions (uniform, gaussian, laplace, beta, poisson)
  • Added three spatial modes: constant, per-pixel, and shared
  • Included approximation parameter for performance optimization
  • Added validation and configuration models for noise parameters
albumentations/augmentations/transforms.py
Added noise generation utility functions
  • Implemented core noise sampling functions for each distribution type
  • Added support for different spatial modes in noise generation
  • Implemented approximation-based noise generation for performance optimization
albumentations/augmentations/functional.py
Refactored RGBShift to use AdditiveNoise as base class
  • Converted RGBShift to inherit from AdditiveNoise
  • Maintained backward compatibility with existing RGBShift interface
  • Updated parameter handling to work with normalized ranges
albumentations/augmentations/transforms.py
Added test coverage for new functionality
  • Added AdditiveNoise to transform test cases
  • Included AdditiveNoise in augmentation definitions
tests/test_transforms.py
tests/aug_definitions.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ternaus - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 77.85235% with 33 lines in your changes missing coverage. Please review.

Project coverage is 90.23%. Comparing base (b1a79c2) to head (789128a).
Report is 281 commits behind head on main.

Files with missing lines Patch % Lines
albumentations/augmentations/functional.py 54.41% 31 Missing ⚠️
albumentations/augmentations/transforms.py 97.53% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           main    #2149       +/-   ##
=========================================
+ Coverage      0   90.23%   +90.23%     
=========================================
  Files         0       48       +48     
  Lines         0     8250     +8250     
=========================================
+ Hits          0     7444     +7444     
- Misses        0      806      +806     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ternaus ternaus merged commit e6b08f8 into main Nov 18, 2024
16 checks passed
@ternaus ternaus deleted the add_additive_noise branch November 18, 2024 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant