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
MMCV
Commits
a1fb0022
Commit
a1fb0022
authored
Jun 17, 2022
by
Ma Zerun
Committed by
zhouzaida
Jul 19, 2022
Browse files
[Refactor] Use `resize_type` instead of `resize_cfg` in `RandomResize` and `RandomChoiceResize`.
parent
a03774d6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
35 deletions
+55
-35
mmcv/transforms/processing.py
mmcv/transforms/processing.py
+39
-22
tests/test_transforms/test_transforms_processing.py
tests/test_transforms/test_transforms_processing.py
+16
-13
No files found.
mmcv/transforms/processing.py
View file @
a1fb0022
...
@@ -909,15 +909,26 @@ class RandomChoiceResize(BaseTransform):
...
@@ -909,15 +909,26 @@ class RandomChoiceResize(BaseTransform):
Args:
Args:
scales (Union[list, Tuple]): Images scales for resizing.
scales (Union[list, Tuple]): Images scales for resizing.
resize_cfg (dict): Base config for resizing. Refer to
resize_type (str): The type of resize class to use. Defaults to
``mmcv.Resize`` for detail. Defaults to
"Resize".
``dict(type='Resize')``.
**resize_kwargs: Other keyword arguments for the ``resize_type``.
Note:
By defaults, the ``resize_type`` is "Resize", if it's not overwritten
by your registry, it indicates the :class:`mmcv.Resize`. And therefore,
``resize_kwargs`` accepts any keyword arguments of it, like
``keep_ratio``, ``interpolation`` and so on.
If you want to use your custom resize class, the class should accept
``scale`` argument and have ``scale`` attribution which determines the
resize shape.
"""
"""
def
__init__
(
def
__init__
(
self
,
self
,
scales
:
Sequence
[
Union
[
int
,
Tuple
]],
scales
:
Sequence
[
Union
[
int
,
Tuple
]],
resize_cfg
:
dict
=
dict
(
type
=
'Resize'
)
resize_type
:
str
=
'Resize'
,
**
resize_kwargs
,
)
->
None
:
)
->
None
:
super
().
__init__
()
super
().
__init__
()
if
isinstance
(
scales
,
list
):
if
isinstance
(
scales
,
list
):
...
@@ -925,7 +936,10 @@ class RandomChoiceResize(BaseTransform):
...
@@ -925,7 +936,10 @@ class RandomChoiceResize(BaseTransform):
else
:
else
:
self
.
scales
=
[
scales
]
self
.
scales
=
[
scales
]
assert
mmcv
.
is_list_of
(
self
.
scales
,
tuple
)
assert
mmcv
.
is_list_of
(
self
.
scales
,
tuple
)
self
.
resize_cfg
=
resize_cfg
self
.
resize_cfg
=
dict
(
type
=
resize_type
,
**
resize_kwargs
)
# create a empty Reisize object
self
.
resize
=
TRANSFORMS
.
build
({
'scale'
:
0
,
**
self
.
resize_cfg
})
@
cache_randomness
@
cache_randomness
def
_random_select
(
self
)
->
Tuple
[
int
,
int
]:
def
_random_select
(
self
)
->
Tuple
[
int
,
int
]:
...
@@ -955,10 +969,8 @@ class RandomChoiceResize(BaseTransform):
...
@@ -955,10 +969,8 @@ class RandomChoiceResize(BaseTransform):
"""
"""
target_scale
,
scale_idx
=
self
.
_random_select
()
target_scale
,
scale_idx
=
self
.
_random_select
()
_resize_cfg
=
self
.
resize_cfg
.
copy
()
self
.
resize
.
scale
=
target_scale
_resize_cfg
.
update
(
dict
(
scale
=
target_scale
))
results
=
self
.
resize
(
results
)
resize_transform
=
TRANSFORMS
.
build
(
_resize_cfg
)
results
=
resize_transform
(
results
)
results
[
'scale_idx'
]
=
scale_idx
results
[
'scale_idx'
]
=
scale_idx
return
results
return
results
...
@@ -1254,30 +1266,35 @@ class RandomResize(BaseTransform):
...
@@ -1254,30 +1266,35 @@ class RandomResize(BaseTransform):
Defaults to None.
Defaults to None.
ratio_range (tuple[float], optional): (min_ratio, max_ratio).
ratio_range (tuple[float], optional): (min_ratio, max_ratio).
Defaults to None.
Defaults to None.
resize_cfg (dict): Config to initialize a ``Resize`` transform.
resize_type (str): The type of resize class to use. Defaults to
Defaults to dict(type='Resize', keep_ratio=True,
"Resize".
clip_object_border=True, backend='cv2', interpolation='bilinear').
**resize_kwargs: Other keyword arguments for the ``resize_type``.
Note:
By defaults, the ``resize_type`` is "Resize", if it's not overwritten
by your registry, it indicates the :class:`mmcv.Resize`. And therefore,
``resize_kwargs`` accepts any keyword arguments of it, like
``keep_ratio``, ``interpolation`` and so on.
If you want to use your custom resize class, the class should accept
``scale`` argument and have ``scale`` attribution which determines the
resize shape.
"""
"""
def
__init__
(
def
__init__
(
self
,
self
,
scale
:
Union
[
Tuple
[
int
,
int
],
Sequence
[
Tuple
[
int
,
int
]]],
scale
:
Union
[
Tuple
[
int
,
int
],
Sequence
[
Tuple
[
int
,
int
]]],
ratio_range
:
Tuple
[
float
,
float
]
=
None
,
ratio_range
:
Tuple
[
float
,
float
]
=
None
,
resize_cfg
:
dict
=
dict
(
resize_type
:
str
=
'Resize'
,
type
=
'Resize'
,
**
resize_kwargs
,
keep_ratio
=
True
,
clip_object_border
=
True
,
backend
=
'cv2'
,
interpolation
=
'bilinear'
)
)
->
None
:
)
->
None
:
self
.
scale
=
scale
self
.
scale
=
scale
self
.
ratio_range
=
ratio_range
self
.
ratio_range
=
ratio_range
self
.
resize_cfg
=
resize_cfg
self
.
resize_cfg
=
dict
(
type
=
resize_type
,
**
resize_kwargs
)
# create a empty Reisize object
# create a empty Reisize object
self
.
resize_cfg
.
update
(
dict
(
scale
=
0
))
self
.
resize
=
TRANSFORMS
.
build
({
'scale'
:
0
,
**
self
.
resize_cfg
})
self
.
resize
=
TRANSFORMS
.
build
(
self
.
resize_cfg
)
@
staticmethod
@
staticmethod
def
_random_sample
(
scales
:
Sequence
[
Tuple
[
int
,
int
]])
->
tuple
:
def
_random_sample
(
scales
:
Sequence
[
Tuple
[
int
,
int
]])
->
tuple
:
...
@@ -1361,7 +1378,7 @@ class RandomResize(BaseTransform):
...
@@ -1361,7 +1378,7 @@ class RandomResize(BaseTransform):
"""
"""
results
[
'scale'
]
=
self
.
_random_scale
()
results
[
'scale'
]
=
self
.
_random_scale
()
self
.
resize
.
scale
=
results
[
'scale'
]
self
.
resize
.
scale
=
results
[
'scale'
]
results
=
self
.
resize
.
transform
(
results
)
results
=
self
.
resize
(
results
)
return
results
return
results
def
__repr__
(
self
)
->
str
:
def
__repr__
(
self
)
->
str
:
...
...
tests/test_transforms/test_transforms_processing.py
View file @
a1fb0022
...
@@ -708,7 +708,8 @@ class TestRandomChoiceResize:
...
@@ -708,7 +708,8 @@ class TestRandomChoiceResize:
transform
=
dict
(
transform
=
dict
(
type
=
'RandomChoiceResize'
,
type
=
'RandomChoiceResize'
,
scales
=
[(
900
,
600
)],
scales
=
[(
900
,
600
)],
resize_cfg
=
dict
(
type
=
'Resize'
,
keep_ratio
=
True
))
resize_type
=
'Resize'
,
keep_ratio
=
True
)
random_multiscale_resize
=
TRANSFORMS
.
build
(
transform
)
random_multiscale_resize
=
TRANSFORMS
.
build
(
transform
)
self
.
reset_results
(
results
)
self
.
reset_results
(
results
)
_input_ratio
=
results
[
'img'
].
shape
[
0
]
/
results
[
'img'
].
shape
[
1
]
_input_ratio
=
results
[
'img'
].
shape
[
0
]
/
results
[
'img'
].
shape
[
1
]
...
@@ -721,7 +722,8 @@ class TestRandomChoiceResize:
...
@@ -721,7 +722,8 @@ class TestRandomChoiceResize:
transform
=
dict
(
transform
=
dict
(
type
=
'RandomChoiceResize'
,
type
=
'RandomChoiceResize'
,
scales
=
[(
200
,
150
)],
scales
=
[(
200
,
150
)],
resize_cfg
=
dict
(
type
=
'Resize'
,
clip_object_border
=
True
))
resize_type
=
'Resize'
,
clip_object_border
=
True
)
random_multiscale_resize
=
TRANSFORMS
.
build
(
transform
)
random_multiscale_resize
=
TRANSFORMS
.
build
(
transform
)
self
.
reset_results
(
results
)
self
.
reset_results
(
results
)
results
[
'gt_bboxes'
]
=
np
.
array
(
gt_bboxes
)
results
[
'gt_bboxes'
]
=
np
.
array
(
gt_bboxes
)
...
@@ -733,7 +735,8 @@ class TestRandomChoiceResize:
...
@@ -733,7 +735,8 @@ class TestRandomChoiceResize:
transform
=
dict
(
transform
=
dict
(
type
=
'RandomChoiceResize'
,
type
=
'RandomChoiceResize'
,
scales
=
[(
200
,
150
)],
scales
=
[(
200
,
150
)],
resize_cfg
=
dict
(
type
=
'Resize'
,
clip_object_border
=
False
))
resize_type
=
'Resize'
,
clip_object_border
=
False
)
random_multiscale_resize
=
TRANSFORMS
.
build
(
transform
)
random_multiscale_resize
=
TRANSFORMS
.
build
(
transform
)
self
.
reset_results
(
results
)
self
.
reset_results
(
results
)
results
[
'gt_bboxes'
]
=
np
.
array
(
gt_bboxes
)
results
[
'gt_bboxes'
]
=
np
.
array
(
gt_bboxes
)
...
@@ -861,9 +864,9 @@ class TestRandomResize:
...
@@ -861,9 +864,9 @@ class TestRandomResize:
'gt_keypoints'
:
np
.
array
([[[
112
,
112
]]])
'gt_keypoints'
:
np
.
array
([[[
112
,
112
]]])
}
}
TRANSFORMS
=
RandomResize
(
TRANSFORMS
=
RandomResize
(
(
224
,
224
),
(
1.0
,
2.0
),
(
224
,
224
),
(
1.0
,
2.0
)
,
resize_type
=
'Resize'
,
resize_cfg
=
dict
(
type
=
'Resize'
,
keep_ratio
=
True
)
)
keep_ratio
=
True
)
results_update
=
TRANSFORMS
.
transform
(
copy
.
deepcopy
(
results
))
results_update
=
TRANSFORMS
.
transform
(
copy
.
deepcopy
(
results
))
assert
224
<=
results_update
[
'img_shape'
][
0
]
assert
224
<=
results_update
[
'img_shape'
][
0
]
assert
448
>=
results_update
[
'img_shape'
][
0
]
assert
448
>=
results_update
[
'img_shape'
][
0
]
...
@@ -874,17 +877,17 @@ class TestRandomResize:
...
@@ -874,17 +877,17 @@ class TestRandomResize:
assert
results
[
'gt_bboxes'
][
0
][
2
]
<=
112
assert
results
[
'gt_bboxes'
][
0
][
2
]
<=
112
# keep ratio is False
# keep ratio is False
TRANSFORMS
=
RandomResize
(
TRANSFORMS
=
RandomResize
(
(
224
,
224
),
(
1.0
,
2.0
),
(
224
,
224
),
(
1.0
,
2.0
)
,
resize_type
=
'Resize'
,
resize_cfg
=
dict
(
type
=
'Resize'
,
keep_ratio
=
False
)
)
keep_ratio
=
False
)
results_update
=
TRANSFORMS
.
transform
(
copy
.
deepcopy
(
results
))
results_update
=
TRANSFORMS
.
transform
(
copy
.
deepcopy
(
results
))
# choose target scale from init when override is False and scale is a
# choose target scale from init when override is False and scale is a
# list of tuples
# list of tuples
results
=
{}
results
=
{}
TRANSFORMS
=
RandomResize
([(
224
,
448
),
(
112
,
224
)],
TRANSFORMS
=
RandomResize
([(
224
,
448
),
(
112
,
224
)],
resize_
cfg
=
dict
(
resize_
type
=
'Resize'
,
type
=
'Resize'
,
keep_ratio
=
True
)
)
keep_ratio
=
True
)
results_update
=
TRANSFORMS
.
transform
(
copy
.
deepcopy
(
results
))
results_update
=
TRANSFORMS
.
transform
(
copy
.
deepcopy
(
results
))
assert
results_update
[
'scale'
][
1
]
>=
224
and
results_update
[
'scale'
][
assert
results_update
[
'scale'
][
1
]
>=
224
and
results_update
[
'scale'
][
1
]
<=
448
1
]
<=
448
...
@@ -895,6 +898,6 @@ class TestRandomResize:
...
@@ -895,6 +898,6 @@ class TestRandomResize:
with
pytest
.
raises
(
NotImplementedError
):
with
pytest
.
raises
(
NotImplementedError
):
results
=
{}
results
=
{}
TRANSFORMS
=
RandomResize
([(
224
,
448
),
[
112
,
224
]],
TRANSFORMS
=
RandomResize
([(
224
,
448
),
[
112
,
224
]],
resize_
cfg
=
dict
(
resize_
type
=
'Resize'
,
type
=
'Resize'
,
keep_ratio
=
True
)
)
keep_ratio
=
True
)
results_update
=
TRANSFORMS
.
transform
(
copy
.
deepcopy
(
results
))
results_update
=
TRANSFORMS
.
transform
(
copy
.
deepcopy
(
results
))
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