MCPcopy Create free account
hub / github.com/PeizeSun/TransTrack / resize

Function resize

datasets/transforms.py:132–188  ·  view source on GitHub ↗
(image, target, size, max_size=None)

Source from the content-addressed store, hash-verified

130
131
132def 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

Callers 1

__call__Method · 0.70

Calls 2

interpolateFunction · 0.90
get_sizeFunction · 0.85

Tested by

no test coverage detected