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
36d0e3e6
Unverified
Commit
36d0e3e6
authored
Feb 06, 2024
by
Mantas
Committed by
GitHub
Feb 06, 2024
Browse files
Allow 2D numpy arrays as inputs for `to_image` (#8256)
Co-authored-by:
Nicolas Hug
<
contact@nicolas-hug.com
>
parent
8e712070
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
1 deletion
+6
-1
test/test_transforms_v2.py
test/test_transforms_v2.py
+5
-0
torchvision/transforms/v2/functional/_type_conversion.py
torchvision/transforms/v2/functional/_type_conversion.py
+1
-1
No files found.
test/test_transforms_v2.py
View file @
36d0e3e6
...
...
@@ -5182,6 +5182,11 @@ class TestToImage:
if
isinstance
(
input
,
torch
.
Tensor
):
assert
output
.
data_ptr
()
==
input
.
data_ptr
()
def
test_2d_np_array
(
self
):
# Non-regression test for https://github.com/pytorch/vision/issues/8255
input
=
np
.
random
.
rand
(
10
,
10
)
assert
F
.
to_image
(
input
).
shape
==
(
1
,
10
,
10
)
def
test_functional_error
(
self
):
with
pytest
.
raises
(
TypeError
,
match
=
"Input can either be a pure Tensor, a numpy array, or a PIL image"
):
F
.
to_image
(
object
())
...
...
torchvision/transforms/v2/functional/_type_conversion.py
View file @
36d0e3e6
...
...
@@ -11,7 +11,7 @@ from torchvision.transforms import functional as _F
def
to_image
(
inpt
:
Union
[
torch
.
Tensor
,
PIL
.
Image
.
Image
,
np
.
ndarray
])
->
tv_tensors
.
Image
:
"""See :class:`~torchvision.transforms.v2.ToImage` for details."""
if
isinstance
(
inpt
,
np
.
ndarray
):
output
=
torch
.
from_numpy
(
inpt
).
permute
((
2
,
0
,
1
)).
contiguous
()
output
=
torch
.
from_numpy
(
np
.
atleast_3d
(
inpt
)
)
.
permute
((
2
,
0
,
1
)).
contiguous
()
elif
isinstance
(
inpt
,
PIL
.
Image
.
Image
):
output
=
pil_to_tensor
(
inpt
)
elif
isinstance
(
inpt
,
torch
.
Tensor
):
...
...
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