-
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
Open
srishtiii28
wants to merge
7
commits into
mlverse:main
Choose a base branch
from
srishtiii28:box-iou-rotated
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6068c5b
Add rotated box IoU CPU kernel
srishtiii28 562d69e
Export box_iou_rotated and regenerate bindings
srishtiii28 52c8eac
Add ops_box_iou_rotated with docs and tests
srishtiii28 868935a
Merge branch 'main' into box-iou-rotated
cregouby 026edec
Fix version note and tighten test tolerances
srishtiii28 9b97820
merge main
cregouby 0bdd249
Merge branch 'main' into box-iou-rotated
cregouby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.23.0) has no box_iou_rotated. | ||
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).