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
d78b462f
Unverified
Commit
d78b462f
authored
Sep 08, 2023
by
Philip Meier
Committed by
GitHub
Sep 08, 2023
Browse files
allow float fill for integer images in F.pad (#7950)
parent
65d50d0f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
8 deletions
+23
-8
test/test_transforms_v2_refactored.py
test/test_transforms_v2_refactored.py
+18
-7
torchvision/transforms/v2/functional/_geometry.py
torchvision/transforms/v2/functional/_geometry.py
+5
-1
No files found.
test/test_transforms_v2_refactored.py
View file @
d78b462f
...
@@ -312,11 +312,12 @@ def adapt_fill(value, *, dtype):
...
@@ -312,11 +312,12 @@ def adapt_fill(value, *, dtype):
return
value
return
value
max_value
=
get_max_value
(
dtype
)
max_value
=
get_max_value
(
dtype
)
value_type
=
float
if
dtype
.
is_floating_point
else
int
if
isinstance
(
value
,
(
int
,
float
)):
if
isinstance
(
value
,
(
int
,
float
)):
return
type
(
value
)
(
value
*
max_value
)
return
value_type
(
value
*
max_value
)
elif
isinstance
(
value
,
(
list
,
tuple
)):
elif
isinstance
(
value
,
(
list
,
tuple
)):
return
type
(
value
)(
type
(
v
)(
v
*
max_value
)
for
v
in
value
)
return
type
(
value
)(
value_
type
(
v
*
max_value
)
for
v
in
value
)
else
:
else
:
raise
ValueError
(
f
"fill should be an int or float, or a list or tuple of the former, but got '
{
value
}
'."
)
raise
ValueError
(
f
"fill should be an int or float, or a list or tuple of the former, but got '
{
value
}
'."
)
...
@@ -417,6 +418,10 @@ def reference_affine_bounding_boxes_helper(bounding_boxes, *, affine_matrix, new
...
@@ -417,6 +418,10 @@ def reference_affine_bounding_boxes_helper(bounding_boxes, *, affine_matrix, new
)
)
# turns all warnings into errors for this module
pytestmark
=
pytest
.
mark
.
filterwarnings
(
"error"
)
class
TestResize
:
class
TestResize
:
INPUT_SIZE
=
(
17
,
11
)
INPUT_SIZE
=
(
17
,
11
)
OUTPUT_SIZES
=
[
17
,
[
17
],
(
17
,),
[
12
,
13
],
(
12
,
13
)]
OUTPUT_SIZES
=
[
17
,
[
17
],
(
17
,),
[
12
,
13
],
(
12
,
13
)]
...
@@ -2577,15 +2582,19 @@ class TestCrop:
...
@@ -2577,15 +2582,19 @@ class TestCrop:
def
test_transform
(
self
,
param
,
value
,
make_input
):
def
test_transform
(
self
,
param
,
value
,
make_input
):
input
=
make_input
(
self
.
INPUT_SIZE
)
input
=
make_input
(
self
.
INPUT_SIZE
)
kwargs
=
{
param
:
value
}
if
param
==
"fill"
:
if
param
==
"fill"
:
# 1. size is required
# 2. the fill parameter only has an affect if we need padding
kwargs
[
"size"
]
=
[
s
+
4
for
s
in
self
.
INPUT_SIZE
]
if
isinstance
(
input
,
tv_tensors
.
Mask
)
and
isinstance
(
value
,
(
tuple
,
list
)):
if
isinstance
(
input
,
tv_tensors
.
Mask
)
and
isinstance
(
value
,
(
tuple
,
list
)):
pytest
.
skip
(
"F.pad_mask doesn't support non-scalar fill."
)
pytest
.
skip
(
"F.pad_mask doesn't support non-scalar fill."
)
kwargs
=
dict
(
# 1. size is required
# 2. the fill parameter only has an affect if we need padding
size
=
[
s
+
4
for
s
in
self
.
INPUT_SIZE
],
fill
=
adapt_fill
(
value
,
dtype
=
input
.
dtype
if
isinstance
(
input
,
torch
.
Tensor
)
else
torch
.
uint8
),
)
else
:
kwargs
=
{
param
:
value
}
check_transform
(
check_transform
(
transforms
.
RandomCrop
(
**
kwargs
,
pad_if_needed
=
True
),
transforms
.
RandomCrop
(
**
kwargs
,
pad_if_needed
=
True
),
input
,
input
,
...
@@ -3478,6 +3487,8 @@ class TestPad:
...
@@ -3478,6 +3487,8 @@ class TestPad:
def
test_image_correctness
(
self
,
padding
,
padding_mode
,
fill
,
fn
):
def
test_image_correctness
(
self
,
padding
,
padding_mode
,
fill
,
fn
):
image
=
make_image
(
dtype
=
torch
.
uint8
,
device
=
"cpu"
)
image
=
make_image
(
dtype
=
torch
.
uint8
,
device
=
"cpu"
)
fill
=
adapt_fill
(
fill
,
dtype
=
torch
.
uint8
)
actual
=
fn
(
image
,
padding
=
padding
,
padding_mode
=
padding_mode
,
fill
=
fill
)
actual
=
fn
(
image
,
padding
=
padding
,
padding_mode
=
padding_mode
,
fill
=
fill
)
expected
=
F
.
to_image
(
F
.
pad
(
F
.
to_pil_image
(
image
),
padding
=
padding
,
padding_mode
=
padding_mode
,
fill
=
fill
))
expected
=
F
.
to_image
(
F
.
pad
(
F
.
to_pil_image
(
image
),
padding
=
padding
,
padding_mode
=
padding_mode
,
fill
=
fill
))
...
...
torchvision/transforms/v2/functional/_geometry.py
View file @
d78b462f
...
@@ -1235,7 +1235,11 @@ def _pad_with_vector_fill(
...
@@ -1235,7 +1235,11 @@ def _pad_with_vector_fill(
output
=
_pad_with_scalar_fill
(
image
,
torch_padding
,
fill
=
0
,
padding_mode
=
"constant"
)
output
=
_pad_with_scalar_fill
(
image
,
torch_padding
,
fill
=
0
,
padding_mode
=
"constant"
)
left
,
right
,
top
,
bottom
=
torch_padding
left
,
right
,
top
,
bottom
=
torch_padding
fill
=
torch
.
tensor
(
fill
,
dtype
=
image
.
dtype
,
device
=
image
.
device
).
reshape
(
-
1
,
1
,
1
)
# We are creating the tensor in the autodetected dtype first and convert to the right one after to avoid an implicit
# float -> int conversion. That happens for example for the valid input of a uint8 image with floating point fill
# value.
fill
=
torch
.
tensor
(
fill
,
device
=
image
.
device
).
to
(
dtype
=
image
.
dtype
).
reshape
(
-
1
,
1
,
1
)
if
top
>
0
:
if
top
>
0
:
output
[...,
:
top
,
:]
=
fill
output
[...,
:
top
,
:]
=
fill
...
...
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