Swift 将Pytork转换为torchscript后会出现不同的结果?将NSnumber转换为Float会导致任何损失吗?

Swift 将Pytork转换为torchscript后会出现不同的结果?将NSnumber转换为Float会导致任何损失吗?,swift,deep-learning,pytorch,nsnumber,torchscript,Swift,Deep Learning,Pytorch,Nsnumber,Torchscript,我将pytorch预训练模型(.pt)转换为torchscript模型(.pt),以便在Swift 5(ios-iphone6s,xcode 11)中使用它。在Swift中,模型的“预测”函数给了我它的嵌入值(张量)。由于它返回NSNumber数组作为预测的结果,因此我使用类型转换[NSNumber]将[Double]或[Float]都转换为[Double]来计算两个嵌入值之间的距离。L2规范化、点积等 然而,虽然pytorch版本得到了正确的答案,但torchscript模型得到了太多错误的答

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

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

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

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

我使用以下代码进行转换。pytorch->torchscript

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")

您是否从以下位置使用InceptionResnetV1: ? 在比较输出时,您指的是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)))
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
在定义“跟踪脚本模块”之后。 我得到以下信息:

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)))
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
如果您在上面的测试中得到了类似的结果,那么您从swift torchscript中获得的前10个输出值(作为NSNumber)得到的值是什么样的,然后,一旦在float中浇铸,与pytorch和torchscript pytorch模型输出中的相同切片进行比较时,会得到什么样的值