interpolate

torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None)

根据给定的size或scale_factor参数来对输入进行下/上采样

使用的插值算法取决于参数mode的设置

支持目前的temporal(1D, 如向量数据), spatial(2D, 如jpg、png等图像数据)和volumetric(3D, 如点云数据)类型的采样数据作为输入,输入数据的格式为minibatch x channels x [optional depth] x [optional height] x width,具体为:

  • 对于一个temporal输入,期待着3D张量的输入,即minibatch x channels x width
  • 对于一个空间spatial输入,期待着4D张量的输入,即minibatch x channels x height x width
  • 对于体积volumetric输入,则期待着5D张量的输入,即minibatch x channels x depth x height x width

可用于重置大小的mode有:最近邻、线性(3D-only),、双线性, 双三次(bicubic,4D-only)和三线性(trilinear,5D-only)插值算法和area算法

参数:

  • input (Tensor) – 输入张量

  • size (int or Tuple[int] or Tuple[intint] or Tuple[intintint]) –输出大小.

  • scale_factor (float or Tuple[float]) – 指定输出为输入的多少倍数。如果输入为tuple,其也要制定为tuple类型

  • mode (str) – 可使用的上采样算法,有'nearest''linear''bilinear''bicubic' , 'trilinear'和'area'. 默认使用'nearest'

  • align_corners (booloptional) –几何上,我们认为输入和输出的像素是正方形,而不是点。如果设置为True,则输入和输出张量由其角像素的中心点对齐,从而保留角像素处的值。如果设置为False,则输入和输出张量由它们的角像素的角点对齐,插值使用边界外值的边值填充;当scale_factor保持不变时,使该操作独立于输入大小。仅当使用的算法为'linear''bilinear', 'bilinear'or 'trilinear'时可以使用。默认设置为False

注意:

使用mode='bicubic'时,可能会导致overshoot问题,即它可以为图像生成负值或大于255的值。如果你想在显示图像时减少overshoot问题,可以显式地调用result.clamp(min=0,max=255)。

When using the CUDA backend, this operation may induce nondeterministic behaviour in be backward that is not easily switched off. Please see the notes on Reproducibility for background.

警告:

当align_corners = True时,线性插值模式(线性、双线性、双三线性和三线性)不按比例对齐输出和输入像素,因此输出值可以依赖于输入的大小。这是0.3.1版本之前这些模式的默认行为。从那时起,默认行为是align_corners = False,如下图:

上面的图是source pixel为4*4上采样为target pixel为8*8的两种情况,这就是对齐和不对齐的差别,会对齐左上角元素,即设置为align_corners = True时输入的左上角元素是一定等于输出的左上角元素。但是有时align_corners = False时左上角元素也会相等,官网上给的例子就不太能说明两者的不同(也没有试出不同的例子,大家理解这个概念就行了)

举例:

import torch
from torch import nn
import torch.nn.functional as F
input = torch.arange(, , dtype=torch.float32).view(, , , )
input

返回:

tensor([[[[., .],
[., .]]]])
x = F.interpolate(input, scale_factor=, mode='nearest')
x

返回:

tensor([[[[., ., ., .],
[., ., ., .],
[., ., ., .],
[., ., ., .]]]])
x = F.interpolate(input, scale_factor=, mode='bilinear', align_corners=True)
x

返回:

tensor([[[[1.0000, 1.3333, 1.6667, 2.0000],
[1.6667, 2.0000, 2.3333, 2.6667],
[2.3333, 2.6667, 3.0000, 3.3333],
[3.0000, 3.3333, 3.6667, 4.0000]]]])

也提供了一些Upsample的方法:

upsample

torch.nn.functional.upsample(input, size=None, scale_factor=None, mode='nearest', align_corners=None)
torch.nn.functional.upsample_nearest(input, size=None, scale_factor=None)
torch.nn.functional.upsample_bilinear(input, size=None, scale_factor=None)

因为这些现在都建议使用上面的interpolate方法实现,所以就不解释了

更加复杂的例子可见:pytorch 不使用转置卷积来实现上采样

