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
chenpangpang
transformers
Commits
1d777359
Unverified
Commit
1d777359
authored
Dec 20, 2023
by
amyeroberts
Committed by
GitHub
Dec 20, 2023
Browse files
Fix yolos resizing (#27663)
* Fix yolos resizing * Update tests * Add a test
parent
45b70384
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
22 deletions
+45
-22
src/transformers/models/detr/image_processing_detr.py
src/transformers/models/detr/image_processing_detr.py
+1
-0
src/transformers/models/yolos/image_processing_yolos.py
src/transformers/models/yolos/image_processing_yolos.py
+11
-11
tests/models/yolos/test_image_processing_yolos.py
tests/models/yolos/test_image_processing_yolos.py
+33
-11
No files found.
src/transformers/models/detr/image_processing_detr.py
View file @
1d777359
...
...
@@ -82,6 +82,7 @@ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
SUPPORTED_ANNOTATION_FORMATS
=
(
AnnotationFormat
.
COCO_DETECTION
,
AnnotationFormat
.
COCO_PANOPTIC
)
# From the original repo: https://github.com/facebookresearch/detr/blob/3af9fa878e73b6894ce3596450a8d9b89d918ca9/datasets/transforms.py#L76
def
get_size_with_aspect_ratio
(
image_size
,
size
,
max_size
=
None
)
->
Tuple
[
int
,
int
]:
"""
Computes the output image size given the input image size and the desired output size.
...
...
src/transformers/models/yolos/image_processing_yolos.py
View file @
1d777359
...
...
@@ -99,7 +99,6 @@ def get_max_height_width(
return
(
max_height
,
max_width
)
# Copied from transformers.models.detr.image_processing_detr.get_size_with_aspect_ratio
def
get_size_with_aspect_ratio
(
image_size
,
size
,
max_size
=
None
)
->
Tuple
[
int
,
int
]:
"""
Computes the output image size given the input image size and the desired output size.
...
...
@@ -119,16 +118,17 @@ def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, in
if
max_original_size
/
min_original_size
*
size
>
max_size
:
size
=
int
(
round
(
max_size
*
min_original_size
/
max_original_size
))
if
(
height
<=
width
and
height
==
size
)
or
(
width
<=
height
and
width
==
size
):
return
height
,
width
if
width
<
height
:
ow
=
size
oh
=
int
(
size
*
height
/
width
)
else
:
oh
=
size
ow
=
int
(
size
*
width
/
height
)
return
(
oh
,
ow
)
if
width
<
height
and
width
!=
size
:
height
=
int
(
size
*
height
/
width
)
width
=
size
elif
height
<
width
and
height
!=
size
:
width
=
int
(
size
*
width
/
height
)
height
=
size
width_mod
=
np
.
mod
(
width
,
16
)
height_mod
=
np
.
mod
(
height
,
16
)
width
=
width
-
width_mod
height
=
height
-
height_mod
return
(
height
,
width
)
# Copied from transformers.models.detr.image_processing_detr.get_resize_output_image_size
...
...
tests/models/yolos/test_image_processing_yolos.py
View file @
1d777359
...
...
@@ -86,18 +86,28 @@ class YolosImageProcessingTester(unittest.TestCase):
if
not
batched
:
image
=
image_inputs
[
0
]
if
isinstance
(
image
,
Image
.
Image
):
w
,
h
=
image
.
size
w
idth
,
height
=
image
.
size
else
:
h
,
w
=
image
.
shape
[
1
],
image
.
shape
[
2
]
if
w
<
h
:
expected_height
=
int
(
self
.
size
[
"shortest_edge"
]
*
h
/
w
)
expected_width
=
self
.
size
[
"shortest_edge"
]
elif
w
>
h
:
expected_height
=
self
.
size
[
"shortest_edge"
]
expected_width
=
int
(
self
.
size
[
"shortest_edge"
]
*
w
/
h
)
else
:
expected_height
=
self
.
size
[
"shortest_edge"
]
expected_width
=
self
.
size
[
"shortest_edge"
]
height
,
width
=
image
.
shape
[
1
],
image
.
shape
[
2
]
size
=
self
.
size
[
"shortest_edge"
]
max_size
=
self
.
size
.
get
(
"longest_edge"
,
None
)
if
max_size
is
not
None
:
min_original_size
=
float
(
min
((
height
,
width
)))
max_original_size
=
float
(
max
((
height
,
width
)))
if
max_original_size
/
min_original_size
*
size
>
max_size
:
size
=
int
(
round
(
max_size
*
min_original_size
/
max_original_size
))
if
width
<
height
and
width
!=
size
:
height
=
int
(
size
*
height
/
width
)
width
=
size
elif
height
<
width
and
height
!=
size
:
width
=
int
(
size
*
width
/
height
)
height
=
size
width_mod
=
width
%
16
height_mod
=
height
%
16
expected_width
=
width
-
width_mod
expected_height
=
height
-
height_mod
else
:
expected_values
=
[]
...
...
@@ -173,6 +183,18 @@ class YolosImageProcessingTest(AnnotationFormatTestMixin, ImageProcessingTestMix
torch
.
allclose
(
encoded_images_with_method
[
"pixel_values"
],
encoded_images
[
"pixel_values"
],
atol
=
1e-4
)
)
def
test_resize_max_size_respected
(
self
):
image_processor
=
self
.
image_processing_class
(
**
self
.
image_processor_dict
)
# create torch tensors as image
image
=
torch
.
randint
(
0
,
256
,
(
3
,
100
,
1500
),
dtype
=
torch
.
uint8
)
processed_image
=
image_processor
(
image
,
size
=
{
"longest_edge"
:
1333
,
"shortest_edge"
:
800
},
do_pad
=
False
,
return_tensors
=
"pt"
)[
"pixel_values"
]
self
.
assertTrue
(
processed_image
.
shape
[
-
1
]
<=
1333
)
self
.
assertTrue
(
processed_image
.
shape
[
-
2
]
<=
800
)
@
slow
def
test_call_pytorch_with_coco_detection_annotations
(
self
):
# prepare image and target
...
...
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