resize
Usage
Scales up or down an image. You can adjust the image width and height by specifying the target size or scaling factor. TorchVision supports multiple interpolation modes. This document optimizes the bilinear and bicubic modes to maintain the image quality during scaling.
Interface
1 | torchvision.transforms.resize(size, interpolation=InterpolationMode.BILINEAR, max_size=None, antialias=True) |
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
size |
Sequence or int number, indicating the size (width and height) of the output image. If set to an int number, it indicates the size of the smaller edge of the image. |
(0, INT_MAX] |
Input |
interpolation |
Interpolation mode. Multiple modes are supported, including InterpolationMode.NEAREST, InterpolationMode.NEAREST_EXACT, InterpolationMode.BILINEAR (default), and InterpolationMode.BICUBIC. This document optimizes the InterpolationMode.BILINEAR and InterpolationMode.BICUBIC modes. |
InterpolationMode.BILINEAR, InterpolationMode.BICUBIC |
Input |
max_size |
(Optional) Specifies the maximum value allowed for the longer edge of the image to be resized. When size is set to an int number, if the longer edge of the image is greater than max_size, the image is resized based on max_size. |
(0, INT_MAX] |
Input |
antialias |
(Optional) Operates on PIL images and has no effect on tensors. This parameter defaults to True. |
bool |
Input |
Return Values
- Success: KP_PT_STS_NO_ERR
- Failure: an error code
Error Codes
Error Code |
Description |
|---|---|
KP_PT_STS_NULL_PTR_ERR |
An error occurs in pointer memory allocation. |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import numpy as np import torch from torchvision import transforms # Define source data. src = np.array([[1, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [1, 0, 0, 0, 0]], dtype=np.float32) # Convert the NumPy array into a tensor and add channel information. src_tensor = torch.from_numpy(src).unsqueeze(0).unsqueeze(0) # Define the target size. size = (4, 4) # Create an image resizing transform. resize = transforms.Resize(size) # Perform image size conversion. resized_src_tensor = resize(src_tensor) # Display the matrix. print(resized_src_tensor) |
Output:
1 2 3 4 | tensor([[[[0.6250, 0.2500, 0.2500, 0.0625], [0.2500, 0.8264, 0.8264, 0.2500], [0.2500, 0.8264, 0.8264, 0.2500], [0.6250, 0.2500, 0.2500, 0.0625]]]]) |