Python设置ONNX运行时返回张量而不是NumPy数组

huangapple go评论63阅读模式
英文:

Python set ONNX runtime to return tensor instead of numpy array

问题

在Python中,我加载了我的预定义模型(super-gradients,yolox-s):

onnx_session = onnxrt.InferenceSession("yolox_s_640_640.onnx")

然后我加载一些数据并运行它:

dataset = MyCostumeDataset(args.path, 'val')
val_dataloader = DataLoader(dataset, batch_size=args.bsize)

for inputs in val_dataloader:
    onnx_inputs = {onnx_session.get_inputs()[0].name: inputs}
    # inputs.shape: torch.Size([4, 3, 640, 640]), 即这是一个Tensor
    
    raw_predictions = onnx_session.run(None, onnx_inputs)
    # 这返回一个numpy数组的列表:
    # type(raw_predictions[0])
    # <class 'numpy.ndarray'>
    # raw_predictions[0].shape
    # (4, 8400, 85)

到目前为止,它正在按预期工作,除了我希望它默认返回torch.Tensor的列表,而不是numpy数组。我对ONNX和PyTorch都不熟悉,感觉我可能错过了一些基本的东西。

如何让onnx_session返回torch.Tensor的列表,而不是numpy数组?这将减少一些转换的开销。谢谢!

英文:

In python I'm loading my predefined model (super-gradients, yolox-s):

onnx_session = onnxrt.InferenceSession(&quot;yolox_s_640_640.onnx&quot;)

Then I load some data and run it:

dataset = MyCostumeDataset(args.path, &#39;val&#39;)
val_dataloader = DataLoader(dataset, batch_size=args.bsize)

for inputs in val_dataloader:
    onnx_inputs = {onnx_session.get_inputs()[0].name: inputs}
    # inputs.shape: torch.Size([4, 3, 640, 640]), i.e., this is a Tensor
    
    raw_predictions = onnx_session.run(None, onnx_inputs)
    # this returns a list of numpy arrays:
    # type(raw_predictions[0])
    #  &lt;class &#39;numpy.ndarray&#39;&gt;
    # raw_predictions[0].shape
    # (4, 8400, 85)

So far it is working as it should, except I'd like it to return, by default, a list of Tensors (torch.Tensor) instead of numpy array. I'm new to both ONNX and PyTorch, and I'm feeling like this is something basic that I'm missing here.

How can I get onnx_session to return a list of torch.Tensor, instead of numpy arrays? This will same some overhead in the conversion. Thanks!

答案1

得分: 1

根据文档,您可以获取以下内容 - 返回结果:结果列表,每个结果可以是NumPy数组、稀疏张量、列表或字典。

文档

英文:

According to the documentation you can get either - Returns: list of results, every result is either a numpy array, a sparse tensor, a list or a dictionary.

Documentation

huangapple
  • 本文由 发表于 2023年2月8日 20:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定