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
4c0f4414
Unverified
Commit
4c0f4414
authored
Feb 06, 2024
by
Johan Edstedt
Committed by
GitHub
Feb 06, 2024
Browse files
Make _get_perspective_coeffs more numerically stable (#8249)
Co-authored-by:
Nicolas Hug
<
nh.nicolas.hug@gmail.com
>
parent
ae147895
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
3 deletions
+8
-3
torchvision/transforms/functional.py
torchvision/transforms/functional.py
+8
-3
No files found.
torchvision/transforms/functional.py
View file @
4c0f4414
...
...
@@ -676,14 +676,19 @@ def _get_perspective_coeffs(startpoints: List[List[int]], endpoints: List[List[i
Returns:
octuple (a, b, c, d, e, f, g, h) for transforming each pixel.
"""
a_matrix
=
torch
.
zeros
(
2
*
len
(
startpoints
),
8
,
dtype
=
torch
.
float
)
if
len
(
startpoints
)
!=
4
or
len
(
endpoints
)
!=
4
:
raise
ValueError
(
f
"Please provide exactly four corners, got
{
len
(
startpoints
)
}
startpoints and
{
len
(
endpoints
)
}
endpoints."
)
a_matrix
=
torch
.
zeros
(
2
*
len
(
startpoints
),
8
,
dtype
=
torch
.
float64
)
for
i
,
(
p1
,
p2
)
in
enumerate
(
zip
(
endpoints
,
startpoints
)):
a_matrix
[
2
*
i
,
:]
=
torch
.
tensor
([
p1
[
0
],
p1
[
1
],
1
,
0
,
0
,
0
,
-
p2
[
0
]
*
p1
[
0
],
-
p2
[
0
]
*
p1
[
1
]])
a_matrix
[
2
*
i
+
1
,
:]
=
torch
.
tensor
([
0
,
0
,
0
,
p1
[
0
],
p1
[
1
],
1
,
-
p2
[
1
]
*
p1
[
0
],
-
p2
[
1
]
*
p1
[
1
]])
b_matrix
=
torch
.
tensor
(
startpoints
,
dtype
=
torch
.
float
).
view
(
8
)
res
=
torch
.
linalg
.
lstsq
(
a_matrix
,
b_matrix
,
driver
=
"gels"
).
solution
b_matrix
=
torch
.
tensor
(
startpoints
,
dtype
=
torch
.
float64
).
view
(
8
)
# do least squares in double precision to prevent numerical issues
res
=
torch
.
linalg
.
lstsq
(
a_matrix
,
b_matrix
,
driver
=
"gels"
).
solution
.
to
(
torch
.
float32
)
output
:
List
[
float
]
=
res
.
tolist
()
return
output
...
...
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