请写一个c++使用ONNX的demo
时间: 2023-02-27 13:05:48 浏览: 157
下面是一个简单的使用ONNX的C语言程序示例:
```
#include <onnxruntime_c_api.h>
#include <stdio.h>
int main() {
OrtEnv* env;
OrtStatus* status = OrtCreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env);
OrtSessionOptions* session_options;
status = OrtCreateSessionOptions(&session_options);
OrtSession* session;
char* model_path = "your_model.onnx";
status = OrtCreateSession(env, model_path, session_options, &session);
OrtAllocator* allocator;
status = OrtCreateDefaultAllocator(&allocator);
// input data
float input_data[1][3] = {{1.0f, 2.0f, 3.0f}};
OrtValue* input_tensor = NULL;
status = OrtCreateTensorWithDataAsOrtValue(allocator, input_data, sizeof(float), (int64_t[]) {1, 3}, &input_tensor);
// output data
float output_data[1][2];
OrtValue* output_tensor = NULL;
status = OrtCreateTensorWithDataAsOrtValue(allocator, output_data, sizeof(float), (int64_t[]) {1, 2}, &output_tensor);
// run model
const char* input_names[1] = {"input_1"};
const char* output_names[1] = {"output_1"};
status = OrtRun(session, NULL, input_names, (const OrtValue* const*)&input_tensor, 1, output_names, 1, &output_tensor);
// get output data
OrtGetTensorMutableData(output_tensor, (void**) &output_data);
// print result
printf("Result: %f %f\n", output_data[0][0], output_data[0][1]);
// release resources
OrtReleaseValue(input_tensor);
OrtReleaseValue(output_tensor);
OrtReleaseSession(session);
OrtReleaseSessionOptions(session_options);
OrtReleaseEnv(env);
return 0;
}
```
请注意,您需要将模型文件路径替换为您自己的模型文件。
阅读全文