Commit 5867a97a authored by YuanLiuuuuuu's avatar YuanLiuuuuuu Committed by zhouzaida
Browse files

[Fix]: Fix random resize

parent eb3bd34c
...@@ -1197,17 +1197,34 @@ class RandomResize(BaseTransform): ...@@ -1197,17 +1197,34 @@ class RandomResize(BaseTransform):
How to choose the target scale to resize the image will follow the rules How to choose the target scale to resize the image will follow the rules
below: below:
- if ``scale`` is a list of tuple, the first value of the target scale is - if ``scale`` is a sequence of tuple
sampled from [``scale[0][0]``, ``scale[1][0]``] uniformally and the
second value of the target scale is sampled from .. math::
[``scale[0][1]``, ``scale[1][1]``] uniformally. Following the resize target\\_scale[0] \\sim Uniform([scale[0][0], scale[1][0]])
order of weight and height in cv2, scale[i][0] is for width, and .. math::
scale[i][1] is for height. target\\_scale[1] \\sim Uniform([scale[0][1], scale[1][1]])
- if ``scale`` is a tuple, the first and second values of the target scale
is equal to the first and second values of ``scale`` multiplied by a Following the resize order of weight and height in cv2, ``scale[i][0]``
value sampled from [``ratio_range[0]``, ``ratio_range[1]``] uniformally. is for width, and ``scale[i][1]`` is for height.
Following the resize order of weight and height in cv2, ratio_range[0] is
for width, and ratio_range[1] is for height. - if ``scale`` is a tuple
.. math::
target\\_scale[0] \\sim Uniform([ratio\\_range[0], ratio\\_range[1]])
* scale[0]
.. math::
target\\_scale[0] \\sim Uniform([ratio\\_range[0], ratio\\_range[1]])
* scale[1]
Following the resize order of weight and height in cv2, ``ratio_range[0]``
is for width, and ``ratio_range[1]`` is for height.
- if ``keep_ratio`` is True, the minimum value of ``target_scale`` will be
used to set the shorter side and the maximum value will be used to
set the longer side.
- if ``keep_ratio`` is False, the value of ``target_scale`` will be used to
reisze the width and height accordingly.
Required Keys: Required Keys:
...@@ -1231,7 +1248,7 @@ class RandomResize(BaseTransform): ...@@ -1231,7 +1248,7 @@ class RandomResize(BaseTransform):
- keep_ratio - keep_ratio
Args: Args:
scale (tuple or list[tuple]): Images scales for resizing. scale (tuple or Sequence[tuple]): Images scales for resizing.
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.
...@@ -1242,7 +1259,7 @@ class RandomResize(BaseTransform): ...@@ -1242,7 +1259,7 @@ class RandomResize(BaseTransform):
def __init__( def __init__(
self, self,
scale: Union[Tuple[int, int], List[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_cfg: dict = dict(
type='Resize', type='Resize',
...@@ -1268,16 +1285,17 @@ class RandomResize(BaseTransform): ...@@ -1268,16 +1285,17 @@ class RandomResize(BaseTransform):
scales (list[tuple]): Images scale range for sampling. scales (list[tuple]): Images scale range for sampling.
There must be two tuples in scales, which specify the lower There must be two tuples in scales, which specify the lower
and upper bound of image scales. and upper bound of image scales.
Returns: Returns:
tuple: The targeted scale of the image to be resized. tuple: The targeted scale of the image to be resized.
""" """
assert mmcv.is_list_of(scales, tuple) and len(scales) == 2 assert mmcv.is_list_of(scales, tuple) and len(scales) == 2
scale_long = [max(s) for s in scales] scale_0 = [scales[0][0], scales[1][0]]
scale_short = [min(s) for s in scales] scale_1 = [scales[0][1], scales[1][1]]
long_edge = np.random.randint(min(scale_long), max(scale_long) + 1) edge_0 = np.random.randint(min(scale_0), max(scale_0) + 1)
short_edge = np.random.randint(min(scale_short), max(scale_short) + 1) edge_1 = np.random.randint(min(scale_1), max(scale_1) + 1)
scale = (long_edge, short_edge) scale = (edge_0, edge_1)
return scale return scale
@staticmethod @staticmethod
...@@ -1288,10 +1306,12 @@ class RandomResize(BaseTransform): ...@@ -1288,10 +1306,12 @@ class RandomResize(BaseTransform):
A ratio will be randomly sampled from the range specified by A ratio will be randomly sampled from the range specified by
``ratio_range``. Then it would be multiplied with ``scale`` to ``ratio_range``. Then it would be multiplied with ``scale`` to
generate sampled scale. generate sampled scale.
Args: Args:
scale (tuple): Images scale base to multiply with ratio. scale (tuple): Images scale base to multiply with ratio.
ratio_range (tuple[float]): The minimum and maximum ratio to scale ratio_range (tuple[float]): The minimum and maximum ratio to scale
the ``scale``. the ``scale``.
Returns: Returns:
tuple: The targeted scale of the image to be resized. tuple: The targeted scale of the image to be resized.
""" """
...@@ -1312,14 +1332,16 @@ class RandomResize(BaseTransform): ...@@ -1312,14 +1332,16 @@ class RandomResize(BaseTransform):
tuple: The targeted scale of the image to be resized. tuple: The targeted scale of the image to be resized.
""" """
if isinstance(self.scale, tuple): if mmcv.is_tuple_of(self.scale, int):
assert self.ratio_range is not None and len(self.ratio_range) == 2 assert self.ratio_range is not None and len(self.ratio_range) == 2
scale = self._random_sample_ratio(self.scale, self.ratio_range) scale = self._random_sample_ratio(
elif mmcv.is_list_of(self.scale, tuple): self.scale, # type: ignore
scale = self._random_sample(self.scale) self.ratio_range)
elif mmcv.is_seq_of(self.scale, tuple):
scale = self._random_sample(self.scale) # type: ignore
else: else:
raise NotImplementedError(f"Do not support sampling function \ raise NotImplementedError('Do not support sampling function '
for '{self.scale}'") f'for "{self.scale}"')
return scale return scale
...@@ -1329,6 +1351,7 @@ class RandomResize(BaseTransform): ...@@ -1329,6 +1351,7 @@ class RandomResize(BaseTransform):
Args: Args:
results (dict): Result dict from loading pipeline. results (dict): Result dict from loading pipeline.
Returns: Returns:
dict: Resized results, ``img``, ``gt_bboxes``, ``gt_semantic_seg``, dict: Resized results, ``img``, ``gt_bboxes``, ``gt_semantic_seg``,
``gt_keypoints``, ``scale``, ``scale_factor``, ``img_shape``, and ``gt_keypoints``, ``scale``, ``scale_factor``, ``img_shape``, and
......
...@@ -886,10 +886,10 @@ class TestRandomResize: ...@@ -886,10 +886,10 @@ class TestRandomResize:
resize_cfg=dict( resize_cfg=dict(
type='Resize', keep_ratio=True)) type='Resize', keep_ratio=True))
results_update = TRANSFORMS.transform(copy.deepcopy(results)) results_update = TRANSFORMS.transform(copy.deepcopy(results))
assert results_update['scale'][0] >= 224 and results_update['scale'][ assert results_update['scale'][1] >= 224 and results_update['scale'][
0] <= 448 1] <= 448
assert results_update['scale'][1] >= 112 and results_update['scale'][ assert results_update['scale'][0] >= 112 and results_update['scale'][
1] <= 224 0] <= 224
# the type of scale is invalid in init # the type of scale is invalid in init
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment