Pytorch上下采样函数--interpolate用法

最近用到了上采样下采样操作,pytorch中使用interpolate可以很轻松的完成

def interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None):
  r"""
  根据给定 size 或 scale_factor,上采样或下采样输入数据input.

  当前支持 temporal, spatial 和 volumetric 输入数据的上采样,其shape 分别为:3-D, 4-D 和 5-D.
  输入数据的形式为:mini-batch x channels x [optional depth] x [optional height] x width.

  上采样算法有:nearest, linear(3D-only), bilinear(4D-only), trilinear(5D-only).

  参数:
  - input (Tensor): input tensor
  - size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]):输出的 spatial 尺寸.
  - scale_factor (float or Tuple[float]): spatial 尺寸的缩放因子.
  - mode (string): 上采样算法:nearest, linear, bilinear, trilinear, area. 默认为 nearest.
  - align_corners (bool, optional): 如果 align_corners=True,则对齐 input 和 output 的角点像素(corner pixels),保持在角点像素的值. 只会对 mode=linear, bilinear 和 trilinear 有作用. 默认是 False.
  """
  from numbers import Integral
  from .modules.utils 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)
    # math.floor might return float in py2.7
    return [int(math.floor(input.size(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:
      warnings.warn("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 nn.Upsample for details.".format(mode))
      align_corners = False

  if input.dim() == 3 and mode == 'nearest':
    return torch._C._nn.upsample_nearest1d(input, _output_size(1))
  elif input.dim() == 4 and mode == 'nearest':
    return torch._C._nn.upsample_nearest2d(input, _output_size(2))
  elif input.dim() == 5 and mode == 'nearest':
    return torch._C._nn.upsample_nearest3d(input, _output_size(3))
  elif input.dim() == 3 and mode == 'area':
    return adaptive_avg_pool1d(input, _output_size(1))
  elif input.dim() == 4 and mode == 'area':
    return adaptive_avg_pool2d(input, _output_size(2))
  elif input.dim() == 5 and mode == 'area':
    return adaptive_avg_pool3d(input, _output_size(3))
  elif input.dim() == 3 and mode == 'linear':
    return torch._C._nn.upsample_linear1d(input, _output_size(1), align_corners)
  elif input.dim() == 3 and mode == 'bilinear':
    raise NotImplementedError("Got 3D input, but bilinear mode needs 4D input")
  elif input.dim() == 3 and mode == 'trilinear':
    raise NotImplementedError("Got 3D input, but trilinear mode needs 5D input")
  elif input.dim() == 4 and mode == 'linear':
    raise NotImplementedError("Got 4D input, but linear mode needs 3D input")
  elif input.dim() == 4 and mode == 'bilinear':
    return torch._C._nn.upsample_bilinear2d(input, _output_size(2), align_corners)
  elif input.dim() == 4 and mode == 'trilinear':
    raise NotImplementedError("Got 4D input, but trilinear mode needs 5D input")
  elif input.dim() == 5 and mode == 'linear':
    raise NotImplementedError("Got 5D input, but linear mode needs 3D input")
  elif input.dim() == 5 and mode == 'bilinear':
    raise NotImplementedError("Got 5D input, but bilinear mode needs 4D input")
  elif input.dim() == 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(input.dim(), mode))

举个例子:

x = Variable(torch.randn([1, 3, 64, 64]))
y0 = F.interpolate(x, scale_factor=0.5)
y1 = F.interpolate(x, size=[32, 32])

y2 = F.interpolate(x, size=[128, 128], mode="bilinear")

print(y0.shape)
print(y1.shape)
print(y2.shape)

这里注意上采样的时候mode默认是“nearest”,这里指定双线性插值“bilinear”

得到结果

torch.Size([1, 3, 32, 32])
torch.Size([1, 3, 32, 32])
torch.Size([1, 3, 128, 128])

补充知识:pytorch插值函数interpolate——图像上采样-下采样,scipy插值函数zoom

在训练过程中,需要对图像数据进行插值,如果此时数据是numpy数据,那么可以使用scipy中的zoom函数:

from scipy.ndimage.interpolation 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 matplotlib.pyplot as plt
  >>> fig = plt.figure()
  >>> ax1 = fig.add_subplot(121) # left side
  >>> ax2 = fig.add_subplot(122) # right side
  >>> ascent = misc.ascent()
  >>> result = ndimage.zoom(ascent, 3.0)
  >>> ax1.imshow(ascent)
  >>> ax2.imshow(result)
  >>> plt.show()
  >>> print(ascent.shape)
  (512, 512)
  >>> print(result.shape)
  (1536, 1536)
  """
  if order < 0 or order > 5:
    raise RuntimeError('spline order not supported')
  input = numpy.asarray(input)
  if numpy.iscomplexobj(input):
    raise TypeError('Complex type not supported')
  if input.ndim < 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, input.ndim)
  output_shape = tuple(
      [int(round(ii * jj)) for ii, jj in zip(input.shape, zoom)])

  output_shape_old = tuple(
      [int(ii * jj) for ii, jj in zip(input.shape, zoom)])
  if output_shape != output_shape_old:
    warnings.warn(
        "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 = numpy.array(output_shape, float) - 1
  # Zooming to infinite values is unpredictable, so just choose
  # zoom factor 1 instead
  zoom = numpy.divide(numpy.array(input.shape) - 1, zoom_div,
            out=numpy.ones_like(input.shape, dtype=numpy.float64),
            where=zoom_div != 0)

  output = _ni_support._get_output(output, input,
                          shape=output_shape)
  zoom = numpy.ascontiguousarray(zoom)
  _nd_image.zoom_shift(filtered, zoom, None, output, order, mode, cval)
  return output

中的zoom函数进行插值,

但是,如果此时的数据是tensor(张量)的时候,使用zoom函数的时候需要将tensor数据转为numpy,将GPU数据转换为CPU数据等,过程比较繁琐,可以使用pytorch自带的函数进行插值操作,interpolate函数有几个参数:size表示输出大小,scale_factor表示缩放倍数,mode表示插值方式,align_corners是bool类型,表示输入和输出中心是否对齐:

from torch.nn.functional 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, i.e.
  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:`~torch.nn.Upsample` for concrete examples on how this
    affects the outputs.
  .. include:: cuda_deterministic_backward.rst
  """
  from .modules.utils 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)
    # math.floor 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 [(torch.floor(input.size(i + 2) * torch.tensor(float(scale_factors[i])))) for i in range(dim)]
    else:
      return [int(math.floor(int(input.size(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:
      warnings.warn("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 nn.Upsample for details.".format(mode))
      align_corners = False

  if input.dim() == 3 and mode == 'nearest':
    return torch._C._nn.upsample_nearest1d(input, _output_size(1))
  elif input.dim() == 4 and mode == 'nearest':
    return torch._C._nn.upsample_nearest2d(input, _output_size(2))
  elif input.dim() == 5 and mode == 'nearest':
    return torch._C._nn.upsample_nearest3d(input, _output_size(3))
  elif input.dim() == 3 and mode == 'area':
    return adaptive_avg_pool1d(input, _output_size(1))
  elif input.dim() == 4 and mode == 'area':
    return adaptive_avg_pool2d(input, _output_size(2))
  elif input.dim() == 5 and mode == 'area':
    return adaptive_avg_pool3d(input, _output_size(3))
  elif input.dim() == 3 and mode == 'linear':
    return torch._C._nn.upsample_linear1d(input, _output_size(1), align_corners)
  elif input.dim() == 3 and mode == 'bilinear':
    raise NotImplementedError("Got 3D input, but bilinear mode needs 4D input")
  elif input.dim() == 3 and mode == 'trilinear':
    raise NotImplementedError("Got 3D input, but trilinear mode needs 5D input")
  elif input.dim() == 4 and mode == 'linear':
    raise NotImplementedError("Got 4D input, but linear mode needs 3D input")
  elif input.dim() == 4 and mode == 'bilinear':
    return torch._C._nn.upsample_bilinear2d(input, _output_size(2), align_corners)
  elif input.dim() == 4 and mode == 'trilinear':
    raise NotImplementedError("Got 4D input, but trilinear mode needs 5D input")
  elif input.dim() == 5 and mode == 'linear':
    raise NotImplementedError("Got 5D input, but linear mode needs 3D input")
  elif input.dim() == 5 and mode == 'bilinear':
    raise NotImplementedError("Got 5D input, but bilinear mode needs 4D input")
  elif input.dim() == 5 and mode == 'trilinear':
    return torch._C._nn.upsample_trilinear3d(input, _output_size(3), align_corners)
  elif input.dim() == 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(input.dim(), mode))
 

以上这篇Pytorch上下采样函数--interpolate用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • pytorch进行上采样的种类实例

    1.其中再语义分割比较常用的上采样: 其实现方法为: def upconv2x2(in_channels, out_channels, mode='transpose'): if mode == 'transpose': # 这个上采用需要设置其输入通道,输出通道.其中kernel_size.stride # 大小要跟对应下采样设置的值一样大小.这样才可恢复到相同的wh.这里时反卷积操作. return nn.ConvTranspose2d( in_channels, out_channels,

  • pytorch sampler对数据进行采样的实现

    PyTorch中还单独提供了一个sampler模块,用来对数据进行采样.常用的有随机采样器:RandomSampler,当dataloader的shuffle参数为True时,系统会自动调用这个采样器,实现打乱数据.默认的是采用SequentialSampler,它会按顺序一个一个进行采样.这里介绍另外一个很有用的采样方法: WeightedRandomSampler,它会根据每个样本的权重选取数据,在样本比例不均衡的问题中,可用它来进行重采样. 构建WeightedRandomSampler时

  • 在Pytorch中使用样本权重(sample_weight)的正确方法

    step: 1.将标签转换为one-hot形式. 2.将每一个one-hot标签中的1改为预设样本权重的值 即可在Pytorch中使用样本权重. eg: 对于单个样本:loss = - Q * log(P),如下: P = [0.1,0.2,0.4,0.3] Q = [0,0,1,0] loss = -Q * np.log(P) 增加样本权重则为loss = - Q * log(P) *sample_weight P = [0.1,0.2,0.4,0.3] Q = [0,0,sample_wei

  • Pytorch上下采样函数--interpolate用法

    最近用到了上采样下采样操作,pytorch中使用interpolate可以很轻松的完成 def interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None): r""" 根据给定 size 或 scale_factor,上采样或下采样输入数据input. 当前支持 temporal, spatial 和 volumetric 输入数据的上采样,其shape 分别为:3-

  • Pytorch上下采样函数之F.interpolate数组采样操作详解

    目录 什么是上采样 F.interpolate——数组采样操作 输入: 注意: 补充: 代码案例 一般用法 size与scale_factor的区别:输入序列时 size与scale_factor的区别:输入整数时 align_corners=True与False的区别 扩展: 总结 什么是上采样 上采样,在深度学习框架中,可以简单的理解为任何可以让你的图像变成更高分辨率的技术. 最简单的方式是重采样和插值:将输入图片input image进行rescale到一个想要的尺寸,而且计算每个点的像素

  • PyTorch中topk函数的用法详解

    听名字就知道这个函数是用来求tensor中某个dim的前k大或者前k小的值以及对应的index. 用法 torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor) input:一个tensor数据 k:指明是得到前k个数据以及其index dim: 指定在哪个维度上排序, 默认是最后一个维度 largest:如果为True,按照大到小排序: 如果为False,按照小到大排序

  • pytorch hook 钩子函数的用法

    钩子编程(hooking),也称作“挂钩”,是计算机程序设计术语,指通过拦截软件模块间的函数调用.消息传递.事件传递来修改或扩展操作系统.应用程序或其他软件组件的行为的各种技术.处理被拦截的函数调用.事件.消息的代码,被称为钩子(hook). Hook 是 PyTorch 中一个十分有用的特性.利用它,我们可以不必改变网络输入输出的结构,方便地获取.改变网络中间层变量的值和梯度.这个功能被广泛用于可视化神经网络中间层的 feature.gradient,从而诊断神经网络中可能出现的问题,分析网络

  • pytorch 中pad函数toch.nn.functional.pad()的用法

    padding操作是给图像外围加像素点. 为了实际说明操作过程,这里我们使用一张实际的图片来做一下处理. 这张图片是大小是(256,256),使用pad来给它加上一个黑色的边框.具体代码如下: import torch.nn,functional as F import torch from PIL import Image im=Image.open("heibai.jpg",'r') X=torch.Tensor(np.asarray(im)) print("shape:

  • Pytorch mask_select 函数的用法详解

    非常简单的函数,但是官网的介绍令人(令我)迷惑,所以稍加解释. mask_select会将满足mask(掩码.遮罩等等,随便翻译)的指示,将满足条件的点选出来. 根据掩码张量mask中的二元值,取输入张量中的指定项( mask为一个 ByteTensor),将取值返回到一个新的1D张量, 张量 mask须跟input张量有相同数量的元素数目,但形状或维度不需要相同 x = torch.randn(3, 4) x 1.2045 2.4084 0.4001 1.1372 0.5596 1.5677

  • pytorch中Parameter函数用法示例

    目录 用法介绍 代码介绍 用法介绍 pytorch中的Parameter函数可以对某个张量进行参数化.它可以将不可训练的张量转化为可训练的参数类型,同时将转化后的张量绑定到模型可训练参数的列表中,当更新模型的参数时一并将其更新. torch.nn.parameter.Parameter data (Tensor):表示需要参数化的张量 requires_grad (bool, optional):表示是否该张量是否需要梯度,默认值为True 代码介绍  pytorch中的Parameter函数具

  • pytorch中的 .view()函数的用法介绍

    目录 一.普通用法(手动调整size) 二.特殊用法:参数-1(自动调整size) 一.普通用法 (手动调整size) view()相当于reshape.resize,重新调整Tensor的形状. import torch a1 = torch.arange(0,16) print(a1) # tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15]) a2 = a1.view(8, 2) a3 = a1.vi

  • pytorch中permute()函数用法补充说明(矩阵维度变化过程)

    目录 一.前言 二.举例解释 1.permute(0,1,2) 2.permute(0,1,2) ⇒ permute(0,2,1) 3.permute(0,2,1) ⇒ permute(1,0,2) 4.permute(1,0,2) ⇒ permute(0,2,1) 三.写在最后 一.前言 之前写了篇torch中permute()函数用法文章,在详细的说一下permute函数里维度变化的详细过程 非常感谢@m0_46225327对本文案例更加细节补充 注意: 本文是这篇torch中permute

  • pytorch中permute()函数用法实例详解

    目录 前言 三维情况 变化一:不改变任何参数 变化二:1与2交换 变化三:0与1交换 变化四:0与2交换 变化五:0与1交换,1与2交换 变化六:0与1交换,0与2交换 总结 前言 本文只讨论二维三维中的permute用法 最近的Attention学习中的一个permute函数让我不理解 这个光说太抽象 我就结合代码与图片解释一下 首先创建一个三维数组小实例 import torch x = torch.linspace(1, 30, steps=30).view(3,2,5) # 设置一个三维

随机推荐