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
e0fd033c
Unverified
Commit
e0fd033c
authored
Jan 26, 2024
by
ahmadsharif1
Committed by
GitHub
Jan 26, 2024
Browse files
Expand the channels to 3 if the user requested as such (#8229)
Co-authored-by:
Nicolas Hug
<
contact@nicolas-hug.com
>
parent
7f55a1b3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
4 deletions
+17
-4
test/test_transforms_v2.py
test/test_transforms_v2.py
+11
-2
torchvision/transforms/v2/functional/_color.py
torchvision/transforms/v2/functional/_color.py
+6
-2
No files found.
test/test_transforms_v2.py
View file @
e0fd033c
...
@@ -4935,15 +4935,24 @@ class TestRgbToGrayscale:
...
@@ -4935,15 +4935,24 @@ class TestRgbToGrayscale:
check_transform
(
transform
,
make_input
())
check_transform
(
transform
,
make_input
())
@
pytest
.
mark
.
parametrize
(
"num_output_channels"
,
[
1
,
3
])
@
pytest
.
mark
.
parametrize
(
"num_output_channels"
,
[
1
,
3
])
@
pytest
.
mark
.
parametrize
(
"color_space"
,
[
"RGB"
,
"GRAY"
])
@
pytest
.
mark
.
parametrize
(
"fn"
,
[
F
.
rgb_to_grayscale
,
transform_cls_to_functional
(
transforms
.
Grayscale
)])
@
pytest
.
mark
.
parametrize
(
"fn"
,
[
F
.
rgb_to_grayscale
,
transform_cls_to_functional
(
transforms
.
Grayscale
)])
def
test_image_correctness
(
self
,
num_output_channels
,
fn
):
def
test_image_correctness
(
self
,
num_output_channels
,
color_space
,
fn
):
image
=
make_image
(
dtype
=
torch
.
uint8
,
device
=
"cpu"
)
image
=
make_image
(
dtype
=
torch
.
uint8
,
device
=
"cpu"
,
color_space
=
color_space
)
actual
=
fn
(
image
,
num_output_channels
=
num_output_channels
)
actual
=
fn
(
image
,
num_output_channels
=
num_output_channels
)
expected
=
F
.
to_image
(
F
.
rgb_to_grayscale
(
F
.
to_pil_image
(
image
),
num_output_channels
=
num_output_channels
))
expected
=
F
.
to_image
(
F
.
rgb_to_grayscale
(
F
.
to_pil_image
(
image
),
num_output_channels
=
num_output_channels
))
assert_equal
(
actual
,
expected
,
rtol
=
0
,
atol
=
1
)
assert_equal
(
actual
,
expected
,
rtol
=
0
,
atol
=
1
)
def
test_expanded_channels_are_not_views_into_the_same_underlying_tensor
(
self
):
image
=
make_image
(
dtype
=
torch
.
uint8
,
device
=
"cpu"
,
color_space
=
"GRAY"
)
output_image
=
F
.
rgb_to_grayscale
(
image
,
num_output_channels
=
3
)
assert_equal
(
output_image
[
0
][
0
][
0
],
output_image
[
1
][
0
][
0
])
output_image
[
0
][
0
][
0
]
=
output_image
[
0
][
0
][
0
]
+
1
assert
output_image
[
0
][
0
][
0
]
!=
output_image
[
1
][
0
][
0
]
@
pytest
.
mark
.
parametrize
(
"num_input_channels"
,
[
1
,
3
])
@
pytest
.
mark
.
parametrize
(
"num_input_channels"
,
[
1
,
3
])
def
test_random_transform_correctness
(
self
,
num_input_channels
):
def
test_random_transform_correctness
(
self
,
num_input_channels
):
image
=
make_image
(
image
=
make_image
(
...
...
torchvision/transforms/v2/functional/_color.py
View file @
e0fd033c
...
@@ -33,9 +33,13 @@ to_grayscale = rgb_to_grayscale
...
@@ -33,9 +33,13 @@ to_grayscale = rgb_to_grayscale
def
_rgb_to_grayscale_image
(
def
_rgb_to_grayscale_image
(
image
:
torch
.
Tensor
,
num_output_channels
:
int
=
1
,
preserve_dtype
:
bool
=
True
image
:
torch
.
Tensor
,
num_output_channels
:
int
=
1
,
preserve_dtype
:
bool
=
True
)
->
torch
.
Tensor
:
)
->
torch
.
Tensor
:
if
image
.
shape
[
-
3
]
==
1
:
# TODO: Maybe move the validation that num_output_channels is 1 or 3 to this function instead of callers.
if
image
.
shape
[
-
3
]
==
1
and
num_output_channels
==
1
:
return
image
.
clone
()
return
image
.
clone
()
if
image
.
shape
[
-
3
]
==
1
and
num_output_channels
==
3
:
s
=
[
1
]
*
len
(
image
.
shape
)
s
[
-
3
]
=
3
return
image
.
repeat
(
s
)
r
,
g
,
b
=
image
.
unbind
(
dim
=-
3
)
r
,
g
,
b
=
image
.
unbind
(
dim
=-
3
)
l_img
=
r
.
mul
(
0.2989
).
add_
(
g
,
alpha
=
0.587
).
add_
(
b
,
alpha
=
0.114
)
l_img
=
r
.
mul
(
0.2989
).
add_
(
g
,
alpha
=
0.587
).
add_
(
b
,
alpha
=
0.114
)
l_img
=
l_img
.
unsqueeze
(
dim
=-
3
)
l_img
=
l_img
.
unsqueeze
(
dim
=-
3
)
...
...
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