Skip to content
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

Detection IoU #1284

Merged
merged 211 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
211 commits
Select commit Hold shift + click to select a range
57cdcc3
Initial work by @jscottcronin
twsl Oct 13, 2022
d0d43a3
Fix devcontainer
twsl Oct 14, 2022
e76151c
Upgrade pre-commit
twsl Oct 14, 2022
b28111b
Restructuring
twsl Oct 16, 2022
4ff4a38
Raw test structure
twsl Oct 16, 2022
2568301
Box class removal
twsl Oct 17, 2022
c736ff1
Fix test issues
twsl Oct 17, 2022
566edfd
Make class abstract
twsl Oct 18, 2022
5416cd6
Fix iou results
twsl Oct 18, 2022
cbc824c
Merge branch 'master' into feature/iou
SkafteNicki Oct 24, 2022
1eadc78
Update src/torchmetrics/detection/ciou.py
twsl Oct 24, 2022
09ce274
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2022
948d115
Update src/torchmetrics/detection/diou.py
twsl Oct 24, 2022
9855c83
Update src/torchmetrics/detection/giou.py
twsl Oct 24, 2022
4775314
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2022
762d635
Update src/torchmetrics/detection/iou.py
twsl Oct 24, 2022
093114f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2022
224aaca
Update src/torchmetrics/detection/helpers.py
twsl Oct 24, 2022
98555fa
Update src/torchmetrics/detection/helpers.py
twsl Oct 24, 2022
22800e7
Apply suggestions from code review
twsl Oct 24, 2022
5f1c3d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2022
696566b
changelog
SkafteNicki Oct 24, 2022
e3bf8e9
docs
SkafteNicki Oct 24, 2022
28c8737
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2022
1508383
Fix method order, missing args description & iou
twsl Oct 24, 2022
7daa0bb
Merge branch 'master' into feature/iou
SkafteNicki Oct 25, 2022
06b1adf
Merge branch 'master' into feature/iou
SkafteNicki Oct 25, 2022
bb2e6d2
Merge branch 'master' into feature/iou
justusschock Oct 31, 2022
7ff90e2
Merge branch 'master' into feature/iou
SkafteNicki Oct 31, 2022
7ed7701
Merge branch 'master' into feature/iou
SkafteNicki Nov 4, 2022
d5bd36f
Label-respecting approach
twsl Dec 22, 2022
d6e52b5
Merge branch 'master' into feature/iou
twsl Dec 22, 2022
373557b
Merge branch 'master' into feature/iou
twsl Dec 25, 2022
68d875a
Merge branch 'master' into feature/iou
stancld Jan 7, 2023
dd27625
Merge branch 'master' into feature/iou
twsl Jan 22, 2023
c52f592
Fix compare_version & typo
twsl Jan 22, 2023
0c6be00
Update src/torchmetrics/detection/ciou.py
twsl Jan 24, 2023
5874af7
Update src/torchmetrics/detection/diou.py
twsl Jan 24, 2023
e872266
Update src/torchmetrics/detection/diou.py
twsl Jan 24, 2023
c91777a
Update src/torchmetrics/detection/diou.py
twsl Jan 24, 2023
ff3c5fa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2023
c70d10c
Merge branch 'master' into feature/iou
twsl Jan 28, 2023
43e0512
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 28, 2023
6bd7c67
Merge branch 'master' into feature/iou
twsl Feb 6, 2023
ffebafe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 6, 2023
54eb1c7
fn variant
twsl Feb 6, 2023
c5ae700
Merge branch 'feature/iou' of https://github.com/twsl/metrics into fe…
twsl Feb 6, 2023
5be9d5f
Applied suggestions
twsl Feb 6, 2023
74a4239
Increase torchvision version
twsl Feb 6, 2023
8f67381
Merge branch 'master' into feature/iou
twsl Feb 6, 2023
f24a48a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 6, 2023
16cbaad
Merge branch 'master' into feature/iou
twsl Feb 6, 2023
a5fa358
Merge branch 'master' into feature/iou
Borda Feb 7, 2023
9ed490a
Fix mypy
twsl Feb 7, 2023
ac9cc26
Add torchvision test condition
twsl Feb 7, 2023
0bf773b
Merge branch 'master' into feature/iou
twsl Feb 7, 2023
a205caf
Attempt to fix tests
twsl Feb 7, 2023
1e1b283
Merge branch 'master' into feature/iou
twsl Feb 9, 2023
334a94c
Merge branch 'master' into feature/iou
twsl Feb 14, 2023
6b6e097
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 14, 2023
8b5109e
Reverse type hint due to python 3.7 conflict
twsl Feb 14, 2023
e945ee0
Merge branch 'master' into feature/iou
twsl Feb 16, 2023
2260d55
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 16, 2023
053f75d
Update __init__.py
twsl Feb 16, 2023
47321bf
Update requirements/detection.txt
twsl Feb 16, 2023
258f1fd
Update requirements/detection_test.txt
twsl Feb 16, 2023
7ef77fb
Merge branch 'master' into feature/iou
mergify[bot] Feb 17, 2023
cac08b6
Merge branch 'master' into feature/iou
mergify[bot] Feb 17, 2023
d29c1d4
Merge branch 'master' into feature/iou
mergify[bot] Feb 17, 2023
3077216
Merge branch 'master' into feature/iou
twsl Feb 22, 2023
adf57e5
Merge branch 'master' into feature/iou
mergify[bot] Feb 23, 2023
ce5fff1
Merge branch 'master' into feature/iou
mergify[bot] Feb 23, 2023
3721da7
Merge branch 'master' into feature/iou
mergify[bot] Feb 23, 2023
927d05e
Merge branch 'master' into feature/iou
mergify[bot] Feb 23, 2023
68a0d3c
Merge branch 'master' into feature/iou
mergify[bot] Feb 23, 2023
3b9f91c
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
ef21d93
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
f7903f6
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
8dc5892
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
3f086ce
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
53566a9
ruff: first line split + imperative mood (#1548)
SkafteNicki Feb 24, 2023
2a3f488
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
a7331b0
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
1a074e9
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
9f40e94
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
eef1f2e
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
5b9dcf0
Merge branch 'master' into feature/iou
mergify[bot] Feb 24, 2023
a7cdf2f
Merge branch 'master' into feature/iou
mergify[bot] Feb 25, 2023
2fcae4d
Merge branch 'master' into feature/iou
mergify[bot] Feb 25, 2023
0969107
Merge branch 'master' into feature/iou
mergify[bot] Feb 25, 2023
4b03a8b
Merge branch 'master' into feature/iou
Borda Feb 27, 2023
b4ac2d6
Merge branch 'master' into feature/iou
mergify[bot] Feb 27, 2023
0998d6e
Merge branch 'master' into feature/iou
mergify[bot] Feb 27, 2023
510dbeb
Merge branch 'master' into feature/iou
mergify[bot] Feb 28, 2023
15187ee
Merge branch 'master' into feature/iou
mergify[bot] Feb 28, 2023
9ab4546
fix docs
Borda Feb 28, 2023
901e819
function as input
Borda Feb 28, 2023
cb92f3c
map
Borda Feb 28, 2023
5329a81
ref name
Borda Feb 28, 2023
8174dbe
Merge branch 'master' into feature/iou
mergify[bot] Feb 28, 2023
4d6cb84
Merge branch 'master' into feature/iou
mergify[bot] Feb 28, 2023
4bac14e
Merge branch 'master' into feature/iou
mergify[bot] Feb 28, 2023
06d8ebf
Merge branch 'master' into feature/iou
mergify[bot] Feb 28, 2023
d00e591
Fix iou type and fn call
twsl Feb 28, 2023
2642f4f
Fix mypy
twsl Feb 28, 2023
4a5859d
Fix doc test results
twsl Feb 28, 2023
eafffda
Merge branch 'master' into feature/iou
mergify[bot] Mar 1, 2023
04eef3c
Fix renamed iou type
twsl Mar 1, 2023
09de1a2
Fix input validator
twsl Mar 1, 2023
5474af9
Update tests
twsl Mar 1, 2023
2c4084b
Fix iou_type check
twsl Mar 1, 2023
9ba74cd
Fix labels_eq
twsl Mar 1, 2023
d09186d
Merge branch 'master' into feature/iou
mergify[bot] Mar 1, 2023
73a47a1
Update iou.py
twsl Mar 1, 2023
68226fb
Merge branch 'master' into feature/iou
twsl Mar 2, 2023
cac5347
Merge branch 'master' into feature/iou
mergify[bot] Mar 3, 2023
2b3a43b
Merge branch 'master' into feature/iou
twsl Mar 4, 2023
0ecadbe
Merge branch 'master' into feature/iou
mergify[bot] Mar 4, 2023
0826edd
Merge branch 'master' into feature/iou
mergify[bot] Mar 4, 2023
208e12e
Merge branch 'master' into feature/iou
mergify[bot] Mar 5, 2023
973e277
Merge branch 'master' into feature/iou
mergify[bot] Mar 6, 2023
b6dd6a7
Merge branch 'master' into feature/iou
mergify[bot] Mar 6, 2023
b580a84
Merge branch 'master' into feature/iou
mergify[bot] Mar 6, 2023
ada72ae
Merge branch 'master' into feature/iou
mergify[bot] Mar 6, 2023
798ec0b
Merge branch 'master' into feature/iou
mergify[bot] Mar 6, 2023
4f926c4
Merge branch 'master' into feature/iou
mergify[bot] Mar 7, 2023
242291a
Merge branch 'master' into feature/iou
mergify[bot] Mar 7, 2023
fa102fb
Merge branch 'master' into feature/iou
mergify[bot] Mar 9, 2023
ccbbc36
Merge branch 'master' into feature/iou
mergify[bot] Mar 9, 2023
a7bb8a2
Merge branch 'master' into feature/iou
mergify[bot] Mar 10, 2023
0d9b638
Merge branch 'master' into feature/iou
mergify[bot] Mar 10, 2023
b7394f6
Merge branch 'master' into feature/iou
mergify[bot] Mar 10, 2023
cec317a
Merge branch 'master' into feature/iou
mergify[bot] Mar 10, 2023
5131e24
Merge branch 'master' into feature/iou
mergify[bot] Mar 10, 2023
3818e81
Merge branch 'master' into feature/iou
mergify[bot] Mar 11, 2023
ffb3414
Merge branch 'master' into feature/iou
mergify[bot] Mar 13, 2023
cbec03d
Merge branch 'master' into feature/iou
mergify[bot] Mar 13, 2023
21562d6
Merge branch 'master' into feature/iou
mergify[bot] Mar 13, 2023
802ad5b
Merge branch 'master' into feature/iou
mergify[bot] Mar 14, 2023
a3ef293
Merge branch 'master' into feature/iou
mergify[bot] Mar 14, 2023
0bf3df1
Merge branch 'master' into feature/iou
mergify[bot] Mar 14, 2023
135859f
Merge branch 'master' into feature/iou
mergify[bot] Mar 14, 2023
d27b833
Merge branch 'master' into feature/iou
mergify[bot] Mar 14, 2023
48f35c8
Merge branch 'master' into feature/iou
mergify[bot] Mar 15, 2023
5f1129b
Merge branch 'master' into feature/iou
mergify[bot] Mar 15, 2023
fdac582
Update devcontainer
twsl Mar 15, 2023
c34592f
Use floats
twsl Mar 15, 2023
df4e3ad
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 15, 2023
a6bdb70
Fix ruff errors
twsl Mar 15, 2023
ac81a81
Merge branch 'master' into feature/iou
mergify[bot] Mar 15, 2023
11bd031
Merge branch 'master' into feature/iou
mergify[bot] Mar 15, 2023
6834e88
Merge branch 'master' into feature/iou
justusschock Mar 17, 2023
7bcf3f0
Merge branch 'master' into feature/iou
mergify[bot] Mar 17, 2023
21e5075
Merge branch 'master' into feature/iou
mergify[bot] Mar 17, 2023
4a3f82e
Merge branch 'master' into feature/iou
Borda Mar 21, 2023
3eb66c9
Merge branch 'master' into feature/iou
mergify[bot] Mar 21, 2023
a4216c1
Merge branch 'master' into feature/iou
mergify[bot] Mar 21, 2023
68bcdbd
Merge branch 'master' into feature/iou
mergify[bot] Mar 21, 2023
c1bf2e2
Merge branch 'master' into feature/iou
mergify[bot] Mar 21, 2023
acca3d5
Clean up
twsl Mar 21, 2023
c3cbf78
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
8d552ad
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
b955e3a
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
ffd2b9b
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
56196cb
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
007c095
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
b6c1ad8
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
632e33b
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
d44dfb0
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
2dfdebf
Merge branch 'master' into feature/iou
mergify[bot] Mar 28, 2023
4c2e726
Merge branch 'master' into feature/iou
mergify[bot] Mar 29, 2023
3ec6240
Merge branch 'master' into feature/iou
mergify[bot] Mar 31, 2023
c99be22
Merge branch 'master' into feature/iou
mergify[bot] Mar 31, 2023
7e213cc
Merge branch 'master' into feature/iou
mergify[bot] Mar 31, 2023
f7b0551
Merge branch 'master' into feature/iou
mergify[bot] Mar 31, 2023
bceb364
Merge branch 'master' into feature/iou
mergify[bot] Mar 31, 2023
7a36261
Merge branch 'master' into feature/iou
mergify[bot] Mar 31, 2023
e5e4611
Merge branch 'master' into feature/iou
mergify[bot] Mar 31, 2023
a916229
missing docs imports
SkafteNicki Apr 13, 2023
d78f01e
merge master
SkafteNicki Apr 13, 2023
753894a
docs
SkafteNicki Apr 13, 2023
15a52e9
changelog
SkafteNicki Apr 13, 2023
b35f077
fix race condition
SkafteNicki Apr 13, 2023
376f0ca
Merge branch 'master' into feature/iou
SkafteNicki Apr 13, 2023
cfc979b
fix test values
SkafteNicki Apr 13, 2023
ca93130
Merge branch 'feature/iou' of https://github.com/twsl/metrics into fe…
SkafteNicki Apr 13, 2023
06a7018
Merge branch 'master' into feature/iou
mergify[bot] Apr 13, 2023
78e13dc
Merge branch 'master' into feature/iou
mergify[bot] Apr 13, 2023
0d3d8bd
Merge branch 'master' into feature/iou
mergify[bot] Apr 13, 2023
023f0fb
add doc examples
SkafteNicki Apr 14, 2023
0b11823
add plot methods
SkafteNicki Apr 14, 2023
d162836
Merge branch 'master' into feature/iou
Borda Apr 14, 2023
c50b6b9
docs require
SkafteNicki Apr 14, 2023
baacd67
add input/output descriptions
SkafteNicki Apr 14, 2023
d2f648c
fix plot doctest
SkafteNicki Apr 14, 2023
adcc552
Merge branch 'master' into feature/iou
SkafteNicki Apr 15, 2023
dbbfd57
Merge branch 'master' into feature/iou
mergify[bot] Apr 17, 2023
aaa0ba1
Merge branch 'master' into feature/iou
Borda Apr 17, 2023
0998047
Merge branch 'master' into feature/iou
mergify[bot] Apr 17, 2023
18d10e6
Merge branch 'master' into feature/iou
mergify[bot] Apr 17, 2023
9e5954a
Merge branch 'master' into feature/iou
mergify[bot] Apr 17, 2023
6fe6a2a
Merge branch 'master' into feature/iou
mergify[bot] Apr 17, 2023
b1ef07a
Merge branch 'master' into feature/iou
mergify[bot] Apr 17, 2023
5a97680
__doctest_requires__
Borda Apr 17, 2023
b7f6b52
doctests
Borda Apr 17, 2023
826c4bb
Merge branch 'master' into feature/iou
mergify[bot] Apr 17, 2023
d088258
import
Borda Apr 17, 2023
1536cde
Merge branch 'feature/iou' of https://github.com/twsl/metrics into fe…
Borda Apr 17, 2023
940eedf
import
Borda Apr 17, 2023
fdc7178
+
Borda Apr 17, 2023
c6d2b74
Merge branch 'master' into feature/iou
mergify[bot] Apr 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"editor.rulers": [
120
],
"files.exclude": {
"**/__pycache__": true
},
"python.pythonPath": "/usr/local/bin/python",
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.languageServer": "Pylance",
Expand All @@ -37,7 +40,7 @@
"python.analysis.useImportHeuristic": true,
"python.sortImports": true,
"python.sortImports.args": [
"--settings-path=${workspaceFolder}/pyproject.toml",
"--settings-path=${workspaceFolder}/pyproject.toml"
],
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
Expand All @@ -63,12 +66,11 @@
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"restructuredtext.confPath": "${workspaceFolder}/docs/source",
"restructuredtext.builtDocumentationPath": "${workspaceFolder}/docs/build",
"restructuredtext.languageServer.disabled": false,
"esbonio.sphinx.confDir": "${workspaceFolder}/docs/source",
"esbonio.sphinx.buildDir": "${workspaceFolder}/docs/build",
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"source.organizeImports": true
}
}
},
Expand All @@ -84,7 +86,7 @@
"shardulm94.trailing-spaces",
"njqdev.vscode-python-typehint",
"lextudio.restructuredtext",
"trond-snekvik.simple-rst",
"trond-snekvik.simple-rst"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

