MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / resize

Function resize

monai/transforms/spatial/functional.py:311–382  ·  view source on GitHub ↗

Functional implementation of resize. This function operates eagerly or lazily according to ``lazy`` (default ``False``). Args: img: data to be changed, assuming `img` is channel-first. out_size: expected shape of spatial dimensions after resize operation. mo

(
    img, out_size, mode, align_corners, dtype, input_ndim, anti_aliasing, anti_aliasing_sigma, lazy, transform_info
)

Source from the content-addressed store, hash-verified

309
310
311def resize(
312 img, out_size, mode, align_corners, dtype, input_ndim, anti_aliasing, anti_aliasing_sigma, lazy, transform_info
313):
314 """
315 Functional implementation of resize.
316 This function operates eagerly or lazily according to
317 ``lazy`` (default ``False``).
318
319 Args:
320 img: data to be changed, assuming `img` is channel-first.
321 out_size: expected shape of spatial dimensions after resize operation.
322 mode: {``"nearest"``, ``"nearest-exact"``, ``"linear"``,
323 ``"bilinear"``, ``"bicubic"``, ``"trilinear"``, ``"area"``}
324 The interpolation mode.
325 See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html
326 align_corners: This only has an effect when mode is
327 'linear', 'bilinear', 'bicubic' or 'trilinear'.
328 dtype: data type for resampling computation. If None, use the data type of input data.
329 input_ndim: number of spatial dimensions.
330 anti_aliasing: whether to apply a Gaussian filter to smooth the image prior
331 to downsampling. It is crucial to filter when downsampling
332 the image to avoid aliasing artifacts. See also ``skimage.transform.resize``
333 anti_aliasing_sigma: {float, tuple of floats}, optional
334 Standard deviation for Gaussian filtering used when anti-aliasing.
335 lazy: a flag that indicates whether the operation should be performed lazily or not
336 transform_info: a dictionary with the relevant information pertaining to an applied transform.
337 """
338 img = convert_to_tensor(img, track_meta=get_track_meta())
339 orig_size = img.peek_pending_shape() if isinstance(img, MetaTensor) else img.shape[1:]
340 extra_info = {
341 "mode": mode,
342 "align_corners": align_corners if align_corners is not None else TraceKeys.NONE,
343 "dtype": str(dtype)[6:], # dtype as string; remove "torch": torch.float32 -> float32
344 "new_dim": len(orig_size) - input_ndim,
345 }
346 meta_info = TraceableTransform.track_transform_meta(
347 img,
348 sp_size=out_size,
349 affine=scale_affine(orig_size, out_size, align_corners=align_corners if align_corners is not None else False),
350 extra_info=extra_info,
351 orig_size=orig_size,
352 transform_info=transform_info,
353 lazy=lazy,
354 )
355 if lazy:
356 if anti_aliasing and lazy:
357 warnings.warn("anti-aliasing is not compatible with lazy evaluation.")
358 out = _maybe_new_metatensor(img)
359 return out.copy_meta_from(meta_info) if isinstance(out, MetaTensor) else meta_info
360 if tuple(convert_to_numpy(orig_size)) == out_size:
361 out = _maybe_new_metatensor(img, dtype=torch.float32)
362 return out.copy_meta_from(meta_info) if isinstance(out, MetaTensor) else out
363 out = _maybe_new_metatensor(img)
364 img_ = convert_to_tensor(out, dtype=dtype, track_meta=False) # convert to a regular tensor
365 if anti_aliasing and any(x < y for x, y in zip(out_size, img_.shape[1:])):
366 factors = torch.div(torch.Tensor(list(img_.shape[1:])), torch.Tensor(out_size))
367 if anti_aliasing_sigma is None:
368 # if sigma is not given, use the default sigma in skimage.transform.resize

Callers 10

__call__Method · 0.90
test_invalid_inputsMethod · 0.85
test_unchangeMethod · 0.85
test_correct_resultsMethod · 0.85
test_invalid_inputsMethod · 0.85
test_unchangeMethod · 0.85
test_correct_resultsMethod · 0.85
test_valueMethod · 0.85
test_valueMethod · 0.85

Calls 12

convert_to_tensorFunction · 0.90
get_track_metaFunction · 0.90
scale_affineFunction · 0.90
convert_to_numpyFunction · 0.90
ensure_tuple_repFunction · 0.90
GaussianSmoothClass · 0.90
resolves_modesFunction · 0.90
convert_to_dst_typeFunction · 0.90
_maybe_new_metatensorFunction · 0.85
peek_pending_shapeMethod · 0.80
track_transform_metaMethod · 0.80
copy_meta_fromMethod · 0.80

Tested by 9

test_invalid_inputsMethod · 0.68
test_unchangeMethod · 0.68
test_correct_resultsMethod · 0.68
test_invalid_inputsMethod · 0.68
test_unchangeMethod · 0.68
test_correct_resultsMethod · 0.68
test_valueMethod · 0.68
test_valueMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…