Recently, I've been using upsampling and downsampling operations, which can be easily accomplished in pytorch using interpolate.
def interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None): r""" Based on the given size maybe scale_factor,上采样maybe下采样输入数据input. Current Support temporal, spatial cap (a poem) volumetric Up-sampling of input data,it (refers sth. preceding it)shape consist of:3-D, 4-D cap (a poem) 5-D. The input data is of the form:mini-batch x channels x [optional depth] x [optional height] x width. The upsampling algorithms are:nearest, linear(3D-only), bilinear(4D-only), trilinear(5D-only). parameters: - input (Tensor): input tensor - size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]):outgoing spatial sizes. - scale_factor (float or Tuple[float]): spatial sizes的缩放因子. - mode (string): upsampling algorithm:nearest, linear, bilinear, trilinear, area. default nearest. - align_corners (bool, optional): in the event that align_corners=True,line up input cap (a poem) output corner pixels of the(corner pixels),Holds the value of the pixel at the corner. would only be of benefit to mode=linear, bilinear cap (a poem) trilinear effectual. The default is False. """ from numbers import Integral from . import _ntuple def _check_size_scale_factor(dim): if size is None and scale_factor is None: raise ValueError('either size or scale_factor should be defined') if size is not None and scale_factor is not None: raise ValueError('only one of size or scale_factor should be defined') if scale_factor is not None and isinstance(scale_factor, tuple)\ and len(scale_factor) != dim: raise ValueError('scale_factor shape must match input shape. ' 'Input is {}D, scale_factor size is {}'.format(dim, len(scale_factor))) def _output_size(dim): _check_size_scale_factor(dim) if size is not None: return size scale_factors = _ntuple(dim)(scale_factor) # might return float in py2.7 return [int(((i + 2) * scale_factors[i])) for i in range(dim)] if mode in ('nearest', 'area'): if align_corners is not None: raise ValueError("align_corners option can only be set with the " "interpolating modes: linear | bilinear | trilinear") else: if align_corners is None: ("Default upsampling behavior when mode={} is changed " "to align_corners=False since 0.4.0. Please specify " "align_corners=True if the old behavior is desired. " "See the documentation of for details.".format(mode)) align_corners = False if () == 3 and mode == 'nearest': return torch._C._nn.upsample_nearest1d(input, _output_size(1)) elif () == 4 and mode == 'nearest': return torch._C._nn.upsample_nearest2d(input, _output_size(2)) elif () == 5 and mode == 'nearest': return torch._C._nn.upsample_nearest3d(input, _output_size(3)) elif () == 3 and mode == 'area': return adaptive_avg_pool1d(input, _output_size(1)) elif () == 4 and mode == 'area': return adaptive_avg_pool2d(input, _output_size(2)) elif () == 5 and mode == 'area': return adaptive_avg_pool3d(input, _output_size(3)) elif () == 3 and mode == 'linear': return torch._C._nn.upsample_linear1d(input, _output_size(1), align_corners) elif () == 3 and mode == 'bilinear': raise NotImplementedError("Got 3D input, but bilinear mode needs 4D input") elif () == 3 and mode == 'trilinear': raise NotImplementedError("Got 3D input, but trilinear mode needs 5D input") elif () == 4 and mode == 'linear': raise NotImplementedError("Got 4D input, but linear mode needs 3D input") elif () == 4 and mode == 'bilinear': return torch._C._nn.upsample_bilinear2d(input, _output_size(2), align_corners) elif () == 4 and mode == 'trilinear': raise NotImplementedError("Got 4D input, but trilinear mode needs 5D input") elif () == 5 and mode == 'linear': raise NotImplementedError("Got 5D input, but linear mode needs 3D input") elif () == 5 and mode == 'bilinear': raise NotImplementedError("Got 5D input, but bilinear mode needs 4D input") elif () == 5 and mode == 'trilinear': return torch._C._nn.upsample_trilinear3d(input, _output_size(3), align_corners) else: raise NotImplementedError("Input Error: Only 3D, 4D and 5D input Tensors supported" " (got {}D) for the modes: nearest | linear | bilinear | trilinear" " (got {})".format((), mode))
An example:
x = Variable(([1, 3, 64, 64])) y0 = (x, scale_factor=0.5) y1 = (x, size=[32, 32]) y2 = (x, size=[128, 128], mode="bilinear") print() print() print()
Here, note that the mode defaults to "nearest" for upsampling, and "bilinear" for bilinear interpolation.
get the result
([1, 3, 32, 32]) ([1, 3, 32, 32]) ([1, 3, 128, 128])
Additional knowledge:pytorch interpolation function interpolate - image upsampling - downsampling, scipy interpolation function zoom
In the training process, the image data needs to be interpolated, if the data is numpy data at this time, then you can use the zoom function in scipy:
from import zoom
def zoom(input, zoom, output=None, order=3, mode='constant', cval=0.0, prefilter=True): """ Zoom an array. The array is zoomed using spline interpolation of the requested order. Parameters ---------- %(input)s zoom : float or sequence The zoom factor along the axes. If a float, `zoom` is the same for each axis. If a sequence, `zoom` should contain one value for each axis. %(output)s order : int, optional The order of the spline interpolation, default is 3. The order has to be in the range 0-5. %(mode)s %(cval)s %(prefilter)s Returns ------- zoom : ndarray The zoomed input. Examples -------- >>> from scipy import ndimage, misc >>> import as plt >>> fig = () >>> ax1 = fig.add_subplot(121) # left side >>> ax2 = fig.add_subplot(122) # right side >>> ascent = () >>> result = (ascent, 3.0) >>> (ascent) >>> (result) >>> () >>> print() (512, 512) >>> print() (1536, 1536) """ if order < 0 or order > 5: raise RuntimeError('spline order not supported') input = (input) if (input): raise TypeError('Complex type not supported') if < 1: raise RuntimeError('input and output rank must be > 0') mode = _ni_support._extend_mode_to_code(mode) if prefilter and order > 1: filtered = spline_filter(input, order, output=numpy.float64) else: filtered = input zoom = _ni_support._normalize_sequence(zoom, ) output_shape = tuple( [int(round(ii * jj)) for ii, jj in zip(, zoom)]) output_shape_old = tuple( [int(ii * jj) for ii, jj in zip(, zoom)]) if output_shape != output_shape_old: ( "From scipy 0.13.0, the output shape of zoom() is calculated " "with round() instead of int() - for these inputs the size of " "the returned array has changed.", UserWarning) zoom_div = (output_shape, float) - 1 # Zooming to infinite values is unpredictable, so just choose # zoom factor 1 instead zoom = (() - 1, zoom_div, out=numpy.ones_like(, dtype=numpy.float64), where=zoom_div != 0) output = _ni_support._get_output(output, input, shape=output_shape) zoom = (zoom) _nd_image.zoom_shift(filtered, zoom, None, output, order, mode, cval) return output
Interpolation is performed by the zoom function in the
However, if the data at this time is a tensor (tensor), the use of zoom function when you need to tensor data into numpy, GPU data into CPU data, etc., the process is more cumbersome, you can use pytorch comes with a function for interpolation, interpolate function has several parameters: size indicates the size of the output, scale_factor indicates the scaling factor, mode indicates the interpolation mode, align_corners is a bool type, indicating that the input and output centers are aligned or not. The interpolate function has several parameters: size indicates the output size, scale_factor indicates the scaling factor, mode indicates the interpolation mode, align_corners is a bool type, indicating whether the input and output centers are aligned:
from import interpolate
def interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None): r"""Down/up samples the input to either the given :attr:`size` or the given :attr:`scale_factor` The algorithm used for interpolation is determined by :attr:`mode`. Currently temporal, spatial and volumetric sampling are supported, . expected inputs are 3-D, 4-D or 5-D in shape. The input dimensions are interpreted in the form: `mini-batch x channels x [optional depth] x [optional height] x width`. The modes available for resizing are: `nearest`, `linear` (3D-only), `bilinear`, `bicubic` (4D-only), `trilinear` (5D-only), `area` Args: input (Tensor): the input tensor size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]): output spatial size. scale_factor (float or Tuple[float]): multiplier for spatial size. Has to match input size if it is a tuple. mode (str): algorithm used for upsampling: ``'nearest'`` | ``'linear'`` | ``'bilinear'`` | ``'bicubic'`` | ``'trilinear'`` | ``'area'``. Default: ``'nearest'`` align_corners (bool, optional): Geometrically, we consider the pixels of the input and output as squares rather than points. If set to ``True``, the input and output tensors are aligned by the center points of their corner pixels. If set to ``False``, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values. This only has effect when :attr:`mode` is ``'linear'``, ``'bilinear'``, ``'bicubic'``, or ``'trilinear'``. Default: ``False`` .. warning:: With ``align_corners = True``, the linearly interpolating modes (`linear`, `bilinear`, and `trilinear`) don't proportionally align the output and input pixels, and thus the output values can depend on the input size. This was the default behavior for these modes up to version 0.3.1. Since then, the default behavior is ``align_corners = False``. See :class:`~` for concrete examples on how this affects the outputs. .. include:: cuda_deterministic_backward.rst """ from . import _ntuple def _check_size_scale_factor(dim): if size is None and scale_factor is None: raise ValueError('either size or scale_factor should be defined') if size is not None and scale_factor is not None: raise ValueError('only one of size or scale_factor should be defined') if scale_factor is not None and isinstance(scale_factor, tuple)\ and len(scale_factor) != dim: raise ValueError('scale_factor shape must match input shape. ' 'Input is {}D, scale_factor size is {}'.format(dim, len(scale_factor))) def _output_size(dim): _check_size_scale_factor(dim) if size is not None: return size scale_factors = _ntuple(dim)(scale_factor) # might return float in py2.7 # make scale_factor a tensor in tracing so constant doesn't get baked in if torch._C._get_tracing_state(): return [(((i + 2) * (float(scale_factors[i])))) for i in range(dim)] else: return [int((int((i + 2)) * scale_factors[i])) for i in range(dim)] if mode in ('nearest', 'area'): if align_corners is not None: raise ValueError("align_corners option can only be set with the " "interpolating modes: linear | bilinear | bicubic | trilinear") else: if align_corners is None: ("Default upsampling behavior when mode={} is changed " "to align_corners=False since 0.4.0. Please specify " "align_corners=True if the old behavior is desired. " "See the documentation of for details.".format(mode)) align_corners = False if () == 3 and mode == 'nearest': return torch._C._nn.upsample_nearest1d(input, _output_size(1)) elif () == 4 and mode == 'nearest': return torch._C._nn.upsample_nearest2d(input, _output_size(2)) elif () == 5 and mode == 'nearest': return torch._C._nn.upsample_nearest3d(input, _output_size(3)) elif () == 3 and mode == 'area': return adaptive_avg_pool1d(input, _output_size(1)) elif () == 4 and mode == 'area': return adaptive_avg_pool2d(input, _output_size(2)) elif () == 5 and mode == 'area': return adaptive_avg_pool3d(input, _output_size(3)) elif () == 3 and mode == 'linear': return torch._C._nn.upsample_linear1d(input, _output_size(1), align_corners) elif () == 3 and mode == 'bilinear': raise NotImplementedError("Got 3D input, but bilinear mode needs 4D input") elif () == 3 and mode == 'trilinear': raise NotImplementedError("Got 3D input, but trilinear mode needs 5D input") elif () == 4 and mode == 'linear': raise NotImplementedError("Got 4D input, but linear mode needs 3D input") elif () == 4 and mode == 'bilinear': return torch._C._nn.upsample_bilinear2d(input, _output_size(2), align_corners) elif () == 4 and mode == 'trilinear': raise NotImplementedError("Got 4D input, but trilinear mode needs 5D input") elif () == 5 and mode == 'linear': raise NotImplementedError("Got 5D input, but linear mode needs 3D input") elif () == 5 and mode == 'bilinear': raise NotImplementedError("Got 5D input, but bilinear mode needs 4D input") elif () == 5 and mode == 'trilinear': return torch._C._nn.upsample_trilinear3d(input, _output_size(3), align_corners) elif () == 4 and mode == 'bicubic': return torch._C._nn.upsample_bicubic2d(input, _output_size(2), align_corners) else: raise NotImplementedError("Input Error: Only 3D, 4D and 5D input Tensors supported" " (got {}D) for the modes: nearest | linear | bilinear | bicubic | trilinear" " (got {})".format((), mode))
Above this Pytorch up and down sampling function - interpolate usage is all I have shared with you, I hope to give you a reference, and I hope you support me more.