default_language_version:
python: python3.8
python: python3.9

ci:
autofix_prs: true
Expand All @@ -38,7 +38,7 @@ repos:
- id: detect-private-key

- repo: https://github.com/asottile/pyupgrade
rev: v2.38.2
rev: v3.1.0
hooks:
- id: pyupgrade
args: [--py36-plus]
Expand All @@ -58,7 +58,7 @@ repos:
require_serial: false

- repo: https://github.com/psf/black
rev: 22.8.0
rev: 22.10.0
hooks:
- id: black
name: Format code
Expand Down
14 changes: 12 additions & 2 deletions src/torchmetrics/detection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from torchmetrics.utilities.imports import _TORCHVISION_GREATER_EQUAL_0_8
from torchmetrics.utilities.imports import (
_TORCHVISION_AVAILABLE,
_TORCHVISION_GREATER_EQUAL_0_8,
_TORCHVISION_GREATER_EQUAL_0_13,
)

if _TORCHVISION_GREATER_EQUAL_0_8:
if _TORCHVISION_AVAILABLE and _TORCHVISION_GREATER_EQUAL_0_13:
from torchmetrics.detection.ciou import CompleteIntersectionOverUnion # noqa: F401
from torchmetrics.detection.diou import DistanceIntersectionOverUnion # noqa: F401

if _TORCHVISION_AVAILABLE and _TORCHVISION_GREATER_EQUAL_0_8:
from torchmetrics.detection.giou import GeneralizedIntersectionOverUnion # noqa: F401
from torchmetrics.detection.iou import IntersectionOverUnion # noqa: F401
from torchmetrics.detection.mean_ap import MeanAveragePrecision # noqa: F401
57 changes: 57 additions & 0 deletions src/torchmetrics/detection/ciou.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Callable, Optional

from torch import Tensor

from torchmetrics.detection.iou import IntersectionOverUnion
from torchmetrics.functional.detection.ciou import complete_intersection_over_union
from torchmetrics.utilities.imports import _TORCHVISION_GREATER_EQUAL_0_8, _TORCHVISION_GREATER_EQUAL_0_13

if _TORCHVISION_GREATER_EQUAL_0_8:
from torchvision.ops import box_convert
else:
box_convert = None
__doctest_skip__ = ["CompleteIntersectionOverUnion"]
twsl marked this conversation as resolved.
Show resolved Hide resolved


class CompleteIntersectionOverUnion(IntersectionOverUnion):
r"""
Computes Complete Intersection Over Union (CIoU) <https://arxiv.org/abs/2005.03572>`_
twsl marked this conversation as resolved.
Show resolved Hide resolved
Args:
box_format:
Input format of given boxes. Supported formats are ``[`xyxy`, `xywh`, `cxcywh`]``.
iou_thresholds:
Optional IoU thresholds for evaluation. If set to `None` the threshold is ignored.
class_metrics:
Option to enable per-class metrics for IoU. Has a performance impact.
"""
twsl marked this conversation as resolved.
Show resolved Hide resolved

