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 keypoints to random sized b box safe crop #1824

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ternaus
Copy link
Collaborator

@ternaus ternaus commented Jun 29, 2024

Fixes #1765

Summary by Sourcery

This pull request refactors crop-related transformations to use a common base class, adds support for keypoints in BBoxSafeRandomCrop and RandomSizedBBoxSafeCrop, and updates the documentation and tests accordingly.

  • New Features:
    • Added support for keypoints in the BBoxSafeRandomCrop and RandomSizedBBoxSafeCrop transformations.
  • Enhancements:
    • Refactored crop-related transformations to use a common base class (_BaseCrop) for better code reuse and maintainability.
    • Unified the method of obtaining crop coordinates across different crop transformations by introducing a get_crop_coords function.
  • Documentation:
    • Updated README to reflect the addition of keypoints support in BBoxSafeRandomCrop and RandomSizedBBoxSafeCrop transformations.
  • Tests:
    • Removed outdated tests for center_crop and random_crop functions.
    • Added new tests for crop transformations to ensure consistency and correctness, including tests for keypoints support.

Copy link
Contributor

sourcery-ai bot commented Jun 29, 2024

Reviewer's Guide by Sourcery

This pull request refactors the cropping transformations in the Albumentations library to include keypoints in the transformations. It introduces a new base class _BaseCrop that centralizes common functionality for cropping operations, and updates existing crop classes to inherit from this base class. Additionally, it consolidates crop coordinate calculation functions and removes redundant methods.

File-Level Changes

Files Changes
albumentations/augmentations/crops/transforms.py
albumentations/augmentations/crops/functional.py
Refactored crop transformations to include keypoints and centralized common functionality in _BaseCrop class.
tests/test_functional.py
tests/test_bbox.py
tests/test_core.py
tests/test_targets.py
tests/test_serialization.py
tests/test_crop.py
Updated and added tests to reflect changes in crop transformations and ensure keypoints are handled correctly.
README.md Updated documentation to reflect new keypoint support in BBoxSafeRandomCrop and RandomSizedBBoxSafeCrop.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

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: 3 issues found
  • 🟢 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 to tell me if it was helpful.


def get_params(self) -> dict[str, float]:
return {"h_start": random.random(), "w_start": random.random()}
def get_params_dependent_on_targets(self, params: dict[str, Any]) -> dict[str, tuple[int, int, int, int]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Check for the presence of 'image' key in params.

It would be prudent to add a check to ensure that the 'image' key exists in the params dictionary before accessing it. This can prevent potential KeyError exceptions.

"crop_and_pad",
"crop_and_pad_bbox",
"crop_and_pad_keypoint",
]


def get_random_crop_coords(
def get_crop_coords(
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider adding validation for input parameters.

Adding validation checks for the input parameters (e.g., ensuring crop_height and crop_width are positive and within the bounds of height and width) can make the function more robust and prevent potential errors.

Suggested change
def get_crop_coords(
def get_crop_coords(
height: int,
width: int,
crop_height: int,
crop_width: int
):
if crop_height <= 0 or crop_width <= 0:
raise ValueError("Crop dimensions must be positive.")
if crop_height > height or crop_width > width:
raise ValueError("Crop dimensions must be within the bounds of the image dimensions.")

x1, y1 = crop_coords[:2]
cropped_bbox = x_min - x1, y_min - y1, x_max - x1, y_max - y1
return cast(BoxInternalType, normalize_bbox(cropped_bbox, crop_height, crop_width))
crop_height = crop_coords[3] - crop_coords[1]
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider adding a check for valid crop coordinates.

Before calculating crop_height and crop_width, it would be beneficial to add a check to ensure that crop_coords are valid and within the expected range.

Suggested change
crop_height = crop_coords[3] - crop_coords[1]
if not (0 <= crop_coords[0] < crop_coords[2] and 0 <= crop_coords[1] < crop_coords[3]):
raise ValueError("Invalid crop coordinates")
crop_height = crop_coords[3] - crop_coords[1]

@@ -0,0 +1,112 @@
import numpy as np

from tests.utils import set_seed
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (code-quality): Don't import test modules. (dont-import-test-modules)

ExplanationDon't import test modules.

Tests should be self-contained and don't depend on each other.

If a helper function is used by multiple tests,
define it in a helper module,
instead of importing one test from the other.

params.update({"x_min": x_min, "x_max": x_max, "y_min": y_min, "y_max": y_max})
crop_coords = x_min, y_min, x_max, y_max

params.update({"crop_coords": crop_coords})
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (code-quality): Add single value to dictionary directly rather than using update() (simplify-dictionary-update)

Suggested change
params.update({"crop_coords": crop_coords})
params["crop_coords"] = crop_coords

Comment on lines 732 to 741
erosive_h = int(image_height * (1.0 - self.erosion_rate))
crop_height = image_height if erosive_h >= image_height else random.randint(erosive_h, image_height)

crop_width = int(crop_height * image_width / image_height)
h_start = random.random()
w_start = random.random()

crop_coords = fcrops.get_crop_coords(image_height, image_width, crop_height, crop_width, h_start, w_start)

return {"crop_coords": crop_coords}
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (code-quality): Extract code out into method (extract-method)

@ternaus ternaus marked this pull request as draft June 29, 2024 02:20
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.

Incorrect keypoints for RandomSizedBBoxSafeCrop
1 participant