(image, target, size, max_size=None)
| 74 | |
| 75 | |
| 76 | def resize(image, target, size, max_size=None): |
| 77 | # size can be min_size (scalar) or (w, h) tuple |
| 78 | # import pdb;pdb.set_trace() |
| 79 | maxs = size |
| 80 | def get_size_with_aspect_ratio(image_size, size, max_size=None): |
| 81 | w, h = image_size |
| 82 | if max_size is not None: |
| 83 | min_original_size = float(min((w, h))) |
| 84 | max_original_size = float(max((w, h))) |
| 85 | if max_original_size / min_original_size * size > max_size: |
| 86 | size = int(round(max_size * min_original_size / max_original_size)) |
| 87 | |
| 88 | if (w <= h and w == size) or (h <= w and h == size): |
| 89 | w_mod = np.mod(w, 16) |
| 90 | h_mod = np.mod(h, 16) |
| 91 | h = h - h_mod |
| 92 | w = w - w_mod |
| 93 | return (h, w) |
| 94 | |
| 95 | if w < h: |
| 96 | ow = size |
| 97 | oh = int(size * h / w) |
| 98 | ow_mod = np.mod(ow, 16) |
| 99 | oh_mod = np.mod(oh, 16) |
| 100 | ow = ow - ow_mod |
| 101 | oh = oh - oh_mod |
| 102 | else: |
| 103 | oh = size |
| 104 | ow = int(size * w / h) |
| 105 | ow_mod = np.mod(ow, 16) |
| 106 | oh_mod = np.mod(oh, 16) |
| 107 | ow = ow - ow_mod |
| 108 | oh = oh - oh_mod |
| 109 | |
| 110 | return (oh, ow) |
| 111 | |
| 112 | def get_size(image_size, size, max_size=None): |
| 113 | if isinstance(size, (list, tuple)): |
| 114 | return size[::-1] |
| 115 | else: |
| 116 | return get_size_with_aspect_ratio(image_size, size, max_size) |
| 117 | |
| 118 | size = get_size(image.size, size, max_size) |
| 119 | # size = (size, size) |
| 120 | rescaled_image = F.resize(image, size) |
| 121 | |
| 122 | if target is None: |
| 123 | return rescaled_image, None |
| 124 | |
| 125 | ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(rescaled_image.size, image.size)) |
| 126 | ratio_width, ratio_height = ratios |
| 127 | |
| 128 | target = target.copy() |
| 129 | if "boxes" in target: |
| 130 | boxes = target["boxes"] |
| 131 | scaled_boxes = boxes * torch.as_tensor([ratio_width, ratio_height, ratio_width, ratio_height]) |
| 132 | target["boxes"] = scaled_boxes |
| 133 |
no test coverage detected