Skip to content

Add box_iou_rotated - #34

Open
srishtiii28 wants to merge 7 commits into
mlverse:mainfrom
srishtiii28:box-iou-rotated
Open

Add box_iou_rotated#34
srishtiii28 wants to merge 7 commits into
mlverse:mainfrom
srishtiii28:box-iou-rotated

Conversation

@srishtiii28

@srishtiii28 srishtiii28 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Fix #31.

Adds ops_box_iou_rotated(), computing IoU between rotated boxes on CPU, in the cxcywhr, xywhr and xyxyxyxy formats.

@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 great addition, thanks for that

return op.call(boxes1, boxes2);
}

// Vendored because the pinned TorchVision (v0.20.1) has no box_iou_rotated.

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 typo : the pinned TorchVision is 0.23.0 according to

GIT_TAG v0.23.0
. Please update so that this note will be read by next Ctrl^F on the version.

b4 <- torch::torch_tensor(matrix(c(2563.74462890625, 1436.790283203125,
2174.702880859375, 214.0949554443359375,
115.11835479736328125), nrow = 1), dtype = dtype)
expect_equal(as.numeric(ops_box_iou_rotated(b3, b4)), 1, tolerance = 1e-3)

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.

question both tolerance looks pretty high in here, can't we expect a lower one ?(both tolerances at 1e-5 passes on my machine).

@srishtiii28
srishtiii28 requested a review from cregouby July 30, 2026 14:14
Comment thread R/utils.R

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 performance We should avoid the usage of torch_unbind() --> .. --> torch_stack() both beeing memory allocation intensive and slow, and rather use the vecorized version x1 <- boxes[.. ,1, drop = FALSE] --> ... --> torch_cat().
suggestion you may try a performance comparison of the 2 methods (as in mlverse/torchvision#372).

Comment on lines +29 to +64
# cxcywhr -> xywhr / xyxyxyxy, used to check the R-side format conversions
# against the cxcywhr path. Formulas mirror torchvision's box_convert.
cxcywhr_to_xywhr <- function(boxes) {
b <- torch::torch_unbind(boxes, dim = -1)
cx <- b[[1]]
cy <- b[[2]]
w <- b[[3]]
h <- b[[4]]
r <- b[[5]]
rad <- r * pi / 180
cos <- torch::torch_cos(rad)
sin <- torch::torch_sin(rad)
x1 <- cx - w / 2 * cos - h / 2 * sin
y1 <- cy - h / 2 * cos + w / 2 * sin
torch::torch_stack(list(x1, y1, w, h, r), dim = -1)
}

xywhr_to_xyxyxyxy <- function(boxes) {
b <- torch::torch_unbind(boxes, dim = -1)
x1 <- b[[1]]
y1 <- b[[2]]
w <- b[[3]]
h <- b[[4]]
r <- b[[5]]
rad <- r * pi / 180
cos <- torch::torch_cos(rad)
sin <- torch::torch_sin(rad)
x2 <- x1 + w * cos
y2 <- y1 - w * sin
x3 <- x2 + h * sin
y3 <- y2 + h * cos
x4 <- x1 + h * sin
y4 <- y1 + h * cos
torch::torch_stack(list(x1, y1, x2, y2, x3, y3, x4, y4), dim = -1)
}

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 helper functions to tests/testthat/helper-ops.R (as there is a chance to reuse them for #32 )

Comment thread R/utils.R
w <- ((x2 - x1) * (x2 - x1) + (y1 - y2) * (y1 - y2))$sqrt()
h <- ((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2))$sqrt()
.box_xywhr_to_cxcywhr(torch::torch_stack(list(x1, y1, w, h, r), dim = -1))
}

@cregouby cregouby Jul 31, 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.

suggestion May we have those two convertion functions added to {torchvision} as well (non exported) (and added to the supported in_fmt and out_fmt list of box_convert() ?

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 box_iou_rotated

2 participants