Add box_iou_rotated - #34
Conversation
cregouby
left a comment
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
todo typo : the pinned TorchVision is 0.23.0 according to
torchvisionlib/csrc/CMakeLists.txt
Line 112 in 49902bc
| 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) |
There was a problem hiding this comment.
question both tolerance looks pretty high in here, can't we expect a lower one ?(both tolerances at 1e-5 passes on my machine).
There was a problem hiding this comment.
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).
| # 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) | ||
| } | ||
|
|
There was a problem hiding this comment.
todo Please move helper functions to tests/testthat/helper-ops.R (as there is a chance to reuse them for #32 )
| 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)) | ||
| } |
There was a problem hiding this comment.
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() ?
Fix #31.
Adds
ops_box_iou_rotated(), computing IoU between rotated boxes on CPU, in thecxcywhr,xywhrandxyxyxyxyformats.