Skip to content

Feat: Add roi_align_rotated - #36

Open
DerrickUnleashed wants to merge 13 commits into
mlverse:mainfrom
DerrickUnleashed:feat/roiAlignedRotate
Open

Feat: Add roi_align_rotated#36
DerrickUnleashed wants to merge 13 commits into
mlverse:mainfrom
DerrickUnleashed:feat/roiAlignedRotate

Conversation

@DerrickUnleashed

@DerrickUnleashed DerrickUnleashed commented Aug 2, 2026

Copy link
Copy Markdown

Adds ops_roi_align_rotated() / nn_roi_align_rotated(), a CPU RoI align pooling op for rotated boxes, ported from mmcv (Apache-2.0). Includes dispatcher + autograd kernels, R API + docs, and tests.

Closes #32

library(torch)
library(torchvision)
library(torchvisionlib)

url <- "https://upload.wikimedia.org/wikipedia/commons/b/b6/Felis_catus-cat_on_snow.jpg"
arr <- base_loader(url)                           
input <- torch_tensor(arr)$permute(c(3, 1, 2))$unsqueeze(1)     

H <- input$shape[3]/2; W <- input$shape[4]/2
rois <- torch_tensor(matrix(c(
  0, 3*W/2, 3*H/2, W, H, pi,
  0, W/2,   3*H/2, W, H, pi,
  0, 3*W/2, H/2, W, H, pi,
  0, W/2,   H/2, W, H, pi
), ncol = 6, byrow = TRUE), dtype = torch_float32())

out <- ops_roi_align_rotated(input, rois, c(128, 128), spatial_scale = 1, sampling_ratio = 2)

grid <- vision_make_grid(out, num_rows = 2, padding = 6, pad_value = 1)

tensor_image_browse(grid)     
file339407cbfb

@DerrickUnleashed
DerrickUnleashed marked this pull request as ready for review August 2, 2026 21:13
Comment thread tests/testthat/test-ops-roi-align-rotated.R Outdated
@cregouby

Copy link
Copy Markdown
Collaborator

@DerrickUnleashed ,
Could you resolve merge conflicts ?

Keep both roi_align_rotated (feature branch) and box_iou_rotated (main)
across all generated export/build/registration files.
@DerrickUnleashed

DerrickUnleashed commented Aug 17, 2026

Copy link
Copy Markdown
Author

@DerrickUnleashed , Could you resolve merge conflicts ?

Yes I have completed resolving them

The TORCH_LIBRARY_FRAGMENT schema registration was not being picked up
at runtime across shared libraries on macOS, causing
findSchemaOrThrow("torchvision::roi_align_rotated") to fail.

Bypass the PyTorch dispatcher by calling the CPU kernels and autograd
function directly instead of routing through findSchemaOrThrow.

@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 addition, clear and straightforward, thanks
todo indexing and documentation

Comment thread R/ops.R
Comment on lines +275 to +276
#' Performs RoI align pooling for rotated proposals, as implemented by the MMCV
#' `roi_align_rotated` operator

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 mention in plain english Regions of interest in the sentence.

Comment thread R/ops.R
#'
#' Performs RoI align pooling for rotated proposals, as implemented by the MMCV
#' `roi_align_rotated` operator
#' (see <https://mmcv.readthedocs.io/en/latest/deployment/mmcv_ops_definition.html#mmcvroialignrotated>).

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 this URL do not bring any additionnal information of what is present here. Please remove

Comment thread R/ops.R
#'
#' @param input (`Tensor[N, C, H, W]`): input feature map.
#' @param rois (`Tensor[K, 6]`): rotated boxes with columns
#' `(batch_index, cx, cy, w, h, angle)`, where `batch_index` is a **0-based**

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 0-indexing is considered as a bug in R. So end-user will never conform to that. Could we switch to 1-based index, Please ?

Comment thread R/ops.R
#' Default: `FALSE`
#'
#' @returns
#' `Tensor[K, C, output_size[1], output_size[2]]`: the pooled features, where

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 clarification Can we use output width and output height here as we are in documentation, not in code.

Comment thread R/ops.R
#' index into the first dimension of `input`, `(cx, cy)` is the box center,
#' `(w, h)` the box size and `angle` the rotation angle in radians
#' (counterclockwise unless `clockwise = TRUE`).
#' @param output_size (int or `Tuple[int, int]`): the output size `(height, width)`

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 Tuple is a unclear concept for the R users. Please use "(integer vector or interger)" like in https://github.com/mlverse/torchvision/blob/61e2c5008a81270146309d9a988edf88bde65575/R/transforms-generics.R#L66

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 roi_aligned_rotated

2 participants