feat(ODEngine): open a model package and run it without unpacking it
An engine's model has always been a path to a password-protected zip: ANSODBase inflates it into a folder, and every engine then picks out train_last.onnx, classes.names and model_config.json. A .ansmodel package replaces all four with one sealed file, and this is the part that turns it back into what the engines already understand. ANSODBase::LoadModel and ::Initialize now recognise a .ansmodel path and take a second route: the package is opened with the process keyring, the model is decrypted into memory, and the classes, task and input geometry come out of the manifest instead of loose files. Nothing is extracted, so the unprotected model never touches the disk — which is the reason the format exists, and it is one function's job to keep. The engines follow it down. BasicOrtHandler gains a constructor that takes the bytes rather than a path; it keeps a view of them, not a copy, because the weights are in RAM either way once a session exists and a copy would only double what every loaded model costs. The owner of the plaintext is ANSODBase, which holds it for as long as the engine object lives — that is what lets the DirectML recovery path rebuild a session after a driver fault — and wipes it on teardown. Wired through ANSONNXYOLO (and so the classification facade), ANSONNXOBB, ANSONNXPOSE, ANSONNXSEG and YOLO12OD. YOLOOD is left alone: it loads darknet weights and OpenCV DNN graphs, neither of which the writer produces. A zip still loads exactly as before, on every engine. Keys come from SetProcessModelKeys, as in ANSMINDCORE, with the same development fallback from the environment. With none set, a package is refused with a sentence saying so and every zip still works, so a build made before the key ceremony runs everything except packages. The engine layer needs CUDA to compile, so the contract it depends on is checked where it can be: ANSModelPackage-UnitTest now opens the fixture ANSMINDCORE wrote through the same glue ANSODBase uses, and covers the refusal a keyless build gives. 32 checks, no failures. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
#pragma once
|
||||
#include <set>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <json.hpp>
|
||||
#include "ANSODEngine.h"
|
||||
#include "ANSLicense.h" // ANS_DBG macro
|
||||
#include "ANSModelOpen.h"
|
||||
#include "OpenVINODeviceConfig.h"
|
||||
#include "ANSYOLOOD.h"
|
||||
#include "ANSTENSORRTOD.h"
|
||||
@@ -61,6 +63,93 @@ namespace ANSCENTER
|
||||
this->_logger.LogFatal("ANSODBase::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
// ────────────────────────────────────────────────────────────────────────
|
||||
// Model packages (.ansmodel)
|
||||
//
|
||||
// A package carries what the zip's loose files carried — the model, the
|
||||
// class names, the task and the input geometry — sealed to a key only
|
||||
// ANSCORE, ANSMINDCORE and the engines hold. Opening one fills in the
|
||||
// same members the inflated folder used to fill in, with one difference
|
||||
// that is the point of the whole format: the model stays in memory.
|
||||
// ────────────────────────────────────────────────────────────────────────
|
||||
bool ANSODBase::IsModelPackagePath(const std::string& path) {
|
||||
return ANSCENTER::modelpkg::IsPackagePath(path);
|
||||
}
|
||||
|
||||
void ANSODBase::WipeModelPackage() {
|
||||
if (!_packageModel.empty()) {
|
||||
std::memset(_packageModel.data(), 0, _packageModel.size());
|
||||
_packageModel.clear();
|
||||
_packageModel.shrink_to_fit();
|
||||
}
|
||||
_fromPackage = false;
|
||||
_packageInputWidth = 0;
|
||||
_packageInputHeight = 0;
|
||||
}
|
||||
|
||||
bool ANSODBase::LoadModelPackage(const std::string& path) {
|
||||
try {
|
||||
WipeModelPackage();
|
||||
ANSCENTER::modelpkg::OpenedModel opened;
|
||||
std::string err;
|
||||
if (!ANSCENTER::modelpkg::OpenModelPackage(path, opened, err)) {
|
||||
this->_logger.LogError("ANSODBase::LoadModelPackage", err, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
if (!opened.warning.empty()) {
|
||||
// An expired release certificate warns and loads, by design:
|
||||
// a model that worked yesterday keeps working on a box that
|
||||
// has been offline for a year.
|
||||
this->_logger.LogWarn("ANSODBase::LoadModelPackage", opened.warning, __FILE__, __LINE__);
|
||||
}
|
||||
_packageModel = std::move(opened.model);
|
||||
if (_packageModel.empty()) {
|
||||
this->_logger.LogError("ANSODBase::LoadModelPackage",
|
||||
"The package carries no model", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
_fromPackage = true;
|
||||
_packageTask = opened.task;
|
||||
_packageFamily = opened.family;
|
||||
_packageMarker = opened.legacyMarker;
|
||||
_packageName = opened.name;
|
||||
|
||||
// The classes come out in order, so there is nothing to parse and
|
||||
// no classes.names to look for.
|
||||
if (!opened.classes.empty()) _classes = opened.classes;
|
||||
_packageInputWidth = opened.inputWidth;
|
||||
_packageInputHeight = opened.inputHeight;
|
||||
ApplyPackageInputSize();
|
||||
|
||||
// The manifest says in a word what the zip said by the presence of
|
||||
// a marker file.
|
||||
if (opened.task == "classification") _modelConfig.detectionType = ANSCENTER::DetectionType::CLASSIFICATION;
|
||||
else if (opened.task == "segmentation") _modelConfig.detectionType = ANSCENTER::DetectionType::SEGMENTATION;
|
||||
else if (opened.task == "obb") _modelConfig.detectionType = ANSCENTER::DetectionType::OBB;
|
||||
else if (opened.task == "pose") _modelConfig.detectionType = ANSCENTER::DetectionType::KEYPOINT;
|
||||
else _modelConfig.detectionType = ANSCENTER::DetectionType::DETECTION;
|
||||
|
||||
// There is no folder and no config file; every engine below tests
|
||||
// for these, and must find them empty rather than stale.
|
||||
_modelFolder.clear();
|
||||
_modelConfigFile.clear();
|
||||
_classFilePath.clear();
|
||||
|
||||
this->_logger.LogDebug("ANSODBase::LoadModelPackage",
|
||||
"Opened " + path + " (" + opened.task + "/" + opened.family + ", "
|
||||
+ std::to_string(_classes.size()) + " classes, "
|
||||
+ std::to_string(_packageModel.size()) + " bytes)", __FILE__, __LINE__);
|
||||
|
||||
// Auto-enable tracker + stabilization by default (BYTETRACK)
|
||||
SetTracker(TrackerType::BYTETRACK, true);
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSODBase::LoadModelPackage", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSODBase::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)
|
||||
{
|
||||
try {
|
||||
@@ -69,6 +158,7 @@ namespace ANSCENTER
|
||||
_modelConfigFile = "";
|
||||
_modelFolder.clear();
|
||||
_modelConfigFile.clear();
|
||||
WipeModelPackage(); // whatever was loaded before, package or not
|
||||
// 0. Check if the modelZipFilePath exist?
|
||||
if (!FileExist(modelZipFilePath)) {
|
||||
if (modelZipFilePath.empty()) {// Support motion detector that does not need model file
|
||||
@@ -82,6 +172,10 @@ namespace ANSCENTER
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 0b. A model package replaces the zip entirely: it is opened in
|
||||
// memory, and no password, no folder and no loose files are involved.
|
||||
if (IsModelPackagePath(modelZipFilePath))
|
||||
return LoadModelPackage(modelZipFilePath);
|
||||
// 1. Unzip model zip file to a special location with folder name as model file (and version)
|
||||
std::string outputFolder;
|
||||
std::vector<std::string> passwordArray;
|
||||
@@ -126,6 +220,7 @@ namespace ANSCENTER
|
||||
_modelFolder.clear();
|
||||
_modelConfigFile.clear();
|
||||
_modelConfig = modelConfig;
|
||||
WipeModelPackage(); // whatever was loaded before, package or not
|
||||
CheckLicense();
|
||||
if (!_licenseValid) {
|
||||
this->_logger.LogError("ANSODBase::Initialize", "Invalid License", __FILE__, __LINE__);
|
||||
@@ -145,6 +240,12 @@ namespace ANSCENTER
|
||||
}
|
||||
|
||||
}
|
||||
// 0b. A model package replaces the zip entirely; see LoadModel.
|
||||
if (IsModelPackagePath(modelZipFilePath)) {
|
||||
if (!LoadModelPackage(modelZipFilePath)) return false;
|
||||
_isInitialized = true;
|
||||
return true;
|
||||
}
|
||||
// 1. Unzip model zip file to a special location with folder name as model file (and version)
|
||||
std::string outputFolder;
|
||||
std::vector<std::string> passwordArray;
|
||||
@@ -952,6 +1053,10 @@ namespace ANSCENTER
|
||||
|
||||
ANSODBase::~ANSODBase() {
|
||||
try {
|
||||
// A decrypted model is what the package format exists to protect.
|
||||
// This runs after the derived destructor has dropped the session,
|
||||
// so nothing is left holding a view of these bytes.
|
||||
WipeModelPackage();
|
||||
// Per-camera trackers are released by CameraData::clear()
|
||||
if (!_cameras.empty())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user