-
Notifications
You must be signed in to change notification settings - Fork 5
Add box_iou_rotated #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
6068c5b
562d69e
52c8eac
868935a
026edec
9b97820
0bdd249
1d72e16
ed72ac7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,50 @@ runtime_error <- function(...) { | |
| rlang::abort(..., class = "runtime_error") | ||
| } | ||
|
|
||
| # Convert a set of rotated boxes to `cxcywhr`, the format expected by the | ||
| # `box_iou_rotated` C++ op. Conversions mirror torchvision's `box_convert`. | ||
| .rotated_boxes_to_cxcywhr <- function(boxes, fmt) { | ||
| switch( | ||
| fmt, | ||
| cxcywhr = boxes, | ||
| xywhr = .box_xywhr_to_cxcywhr(boxes), | ||
| xyxyxyxy = .box_xyxyxyxy_to_cxcywhr(boxes), | ||
| runtime_error(sprintf( | ||
| "Unsupported format '%s'. Supported rotated formats: cxcywhr, xywhr, xyxyxyxy.", | ||
| fmt | ||
| )) | ||
| ) | ||
| } | ||
|
|
||
| .box_xywhr_to_cxcywhr <- function(boxes) { | ||
| b <- torch::torch_unbind(boxes, dim = -1) | ||
| x1 <- b[[1]] | ||
| y1 <- b[[2]] | ||
| w <- b[[3]] | ||
| h <- b[[4]] | ||
| r <- b[[5]] | ||
| r_rad <- r * pi / 180 | ||
| cos <- torch::torch_cos(r_rad) | ||
| sin <- torch::torch_sin(r_rad) | ||
| cx <- x1 + w / 2 * cos + h / 2 * sin | ||
| cy <- y1 - w / 2 * sin + h / 2 * cos | ||
| torch::torch_stack(list(cx, cy, w, h, r), dim = -1) | ||
| } | ||
|
|
||
| .box_xyxyxyxy_to_cxcywhr <- function(boxes) { | ||
| b <- torch::torch_unbind(boxes, dim = -1) | ||
| x1 <- b[[1]] | ||
| y1 <- b[[2]] | ||
| x2 <- b[[3]] | ||
| y2 <- b[[4]] | ||
| x3 <- b[[5]] | ||
| y3 <- b[[6]] | ||
| r <- torch::torch_atan2(y1 - y2, x2 - x1) * 180 / pi | ||
| 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)) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| # Efficient version of torch.cat that avoids a copy if there is only a single element in a list | ||
| .cat <- function(tensors, dim = 1) { | ||
| if (length(tensors) == 1) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,28 @@ | ||||
| #include "box_iou_rotated.h" | ||||
|
|
||||
| #include <ATen/core/dispatch/Dispatcher.h> | ||||
| #include <torch/library.h> | ||||
| #include <torch/types.h> | ||||
|
|
||||
| namespace vision { | ||||
| namespace ops { | ||||
|
|
||||
| at::Tensor box_iou_rotated( | ||||
| const at::Tensor& boxes1, | ||||
| const at::Tensor& boxes2) { | ||||
| static auto op = c10::Dispatcher::singleton() | ||||
| .findSchemaOrThrow("torchvision::box_iou_rotated", "") | ||||
| .typed<decltype(box_iou_rotated)>(); | ||||
| return op.call(boxes1, boxes2); | ||||
| } | ||||
|
|
||||
| // Vendored because the pinned TorchVision (v0.20.1) has no box_iou_rotated. | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo typo : the pinned TorchVision is 0.23.0 according to torchvisionlib/csrc/CMakeLists.txt Line 112 in 49902bc
|
||||
| // Drop this directory if TorchVision is bumped to a release that ships the op, | ||||
| // otherwise the schema below is registered twice and loading fails. | ||||
| TORCH_LIBRARY_FRAGMENT(torchvision, m) { | ||||
| m.def(TORCH_SELECTIVE_SCHEMA( | ||||
| "torchvision::box_iou_rotated(Tensor boxes1, Tensor boxes2) -> Tensor")); | ||||
| } | ||||
|
|
||||
| } // namespace ops | ||||
| } // namespace vision | ||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #pragma once | ||
|
|
||
| #include <ATen/ATen.h> | ||
|
|
||
| namespace vision { | ||
| namespace ops { | ||
|
|
||
| at::Tensor box_iou_rotated( | ||
| const at::Tensor& boxes1, | ||
| const at::Tensor& boxes2); | ||
|
|
||
| } // namespace ops | ||
| } // namespace vision |
There was a problem hiding this comment.
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).