Skip to content

Conversation

@abhayrajjais01
Copy link

Summary

This PR fixes Issue #1274 where PadIfNeeded was incorrectly cropping images larger than the target size because it was aliased directly to Kornia.PadTo.

Changes

  • New PadIfNeeded Class: Implemented a custom PadIfNeeded augmentation (inheriting from GeometricAugmentationBase2D) that:
    • Checks if the input image dimensions are smaller than the target size.
    • Pads only if needed.
    • Returns the identity (unchanged image) if the image is already larger than the target size.
  • Added PadTo: Added a new key "PadTo" to _SUPPORTED_TRANSFORMS that maps to the original Kornia.PadTo behavior. This preserves backward compatibility for users who relied on the cropping behavior.
  • Tests: Added a new regression test test_pad_if_needed in tests/test_augmentations.py to verify:
    • Small images are padded correctly.
    • Large images are NOT cropped (the fix).
    • PadTo still works as expected (legacy behavior).

Verification

Ran tests via Docker:
docker compose run --rm deepforest pytest tests/test_augmentations.py
Result: All 28 tests passed.

I have taken help of ai to understand the issue.

@abhayrajjais01 abhayrajjais01 marked this pull request as draft January 29, 2026 14:15
@Prathamesh8989
Copy link

Hey @abhayrajjais01
I just went through your code — great job overall! 🙌

I noticed one small thing that could potentially be improved.

Padding Behavior

On line 135, the current implementation is:

return torch.nn.functional.pad(
    input,
    [0, pad_width, 0, pad_height],  # [left, right, top, bottom]
    mode=flags["pad_mode"],
    value=flags["pad_value"],
)

This applies padding only to the right and bottom of the image.

Why This Matters

Padding on only one side can cause a noticeable shift in the feature center, which may affect how the neural network perceives spatial information.

Using symmetric padding (padding equally on all sides) helps:

  • Keep the original image content centered
  • Preserve spatial alignment of features
  • Reduce unintended bias introduced by directional padding

Suggested Improvement

Consider updating this to use symmetric padding, where padding is distributed evenly across left/right and top/bottom (when possible). This can improve consistency and stability during training and inference.

Let me know what you think — happy to help iterate on this 👍

@abhayrajjais01 abhayrajjais01 deleted the fix/issue-1274-padifneeded-rename branch February 2, 2026 15:40
abhayrajjais01 pushed a commit to abhayrajjais01/DeepForest that referenced this pull request Feb 2, 2026
- Distribute padding evenly on all sides (left/right, top/bottom)
- Keeps image centered to preserve spatial alignment
- Addresses feedback from @Prathamesh8989 in PR weecology#1289
@abhayrajjais01
Copy link
Author

thank you @Prathamesh8989 for the suggestion

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.

3 participants