Skip to content

Commit eba2645

Browse files
authored
ML: check if ORT API is null before initializing ORT Env. (#893)
* ML: check if ORT API is null before initializing ORT Env. * fix build
1 parent b4153a5 commit eba2645

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

SerialPrograms/Source/ML/DataLabeling/ML_SegmentAnythingModel.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ namespace ML{
2525

2626

2727
SAMEmbedderSession::SAMEmbedderSession(const std::string& model_path, bool use_gpu)
28-
: m_session_options{create_session_options(ML_MODEL_CACHE_PATH() + "SAMEmbedder/", use_gpu)}
28+
: m_env{create_ORT_env()}
29+
, m_session_options{create_session_options(ML_MODEL_CACHE_PATH() + "SAMEmbedder/", use_gpu)}
2930
, session{create_session(m_env, m_session_options, model_path, ML_MODEL_CACHE_PATH() + "SAMEmbedder/")}
3031
, memory_info{Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU)}
3132
, input_names{session.GetInputNames()}
@@ -65,7 +66,8 @@ void SAMEmbedderSession::run(cv::Mat& input_image, std::vector<float>& model_out
6566

6667

6768
SAMSession::SAMSession(const std::string& model_path, bool use_gpu)
68-
: m_session_options{create_session_options(ML_MODEL_CACHE_PATH() + "SAM/", use_gpu)}
69+
: m_env{create_ORT_env()}
70+
, m_session_options{create_session_options(ML_MODEL_CACHE_PATH() + "SAM/", use_gpu)}
6971
, session{create_session(m_env, m_session_options, model_path, ML_MODEL_CACHE_PATH() + "SAM/")}
7072
, memory_info{Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU)}
7173
, input_names{session.GetInputNames()}

SerialPrograms/Source/ML/Models/ML_ONNXRuntimeHelpers.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <fstream>
1717
#include <onnxruntime_cxx_api.h>
1818
#include "3rdParty/ONNX/OnnxToolsPA.h"
19+
#include "Common/Cpp/Exceptions.h"
1920
#include "Common/Compiler.h"
2021
#include "ML_ONNXRuntimeHelpers.h"
2122

@@ -194,5 +195,13 @@ void print_model_input_output_info(const Ort::Session& session){
194195
}
195196
}
196197

198+
Ort::Env create_ORT_env(){
199+
if (Ort::Global<void>::api_ == nullptr){
200+
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Onnx API returned a null pointer.");
201+
}
202+
203+
return Ort::Env();
204+
}
205+
197206
}
198207
}

SerialPrograms/Source/ML/Models/ML_ONNXRuntimeHelpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ std::string to_string(std::vector<T>& vec){
5454
// Print model input and output types and shapes to cout. Useful for debugging.
5555
void print_model_input_output_info(const Ort::Session& session);
5656

57+
Ort::Env create_ORT_env();
58+
5759

5860
}
5961
}

SerialPrograms/Source/ML/Models/ML_YOLOv5Model.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ std::tuple<int, int, double, double> resize_image_with_border(
6060

6161
YOLOv5Session::YOLOv5Session(const std::string& model_path, std::vector<std::string> label_names, bool use_gpu)
6262
: m_label_names(std::move(label_names))
63+
, m_env{create_ORT_env()}
6364
, m_session_options(create_session_options(ML_MODEL_CACHE_PATH() + "YOLOv5", use_gpu))
6465
, m_session{create_session(m_env, m_session_options, model_path, ML_MODEL_CACHE_PATH() + "YOLOv5")}
6566
, m_memory_info{Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU)}

0 commit comments

Comments
 (0)