iou_fn: Callable[[Tensor, Tensor, bool], Tensor] = complete_intersection_over_union
type: str = "ciou"
twsl marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
box_format: str = "xyxy",
iou_threshold: Optional[float] = None,
class_metrics: bool = False,
**kwargs: Any,
) -> None:
super().__init__(box_format, iou_threshold, class_metrics, **kwargs)
twsl marked this conversation as resolved.
Show resolved Hide resolved

if not _TORCHVISION_GREATER_EQUAL_0_13:
raise ModuleNotFoundError(
f"Metric `{self.type.upper()}` requires that `torchvision` version 0.13.0 or newer is installed."
" Please install with `pip install torchvision>=0.13` or `pip install torchmetrics[detection]`."
twsl marked this conversation as resolved.
Show resolved Hide resolved
)
57 changes: 57 additions & 0 deletions src/torchmetrics/detection/diou.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Callable, Optional

from torch import Tensor

from torchmetrics.detection.iou import IntersectionOverUnion
from torchmetrics.functional.detection.diou import distance_intersection_over_union
from torchmetrics.utilities.imports import _TORCHVISION_GREATER_EQUAL_0_8, _TORCHVISION_GREATER_EQUAL_0_13

if _TORCHVISION_GREATER_EQUAL_0_8:
from torchvision.ops import box_convert
else:
box_convert = None
__doctest_skip__ = ["DistanceIntersectionOverUnion"]
twsl marked this conversation as resolved.
Show resolved Hide resolved


