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 ElasticTransform blur kernel and add missing args #2378

Merged
merged 2 commits into from
Mar 5, 2025

Conversation

liamjwang
Copy link
Contributor

@liamjwang liamjwang commented Mar 3, 2025

Hello, a few suggested changes to ElasticTransform.

I think the approximate argument was reversed by mistake.

Also, currently the blur step is skipped if the kernel size is (0, 0).
However, I think it was intended to let cv2.GaussianBlur automatically compute the kernel size.

With this change, the behavior matches the ElasticTransform output from older versions (e.g. 1.4.15).

Also added border_mode, fill, and fill_mask args.

Summary by Sourcery

Fixes issues in the ElasticTransform augmentation, including a reversed approximate argument, skipping the blur step when the kernel size is (0, 0), and missing border_mode, fill, and fill_mask arguments.

Bug Fixes:

  • Fixes a bug where the blur step was skipped in ElasticTransform when the kernel size was (0, 0).
  • Fixes a bug where the approximate argument was reversed in ElasticTransform.

Enhancements:

  • Adds border_mode, fill, and fill_mask arguments to ElasticTransform to provide more control over the transformation.

Copy link
Contributor

sourcery-ai bot commented Mar 3, 2025

Reviewer's Guide by Sourcery

This pull request fixes the ElasticTransform blur kernel, corrects the approximate argument, and adds border_mode, fill, and fill_mask arguments. The blur step in generate_displacement_fields was modified to allow cv2.GaussianBlur to automatically compute the kernel size when kernel_size is (0, 0). The approximate argument logic was corrected by swapping the values assigned to the kernel_size variable. The border_mode, fill, and fill_mask arguments were added to the InitSchema and ElasticTransform classes, including updates to the get_transform_init_args_names methods.

Sequence diagram for generate_displacement_fields with blur

sequenceDiagram
    participant fgeometric.generate_displacement_fields
    participant cv2.GaussianBlur

    fgeometric.generate_displacement_fields->>cv2.GaussianBlur: Apply Gaussian blur to displacement fields
    activate cv2.GaussianBlur
    cv2.GaussianBlur-->>fgeometric.generate_displacement_fields: Blurred displacement fields
    deactivate cv2.GaussianBlur
    fgeometric.generate_displacement_fields->>fgeometric.generate_displacement_fields: Scale by alpha
Loading

Updated class diagram for ElasticTransform

classDiagram
    class ElasticTransform {
        -alpha: float
        -sigma: float
        -approximate: bool
        -same_dxdy: bool
        -noise_distribution: str
        -keypoint_remapping_method: str
        -border_mode: int
        -fill: tuple[float, ...] | float
        -fill_mask: tuple[float, ...] | float
        +__init__(
            alpha: float,
            sigma: float,
            approximate: bool,
            same_dxdy: bool,
            interpolation: int,
            mask_interpolation: int,
            noise_distribution: str,
            keypoint_remapping_method: str,
            border_mode: int,
            fill: tuple[float, ...] | float,
            fill_mask: tuple[float, ...] | float,
            p: float
        )
        +get_params_dependent_on_data(data: dict[str, Any]) : dict[str, Any]
        +get_transform_init_args_names() : tuple[str, ...]
    }
    ElasticTransform --|> BaseDistortion
    note for ElasticTransform "Added border_mode, fill, and fill_mask attributes and constructor parameters."
Loading

File-Level Changes

Change Details Files
Added border_mode, fill, and fill_mask arguments to the InitSchema and ElasticTransform classes.
  • Added border_mode to the InitSchema class.
  • Added fill to the InitSchema class.
  • Added fill_mask to the InitSchema class.
  • Added border_mode to the ElasticTransform class.
  • Added fill to the ElasticTransform class.
  • Added fill_mask to the ElasticTransform class.
  • Added border_mode, fill, and fill_mask to the get_transform_init_args_names method in both classes.
albumentations/augmentations/geometric/transforms.py
Corrected the logic for the approximate argument in the ElasticTransform class.
  • The kernel_size is now set to (17, 17) when approximate is true, and (0, 0) when approximate is false.
albumentations/augmentations/geometric/transforms.py
Modified the Gaussian blur application in generate_displacement_fields to allow cv2.GaussianBlur to automatically compute the kernel size when kernel_size is (0, 0).
  • Removed the conditional check that skipped the blur step when kernel_size was (0, 0).
  • The Gaussian blur is now always applied.
albumentations/augmentations/geometric/functional.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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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 @liamjwang - I've reviewed your changes - here's some feedback:

Overall Comments:

  • It might be worth adding a test case that specifically checks the behavior of ElasticTransform with the new border_mode, fill, and fill_mask parameters.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 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.

@ternaus
Copy link
Collaborator

ternaus commented Mar 3, 2025

border_mode
fill
fill_mask

Do not really have material effect in Elastic Transform

@liamjwang liamjwang requested a review from ternaus March 4, 2025 01:20
@liamjwang
Copy link
Contributor Author

Thank you!

@ternaus
Copy link
Collaborator

ternaus commented Mar 5, 2025

@liamjwang you still need to update the code for all checks to pass.

Please read Contributoru's guide - https://github.com/albumentations-team/albumentations/blob/main/CONTRIBUTING.md

@ternaus ternaus merged commit 53e73eb into albumentations-team:main Mar 5, 2025
14 checks passed
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.

2 participants