[Pytorch基础操作] 上/下采样函数 torch.nn.functional.interpolate 插值

torch.nn.functional.interpolate(input_tensor, size=None, scale_factor=8, mode='bilinear', align_corners=False)
'''
Down/up samples the input to either the given size or the given scale_factor
The algorithm used for interpolation is determined by 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
'''

这个函数是用来上采样下采样tensor的空间维度(h,w)

  • input_tensor支持输入3D (b, c, w)或(batch,seq_len,dim)、4D (b, c, h, w)、5D (b, c, f, h, w)的 tensor shape。其中b表示batch_size,c表示channel,f表示frames,h表示height,w表示weight。
  • size是目标tensor的(w)/(h,w)/(f,h,w)的形状;scale_factor是采样tensor的saptial shape(w)/(h,w)/(f,h,w)的缩放系数,sizescale_factor两个参数只能定义一个,具体是上采样,还是下采样根据这两个参数判断。如果size或者scale_factorlist序列,则必须匹配输入的大小。
    • 如果输入3D,则它们的序列长度必须是1(只缩放最后1个维度w)。
    • 如果输入4D,则它们的序列长度必须是2(缩放最后2个维度h,w)。
    • 如果输入是5D,则它们的序列长度必须是3(缩放最后3个维度f,h,w)。
  • 插值算法mode可选:最近邻(nearest, 默认)线性(linear, 3D-only)双线性(bilinear, 4D-only)三线性(trilinear, 5D-only)等等。
  • 是否align_corners对齐角点:可选的bool值, 如果 align_corners=True,则对齐 input 和 output 的角点像素(corner pixels),保持在角点像素的值. 只会对 mode=linear, bilinear, trilinear 有作用. 默认是 False。一图看懂align_corners=TrueFalse的区别,从4×4上采样成8×8。一个是按四角的像素点中心对齐,另一个是按四角的像素角点对齐:在这里插入图片描述
import torch
import torch.nn.functional as F
b, c, f, h, w = 1, 3, 8, 64, 64

1. upsample/downsample 3D tensor

# interpolate 3D tensor
x = torch.randn([b, c, w])
## downsample to (b, c, w/2)
y0 = F.interpolate(x, scale_factor=0.5, mode='nearest')
y1 = F.interpolate(x, size=[w//2], mode='nearest')
y2 = F.interpolate(x, scale_factor=0.5, mode='linear')  # only 3D
y3 = F.interpolate(x, size=[w//2], mode='linear')  # only 3D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 32]) torch.Size([1, 3, 32]) torch.Size([1, 3, 32]) torch.Size([1, 3, 32])

## upsample to (b, c, w*2)
y0 = F.interpolate(x, scale_factor=2, mode='nearest')
y1 = F.interpolate(x, size=[w*2], mode='nearest')
y2 = F.interpolate(x, scale_factor=2, mode='linear')  # only 3D
y3 = F.interpolate(x, size=[w*2], mode='linear')  # only 3D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 128]) torch.Size([1, 3, 128]) torch.Size([1, 3, 128]) torch.Size([1, 3, 128])

2. upsample/downsample 4D tensor

# interpolate 4D tensor
x = torch.randn(b, c, h, w)
## downsample to (b, c, h/2, w/2)
y0 = F.interpolate(x, scale_factor=0.5, mode='nearest')
y1 = F.interpolate(x, size=[h//2, w//2], mode='nearest')
y2 = F.interpolate(x, scale_factor=0.5, mode='bilinear')  # only 4D
y3 = F.interpolate(x, size=[h//2, w//2], mode='bilinear')  # only 4D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 32, 32]) torch.Size([1, 3, 32, 32]) torch.Size([1, 3, 32, 32]) torch.Size([1, 3, 32, 32])

## upsample to (b, c, h*2, w*2)
y0 = F.interpolate(x, scale_factor=2, mode='nearest')
y1 = F.interpolate(x, size=[h*2, w*2], mode='nearest')
y2 = F.interpolate(x, scale_factor=2, mode='bilinear')  # only 4D
y3 = F.interpolate(x, size=[h*2, w*2], mode='bilinear')  # only 4D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 128, 128]) torch.Size([1, 3, 128, 128]) torch.Size([1, 3, 128, 128]) torch.Size([1, 3, 128, 128])

3. upsample/downsample 5D tensor

# interpolate 5D tensor
x = torch.randn(b, c, f, h, w)
## downsample to (b, c, f/2, h/2, w/2)
y0 = F.interpolate(x, scale_factor=0.5, mode='nearest')
y1 = F.interpolate(x, size=[f//2, h//2, w//2], mode='nearest')
y2 = F.interpolate(x, scale_factor=2, mode='trilinear')  # only 5D
y3 = F.interpolate(x, size=[f//2, h//2, w//2], mode='trilinear')  # only 5D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 4, 32, 32]) torch.Size([1, 3, 4, 32, 32]) torch.Size([1, 3, 16, 128, 128]) torch.Size([1, 3, 4, 32, 32])

## upsample to (b, c, f*2, h*2, w*2)
y0 = F.interpolate(x, scale_factor=2, mode='nearest')
y1 = F.interpolate(x, size=[f*2, h*2, w*2], mode='nearest')
y2 = F.interpolate(x, scale_factor=2, mode='trilinear')  # only 5D
y3 = F.interpolate(x, size=[f*2, h*2, w*2], mode='trilinear')  # only 5D
print(y0.shape, y1.shape, y2.shape, y3.shape)
# torch.Size([1, 3, 16, 128, 128]) torch.Size([1, 3, 16, 128, 128]) torch.Size([1, 3, 16, 128, 128]) torch.Size([1, 3, 16, 128, 128])
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yuezero_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值