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

Added bounding box pre filtering #2287

Merged
merged 1 commit into from
Jan 22, 2025
Merged

Added bounding box pre filtering #2287

merged 1 commit into from
Jan 22, 2025

Conversation

ternaus
Copy link
Collaborator

@ternaus ternaus commented Jan 22, 2025

Summary by Sourcery

Add pre-filtering of bounding boxes to remove invalid boxes before applying transformations.

Bug Fixes:

  • Fix: Bounding box clipping now correctly handles edge cases and floating-point precision.

Enhancements:

  • Improve bounding box processing by adding options to filter invalid bounding boxes and clip boxes to image boundaries.

Tests:

  • Add tests for invalid bounding box filtering and clipping.

Copy link
Contributor

sourcery-ai bot commented Jan 22, 2025

Reviewer's Guide by Sourcery

This pull request introduces a new filter_invalid_bboxes parameter to the BboxParams class, which allows users to filter out invalid bounding boxes (e.g., boxes with negative dimensions or where x_max < x_min or y_max < y_min) at the beginning of the pipeline. Additionally, the BboxProcessor class was updated to include the filtering logic and the documentation was updated to reflect the new functionality.

Sequence diagram for bounding box processing pipeline

sequenceDiagram
    participant Client
    participant BboxProcessor
    participant BboxParams

    Note over BboxProcessor: New processing order with filtering

    Client->>BboxProcessor: Process bounding boxes
    activate BboxProcessor
    BboxProcessor->>BboxProcessor: check_and_convert(direction='to')
    BboxProcessor->>BboxParams: Check clip parameter
    alt clip=True
        BboxProcessor->>BboxProcessor: Clip boxes to [0,1]
    end
    BboxProcessor->>BboxParams: Check filter_invalid_bboxes
    alt filter_invalid_bboxes=True
        BboxProcessor->>BboxProcessor: Filter invalid boxes
    end
    BboxProcessor->>BboxProcessor: Apply transformations
    BboxProcessor->>BboxProcessor: Filter based on min params
    BboxProcessor->>BboxProcessor: check_and_convert(direction='from')
    BboxProcessor-->>Client: Return processed boxes
    deactivate BboxProcessor
Loading

Class diagram showing BboxParams and BboxProcessor changes

classDiagram
    class BboxParams {
        +str format
        +Sequence[str] label_fields
        +float min_area
        +float min_visibility
        +float min_width
        +float min_height
        +bool check_each_transform
        +bool clip
        +bool filter_invalid_bboxes
        +__init__()
        +to_dict_private()
    }

    class BboxProcessor {
        +BboxParams params
        +dict additional_targets
        +__init__()
        +default_data_name()
        +ensure_data_valid()
        +filter()
        +check_and_convert()
        +check()
    }

    BboxProcessor --> BboxParams: uses

    note for BboxParams "Added filter_invalid_bboxes parameter"
    note for BboxProcessor "Added check_and_convert method"
Loading

File-Level Changes

Change Details Files
Added filter_invalid_bboxes parameter to BboxParams.
  • Added filter_invalid_bboxes parameter to the BboxParams class.
  • Updated the __init__ method to accept the new parameter.
  • Updated the to_dict_private method to include the new parameter.
  • Updated the __repr__ method to include the new parameter.
  • Added documentation for the new parameter.
albumentations/core/bbox_utils.py
Updated BboxProcessor to include filtering logic.
  • Updated the check_and_convert method to include filtering of invalid bounding boxes when filter_invalid_bboxes is set to True.
  • Updated the check_and_convert method to clip bounding boxes before filtering when clip is set to True.
  • Updated the documentation to reflect the new processing order.
albumentations/core/bbox_utils.py
Added tests for the new functionality.
  • Added tests to verify that BboxProcessor raises an error when invalid bounding boxes are present and filter_invalid_bboxes is set to False.
  • Added tests to verify that BboxProcessor correctly filters invalid bounding boxes when filter_invalid_bboxes is set to True.
  • Added tests to verify that BboxProcessor correctly handles both clipping and filtering.
tests/test_bbox.py
Updated tests to use assert_allclose instead of assert_almost_equal and assert_array_equal.
  • Updated tests to use assert_allclose instead of assert_almost_equal to handle floating point precision.
  • Updated tests to use assert_allclose instead of assert_array_equal to handle floating point precision.
tests/test_bbox.py
tests/test_core.py
tests/test_pytorch.py
tests/test_crop.py
Updated tests to set seed for reproducibility.
  • Updated tests to set seed for reproducibility.
tests/test_core.py
tests/test_pytorch.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 @ternaus - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 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.

albumentations/core/bbox_utils.py Show resolved Hide resolved
tests/test_bbox.py Show resolved Hide resolved
tests/test_bbox.py Show resolved Hide resolved
tests/test_bbox.py Show resolved Hide resolved
tests/test_core.py Show resolved Hide resolved
tests/test_core.py Show resolved Hide resolved
tests/test_core.py Show resolved Hide resolved
tests/test_core.py Show resolved Hide resolved
Copy link

codecov bot commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.78%. Comparing base (b1a79c2) to head (86eacb2).
Report is 386 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##           main    #2287       +/-   ##
=========================================
+ Coverage      0   89.78%   +89.78%     
=========================================
  Files         0       50       +50     
  Lines         0     8854     +8854     
=========================================
+ Hits          0     7950     +7950     
- Misses        0      904      +904     
Flag Coverage Δ
ubuntu-py3.12 89.78% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@ternaus ternaus merged commit c5f2e8e into main Jan 22, 2025
16 checks passed
@ternaus ternaus deleted the add_bbox_pre_filtering branch January 22, 2025 22:44
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