Onnxruntime c接口说明及 动态调用示例
时间: 2023-12-12 18:05:50 浏览: 137
onnxruntime-win-x64-1.14.1
5星 · 资源好评率100%
Onnxruntime 是一个用于运行机器学习模型的开源框架,它支持多种语言,包括 C++、Python、C#、Java 等。下面是 Onnxruntime 的 C 接口说明及动态调用示例:
1. 引入头文件
```
#include "onnxruntime_c_api.h"
```
2. 创建一个 Onnxruntime 的 session
```
OrtEnv* env;
OrtCreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env);
OrtSessionOptions* session_options;
OrtCreateSessionOptions(&session_options);
OrtSession* session;
OrtCreateSession(env, "model.onnx", session_options, &session);
```
3. 获取输入和输出名称及形状
```
size_t num_input_nodes;
OrtStatus* status = OrtSessionGetInputCount(session, &num_input_nodes);
OrtStatus* status = OrtSessionGetOutputCount(session, &num_output_nodes);
OrtAllocator* allocator;
OrtCreateDefaultAllocator(&allocator);
OrtTypeInfo* type_info;
OrtSessionGetInputTypeInfo(session, 0, &type_info);
const OrtTensorTypeAndShapeInfo* tensor_info;
OrtGetTensorTypeAndShape(type_info, &tensor_info);
size_t num_dims = OrtGetNumOfDimensions(tensor_info);
int64_t* dims = (int64_t*)malloc(num_dims * sizeof(int64_t));
OrtGetDimensions(tensor_info, dims, num_dims);
char* input_name = NULL;
OrtSessionGetInputName(session, 0, allocator, &input_name);
OrtGetTensorElementType(tensor_info);
```
4. 准备输入数据
```
float* input = (float*)malloc(num_dims * sizeof(float));
// fill input data
```
5. 运行 session
```
OrtValue* input_tensor;
OrtCreateTensorWithDataAsOrtValue(allocator, input, num_dims * sizeof(float), dims, num_dims, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &input_tensor);
const char* const input_names[] = {input_name};
OrtValue* input_tensors[] = {input_tensor};
OrtValue* output_tensor;
OrtSessionRun(session, NULL, input_names, input_tensors, 1, output_names, 1, &output_tensor);
float* output = (float*)OrtGetTensorMutableData(output_tensor);
// process output data
OrtReleaseValue(output_tensor);
OrtReleaseValue(input_tensor);
```
6. 释放资源
```
OrtReleaseTypeInfo(type_info);
OrtReleaseSession(session);
OrtReleaseSessionOptions(session_options);
OrtReleaseEnv(env);
free(input);
free(dims);
```
阅读全文