pytorch中的插值算法函数模块--interpolate

pytorch中的插值算法函数模块--interpolate,第1张

官方函数说明
torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None)
  1. 根据给定的size或者scale_factor(放缩因子)下采样/上采样 输入
  2. mode指定使用的插值算法
  3. 支持输入为时序、空间或者三维立体等输入,输入应该是3-D、4-D、5-D
  4. 输入维度格式应该是: mini-batch x channels x [optional depth] x [optional height] x width.
  5. mode可以使用的算法包括:nearest, linear (3D-only), bilinear, bicubic (4D-only), trilinear (5D-only), area
参数说明
  • input (Tensor) – the input tensor

  • size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]) – output spatial size.
    选择输出的size大小与scale_factor参数功能类似只能选择其中一个使用

  • scale_factor (float or Tuple[float]) – multiplier for spatial size. If scale_factor is a tuple, its length has to match input.dim().
    放缩因子,比如 设置为2,输入维度为2X2,则输出为4X4

  • 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, preserving the values at the 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, making this operation independent of input size when scale_factor is kept the same. This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: False
    对齐角–在几何上,我们考虑输入和输出的像素是正方形而不是点。如果设置为True,输入和输出张量将按其角像素的中心点对齐,从而保留角像素处的值。如果设置为False,则输入和输出张量将通过其角像素的角点对齐,插值使用边界外值的边值填充,当比例因子保持不变时,此 *** 作与输入大小无关。这仅在mode为“线性”、“双线性”、“双三次”或“三线性”时有效。默认值:False

  • recompute_scale_factor (bool, optional) – recompute the scale_factor for use in the interpolation calculation. If recompute_scale_factor is True, then scale_factor must be passed in and scale_factor is used to compute the output size. The computed output size will be used to infer new scales for the interpolation. Note that when scale_factor is floating-point, it may differ from the recomputed scale_factor due to rounding and precision issues. If recomputed_scale_factor is False, then size or scale_factor will be used directly for interpolation.
    如果“重新计算比例因子”为True,则必须传入放缩因子,并使用放缩因子计算输出大小。计算出的输出大小将用于推断插值的新比例。请注意,当scale_factor为浮点时,由于舍入和精度问题,它可能与重新计算的scale_factor不同。如果重新计算的比例因子为假,则大小或比例因子将直接用于插值。

example

code 1


import torch 
from torch.nn import functional as F 
input = torch.arange(1,5,dtype=torch.float32).view(1,1,2,2)
print(input)
out1=F.interpolate(input ,scale_factor=3,mode="nearest")
print(out1)

输出:

code2:

out3 = F.interpolate(input,scale_factor=2,mode="bilinear",align_corners=False)
out4=F.interpolate(input,size=[4,4],mode="bilinear",align_corners=False)
print(out3,"\n",out3==out4)

out:
可以发现scale因子与size都是用来指定输出大小

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/740777.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存