Skip to content

Item transform affine - #367

Open
srishtiii28 wants to merge 2 commits into
mlverse:mainfrom
srishtiii28:feat/item_transform_affine
Open

Item transform affine#367
srishtiii28 wants to merge 2 commits into
mlverse:mainfrom
srishtiii28:feat/item_transform_affine

Conversation

@srishtiii28

Copy link
Copy Markdown
Contributor

Closes #347.

Adds item_transform_affine(), applying an affine transform that is rotation, translation, scale and shear to a dataset item and keeping the target aligned with the image. Follows the S3-dispatch convention from #343.

@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 nice work, thanks !
todo the underlying target transform shall be exported with the convenient name and S3 dispatch
todo missing please also provide a solution for item_transform_affine.image_with_rotated_box

Comment thread R/item-transform-geometry.R Outdated
Comment on lines +184 to +185
orig_h <- as.numeric(x$x$shape[length(x$x$shape) - 1])
orig_w <- as.numeric(x$x$shape[length(x$x$shape)])

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 readability

Suggested change
orig_h <- as.numeric(x$x$shape[length(x$x$shape) - 1])
orig_w <- as.numeric(x$x$shape[length(x$x$shape)])
c(orig_h, orig_w) %<-% tail(x$x$shape, 2)

Comment thread R/item-transform-geometry.R Outdated
x
}

affine_boxes_xyxy <- function(boxes, angle, translate, scale, shear, center,

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 Please rename the function into target_transform_affine.list() and move into target_transform_detection.R
todo Please document and export with the #' @family target_transforms_detection
todo Please add a generic method target_transform_affine() and a default method for proper S3 dispatch

Comment on lines +196 to +209
make_seg_item <- function(image_size = c(100L, 200L), num_masks = 3L) {
x <- torch_randn(3, image_size[1], image_size[2])
masks <- torch_rand(num_masks, image_size[1], image_size[2]) > 0.5
labels <- torch_ones(num_masks, dtype = torch_long())
y <- list(
masks = masks,
labels = labels,
image_height = image_size[1],
image_width = image_size[2]
)
item <- list(x = x, y = y)
class(item) <- c("image_with_segmentation_mask", "list")
item
}

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 please move this helper function into helper-torchvision.R
todo please rename it into make_segmentation_item for more transparency

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 Please check the helper function may already be present according to #366
todo Please add a test showing affine can be composed like in

test_that("item_transform_rotate can be composed", {
boxes <- matrix(c(
10, 20, 50, 60,
100, 200, 150, 250,
0, 0, 300, 400
), ncol = 4, byrow = TRUE)
labels <- sample.int(2^16, 3)
item <- make_detection_item(boxes, labels = labels, image_size = c(410, 300))
result <- item |>
item_transform_rotate(angle = 90) |>
item_transform_rotate(angle = 90)

todo Please note that make_item() is now make_detection_item()

@srishtiii28
srishtiii28 force-pushed the feat/item_transform_affine branch from f6437af to 6904e69 Compare July 28, 2026 17:32
@cregouby

cregouby commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

todo the current target transform is not correct, it should be rotated, translated and scaled, around the given center, as the image is. on the provided example and should be fixed :
image

@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 This is a major one ! Thanks

Comment on lines +214 to +223
masks <- x$y$masks
if (!is.null(masks) && masks$ndim >= 3) {
dtype <- masks$dtype
masks <- transform_affine(masks, angle = angle, translate = translate,
scale = scale, shear = shear,
interpolation = 0, fill = fill, center = center)
x$y$masks <- masks$to(dtype = dtype)
}
x
}

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 Can we move that target_transform_affine.list() as well ?
thought I know this will require ugly test on the presence of parameters, waiting for #364

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 missing can we manage also the .image_with_rotated_box() S3 class ?

@srishtiii28
srishtiii28 force-pushed the feat/item_transform_affine branch from 6904e69 to 3830e65 Compare July 30, 2026 13:50
@srishtiii28
srishtiii28 requested a review from cregouby July 30, 2026 14:14

@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.

todo can you resolve the merge conflicts, please ?

@srishtiii28
srishtiii28 force-pushed the feat/item_transform_affine branch from 3830e65 to bad7f90 Compare August 6, 2026 14:07
@srishtiii28
srishtiii28 requested a review from cregouby August 6, 2026 14:08
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.

Add item_transformation_affine

2 participants