pytorch 中使用 torch.nn.functional.interpolate实现插值和上采样

pytorch 中使用 torch.nn.functional实现插值和上采样

interpolate的用法

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

输入要进行上下采样的feature(input),根据给定的size或scale_factor参数来对输入进行下/上采样

支持目前的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

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
    List item

注意

使用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.

栗子

  • 上采样
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:', input)
x = F.interpolate(input, scale_factor=2, mode='nearest')
print('x:', x)
input:
tensor([[[[1., 2.],
          [3., 4.]]]])
x:
tensor([[[[1., 1., 2., 2.],
          [1., 1., 2., 2.],
          [3., 3., 4., 4.],
          [3., 3., 4., 4.]]]])

转载于:原文链接

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 torch.nn.functional.interpolate 函数进行上采样,具体代码如下: import torch x = torch.randn(32, 256, 3) y = torch.nn.functional.interpolate(x, scale_factor=(1, 1, 3), mode='nearest') print(y.size()) # 输出 torch.Size([32, 256, 9]) ### 回答2: 要将大小为torch.Size([32, 256, 3])的张量上采样torch.Size([32, 256, 9]),可以使用torch.nn.functional.interpolate()函数来实现。 首先,需要将尺寸为[32, 256, 3]的张量转换为[32, 3, 256],即交换最后两个维度的顺序。可以使用torch.transpose()函数实现。 ```python import torch # 原始张量大小为torch.Size([32, 256, 3]) tensor = torch.randn((32, 256, 3)) # 将最后两个维度交换位置 transposed_tensor = tensor.transpose(1, 2) ``` 然后,使用torch.nn.functional.interpolate()函数进行上采样。该函数会根据给定的目标尺寸,在最后一个维度上进行线性插值,并返回新的张量。 ```python import torch.nn.functional as F # 目标尺寸为torch.Size([32, 256, 9]) target_size = (9,) # 进行上采样 upsampled_tensor = F.interpolate(transposed_tensor, size=target_size, mode='linear') ``` 最后,再次将尺寸为[32, 3, 256]的张量转换为[32, 256, 9]的张量,即再次交换最后两个维度的顺序。 ```python # 将最后两个维度再次交换位置 result_tensor = upsampled_tensor.transpose(1, 2) ``` 最终,得到的result_tensor就是尺寸为torch.Size([32, 256, 9])的上采样后的张量。 ### 回答3: 要将torch.Size([32, 256, 3])上采样torch.Size([32, 256, 9]), 可以使用PyTorchtorch.nn.functional.interpolate函数来实现。 首先,我们需要将输入的维度进行调整,使其变为4维的张量。torch.Size([32, 256, 3])可以变为torch.Size([32, 3, 256, 1])。 接下来,使用torch.nn.functional.interpolate函数对调整后的张量进行上采样。在上采样时,可以指定目标大小,默认情况下,目标大小与输入大小相同。 代码如下: ```python import torch import torch.nn.functional as F # 假设输入的张量为input_tensor,维度为torch.Size([32, 256, 3]) input_tensor = torch.randn(32, 256, 3) # 将输入的维度调整为4维的张量 input_tensor = input_tensor.unsqueeze(2).permute(0, 2, 1, 3) # torch.Size([32, 3, 256, 1]) # 使用torch.nn.functional.interpolate函数进行上采样 output_tensor = F.interpolate(input_tensor, scale_factor=(1, 1, 3, 1)) # 打印上采样后的张量维度 print(output_tensor.size()) # torch.Size([32, 3, 256, 9]) ``` 使用上述代码,就可以将torch.Size([32, 256, 3])上采样torch.Size([32, 256, 9])。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值