Skip to content

Commit

Permalink
[Feature] Add RandomCenterCrop into transform.py (PaddlePaddle#1475)
Browse files Browse the repository at this point in the history
@zhangjin12138 嗨,非常感谢您的贡献,我们会将您的pr纳入PaddleSeg2.4的重要feature发版,能麻烦你将这个pr提交到develop分支吗?
  • Loading branch information
zhangjinCV authored Nov 16, 2021
1 parent 602b796 commit e4c5b00
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions paddleseg/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,73 @@ def __call__(self, im, label=None):
else:
return (im, label)


@manager.TRANSFORMS.add_component
class RandomCenterCrop:
"""
Crops the given the input data at the center.
Args:
retain_ratio (tuple or list, optional): The length of the input list or tuple must be 2. default:(0.5, 0.5).
the first value is used for width and the second is for height.
In addition, The minimum size of the cropped image is [width * retain_ratio[0], height * retain_ratio[1].
Raises:
TypeError: When retain_ratio is neither list nor tuple. Default:None.
ValueError: When the value of retain_ratio is not in [0-1].
"""

def __init__(self,
retain_ratio=(0.5, 0.5)):
if isinstance(retain_ratio, list) or isinstance(retain_ratio, tuple):
if len(retain_ratio) != 2:
raise ValueError(
'When type of `retain_ratio` is list or tuple, it shoule include 2 elements, but it is {}'.format(
retain_ratio)
)
if retain_ratio[0] > 1 or retain_ratio[1] > 1 or retain_ratio[0] < 0 or retain_ratio[1] < 0:
raise ValueError(
'Value of `retain_ratio` should be in [0, 1], but it is {}'.format(retain_ratio)
)

else:
raise TypeError(
"The type of `retain_ratio` is invalid. It should be list or tuple, but it is {}"
.format(type(retain_ratio)))
self.retain_ratio = retain_ratio

def __call__(self, im, label=None):
"""
Args:
im (np.ndarray): The Image data.
label (np.ndarray, optional): The label data. Default: None.
Returns:
(tuple). When label is None, it returns (im, ), otherwise it returns (im, label).
"""
retain_width = self.retain_ratio[0]
retain_height = self.retain_ratio[1]

img_height = im.shape[0]
img_width = im.shape[1]

if retain_width == 1. and retain_height == 1.:
if label is None:
return (im,)
else:
return (im, label)
else:
randw = np.random.randint(img_width * (1 - retain_width))
randh = np.random.randint(img_height * (1 - retain_height))
offsetw = 0 if randw == 0 else np.random.randint(randw)
offseth = 0 if randh == 0 else np.random.randint(randh)
p0, p1, p2, p3 = offseth, img_height + offseth - randh, offsetw, img_width + offsetw - randw
im = im[p0:p1, p2:p3, :]
if label is not None:
label = label[p0:p1, p2:p3, :]

if label is None:
return (im,)
else:
return (im, label)


@manager.TRANSFORMS.add_component
class ScalePadding:
Expand Down

0 comments on commit e4c5b00

Please sign in to comment.