class DistanceIntersectionOverUnion(IntersectionOverUnion):
r"""
Computes Distance Intersection Over Union (DIoU) <https://arxiv.org/abs/1911.08287v1>`_
twsl marked this conversation as resolved.
Show resolved Hide resolved
Args:
box_format:
Input format of given boxes. Supported formats are ``[`xyxy`, `xywh`, `cxcywh`]``.
twsl marked this conversation as resolved.
Show resolved Hide resolved
iou_thresholds:
Optional IoU thresholds for evaluation. If set to `None` the threshold is ignored.
class_metrics:
Option to enable per-class metrics for IoU. Has a performance impact.
twsl marked this conversation as resolved.
Show resolved Hide resolved
"""

iou_fn: Callable[[Tensor, Tensor, bool], Tensor] = distance_intersection_over_union
type: str = "diou"

def __init__(
self,
box_format: str = "xyxy",
twsl marked this conversation as resolved.
Show resolved Hide resolved
iou_threshold: Optional[float] = None,
class_metrics: bool = False,
**kwargs: Any,
) -> None:
super().__init__(box_format, iou_threshold, class_metrics, **kwargs)

if not _TORCHVISION_GREATER_EQUAL_0_13:
raise ModuleNotFoundError(
f"Metric `{self.type.upper()}` requires that `torchvision` version 0.13.0 or newer is installed."
" Please install with `pip install torchvision>=0.13` or `pip install torchmetrics[detection]`."
)
51 changes: 51 additions & 0 deletions src/torchmetrics/detection/giou.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Callable, Optional

from torch import Tensor

from torchmetrics.detection.iou import IntersectionOverUnion
from torchmetrics.functional.detection.giou import generalized_intersection_over_union
from torchmetrics.utilities.imports import _TORCHVISION_GREATER_EQUAL_0_8

if _TORCHVISION_GREATER_EQUAL_0_8:
from torchvision.ops import box_convert
else:
box_convert = None
__doctest_skip__ = ["GeneralizedIntersectionOverUnion"]


class GeneralizedIntersectionOverUnion(IntersectionOverUnion):
r"""
Computes Generalized Intersection Over Union (GIoU) <https://arxiv.org/abs/1902.09630>`_
Args:
box_format:
Input format of given boxes. Supported formats are ``[`xyxy`, `xywh`, `cxcywh`]``.
iou_thresholds:
Optional IoU thresholds for evaluation. If set to `None` the threshold is ignored.
class_metrics:
Option to enable per-class metrics for IoU. Has a performance impact.
twsl marked this conversation as resolved.
Show resolved Hide resolved
"""

iou_fn: Callable[[Tensor, Tensor, bool], Tensor] = generalized_intersection_over_union
type: str = "giou"

def __init__(
self,
box_format: str = "xyxy",
iou_threshold: Optional[float] = None,
class_metrics: bool = False,
**kwargs: Any,
) -> None:
super().__init__(box_format, iou_threshold, class_metrics, **kwargs)
66 changes: 66 additions & 0 deletions src/torchmetrics/detection/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict, Sequence

