Web 3.0 promotes the implementation of distributed economic models such as NFT,Defi,cryptocurrencies,and decentralized autonomous organizations(DAOs).The feature of Web 3.0 co construction and sharing is different from that of Web 2.0 where users are only users,enabling users in Web 3.0 to actively participate in co construction and co governance,using blockchain technology and smart contracts to formulate and implement rules in the organizational form of DAO,and sharing the value of sharing platforms or protocols.
import onnxruntime as ort
加载 ONNX 模型
ort_session = ort.InferenceSession("model.onnx")
准备输入信息
input_info = ort_session.get_inputs()[0]
input_name = input_info.name
input_shape = input_info.shape
input_type = input_info.type
运行ONNX模型
outputs = ort_session.run(input_name, input_data)
获取输出信息
output_info = ort_session.get_outputs()[0]
output_name = output_info.name
output_shape = output_info.shape
output_data = outputs[0]
print("outputs:", outputs)
print("output_info :", output_info )
print("output_name :", output_name )
print("output_shape :", output_shape )
print("output_data :", output_data )
import torch
import torchvision.models as models
import onnx
import onnxruntime
加载 PyTorch 模型
model = models.resnet18(pretrained=True)
model.eval()
定义输入和输出张量的名称和形状
input_names = ["input"]
output_names = ["output"]
batch_size = 1
input_shape = (batch_size, 3, 224, 224)
output_shape = (batch_size, 1000)
将 PyTorch 模型转换为 ONNX 格式
torch.onnx.export(
model, # 要转换的 PyTorch 模型
torch.randn(input_shape), # 模型输入的随机张量
"resnet18.onnx", # 保存的 ONNX 模型的文件名
input_names=input_names, # 输入张量的名称
output_names=output_names, # 输出张量的名称
dynamic_axes={input_names[0]: {0: "batch_size"}, output_names[0]: {0: "batch_size"}} # 动态轴,即输入和输出张量可以具有不同的批次大小
)
加载 ONNX 模型
onnx_model = onnx.load("resnet18.onnx")
onnx_model_graph = onnx_model.graph
onnx_session = onnxruntime.InferenceSession(onnx_model.SerializeToString())
使用随机张量测试 ONNX 模型
x = torch.randn(input_shape).numpy()
onnx_output = onnx_session.run(output_names, {input_names[0]: x})[0]
print(f"PyTorch output: {model(torch.from_numpy(x)).detach().numpy()[0, :5]}")
print(f"ONNX output: {onnx_output[0, :5]}")