先进入get_transform函数,根据sample_style选择不同的size
然后进入get_output_shape函数
1.找到短边,根据短边与提供的最短长度列表,计算缩放比例
2.初步缩放,得到多组h, w
3.Max_size判断:
1)若max(h,w)<max_size , 正常用
2)若max(h,w)>max——size, 继续缩放--根据max(h,w)缩放到max_size这个比例来缩
class ResizeShortestEdge(Augmentation):
"""
Resize the image while keeping the aspect ratio unchanged.
It attempts to scale the shorter edge to the given `short_edge_length`,
as long as the longer edge does not exceed `max_size`.
If `max_size` is reached, then downscale so that the longer edge does not exceed max_size.
"""
@torch.jit.unused
def __init__(
self, short_edge_length, max_size=sys.maxsize, sample_style="range", interp=Image.BILINEAR
):
"""
Args:
short_edge_length (list[int]): If ``sample_style=="range"``,
a [min, max] interval from which to sample the shortest edge length.
If ``sample_style=="choice"``, a list of shortest edge lengths to sample from.
max_size (int): maximum allowed longest edge length.
sample_style (str): either "range" or "choice".
"""
super().__init__()
assert sample_style in ["range", "choice"], sample_style
self.is_range = sample_style == "range"
if isinstance(short_edge_length, int):
short_edge_length = (short_edge_length, short_edge_length)
if self.is_range:
assert len(short_edge_length) == 2, (
"short_edge_length must be two values using 'range' sample style."
f" Got {short_edge_length}!"
)
self._init(locals())
@torch.jit.unused
def get_transform(self, image):
h, w = image.shape[:2]
#print('5555555555 = ',h, w )
if self.is_range:
size = np.random.randint(self.short_edge_length[0], self.short_edge_length[1] + 1)
else:
size = np.random.choice(self.short_edge_length)
if size == 0:
return NoOpTransform()
#print('77777777777 = ',h, w, size, self.max_size )
newh, neww = ResizeShortestEdge.get_output_shape(h, w, size, self.max_size)
print('66666666 = ',newh, neww)
return ResizeTransform(h, w, newh, neww, self.interp)
# return ResizeTransform(h, w, h, w, self.interp)
@staticmethod
def get_output_shape(
oldh: int, oldw: int, short_edge_length: int, max_size: int
) -> Tuple[int, int]:
"""
Compute the output size given input size and target short edge length.
"""
h, w = oldh, oldw
size = short_edge_length * 1.0
scale = size / min(h, w)
if h < w:
newh, neww = size, scale * w
else:
newh, neww = scale * h, size
if max(newh, neww) > max_size:
scale = max_size * 1.0 / max(newh, neww)
newh = newh * scale
neww = neww * scale
neww = int(neww + 0.5)
newh = int(newh + 0.5)
return (newh, neww)