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 A.AtLeastOneBBoxRandomCrop #2207

Conversation

guillaume-rochette-oxb
Copy link
Contributor

@guillaume-rochette-oxb guillaume-rochette-oxb commented Dec 18, 2024

Hi,

I'd like to add the transform AtLeastOneBBoxRandomCrop to the list of available transforms.

What does it do?
It crops an image at a given resolution, similarly to RandomCrop. However, unlike BBoxSafeRandomCrop, which ensures that all the bounding boxes are retained, AtLeastOneBBoxRandomCrop will ensure that at least one bounding box is retained.

Why?

  1. Unlike RandomCrop, it ensures to keep at least one bounding box, so models, therefore models, such as FasterRCNN or MaskRCNN, which require at least one box to compute the losses. This allow avoiding that "annoying moment", where the training would crash in the event where all bounding boxes would be dropped.
  2. BBoxSafeRandomCrop requires an extra resizing transform in order to align every image to a common resolution, which is beneficial for accelerating training using options like torch.backends.cudnn.benchmark.
  3. A varying amount of boxes per sample is beneficial as it is a form of implicit regularization, in the sense that it affect the number of predicted region of interests, detections, etc.

Feel free to ask further questions about the motivations and/or modifications to in the PR.
Best regards,

Summary by Sourcery

Add the AtLeastOneBBoxRandomCrop transform to ensure at least one bounding box is retained during cropping, enhancing model training stability and efficiency.

New Features:

  • Introduce the AtLeastOneBBoxRandomCrop transform, which crops an image to a fixed resolution while ensuring that at least one bounding box is retained.

Tests:

  • Add tests for the AtLeastOneBBoxRandomCrop transform to ensure its functionality and integration with existing augmentation workflows.

Copy link
Contributor

sourcery-ai bot commented Dec 18, 2024

Reviewer's Guide by Sourcery

This PR introduces a new image transformation called AtLeastOneBBoxRandomCrop that crops an image to a fixed resolution while ensuring at least one bounding box is retained in the crop. The implementation includes the core transform logic and necessary test updates across multiple test files.

Class diagram for AtLeastOneBBoxRandomCrop

classDiagram
    class AtLeastOneBBoxRandomCrop {
        +int height
        +int width
        +float erosion_factor
        +float p
        +bool|None always_apply
        +get_params_dependent_on_data(params: dict[str, Any], data: dict[str, Any]) dict[str, tuple[int, int, int, int]]
        +get_transform_init_args_names() tuple[str, ...]
    }
    AtLeastOneBBoxRandomCrop --|> BaseCrop
    class BaseCrop {
        <<abstract>>
    }
Loading

File-Level Changes

Change Details Files
Added new AtLeastOneBBoxRandomCrop transform class implementation
  • Implemented crop coordinate calculation logic that ensures at least one bounding box is retained
  • Added support for an erosion factor parameter to control bounding box thinning
  • Added fallback logic for cases with no bounding boxes
  • Implemented parameter validation and initialization schema
albumentations/augmentations/crops/transforms.py
Updated test suite to include the new transform
  • Added transform to image-only augmentation tests
  • Added transform to dual augmentation tests
  • Added transform to multichannel image tests
  • Added transform to serialization tests
  • Added transform to target handling tests
tests/test_augmentations.py
tests/test_transforms.py
tests/test_core.py
tests/test_serialization.py
tests/aug_definitions.py
tests/test_targets.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 @guillaume-rochette-oxb - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding specific unit tests to verify the bounding box retention logic and erosion factor behavior
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟡 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.

tests/test_transforms.py Show resolved Hide resolved
@ternaus
Copy link
Collaborator

ternaus commented Dec 18, 2024

Looks good.

One small thing. When new transform is added, one need to run:

python -m tools.make_transforms_docs make

it will output the new table with transforms and targets. You will need to paste it to the readme, instead of the original one.

@guillaume-rochette-oxb
Copy link
Contributor Author

Hi,
Thanks :)
Sorry about the README.md, I did not know about that part, I've only read the CONTRIBUTING.md.

@ternaus ternaus merged commit cd4173b into albumentations-team:main Dec 19, 2024
14 checks passed
@ternaus
Copy link
Collaborator

ternaus commented Dec 19, 2024

@guillaume-rochette-oxb Thank you for the PR.

I will update Contributing.md. Info about updating readme should be there.

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