(image, target, size, max_size=None)
| 130 | |
| 131 | |
| 132 | def resize(image, target, size, max_size=None): |
| 133 | # size can be min_size (scalar) or (w, h) tuple |
| 134 | |
| 135 | def get_size_with_aspect_ratio(image_size, size, max_size=None): |
| 136 | w, h = image_size |
| 137 | if max_size is not None: |
| 138 | min_original_size = float(min((w, h))) |
| 139 | max_original_size = float(max((w, h))) |
| 140 | if max_original_size / min_original_size * size > max_size: |
| 141 | size = int(round(max_size * min_original_size / max_original_size)) |
| 142 | |
| 143 | if (w <= h and w == size) or (h <= w and h == size): |
| 144 | return (h, w) |
| 145 | |
| 146 | if w < h: |
| 147 | ow = size |
| 148 | oh = int(size * h / w) |
| 149 | else: |
| 150 | oh = size |
| 151 | ow = int(size * w / h) |
| 152 | |
| 153 | return (oh, ow) |
| 154 | |
| 155 | def get_size(image_size, size, max_size=None): |
| 156 | if isinstance(size, (list, tuple)): |
| 157 | return size[::-1] |
| 158 | else: |
| 159 | return get_size_with_aspect_ratio(image_size, size, max_size) |
| 160 | |
| 161 | size = get_size(image.size, size, max_size) |
| 162 | rescaled_image = F.resize(image, size) |
| 163 | |
| 164 | if target is None: |
| 165 | return rescaled_image, None |
| 166 | |
| 167 | ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(rescaled_image.size, image.size)) |
| 168 | ratio_width, ratio_height = ratios |
| 169 | |
| 170 | target = target.copy() |
| 171 | if "boxes" in target: |
| 172 | boxes = target["boxes"] |
| 173 | scaled_boxes = boxes * torch.as_tensor([ratio_width, ratio_height, ratio_width, ratio_height]) |
| 174 | target["boxes"] = scaled_boxes |
| 175 | |
| 176 | if "area" in target: |
| 177 | area = target["area"] |
| 178 | scaled_area = area * (ratio_width * ratio_height) |
| 179 | target["area"] = scaled_area |
| 180 | |
| 181 | h, w = size |
| 182 | target["size"] = torch.tensor([h, w]) |
| 183 | |
| 184 | if "masks" in target: |
| 185 | target['masks'] = interpolate( |
| 186 | target['masks'][:, None].float(), size, mode="nearest")[:, 0] > 0.5 |
| 187 | |
| 188 | return rescaled_image, target |
| 189 |
no test coverage detected