【问题标题】:Different results after converting pytorch to torchscript? Converting NSnumber to Float cause any loss?将pytorch转换为torchscript后结果不同?将 NSnumber 转换为 Float 会导致任何损失吗?
【发布时间】:2020-05-24 09:28:26
【问题描述】:

我将 pytorch 预训练模型(.pt)转换为 torchscript 模型(.pt),以便在 Swift 5(ios-iphone6s,xcode 11)中使用它。在 Swift 中,模型的“预测”功能给了我它的嵌入值(张量)。由于它返回 NSNumber 数组作为预测的结果,我使用类型转换 [NSNumber] 到 [Double] 或 [Float] 来计算两个嵌入值之间的距离。 L2归一化、点积等

然而,虽然pytorch版本得到了正确答案,但torchscript模型得到了这么多错误答案。不仅答案不同,torchscript中两个embedding对的距离计算也不同来自PC(CPU,Pycharm)上的pytorch模型的结果。事实上,在使用类型转换进行距离计算之前,NSNumber(Swift) 中的嵌入值与 float32(pytorch) 中的值相差甚远。我使用了相同的输入图像。

我试图找到原因..有一次,我从swift-torchscript中复制了嵌入值([NSNumber])并计算了pytorch中两个嵌入之间的距离,以检查我的距离计算是否有问题在 Swift 中实现。我使用了 torch.FloatTensor 来使用类型转换 [NSNumber] -> [Float]。我也试过[双]。结果,我发现了许多无限数。这个无限数是否与错误答案有关?

这个“inf”是什么意思?是计算错误还是类型转换错误?从 NSNumber 转换为 Float 或 Double 时我是否丢失了信息?如何快速从 torchscript 模型中获得正确的值? 我应该检查什么?

我使用以下代码进行转换。 pytorch -> 火炬脚本。

import torch

from models.inception_resnet_v1 import InceptionResnetV1

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device)

example = torch.rand(1, 3, 160, 160)
traced_script_module = torch.jit.trace(resnet, example)
traced_script_module.save("mobile_model.pt")

【问题讨论】:

    标签: swift deep-learning pytorch nsnumber torchscript


    【解决方案1】:

    您是否正在使用以下来源的 InceptionResnetV1: https://github.com/timesler/facenet-pytorch ? 当您在比较输出时引用 pytorch 模型时,您是指在 pytorch 中运行时的 torchscript 模型,还是原样的 resnet?

    如果是后者,您是否已经检查过类似以下的内容?

    运行以下命令会得到什么:

    print('Original:')
    orig_res = resnet(example)
    print(orig_res.shape)
    print(orig_res[0, 0:10])
    print('min abs value:{}'.format(torch.min(torch.abs(orig_res))))
    print('Torchscript:')
    ts_res = traced_script_module(example)
    print(ts_res.shape)
    print(ts_res[0, 0:10])
    print('min abs value:{}'.format(torch.min(torch.abs(ts_res))))
    print('Dif sum:')
    abs_diff = torch.abs(orig_res-ts_res)
    print(torch.sum(abs_diff))
    print('max dif:{}'.format(torch.max(abs_diff)))
    

    在定义“traced_script_module”之后。 我得到以下信息:

    Original:
    torch.Size([1, 512])
    tensor([ 0.0347,  0.0145, -0.0124,  0.0723, -0.0102,  0.0653, -0.0574,  0.0004,
            -0.0686,  0.0695], device='cuda:0', grad_fn=<SliceBackward>)
    min abs value:0.00034740756382234395
    Torchscript:
    torch.Size([1, 512])
    tensor([ 0.0347,  0.0145, -0.0124,  0.0723, -0.0102,  0.0653, -0.0574,  0.0004,
            -0.0686,  0.0695], device='cuda:0', grad_fn=<SliceBackward>)
    min abs value:0.0003474018594715744
    Dif sum:
    tensor(8.1539e-06, device='cuda:0', grad_fn=<SumBackward0>)
    max dif:5.960464477539063e-08
    

    这并不完美,但考虑到输出最小为 10^-4 的顺序,并且最后一个数字是 512 个元素的绝对差之和,而不是平均值,这似乎并不太远我。最大差异在 10^-8 左右。

    顺便说一句,你可能想改成:

    example = torch.rand(1, 3, 160, 160).to(device)
    

    如果您在上面的测试中得到类似的结果,那么您从 swift-torchscript 获得的前 10 个输出值作为 NSNumber 获得的值是什么类型,然后,一旦转换为浮点数,当与两者进行比较时pytorch 和 torchscript-pytorch 模型输出中的相同切片?

    【讨论】:

      猜你喜欢
      • 2011-12-17
      • 2021-12-05
      • 2019-05-18
      • 2022-08-05
      • 1970-01-01
      • 2022-09-26
      • 2013-06-08
      • 2010-11-24
      • 2011-09-05
      相关资源
      最近更新 更多