Skip to content

Commit

Permalink
Adding keep ration to Resize function
Browse files Browse the repository at this point in the history
  • Loading branch information
ccanamero committed Aug 21, 2024
1 parent 00ee948 commit e2241da
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions mmcls/datasets/pipelines/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,14 +687,18 @@ class Resize(object):
"short", "long", "height", "width". Default to "short".
backend (str): The image resize backend type, accepted values are
`cv2` and `pillow`. Default: `cv2`.
img_scale (tuple or list[tuple]): Images scales for resizing.
keep_ratio (bool): Whether to keep the aspect ratio when resizing the
image.
"""

def __init__(self,
size,
interpolation='bilinear',
adaptive_side='short',
backend='cv2',
img_scale=None):
img_scale=None,
keep_ratio=True):

if img_scale is None:
self.img_scale = None
Expand Down Expand Up @@ -730,6 +734,7 @@ def __init__(self,
self.size = size
self.interpolation = interpolation
self.backend = backend
self.keep_ratio = keep_ratio

def _resize_img(self, results):
for key in results.get('img_fields', ['img']):
Expand Down Expand Up @@ -760,7 +765,7 @@ def _resize_img(self, results):
width = int(target_size * w / h)
else:
height, width = self.size
if not ignore_resize:
if not ignore_resize and self.keep_ratio:
img = mmcv.imresize(
img,
size=(width, height),
Expand All @@ -769,14 +774,24 @@ def _resize_img(self, results):
backend=self.backend)
results[key] = img
results['img_shape'] = img.shape
results['keep_ratio'] = self.keep_ratio

def __call__(self, results):
"""Call function to resize images
Args:
results (dict): Result dict from loading pipeline.
Returns:
dict: Resized results, keep_ratio keys are added into result dict.
"""
self._resize_img(results)
return results

def __repr__(self):
repr_str = self.__class__.__name__
repr_str += f'(img_scale={self.img_scale}, '
repr_str += f'keep_ratio={self.keep_ratio}, '
repr_str += f'(size={self.size}, '
repr_str += f'interpolation={self.interpolation})'
return repr_str
Expand Down

0 comments on commit e2241da

Please sign in to comment.