pytorch torch.nn.functional实现插值和上采样的更多相关文章

  1. PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx

    PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx 在写 PyTorch 代码时,我们会发现一些功能重复的操作,比如卷积.激活.池化等操作.这些操作分别可 ...

  2. [pytorch笔记] torch.nn vs torch.nn.functional; model.eval() vs torch.no_grad(); nn.Sequential() vs nn.moduleList

    1. torch.nn与torch.nn.functional之间的区别和联系 https://blog.csdn.net/GZHermit/article/details/78730856 nn和n ...

  3. Pytorch本人疑问(1) torch.nn和torch.nn.functional之间的区别

    在写代码时发现我们在定义Model时,有两种定义方法: torch.nn.Conv2d()和torch.nn.functional.conv2d() 那么这两种方法到底有什么区别呢,我们通过下述代码看 ...

  4. torch.nn.functional中softmax的作用及其参数说明

    参考:https://pytorch-cn.readthedocs.io/zh/latest/package_references/functional/#_1 class torch.nn.Soft ...

  5. 从 relu 的多种实现来看 torch.nn 与 torch.nn.functional 的区别与联系

    从 relu 的多种实现来看 torch.nn 与 torch.nn.functional 的区别与联系 relu多种实现之间的关系 relu 函数在 pytorch 中总共有 3 次出现: torc ...

  6. pytorch torch.nn 实现上采样——nn.Upsample

    Vision layers 1)Upsample CLASS torch.nn.Upsample(size=None, scale_factor=None, mode='nearest', align ...

  7. Pytorch——torch.nn.Sequential()详解

    参考:官方文档    源码 官方文档 nn.Sequential A sequential container. Modules will be added to it in the order th ...

  8. PyTorch官方中文文档:torch.nn

    torch.nn Parameters class torch.nn.Parameter() 艾伯特(http://www.aibbt.com/)国内第一家人工智能门户,微信公众号:aibbtcom ...

  9. 『PyTorch』第十二弹_nn.Module和nn.functional

    大部分nn中的层class都有nn.function对应,其区别是: nn.Module实现的layer是由class Layer(nn.Module)定义的特殊类,会自动提取可学习参数nn.Para ...

随机推荐

  1. python __main__ 里的变量是全局变量 因此在函数里面可以访问到

    __main__ and scoping in python from:https://stackoverflow.com/questions/4775579/main-and-scoping-in- ...

  2. 十五.ProtoBuf3的基础总结

    转自: https://blog.csdn.net/u011518120/article/details/54604615 定义一个消息类型 指定字段类型 分配标识号 指定字段规则 添加更多消息类型 ...

  3. PPT扁平化设计总结

    注:以下内容基本都来自知乎,由于已经不记得网址了,所以未能附上所有相关链接,抱歉. PPT扁平化设计原则一.亲密:意思相近的内容放在一起二.对齐:页面上的某两个元素之间总是围绕一条直线对齐三.对比:有 ...

  4. simple模式下rabbitmq的代码

    simple模式代码 package RabbitMQ import ( "fmt" "github.com/streadway/amqp" "log ...

  5. 用LinkedList和ArrayList实现自定义栈的异同

    //ArrayList已连续的空间进行存储数据  //LinkedList已链表的结构存储数据    //栈  MyStark ms=new MyStark();//new 一个实现栈的类  //压栈 ...

  6. 性能测试解读:Kyligence vs Spark SQL

    全球各种大数据技术涌现的今天,为了充分利用大量数据获得竞争优势,企业需要高性能的数据分析平台,可靠并及时地提供对海量数据的分析见解.对于数据驱动型企业,在海量数据上交互式分析的能力是非常重要的能力之一 ...

  7. 解决VS2010自带的C/C++编译器CL找不到mspdb100.dll的问题

    https://www.cnblogs.com/dudu/archive/2011/05/21/2053104.html 更好解决方法是在命令行中运行vsvars32.bat: "C:\Pr ...

  8. RookeyFrame 字典 新增和绑定

    原文:https://www.cnblogs.com/rookey/p/10856657.html 注意: 数据字典 -> 新增 把“是否生效”勾上 是否生效都要勾上哦 !!! 应该自动勾上才对 ...

  9. Python中多层List展平为一层

    小书匠python 使用Python脚本的过程中,偶尔需要使用list多层转一层,又总是忘记怎么写搜索关键词,所以总是找了很久,现在把各种方法记录下来,方便自己也方便大家. 方法很多,现在就简单写8种 ...

  10. 公司和家里代码文件同步方案: (git和dropbox实现)

    公司和家里代码文件同步方案: (git和dropbox实现) 参与公司福利购入了有补贴的macbook pro后, 就不用上下班背着电脑了. 但是也出现了另外一问题: 家里和公司代码同步的问题 公司有 ...