Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
51500c7e
Unverified
Commit
51500c7e
authored
Feb 11, 2021
by
Vasilis Vryniotis
Committed by
GitHub
Feb 11, 2021
Browse files
Make generalized_box_iou and box_iou share common code. (#3382)
parent
059b19ba
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
21 deletions
+18
-21
torchvision/ops/boxes.py
torchvision/ops/boxes.py
+18
-21
No files found.
torchvision/ops/boxes.py
View file @
51500c7e
...
...
@@ -187,6 +187,21 @@ def box_area(boxes: Tensor) -> Tensor:
# implementation from https://github.com/kuangliu/torchcv/blob/master/torchcv/utils/box.py
# with slight modifications
def
_box_inter_union
(
boxes1
:
Tensor
,
boxes2
:
Tensor
)
->
Tuple
[
Tensor
,
Tensor
]:
area1
=
box_area
(
boxes1
)
area2
=
box_area
(
boxes2
)
lt
=
torch
.
max
(
boxes1
[:,
None
,
:
2
],
boxes2
[:,
:
2
])
# [N,M,2]
rb
=
torch
.
min
(
boxes1
[:,
None
,
2
:],
boxes2
[:,
2
:])
# [N,M,2]
wh
=
(
rb
-
lt
).
clamp
(
min
=
0
)
# [N,M,2]
inter
=
wh
[:,
:,
0
]
*
wh
[:,
:,
1
]
# [N,M]
union
=
area1
[:,
None
]
+
area2
-
inter
return
inter
,
union
def
box_iou
(
boxes1
:
Tensor
,
boxes2
:
Tensor
)
->
Tensor
:
"""
Return intersection-over-union (Jaccard index) of boxes.
...
...
@@ -200,16 +215,8 @@ def box_iou(boxes1: Tensor, boxes2: Tensor) -> Tensor:
Returns:
iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2
"""
area1
=
box_area
(
boxes1
)
area2
=
box_area
(
boxes2
)
lt
=
torch
.
max
(
boxes1
[:,
None
,
:
2
],
boxes2
[:,
:
2
])
# [N,M,2]
rb
=
torch
.
min
(
boxes1
[:,
None
,
2
:],
boxes2
[:,
2
:])
# [N,M,2]
wh
=
(
rb
-
lt
).
clamp
(
min
=
0
)
# [N,M,2]
inter
=
wh
[:,
:,
0
]
*
wh
[:,
:,
1
]
# [N,M]
iou
=
inter
/
(
area1
[:,
None
]
+
area2
-
inter
)
inter
,
union
=
_box_inter_union
(
boxes1
,
boxes2
)
iou
=
inter
/
union
return
iou
...
...
@@ -234,17 +241,7 @@ def generalized_box_iou(boxes1: Tensor, boxes2: Tensor) -> Tensor:
assert
(
boxes1
[:,
2
:]
>=
boxes1
[:,
:
2
]).
all
()
assert
(
boxes2
[:,
2
:]
>=
boxes2
[:,
:
2
]).
all
()
area1
=
box_area
(
boxes1
)
area2
=
box_area
(
boxes2
)
lt
=
torch
.
max
(
boxes1
[:,
None
,
:
2
],
boxes2
[:,
:
2
])
# [N,M,2]
rb
=
torch
.
min
(
boxes1
[:,
None
,
2
:],
boxes2
[:,
2
:])
# [N,M,2]
wh
=
(
rb
-
lt
).
clamp
(
min
=
0
)
# [N,M,2]
inter
=
wh
[:,
:,
0
]
*
wh
[:,
:,
1
]
# [N,M]
union
=
area1
[:,
None
]
+
area2
-
inter
inter
,
union
=
_box_inter_union
(
boxes1
,
boxes2
)
iou
=
inter
/
union
lti
=
torch
.
min
(
boxes1
[:,
None
,
:
2
],
boxes2
[:,
:
2
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment