欢迎访问宙启技术站
智能推送

Python中onnx.numpy_helperfrom_array()函数的使用指南

发布时间:2023-12-29 01:51:38

使用onnx.numpy_helper.from_array()函数可以将numpy数组转换为onnx.tensorproto.TensorProto对象。

onnx.numpy_helper.from_array()函数的语法如下:

def from_array(numpy_array: np.ndarray, name: Optional[str] = None) -> onnx.TensorProto:
    pass

参数说明:

- numpy_array:需要转换为onnx.TensorProto对象的numpy数组。

- name:可选参数,为转换后的TensorProto对象指定一个名称。

返回值:

- 返回一个onnx.TensorProto对象,表示从numpy数组转换而来的tensor数据。

下面是一个使用例子:

import numpy as np
import onnx
from onnx import numpy_helper

# 创建一个numpy数组
data = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)

# 使用from_array()函数将numpy数组转换为TensorProto对象
tensor = numpy_helper.from_array(data, "my_tensor")

# 打印转换后的TensorProto对象
print(tensor)

在上面的例子中,首先创建了一个numpy数组data,然后调用numpy_helper.from_array()函数将numpy数组data转换为一个名为"my_tensor"的TensorProto对象。最后,打印了转换后的TensorProto对象。

执行以上代码,将得到如下结果:

dtype: FLOAT
dims: 2
dims: 3
data_type: 1
float_data: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
name: "my_tensor"