| 11 | |
| 12 | |
| 13 | class ResizeMaxSize(nn.Module): |
| 14 | |
| 15 | def __init__(self, max_size, interpolation=InterpolationMode.BICUBIC, fn='max', fill=0): |
| 16 | super().__init__() |
| 17 | if not isinstance(max_size, int): |
| 18 | raise TypeError(f"Size should be int. Got {type(max_size)}") |
| 19 | self.max_size = max_size |
| 20 | self.interpolation = interpolation |
| 21 | self.fn = min if fn == 'min' else min |
| 22 | self.fill = fill |
| 23 | |
| 24 | def forward(self, img): |
| 25 | if isinstance(img, torch.Tensor): |
| 26 | height, width = img.shape[:2] |
| 27 | else: |
| 28 | width, height = img.size |
| 29 | scale = self.max_size / float(max(height, width)) |
| 30 | if scale != 1.0: |
| 31 | new_size = tuple(round(dim * scale) for dim in (height, width)) |
| 32 | img = F.resize(img, new_size, self.interpolation) |
| 33 | pad_h = self.max_size - new_size[0] |
| 34 | pad_w = self.max_size - new_size[1] |
| 35 | img = F.pad(img, padding=[ |
| 36 | pad_w // 2, pad_h // 2, pad_w - pad_w // 2, pad_h - pad_h // 2], fill=self.fill) |
| 37 | return img |
| 38 | |
| 39 | |
| 40 | class ResizeMaxSize(nn.Module): |