MCPcopy Create free account
hub / github.com/OpenGVLab/HumanBench / ResizeTransform

Class ResizeTransform

PATH/core/data/transforms/seg_aug_dev.py:326–391  ·  view source on GitHub ↗

Resize the image to a target size.

Source from the content-addressed store, hash-verified

324
325
326class ResizeTransform(Transform):
327 """
328 Resize the image to a target size.
329 """
330
331 def __init__(self, h, w, new_h, new_w, interp=None):
332 """
333 Args:
334 h, w (int): original image size
335 new_h, new_w (int): new image size
336 interp: PIL interpolation methods, defaults to bilinear.
337 """
338 # TODO decide on PIL vs opencv
339 super().__init__()
340 if interp is None:
341 interp = Image.BILINEAR
342 self._set_attributes(locals())
343
344 def apply_image(self, img, interp=None):
345 assert img.shape[:2] == (self.h, self.w)
346 assert len(img.shape) <= 4
347 interp_method = interp if interp is not None else self.interp
348
349 if img.dtype == np.uint8:
350 if len(img.shape) > 2 and img.shape[2] == 1:
351 pil_image = Image.fromarray(img[:, :, 0], mode="L")
352 else:
353 pil_image = Image.fromarray(img)
354 pil_image = pil_image.resize((self.new_w, self.new_h), interp_method)
355 ret = np.asarray(pil_image)
356 if len(img.shape) > 2 and img.shape[2] == 1:
357 ret = np.expand_dims(ret, -1)
358 else:
359 # PIL only supports uint8
360 if any(x < 0 for x in img.strides):
361 img = np.ascontiguousarray(img)
362 img = torch.from_numpy(img)
363 shape = list(img.shape)
364 shape_4d = shape[:2] + [1] * (4 - len(shape)) + shape[2:]
365 img = img.view(shape_4d).permute(2, 3, 0, 1) # hw(c) -> nchw
366 _PIL_RESIZE_TO_INTERPOLATE_MODE = {
367 Image.NEAREST: "nearest",
368 Image.BILINEAR: "bilinear",
369 Image.BICUBIC: "bicubic",
370 }
371 mode = _PIL_RESIZE_TO_INTERPOLATE_MODE[interp_method]
372 align_corners = None if mode == "nearest" else False
373 img = F.interpolate(
374 img, (self.new_h, self.new_w), mode=mode, align_corners=align_corners
375 )
376 shape[:2] = (self.new_h, self.new_w)
377 ret = img.permute(2, 3, 0, 1).view(shape).numpy() # nchw -> hw(c)
378
379 return ret
380
381 def apply_coords(self, coords):
382 coords[:, 0] = coords[:, 0] * (self.new_w * 1.0 / self.w)
383 coords[:, 1] = coords[:, 1] * (self.new_h * 1.0 / self.h)

Callers 3

tta_mapperMethod · 0.90
inverseMethod · 0.70
get_transformMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected