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.
| 424 | |
| 425 | |
| 426 | class ResizeShortestEdge(Augmentation): |
| 427 | """ |
| 428 | Resize the image while keeping the aspect ratio unchanged. |
| 429 | It attempts to scale the shorter edge to the given `short_edge_length`, |
| 430 | as long as the longer edge does not exceed `max_size`. |
| 431 | If `max_size` is reached, then downscale so that the longer edge does not exceed max_size. |
| 432 | """ |
| 433 | |
| 434 | @torch.jit.unused |
| 435 | def __init__( |
| 436 | self, short_edge_length, max_size=sys.maxsize, sample_style="range", interp=Image.BILINEAR |
| 437 | ): |
| 438 | """ |
| 439 | Args: |
| 440 | short_edge_length (list[int]): If ``sample_style=="range"``, |
| 441 | a [min, max] interval from which to sample the shortest edge length. |
| 442 | If ``sample_style=="choice"``, a list of shortest edge lengths to sample from. |
| 443 | max_size (int): maximum allowed longest edge length. |
| 444 | sample_style (str): either "range" or "choice". |
| 445 | """ |
| 446 | super().__init__() |
| 447 | assert sample_style in ["range", "choice"], sample_style |
| 448 | |
| 449 | self.is_range = sample_style == "range" |
| 450 | if isinstance(short_edge_length, int): |
| 451 | short_edge_length = (short_edge_length, short_edge_length) |
| 452 | if self.is_range: |
| 453 | assert len(short_edge_length) == 2, ( |
| 454 | "short_edge_length must be two values using 'range' sample style." |
| 455 | f" Got {short_edge_length}!" |
| 456 | ) |
| 457 | self._init(locals()) |
| 458 | |
| 459 | @torch.jit.unused |
| 460 | def get_transform(self, image): |
| 461 | h, w = image.shape[:2] |
| 462 | if self.is_range: |
| 463 | size = np.random.randint(self.short_edge_length[0], self.short_edge_length[1] + 1) |
| 464 | else: |
| 465 | size = np.random.choice(self.short_edge_length) |
| 466 | if size == 0: |
| 467 | return NoOpTransform() |
| 468 | |
| 469 | newh, neww = ResizeShortestEdge.get_output_shape(h, w, size, self.max_size) |
| 470 | return ResizeTransform(h, w, newh, neww, self.interp) |
| 471 | |
| 472 | @staticmethod |
| 473 | def get_output_shape( |
| 474 | oldh: int, oldw: int, short_edge_length: int, max_size: int |
| 475 | ) -> Tuple[int, int]: |
| 476 | """ |
| 477 | Compute the output size given input size and target short edge length. |
| 478 | """ |
| 479 | h, w = oldh, oldw |
| 480 | size = short_edge_length * 1.0 |
| 481 | scale = size / min(h, w) |
| 482 | if h < w: |
| 483 | newh, neww = size, scale * w |