Pytorch之interpolate() 采样函数

nn.functional.interpolate实现插值和上采样

  • input (Tensor) – 输入张量

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

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

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

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

其中参数mode

import torch 
from torch import nn 
import torch.nn.functional as F
input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
print(input)
输出
tensor([[[[1., 2.],
          [3., 4.]]]])
  1. mode=‘nearest’
result = F.interpolate(input, scale_factor=2, mode='nearest')
print(result)
输出
tensor([[[[1., 1., 2., 2.],
          [1., 1., 2., 2.],
          [3., 3., 4., 4.],
          [3., 3., 4., 4.]]]])
  1. mode=‘linear’
NotImplementedError: Got 4D input, but linear mode needs 3D input
  1. mode=‘bilinear’
tensor([[[[1.0000, 1.2500, 1.7500, 2.0000],
          [1.5000, 1.7500, 2.2500, 2.5000],
          [2.5000, 2.7500, 3.2500, 3.5000],
          [3.0000, 3.2500, 3.7500, 4.0000]]]])
  1. mode=‘bicubic’
tensor([[[[0.6836, 1.0156, 1.5625, 1.8945],
          [1.3477, 1.6797, 2.2266, 2.5586],
          [2.4414, 2.7734, 3.3203, 3.6523],
          [3.1055, 3.4375, 3.9844, 4.3164]]]])
  1. mode=‘trilinear’
NotImplementedError: Got 4D input, but trilinear mode needs 5D input
  1. mode=‘area’
tensor([[[[1., 1., 2., 2.],
          [1., 1., 2., 2.],
          [3., 3., 4., 4.],
          [3., 3., 4., 4.]]]])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值