pytorch nearest upsample整数型tensor

在用 torch.nn.Upsample 给分割 label 上采样时报错:RuntimeError: "upsample_nearest2d_out_frame" not implemented for 'Long'

参考 [1-3],用 [3] 给出的实现。稍微扩展一下,支持 h、w 用不同的 scale factor,并测试其与 PyTorch 的几个 upsample 类的异同,验证 [3] 的实现用 nearest 插值。

Code

  • linear 要 3D 输入、trilinear 要 5D 输入,故此两种插值法没比。
import torch
import torch.nn as nn


class UpsampleDeterministic(nn.Module):
    """deterministic upsample with `nearest` interpolation"""

    def __init__(self, scale_factor=2):
        """
        Input:
            scale_factor: int or (int, int), ratio to scale (along heigth & width)
        """
        super(UpsampleDeterministic, self).__init__()
        if isinstance(scale_factor, (tuple, list)):
            assert len(scale_factor) == 2
            self.scale_h, self.scale_w = scale_factor
        else:
            self.scale_h = self.scale_w = scale_factor
        assert isinstance(self.scale_h, int) and isinstance(self.scale_w, int)

    def forward(self, x):
        """
        Input:
            x: [n, c, h, w], torch.Tensor
        Output:
            upsampled x': [n, c, h * scale_h, w * scale_w]
        """
        return x[:, :, :, None, :, None].expand(
            -1, -1, -1, self.scale_h, -1, self.scale_w).reshape(
                x.size(0), x.size(1), x.size(2) * self.scale_h, x.size(3) * self.scale_w)


# 随机数据
x = torch.rand(2, 3, 4, 4) # [n, c, h, w]
# [3] 的实现
us_det = UpsampleDeterministic((2, 3))
# pytorch 自带的几种实现
us_list = {mode: nn.Upsample(scale_factor=(2, 3), mode=mode)
           for mode in ('nearest', 'bilinear', 'bicubic')}
# linear: 3D
# trilinear: 5D

y_det = us_det(x)
print(y_det.size())
for us_name, us in us_list.items():
    y = us(x)
    print(us_name, y.size(), (y_det != y).sum())

输出:

torch.Size([2, 3, 8, 12])
nearest torch.Size([2, 3, 8, 12]) tensor(0)
bilinear torch.Size([2, 3, 8, 12]) tensor(507)
bicubic torch.Size([2, 3, 8, 12]) tensor(576)

可见 [3] 的实现与 nearest 结果一致。

References

  1. 请慎用torch.nn.Upsample
  2. PyTorch中模型的可复现性
  3. Non Deterministic Behaviour even after cudnn.deterministic = True and cudnn.benchmark=False #12207
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyTorch 中,我们可以使用 `torch.tensor` 创建张量。PyTorch 的张量是具有多种数学运算功能的多维数组。与整数除法相关的操作有两个主要方面:整数或整数张量的除法运算,以及整数除法运算的结果类型。 首先,在 PyTorch 中,整数除法运算可以使用 `/` 符号完成。例如,对于整数变量 `x` 和整数变量 `y`,我们可以使用 `result = x / y` 进行整数除法运算。这将返回一个浮点数结果,即使 `x` 和 `y` 均为整数。 在进行整数除法运算时,PyTorch 使用了自适应类型推断功能。这意味着结果的类型将根据操作数的类型自动确定。例如,如果进行整数除法运算的操作数是整数型张量,则结果将是浮点型张量。而如果操作数是整数,则结果将是浮点数。 对于整数型张量的除法运算,我们可以使用 `torch.div` 函数。该函数的第一个参数是一个整数型张量,第二个参数可以是整数或整数型张量。例如,`result = torch.div(tensor, 5)` 将整数型张量 `tensor` 中的每个元素除以 5,并返回一个浮点型张量 `result`。 综上所述,PyTorch 中的张量与整数除法的关系是,张量支持整数除法运算,并可以根据操作数的类型自动确定结果的类型。我们可以使用 `/` 符号进行整数除法运算,也可以使用 `torch.div` 函数进行整数型张量的除法运算。无论哪种方式,结果将是浮点型张量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值