4 changed files with 159 additions and 82 deletions
@ -0,0 +1,94 @@ |
|||||
|
#include "ofxOnnxRuntime.h" |
||||
|
#include "ofMain.h" |
||||
|
|
||||
|
namespace ofxOnnxRuntime |
||||
|
{ |
||||
|
#ifdef _MSC_VER |
||||
|
static std::wstring to_wstring(const std::string &str) |
||||
|
{ |
||||
|
unsigned len = str.size() * 2; |
||||
|
setlocale(LC_CTYPE, ""); |
||||
|
wchar_t *p = new wchar_t[len]; |
||||
|
mbstowcs(p, str.c_str(), len); |
||||
|
std::wstring wstr(p); |
||||
|
delete[] p; |
||||
|
return wstr; |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
void BaseHandler::setup(const std::string & onnx_path, const BaseSetting & base_setting) |
||||
|
{ |
||||
|
Ort::SessionOptions session_options; |
||||
|
if (base_setting.infer_type == INFER_TENSORRT) { |
||||
|
OrtTensorRTProviderOptions op; |
||||
|
memset(&op, 0, sizeof(op)); |
||||
|
op.device_id = base_setting.device_id; |
||||
|
op.trt_fp16_enable = 1; |
||||
|
op.trt_engine_cache_enable = 1; |
||||
|
std::string path = ofToDataPath(onnx_path, true); |
||||
|
ofStringReplace(path, ".onnx", "_trt_cache"); |
||||
|
op.trt_engine_cache_path = path.c_str(); |
||||
|
session_options.AppendExecutionProvider_TensorRT(op); |
||||
|
} |
||||
|
if (base_setting.infer_type == INFER_CUDA || base_setting.infer_type == INFER_TENSORRT) { |
||||
|
OrtCUDAProviderOptions op; |
||||
|
op.device_id = base_setting.device_id; |
||||
|
session_options.AppendExecutionProvider_CUDA(op); |
||||
|
} |
||||
|
this->setup2(onnx_path, session_options); |
||||
|
} |
||||
|
|
||||
|
void BaseHandler::setup2(const std::string & onnx_path, const Ort::SessionOptions & session_options) |
||||
|
{ |
||||
|
std::string path = ofToDataPath(onnx_path, true); |
||||
|
#ifdef _MSC_VER |
||||
|
ort_session = std::make_shared<Ort::Session>(ort_env, to_wstring(path).c_str(), session_options); |
||||
|
#else |
||||
|
ort_session = std::make_shared<Ort::Session>(ort_env, path.c_str(), session_options); |
||||
|
#endif |
||||
|
|
||||
|
Ort::AllocatorWithDefaultOptions allocator; |
||||
|
|
||||
|
// 2. input name & input dims
|
||||
|
auto* input_name = ort_session->GetInputName(0, allocator); |
||||
|
input_node_names.resize(1); |
||||
|
input_node_names[0] = input_name; |
||||
|
|
||||
|
// 3. type info.
|
||||
|
Ort::TypeInfo type_info = ort_session->GetInputTypeInfo(0); |
||||
|
auto tensor_info = type_info.GetTensorTypeAndShapeInfo(); |
||||
|
input_tensor_size = 1; |
||||
|
input_node_dims = tensor_info.GetShape(); |
||||
|
for (unsigned int i = 0; i < input_node_dims.size(); ++i) |
||||
|
input_tensor_size *= input_node_dims.at(i); |
||||
|
input_values_handler.resize(input_tensor_size); |
||||
|
|
||||
|
// 4. output names & output dimms
|
||||
|
num_outputs = ort_session->GetOutputCount(); |
||||
|
output_node_names.resize(num_outputs); |
||||
|
for (unsigned int i = 0; i < num_outputs; ++i) |
||||
|
{ |
||||
|
output_node_names[i] = ort_session->GetOutputName(i, allocator); |
||||
|
Ort::TypeInfo output_type_info = ort_session->GetOutputTypeInfo(i); |
||||
|
auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo(); |
||||
|
auto output_dims = output_tensor_info.GetShape(); |
||||
|
output_node_dims.push_back(output_dims); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
Ort::Value& BaseHandler::run() |
||||
|
{ |
||||
|
auto input_tensor_ = Ort::Value::CreateTensor<float>( |
||||
|
memory_info_handler, input_values_handler.data(), input_tensor_size, |
||||
|
input_node_dims.data(), input_node_dims.size()); |
||||
|
auto result = ort_session->Run(Ort::RunOptions{ nullptr }, input_node_names.data(), &input_tensor_, input_node_names.size(), |
||||
|
output_node_names.data(), output_node_names.size()); |
||||
|
|
||||
|
if (result.size() == 1) { |
||||
|
return result.front(); |
||||
|
} |
||||
|
else { |
||||
|
return dummy_tensor_; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -1,3 +1,44 @@ |
|||||
#pragma once |
#pragma once |
||||
|
|
||||
#include <onnxruntime_cxx_api.h> |
#include <onnxruntime_cxx_api.h> |
||||
|
|
||||
|
namespace ofxOnnxRuntime |
||||
|
{ |
||||
|
enum InferType |
||||
|
{ |
||||
|
INFER_CPU = 0, |
||||
|
INFER_CUDA, |
||||
|
INFER_TENSORRT |
||||
|
}; |
||||
|
|
||||
|
struct BaseSetting |
||||
|
{ |
||||
|
InferType infer_type; |
||||
|
int device_id; |
||||
|
}; |
||||
|
|
||||
|
class BaseHandler |
||||
|
{ |
||||
|
public: |
||||
|
void setup(const std::string& onnx_path, const BaseSetting& base_setting = BaseSetting{ INFER_CPU, 0 }); |
||||
|
void setup2(const std::string& onnx_path, const Ort::SessionOptions& session_options); |
||||
|
|
||||
|
Ort::Value& run(); |
||||
|
|
||||
|
float* getInputTensorData() { |
||||
|
return this->input_values_handler.data(); |
||||
|
} |
||||
|
protected: |
||||
|
Ort::Env ort_env; |
||||
|
std::shared_ptr<Ort::Session> ort_session; |
||||
|
std::vector<const char *> input_node_names; |
||||
|
std::vector<int64_t> input_node_dims; // 1 input only.
|
||||
|
std::size_t input_tensor_size = 1; |
||||
|
std::vector<float> input_values_handler; |
||||
|
Ort::Value dummy_tensor_{ nullptr }; |
||||
|
Ort::MemoryInfo memory_info_handler = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); |
||||
|
std::vector<const char *> output_node_names; |
||||
|
std::vector<std::vector<int64_t>> output_node_dims; // >=1 outputs
|
||||
|
int num_outputs = 1; |
||||
|
}; |
||||
|
} |
Loading…
Reference in new issue