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

Fixed CenterCropPad incorrectly using fill value for the mask #2195

Merged
merged 1 commit into from
Dec 13, 2024

Conversation

iRyoka
Copy link
Contributor

@iRyoka iRyoka commented Dec 13, 2024

Summary by Sourcery

Fix the issue with CenterCropPad incorrectly using the fill value for the mask and add a corresponding test to ensure correct behavior.

Bug Fixes:

  • Fix CenterCropPad to correctly use the fill value for the mask during padding operations.

Tests:

  • Add a test to verify that BaseCropAndPad uses the correct fill values for constant borders in images and masks.

Copy link
Contributor

sourcery-ai bot commented Dec 13, 2024

Reviewer's Guide by Sourcery

This PR fixes a bug in the CenterCropPad transformation where the fill value for masks was not being properly applied. The implementation adds a new apply_to_mask method to handle mask padding with the correct fill value and includes tests to verify the behavior.

Class diagram for CenterCropPad changes

classDiagram
    class CenterCropPad {
        +apply(img, crop_coords, **params)
        +apply_to_mask(mask, crop_coords, **params)
        +apply_to_bboxes(bboxes, **params)
    }
    class BaseCrop {
        +apply(self, mask, crop_coords, **params)
    }
    CenterCropPad --|> BaseCrop
    note for CenterCropPad "apply_to_mask method added to handle mask padding with correct fill value"
Loading

File-Level Changes

Change Details Files
Added dedicated mask handling in BaseCropAndPad
  • Implemented apply_to_mask method to handle mask padding separately from image padding
  • Used fill_mask value for mask padding instead of the image fill value
  • Prevented double padding by calling BaseCrop.apply directly
albumentations/augmentations/crops/transforms.py
Added test cases for fill value behavior
  • Created test to verify correct fill values for both image and mask padding
  • Added test case for default fill_mask value
  • Verified padding behavior with different fill values for image and mask
tests/test_crop.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 @iRyoka - 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: all looks good
  • 🟢 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.

@iRyoka
Copy link
Contributor Author

iRyoka commented Dec 13, 2024

A minimal example to reprodice the addressed problem it:

import albumentations as A
import numpy as np

c = A.CenterCrop(
    4, 
    3,
    pad_if_needed = True,
    fill = 1,
    fill_mask = 2
)

out = c(image = np.zeros((1,1,3)), mask = np.zeros((1,1)))

assert 2 in out['mask'], f"Wrong padding: {np.unique(out['mask'])}"

@ternaus ternaus merged commit 852dfef into albumentations-team:main Dec 13, 2024
14 checks passed
@ternaus
Copy link
Collaborator

ternaus commented Dec 13, 2024

@iRyoka

Thanks, good catch!

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