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


Posted in Python onJuly 07, 2020

最近用到了上采样下采样操作,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用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
跟老齐学Python之传说中的函数编写条规
Oct 11 Python
Python实现HTTP协议下的文件下载方法总结
Apr 20 Python
为Python的Tornado框架配置使用Jinja2模板引擎的方法
Jun 30 Python
Python学生成绩管理系统简洁版
Apr 05 Python
pandas获取groupby分组里最大值所在的行方法
Apr 20 Python
使用Python来开发微信功能
Jun 13 Python
Python面向对象之类和对象属性的增删改查操作示例
Dec 14 Python
Python3爬楼梯算法示例
Mar 04 Python
Python基于进程池实现多进程过程解析
Apr 30 Python
python中如何设置代码自动提示
Jul 15 Python
python合并多个excel文件的示例
Sep 23 Python
python模拟点击玩游戏的实例讲解
Nov 26 Python
pytorch随机采样操作SubsetRandomSampler()
Jul 07 #Python
pytorch加载自己的图像数据集实例
Jul 07 #Python
keras实现VGG16 CIFAR10数据集方式
Jul 07 #Python
使用darknet框架的imagenet数据分类预训练操作
Jul 07 #Python
Python调用C语言程序方法解析
Jul 07 #Python
keras实现VGG16方式(预测一张图片)
Jul 07 #Python
通过实例解析Python RPC实现原理及方法
Jul 07 #Python
You might like
PHP 如何向 MySQL 发送数据
2006/10/09 PHP
require(),include(),require_once()和include_once()区别
2008/03/27 PHP
PHP 引用是个坏习惯
2010/03/12 PHP
file_get_contents(&quot;php://input&quot;, &quot;r&quot;)实例介绍
2013/07/01 PHP
php命令行用法入门实例教程
2014/10/27 PHP
静态图片的十一种滤镜效果--不支持Ie7及非IE浏览器。
2007/03/06 Javascript
用Javscript实现表单复选框的全选功能
2007/05/25 Javascript
jquery实现图片等比例缩放以及max-width在ie中不兼容解决
2013/03/21 Javascript
javascript贪吃蛇完整版(源码)
2013/12/09 Javascript
jquery制作 随机弹跳的小球特效
2015/02/01 Javascript
jQuery获取标签文本内容和html内容的方法
2015/03/27 Javascript
纯js实现手风琴效果代码
2020/04/17 Javascript
jquery对象和DOM对象的相互转换详解
2016/10/18 Javascript
jQuery控制元素隐藏和显示
2017/03/03 Javascript
js学习总结_基于数据类型检测的四种方式(必看)
2017/07/04 Javascript
聊聊Vue.js的template编译的问题
2017/10/09 Javascript
解析vue中的$mount
2017/12/21 Javascript
[01:10:02]IG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
[39:08]完美世界DOTA2联赛PWL S3 LBZS vs CPG 第一场 12.12
2020/12/16 DOTA
wxPython框架类和面板类的使用实例
2014/09/28 Python
Python读写及备份oracle数据库操作示例
2018/05/17 Python
解决python测试opencv时imread导致的错误问题
2019/01/26 Python
浅谈python常用程序算法
2019/03/22 Python
Python 实现敏感目录扫描的示例代码
2020/05/21 Python
销售人员个人求职信
2013/09/26 职场文书
运动会入场解说词
2014/02/07 职场文书
2014年三八妇女节活动方案
2014/02/28 职场文书
合作协议书怎么写
2014/04/18 职场文书
2014年大学教师工作总结
2014/12/02 职场文书
基层组织建设年活动总结
2015/05/09 职场文书
小学教研工作总结2015
2015/05/13 职场文书
辞职申请书范本
2019/05/20 职场文书
CSS实现多个元素在盒子内两端对齐效果
2021/03/30 HTML / CSS
教你怎么用Python实现多路径迷宫
2021/04/29 Python
浅析MySQL如何实现事务隔离
2021/06/26 MySQL
在项目中使用redis做缓存的一些思路
2021/09/14 Redis