feat(ODEngine): load a model from memory, not only from a path

Every engine built its session from a file path, which is why opening an
encrypted model package means inflating it to a directory first: the
unprotected model lands on disk, is optimised, and is zipped back into a
cache. The new package format cannot avoid that from the outside — the
engines have to be able to take bytes.

ANSModelSource.h introduces a ModelSource (a path, or bytes with a name
for logs) and one MakeSession that builds from either. The six ONNX
engines — classification, OBB, pose, segmentation, YOLO12 and YOLO —
each gain a load overload taking it, with the existing string form
delegating so no caller changes. The Windows wide-path conversion and
the from-memory call now live in one place instead of the eleven copies
they were spread across; YOLOOD, which had only the Windows branch,
becomes portable as a side effect.

The factories are named rather than implicit, so a call with a plain
string literal still resolves to the path overload without ambiguity.

Not compiled here: this layer builds only where CUDA is present, so
verification on this machine was the new header compiled and run, plus a
structural check that every engine declares and defines both overloads,
calls MakeSession exactly once, and has no raw session construction
left. It needs a build on a CUDA box before it ships.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 20:29:59 +10:00
co-authored by Claude Opus 5
parent 430c8057b8
commit 5820a5e61b
13 changed files with 131 additions and 33 deletions
+76
View File
@@ -0,0 +1,76 @@
#ifndef ANSMODELSOURCE_H
#define ANSMODELSOURCE_H
#pragma once
// ANSModelSource — where a model's bytes come from: a file, or memory.
//
// Every engine here built its session from a file path, which is why loading
// an encrypted model package means inflating it to a directory first: the
// unprotected model lands on disk, is optimised, and is zipped back into a
// cache. Handing the runtime a buffer instead removes both the round trip and
// the plaintext on disk, and it is the one change the new .ansmodel package
// format cannot work without.
//
// A model in memory still needs two things a path gave for free. `name` is
// what logs call it, since there is no file name to fall back on. And
// ONNX Runtime copies what it needs out of the buffer while the session is
// being built, so the caller may wipe its plaintext the moment MakeSession
// returns — which is exactly what a package reader should do.
#include <string>
#include <onnxruntime_cxx_api.h>
namespace ANSCENTER {
struct ModelSource {
std::string path; // file on disk; empty for a model already in memory
std::string name; // what to call it in logs
const void* data = nullptr;
size_t size = 0;
bool inMemory() const { return data != nullptr && size > 0; }
// Named factories, never implicit conversions: an engine that offers
// both Init(std::string) and Init(ModelSource) must not become
// ambiguous for a caller passing a plain string literal.
static ModelSource File(std::string p) {
ModelSource s;
s.name = p;
s.path = std::move(p);
return s;
}
static ModelSource Memory(const void* d, size_t n, std::string name) {
ModelSource s;
s.data = d;
s.size = n;
s.name = std::move(name);
return s;
}
};
// What to call this model in a log line.
inline std::string ModelSourceName(const ModelSource& src) {
if (!src.name.empty()) return src.name;
if (!src.path.empty()) return src.path;
return "<memory>";
}
// Build a session from wherever the bytes are. One place, so the Windows
// wide-path conversion and the from-memory call are not repeated in every
// engine — they were already duplicated eleven times.
inline Ort::Session MakeSession(Ort::Env& env, const ModelSource& src,
const Ort::SessionOptions& options) {
if (src.inMemory())
return Ort::Session(env, src.data, src.size, options);
#ifdef _WIN32
// ORT's Windows API takes a wide (ORTCHAR_T = wchar_t) path.
const std::wstring wide(src.path.begin(), src.path.end());
return Ort::Session(env, wide.c_str(), options);
#else
return Ort::Session(env, src.path.c_str(), options);
#endif
}
} // namespace ANSCENTER
#endif // ANSMODELSOURCE_H
+7 -7
View File
@@ -55,6 +55,11 @@ namespace ANSCENTER
return std::accumulate(vector.begin(), vector.end(), 1LL, std::multiplies<int64_t>());
}
bool ANSONNXCL::Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU)
{
return Init(ModelSource::File(modelPath), targetInputShape, useGPU);
}
bool ANSONNXCL::Init(const ModelSource& model, const cv::Size& targetInputShape, bool useGPU)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
@@ -198,12 +203,7 @@ namespace ANSCENTER
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session_ = Ort::Session(env_, w_modelPath.c_str(), sessionOptions_);
#else
session_ = Ort::Session(env_, modelPath.c_str(), sessionOptions_);
#endif
session_ = MakeSession(env_, model, sessionOptions_);
Ort::AllocatorWithDefaultOptions allocator;
@@ -300,7 +300,7 @@ namespace ANSCENTER
std::cout << "Warning: No class names loaded. Predictions will use numeric IDs." << std::endl;
}
std::cout << "[ANSONNXCL] Initialized successfully. Model: " << modelPath << std::endl;
std::cout << "[ANSONNXCL] Initialized successfully. Model: " << ModelSourceName(model) << std::endl;
// ── Warmup ──────────────────────────────────────────────────────────
DEBUG_PRINT("Starting model warmup...");
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXCL_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
#include <memory>
@@ -65,6 +66,8 @@ namespace ANSCENTER {
const std::string& strategy = "resize");
void warmupModel();
bool Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU = true);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, const cv::Size& targetInputShape, bool useGPU = true);
bool preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() (the
// ORT API that doesn't hang on AMD DirectML) requires a non-const
+6 -6
View File
@@ -686,6 +686,11 @@ namespace ANSCENTER {
}
}
bool ANSONNXOBB::Init(const std::string& modelPath, bool useGPU, int deviceId)
{
return Init(ModelSource::File(modelPath), useGPU, deviceId);
}
bool ANSONNXOBB::Init(const ModelSource& model, bool useGPU, int deviceId)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
@@ -834,12 +839,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXOBB_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
namespace ANSCENTER {
@@ -73,6 +74,8 @@ namespace ANSCENTER {
void warmupModel();
bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0);
cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() requires
// a non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+6 -6
View File
@@ -270,6 +270,11 @@ namespace ANSCENTER {
}
bool ANSONNXPOSE::Init(const std::string& modelPath, bool useGPU, int deviceId)
{
return Init(ModelSource::File(modelPath), useGPU, deviceId);
}
bool ANSONNXPOSE::Init(const ModelSource& model, bool useGPU, int deviceId)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
@@ -418,12 +423,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXPOSE_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
namespace ANSCENTER {
@@ -40,6 +41,8 @@ namespace ANSCENTER {
float kptThreshold = 0.5);
void warmupModel();
bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0);
cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() requires
// a non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+6 -6
View File
@@ -267,6 +267,11 @@ namespace ANSCENTER {
}
}
bool ANSONNXSEG::Init(const std::string& modelPath, bool useGPU, int deviceId)
{
return Init(ModelSource::File(modelPath), useGPU, deviceId);
}
bool ANSONNXSEG::Init(const ModelSource& model, bool useGPU, int deviceId)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
@@ -415,12 +420,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
std::wstring w_modelPath = std::wstring(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
session = MakeSession(env, model, sessionOptions);
numInputNodes = session.GetInputCount();
numOutputNodes = session.GetOutputCount();
+3
View File
@@ -2,6 +2,7 @@
#define ANSONNXSEG_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
namespace ANSCENTER {
@@ -50,6 +51,8 @@ namespace ANSCENTER {
void warmupModel();
bool Init(const std::string& modelPath, bool useGPU=true, int deviceId = 0);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, bool useGPU = true, int deviceId = 0);
cv::Mat preprocess(const cv::Mat& image,float*& blobPtr,std::vector<int64_t>& inputTensorShape);
// outputs is non-const because GetTensorMutableData() requires a
// non-const Ort::Value receiver — see ANSONNXCL.h for full note.
+6 -6
View File
@@ -278,6 +278,11 @@ namespace ANSCENTER {
}
}
bool YOLO12OD::loadModel(const std::string& modelPath, bool useGPU)
{
return loadModel(ModelSource::File(modelPath), useGPU);
}
bool YOLO12OD::loadModel(const ModelSource& model, bool useGPU)
{
try {
const auto& ep = ANSCENTER::EPLoader::Current();
@@ -418,12 +423,7 @@ namespace ANSCENTER {
}
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
std::wstring w_modelPath(modelPath.begin(), modelPath.end());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
#else
session = Ort::Session(env, modelPath.c_str(), sessionOptions);
#endif
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
+3
View File
@@ -2,6 +2,7 @@
#define ANSYOL12OOD_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include <onnxruntime_cxx_api.h>
#include <algorithm>
#include <fstream>
@@ -46,6 +47,8 @@ namespace ANSCENTER {
float m_imgHeight = 0;
protected:
bool loadModel(const std::string& modelPath, bool useGPU = true);
// Same load, from a model already in memory (an opened package).
bool loadModel(const ModelSource& model, bool useGPU = true);
std::vector<Object> detect(const cv::Mat& image, float confThreshold = 0.4f, float iouThreshold = 0.45f);
//cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
cv::Mat preprocess(const cv::Mat& image, std::vector<float>& blob, std::vector<int64_t>& inputTensorShape);
+6 -2
View File
@@ -205,6 +205,11 @@ namespace ANSCENTER
}
}
bool YOLOOD::loadYoloModel(const std::string& modelPath, const bool& isGPU, const cv::Size& inputSize)
{
return loadYoloModel(ModelSource::File(modelPath), isGPU, inputSize);
}
bool YOLOOD::loadYoloModel(const ModelSource& model, const bool& isGPU, const cv::Size& inputSize)
{
try {
const auto& ep = ANSCENTER::EPLoader::Current();
@@ -358,8 +363,7 @@ namespace ANSCENTER
}
// ── Load model ──────────────────────────────────────────────────────
std::wstring w_modelPath = String2WString(modelPath.c_str());
session = Ort::Session(env, w_modelPath.c_str(), sessionOptions);
session = MakeSession(env, model, sessionOptions);
Ort::AllocatorWithDefaultOptions allocator;
// ── Input shape ─────────────────────────────────────────────────────
+3
View File
@@ -2,6 +2,7 @@
#define ANSYOLOOD_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include <onnxruntime_cxx_api.h>
//#define USEOPENCVDNN
namespace ANSCENTER {
@@ -38,6 +39,8 @@ namespace ANSCENTER {
Ort::SessionOptions sessionOptions{ nullptr };
Ort::Session session{ nullptr };
bool loadYoloModel(const std::string& modelPath,const bool& isGPU = true,const cv::Size& inputSize = cv::Size(640, 640));
// Same load, from a model already in memory (an opened package).
bool loadYoloModel(const ModelSource& model, const bool& isGPU = true, const cv::Size& inputSize = cv::Size(640, 640));
std::vector<Object> detect(cv::Mat& image, const float& confThreshold, const float& iouThreshold);
void preprocessing(const cv::Mat& image,std::vector<float>& blob,std::vector<int64_t>& inputTensorShape);
//cv::Mat preprocessv11(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);