Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ namespace ML{


SAMEmbedderSession::SAMEmbedderSession(const std::string& model_path, bool use_gpu)
: m_env{create_ORT_env()}
, session{create_session(m_env, model_path, ML_MODEL_CACHE_PATH() + "SAMEmbedder/", use_gpu)}
: session{create_session(model_path, ML_MODEL_CACHE_PATH() + "SAMEmbedder/", use_gpu)}
, memory_info{Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU)}
, input_names{session.GetInputNames()}
, output_names{session.GetOutputNames()}
Expand Down Expand Up @@ -67,8 +66,7 @@ void SAMEmbedderSession::run(cv::Mat& input_image, std::vector<float>& model_out


SAMSession::SAMSession(const std::string& model_path, bool use_gpu)
: m_env{create_ORT_env()}
, session{create_session(m_env, model_path, ML_MODEL_CACHE_PATH() + "SAM/", use_gpu)}
: session{create_session(model_path, ML_MODEL_CACHE_PATH() + "SAM/", use_gpu)}
, memory_info{Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU)}
, input_names{session.GetInputNames()}
, output_names{session.GetOutputNames()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class SAMEmbedderSession{
void run(cv::Mat& input_image, std::vector<float>& output_image_embedding);

private:
Ort::Env m_env;
Ort::Session session;
Ort::MemoryInfo memory_info;
Ort::RunOptions run_options;
Expand Down Expand Up @@ -72,7 +71,6 @@ class SAMSession{
const std::vector<int>& input_box,
std::vector<bool>& output_boolean_mask);
private:
Ort::Env m_env;
Ort::Session session;
Ort::MemoryInfo memory_info;
Ort::RunOptions run_options;
Expand Down Expand Up @@ -102,4 +100,4 @@ class SAMSession{

}
}
#endif
#endif
4 changes: 1 addition & 3 deletions SerialPrograms/Source/ML/Inference/ML_PaddleOCRPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ PaddleOCRPipeline::PaddleOCRPipeline(Language language)
{}

PaddleOCRPipeline::PaddleOCRPipeline(Language language, std::string rec_path, std::string dict_path)
: m_env{create_ORT_env()}
// , det_session(env, std::wstring(det_path.begin(), det_path.end()).c_str(), Ort::SessionOptions{})
, m_rec_session(
: m_rec_session(
create_session(
m_env,
rec_path,
ML_MODEL_CACHE_PATH() + "PaddleOCRPipeline/",
GlobalSettings::instance().USE_GPU_FOR_ML_INFERENCE0
Expand Down
2 changes: 1 addition & 1 deletion SerialPrograms/Source/ML/Inference/ML_PaddleOCRPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <onnxruntime_cxx_api.h>
#include <opencv2/opencv.hpp>
#include "Common/Cpp/Logging/TaggedLogger.h"
#include "Common/Cpp/Filesystem/FilePath.h"
#include "CommonFramework/Language.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
Expand All @@ -37,7 +38,6 @@ class PaddleOCRPipeline{
private:
void load_dictionary(const Filesystem::Path& path);

Ort::Env m_env;
// Ort::Session det_session;
Ort::Session m_rec_session;
// Ort::MemoryInfo memory_info;
Expand Down
16 changes: 5 additions & 11 deletions SerialPrograms/Source/ML/Models/ML_ONNXRuntimeHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/Filesystem/Filesystem.h"
#include "CommonFramework/Logging/Logger.h"
#include "ML_OrtEnv.h"
#include "ML_ONNXRuntimeHelpers.h"

namespace PokemonAutomation{
Expand Down Expand Up @@ -181,7 +182,6 @@ void write_cache_flag_file(const std::string& model_cache_path, const std::strin


Ort::Session create_session(
const Ort::Env& env,
const std::string& model_path,
const std::string& model_cache_path,
bool try_gpu
Expand All @@ -200,7 +200,7 @@ Ort::Session create_session(
try{
logger.log("Attempting to create Ort::Session with GPU acceleration...");
Ort::SessionOptions gpu_options = create_session_options(model_cache_path, true);
Ort::Session session{env, onnx_path.c_str(), gpu_options};
Ort::Session session{global_ort_env(), onnx_path.c_str(), gpu_options};
logger.log("Ort::Session created");
// when Ort::Ssssion is created, if possible, it will create a model cache
if (write_flag_file){
Expand All @@ -222,7 +222,7 @@ Ort::Session create_session(

Ort::SessionOptions cpu_options = create_session_options(model_cache_path, false);

Ort::Session session{env, onnx_path.c_str(), cpu_options};
Ort::Session session{global_ort_env(), onnx_path.c_str(), cpu_options};
logger.log("Ort::Session created");
return session;
}
Expand Down Expand Up @@ -272,14 +272,8 @@ void print_model_input_output_info(const Ort::Session& session){
}
}

Ort::Env create_ORT_env(){
#if ORT_API_VERSION < 24 // Removed in ONNX Runtime 1.24.0
if (Ort::Global<void>::api_ == nullptr){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Onnx API returned a null pointer.");
}
#endif
return Ort::Env();
}



}
}
4 changes: 1 addition & 3 deletions SerialPrograms/Source/ML/Models/ML_ONNXRuntimeHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Ort::SessionOptions create_session_options(const std::string& model_cache_path,
// used in `create_session_options()` to construct the passed-in session options so.
// NOTE: it may throw `MLModelSessionCreationError` if failed to create session.
Ort::Session create_session(
const Ort::Env& env,
const std::string& model_path,
const std::string& model_cache_path,
bool try_gpu
Expand Down Expand Up @@ -59,9 +58,8 @@ std::string to_string(std::vector<T>& vec){
// Print model input and output types and shapes to cout. Useful for debugging.
void print_model_input_output_info(const Ort::Session& session);

Ort::Env create_ORT_env();


}
}
#endif
#endif
37 changes: 37 additions & 0 deletions SerialPrograms/Source/ML/Models/ML_OrtEnv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Global Ort::Env
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "Common/Cpp/Exceptions.h"
#include "CommonFramework/Options/Environment/PerformanceOptions.h"
#include "ML_OrtEnv.h"

namespace PokemonAutomation{



Ort::ThreadingOptions make_ort_threading_options(){
Ort::ThreadingOptions ret;
ret.SetGlobalIntraOpNumThreads((int)PerformanceOptions::instance().NORMAL_THREAD_POOL.MAX_THREADS);
return ret;
}

Ort::Env& global_ort_env(){
#if ORT_API_VERSION < 24 // Removed in ONNX Runtime 1.24.0
if (Ort::Global<void>::api_ == nullptr){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Onnx API returned a null pointer.");
}
#endif
static Ort::Env env(
make_ort_threading_options(),
ORT_LOGGING_LEVEL_WARNING,
"Global_ThreadPool_Env"
);
return env;
}



}
21 changes: 21 additions & 0 deletions SerialPrograms/Source/ML/Models/ML_OrtEnv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Global Ort::Env
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_ML_OrtEnv_H
#define PokemonAutomation_ML_OrtEnv_H

#include <onnxruntime_cxx_api.h>

namespace PokemonAutomation{


Ort::Env& global_ort_env();




}
#endif
11 changes: 5 additions & 6 deletions SerialPrograms/Source/ML/Models/ML_YOLOv5Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,11 @@ std::tuple<int, int, double, double> resize_image_with_border(


YOLOv5Session::YOLOv5Session(const std::string& model_path, bool use_gpu)
: m_env{create_ORT_env()}
, m_session{create_session(m_env, model_path, ML_MODEL_CACHE_PATH() + "YOLOv5", use_gpu)}
, m_memory_info{Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU)}
, m_input_names{m_session.GetInputNames()}
, m_output_names{m_session.GetOutputNames()}
, m_model_input(3*YOLO5_INPUT_IMAGE_SIZE*YOLO5_INPUT_IMAGE_SIZE)
: m_session{create_session(model_path, ML_MODEL_CACHE_PATH() + "YOLOv5", use_gpu)}
, m_memory_info{Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU)}
, m_input_names{m_session.GetInputNames()}
, m_output_names{m_session.GetOutputNames()}
, m_model_input(3*YOLO5_INPUT_IMAGE_SIZE*YOLO5_INPUT_IMAGE_SIZE)
{
// Extract YOLO labels from model metadata
try{
Expand Down
1 change: 0 additions & 1 deletion SerialPrograms/Source/ML/Models/ML_YOLOv5Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class YOLOv5Session{

std::vector<std::string> m_label_names;

Ort::Env m_env;
Ort::Session m_session;
Ort::MemoryInfo m_memory_info;
Ort::RunOptions m_run_options;
Expand Down
2 changes: 2 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,8 @@ file(GLOB LIBRARY_SOURCES
Source/ML/ML_Panels.h
Source/ML/Models/ML_ONNXRuntimeHelpers.cpp
Source/ML/Models/ML_ONNXRuntimeHelpers.h
Source/ML/Models/ML_OrtEnv.cpp
Source/ML/Models/ML_OrtEnv.h
Source/ML/Models/ML_YOLOv5Model.cpp
Source/ML/Models/ML_YOLOv5Model.h
Source/ML/Programs/ML_LabelImages.cpp
Expand Down
Loading