from torch import Tensor


def _input_validator(preds: Sequence[Dict[str, Tensor]], targets: Sequence[Dict[str, Tensor]]) -> None:
"""Ensure the correct input format of `preds` and `targets`"""
if not isinstance(preds, Sequence):
raise ValueError("Expected argument `preds` to be of type Sequence")
twsl marked this conversation as resolved.
Show resolved Hide resolved
if not isinstance(targets, Sequence):
raise ValueError("Expected argument `target` to be of type Sequence")
twsl marked this conversation as resolved.
Show resolved Hide resolved
if len(preds) != len(targets):
raise ValueError("Expected argument `preds` and `target` to have the same length")
twsl marked this conversation as resolved.
Show resolved Hide resolved

for k in ["boxes", "scores", "labels"]:
if any(k not in p for p in preds):
raise ValueError(f"Expected all dicts in `preds` to contain the `{k}` key")

for k in ["boxes", "labels"]:
if any(k not in p for p in targets):
raise ValueError(f"Expected all dicts in `target` to contain the `{k}` key")

if any(type(pred["boxes"]) is not Tensor for pred in preds):
raise ValueError("Expected all boxes in `preds` to be of type Tensor")
if any(type(pred["scores"]) is not Tensor for pred in preds):
raise ValueError("Expected all scores in `preds` to be of type Tensor")
if any(type(pred["labels"]) is not Tensor for pred in preds):
raise ValueError("Expected all labels in `preds` to be of type Tensor")
if any(type(target["boxes"]) is not Tensor for target in targets):
raise ValueError("Expected all boxes in `target` to be of type Tensor")
if any(type(target["labels"]) is not Tensor for target in targets):
raise ValueError("Expected all labels in `target` to be of type Tensor")

for i, item in enumerate(targets):
if item["boxes"].size(0) != item["labels"].size(0):
raise ValueError(
f"Input boxes and labels of sample {i} in targets have a"
f" different length (expected {item['boxes'].size(0)} labels, got {item['labels'].size(0)})"
)
for i, item in enumerate(preds):
if not (item["boxes"].size(0) == item["labels"].size(0) == item["scores"].size(0)):
raise ValueError(
f"Input boxes, labels and scores of sample {i} in predictions have a"
f" different length (expected {item['boxes'].size(0)} labels and scores,"
f" got {item['labels'].size(0)} labels and {item['scores'].size(0)})"
)


def _fix_empty_tensors(boxes: Tensor) -> Tensor:
"""Empty tensors can cause problems in DDP mode, this methods corrects them."""
if boxes.numel() == 0 and boxes.ndim == 1:
return boxes.unsqueeze(0)
return boxes
Loading