Skip to content

Feat: Add SAHI (Slicing Aided Hyper Inference) Transform#329

Open
DerrickUnleashed wants to merge 31 commits into
mlverse:mainfrom
DerrickUnleashed:feat/sahiTransform
Open

Feat: Add SAHI (Slicing Aided Hyper Inference) Transform#329
DerrickUnleashed wants to merge 31 commits into
mlverse:mainfrom
DerrickUnleashed:feat/sahiTransform

Conversation

@DerrickUnleashed

@DerrickUnleashed DerrickUnleashed commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

This PR Adds:

  • The implementation of transform_sahi_crop()
  • The implementation of target_transform_sahi_crop()
  • Adds test suites for the same
  • Adds export command to fix this error
transforms-tensor.R:480: S3 method
  `get_image_size.torch_tensor` needs
  @export or @exportS3Method tag.

@DerrickUnleashed DerrickUnleashed marked this pull request as ready for review June 11, 2026 20:02
@DerrickUnleashed

Copy link
Copy Markdown
Contributor Author

@cregouby, I'm still a bit unclear about the usage of target_transform_sahi_crop() However, the transform_sahi_crop() seems to be working as expected

@cregouby cregouby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

praise I think that the cropping values logic is there.
todo design all transform_ functions shall return a object of the same type as the input (except transform to tensor) as they will be piped. Your only freedom is to change the shape of the tensor, and move the crops into a sequential batch, as transform_five_crop() do.
ex for x$shape [1, 3, 10, 10], transform_sahi_crop(x, c(4,4) , c(.2, .2)) shall output a tensor of shape [25, 3, 3, 3] (see

test_that("five_crop", {
x <- torch_randn(3, 10, 12)
o <- transform_five_crop(x, c(3, 3))
expect_length(o, 5)
expect_tensor_shape(o[[1]], c(3,3,3))
ob <- transform_five_crop(x$unsqueeze(1), c(3, 3))
expect_length(ob, 5)
expect_tensor_shape(ob[[1]], c(1,3,3,3))
})
)
suggestion you should rely on the existing crop function to crop the tensor, saving you a lot of unit tests.
todo software design this transform shall be an S3 transform so shall have an entry in transform-generics.R for the dispatch, one in transforms-defaults.R, one in transforms-magick.R, one in transforms-tensor.R, as you never know what transform goes before, what transform is piped after.

todo code relocation Please keep (the badly named, my fault) transform-segmentation.R file for target_transforms
suggestion for clarity, you may rename the transform-segmentation.R into target-transform-segmentation.R (and the test file accordingly)

thought as SAHI intimately rely on input image size and coco bbox (so both x and y at the same time), and as there is no way to pass information from transform_sahi_crop to target_transform_sahi_crop, I think maybe a prepare_sahi_crop(dataset, size, overlap_size_ratio, ... ) function gathering all the needed context into a specific class object sahi_preparation and having both functions using it a the only input parameter transform_sahi_crop(x, sahi_preparation) and target_transform_sahi_crop(x, sahi_preparation) could be a nice API to SAHI.

Comment thread .vscode/settings.json Outdated
Comment thread R/transforms-tensor.R
Comment thread R/transforms-segmentation.R Outdated
Comment thread R/transforms-segmentation.R Outdated
#' SAHI Documentation:
#' https://obss.github.io/sahi/slicing/
#'
#' @family transforms

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

todo family transform does not exist. please stick to an existing transform family.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was thinking that we could edit the orginal one from target_transforms to segmentation_transforms
the existing title for the family is also a bit ambiguous

- subtitle: Target transformations for segmentation
  desc: >
    Functions to convert dataset native targets annotations into segmentation masks compatible
    with draw_segmentation_masks() and segmentation models.
  contents:
    - has_concept("target_transforms")

And it would be better if we keep it as segmentation_transforms and add both target and normal transforms to it ?

@cregouby cregouby Jun 13, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The description of the family should be reworked (i.e. remove 'for segmentation') and each individual function title could me more explicit if it is for segmentation purpose.
but the family shall remain target_transforms

@DerrickUnleashed DerrickUnleashed Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree.

How does this look?

- subtitle: Target transformations
  desc: >
    Functions to convert dataset-native target annotations into segmentation masks compatible
    with draw_segmentation_masks() and segmentation models.
  contents:
    - has_concept("target_transforms")
    ```
    
 This helps us combine all the target transforms into a single family.
 
 Also, regarding the sahi transform, I have removed the family called transform that was an overlook on my behalf.

@DerrickUnleashed

Copy link
Copy Markdown
Contributor Author

todo software design this transform shall be an S3 transform so shall have an entry in transform-generics.R for the dispatch, one in transforms-defaults.R, one in transforms-magick.R, one in transforms-tensor.R, as you never know what transform goes before, what transform is piped after.

I didn't understand properly, Do I need to move the code of transform_sahi and target_transform_sahi to transform-generics ?

@cregouby

cregouby commented Jun 15, 2026 via email

Copy link
Copy Markdown
Collaborator

@DerrickUnleashed

Copy link
Copy Markdown
Contributor Author
  • transform_sahi_crop as S3 has been added across all four dispatch files:

    • transforms-generics.R
    • transforms-defaults.R
    • transforms-magick.R
    • transforms-tensor.R
  • Return type has been made consistent, transform_sahi_crop returns the same type as input

  • The existing transform_crop is reused for all cropping.

  • File renaming - transform-segmentation.R is now target-transforms-segmentation.R (both source and test file).

fileef411e24730b

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.

Implement SAHI: (Slicing Aided Hyper Inference) as both transform_ and target_transform_

2 participants