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
+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...");