-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Adds bounding boxes conversion #2710
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
34d86e7
adds boxes conversion
oke-aditya 466b43b
adds documentation
oke-aditya bd08535
adds xywh tests
oke-aditya aeabf25
fixes small typo
oke-aditya 5f997c1
adds tests
oke-aditya 912bfba
Remove sphinx theme
oke-aditya 01f5bed
corrects assertions
oke-aditya 7a1568d
cleans code as per suggestion
oke-aditya 2049750
reverts assertion
oke-aditya 4cf8710
Merge branch 'master' into bbox_conv
oke-aditya 2bf077c
Merge branch 'master' into bbox_conv
oke-aditya 26d53eb
fixes to assertEqual
oke-aditya b9cb8ee
fixes inplace operations
oke-aditya 84abd9a
Merge branch 'bbox_conv' of github.com:oke-aditya/vision into bbox_conv
oke-aditya d616e89
Adds docstrings
oke-aditya 23da6ec
Merge branch 'master' into bbox_conv
oke-aditya b20d211
added documentation
oke-aditya 11b7c06
changes tests
oke-aditya 260526a
moves code to box_convert
oke-aditya 2db8614
adds more tests
oke-aditya 216b6db
Merge branch 'master' into bbox_conv
oke-aditya 9bad964
Apply suggestions from code review
fmassa 2425f45
fixes documentation
oke-aditya 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import torch | ||
| from torch.jit.annotations import Tuple | ||
| from torch import Tensor | ||
| import torchvision | ||
|
|
||
|
|
||
| def _box_cxcywh_to_xyxy(boxes: Tensor) -> Tensor: | ||
| """ | ||
| Converts bounding boxes from (cx, cy, w, h) format to (x1, y1, x2, y2) format. | ||
| (cx, cy) refers to center of bounding box | ||
| (w, h) are width and height of bounding box | ||
| Arguments: | ||
| boxes (Tensor[N, 4]): boxes in (cx, cy, w, h) format which will be converted. | ||
|
|
||
| Returns: | ||
| boxes (Tensor(N, 4)): boxes in (x1, y1, x2, y2) format. | ||
| """ | ||
| # We need to change all 4 of them so some temporary variable is needed. | ||
| cx, cy, w, h = boxes.unbind(-1) | ||
| x1 = cx - 0.5 * w | ||
| y1 = cy - 0.5 * h | ||
| x2 = cx + 0.5 * w | ||
| y2 = cy + 0.5 * h | ||
|
|
||
| boxes = torch.stack((x1, y1, x2, y2), dim=-1) | ||
|
|
||
| return boxes | ||
|
|
||
|
|
||
| def _box_xyxy_to_cxcywh(boxes: Tensor) -> Tensor: | ||
| """ | ||
| Converts bounding boxes from (x1, y1, x2, y2) format to (cx, cy, w, h) format. | ||
| (x1, y1) refer to top left of bounding box | ||
| (x2, y2) refer to bottom right of bounding box | ||
| Arguments: | ||
| boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) format which will be converted. | ||
|
|
||
| Returns: | ||
| boxes (Tensor(N, 4)): boxes in (cx, cy, w, h) format. | ||
| """ | ||
| x1, y1, x2, y2 = boxes.unbind(-1) | ||
| cx = (x1 + x2) / 2 | ||
| cy = (y1 + y2) / 2 | ||
| w = x2 - x1 | ||
| h = y2 - y1 | ||
|
|
||
| boxes = torch.stack((cx, cy, w, h), dim=-1) | ||
|
|
||
| return boxes | ||
|
|
||
|
|
||
| def _box_xywh_to_xyxy(boxes: Tensor) -> Tensor: | ||
| """ | ||
| Converts bounding boxes from (x, y, w, h) format to (x1, y1, x2, y2) format. | ||
| (x, y) refers to top left of bouding box. | ||
| (w, h) refers to width and height of box. | ||
| Arguments: | ||
| boxes (Tensor[N, 4]): boxes in (x, y, w, h) which will be converted. | ||
|
|
||
| Returns: | ||
| boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) format. | ||
| """ | ||
| x, y, w, h = boxes.unbind(-1) | ||
| boxes = torch.stack([x, y, x + w, y + h], dim=-1) | ||
| return boxes | ||
|
|
||
|
|
||
| def _box_xyxy_to_xywh(boxes: Tensor) -> Tensor: | ||
| """ | ||
| Converts bounding boxes from (x1, y1, x2, y2) format to (x, y, w, h) format. | ||
| (x1, y1) refer to top left of bounding box | ||
| (x2, y2) refer to bottom right of bounding box | ||
| Arguments: | ||
| boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) which will be converted. | ||
|
|
||
| Returns: | ||
| boxes (Tensor[N, 4]): boxes in (x, y, w, h) format. | ||
| """ | ||
| x1, y1, x2, y2 = boxes.unbind(-1) | ||
| x2 = x2 - x1 # x2 - x1 | ||
| y2 = y2 - y1 # y2 - y1 | ||
oke-aditya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| boxes = torch.stack((x1, y1, x2, y2), dim=-1) | ||
| return boxes | ||
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
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.
Two options here:
Do you remember what type of errors you were facing? I'm fine with both approaches, so that we can move forward with this PR (but we should fix torchscript soon if we merge this without torchscript support)