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:
@@ -41,6 +41,37 @@ namespace ANSCENTER {
|
||||
}
|
||||
}
|
||||
|
||||
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
|
||||
const std::string& logName, unsigned int _num_threads)
|
||||
: BasicOrtHandler(modelData, modelSize, logName, _num_threads)
|
||||
{
|
||||
if (input_node_dims.size() >= 4) {
|
||||
int h = static_cast<int>(input_node_dims[2]);
|
||||
int w = static_cast<int>(input_node_dims[3]);
|
||||
isDynamicInputShape = (h == -1 || w == -1);
|
||||
inputImageShape = isDynamicInputShape ? cv::Size(640, 640) : cv::Size(w, h);
|
||||
}
|
||||
else {
|
||||
inputImageShape = cv::Size(640, 640);
|
||||
}
|
||||
}
|
||||
|
||||
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
|
||||
const std::string& logName, EngineType engineType,
|
||||
unsigned int _num_threads)
|
||||
: BasicOrtHandler(modelData, modelSize, logName, engineType, _num_threads)
|
||||
{
|
||||
if (input_node_dims.size() >= 4) {
|
||||
int h = static_cast<int>(input_node_dims[2]);
|
||||
int w = static_cast<int>(input_node_dims[3]);
|
||||
isDynamicInputShape = (h == -1 || w == -1);
|
||||
inputImageShape = isDynamicInputShape ? cv::Size(640, 640) : cv::Size(w, h);
|
||||
}
|
||||
else {
|
||||
inputImageShape = cv::Size(640, 640);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// letterBox — Ultralytics-compatible LetterBox transform
|
||||
// ------------------------------------------------------------------
|
||||
@@ -1675,6 +1706,20 @@ namespace ANSCENTER {
|
||||
|
||||
bool ANSONNXYOLO::InitOrtEngine() {
|
||||
try {
|
||||
// A model that came out of an .ansmodel package has no file
|
||||
// behind it; the handler reads the bytes ANSODBase decrypted.
|
||||
// Those bytes outlive the handler, which is what lets the DML
|
||||
// recovery path below rebuild the session from them.
|
||||
if (_fromPackage) {
|
||||
if (_packageModel.empty()) {
|
||||
_logger.LogError("ANSONNXYOLO::InitOrtEngine",
|
||||
"The package carries no model", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
m_ortEngine = std::make_shared<class ONNXYOLO>(
|
||||
_packageModel.data(), _packageModel.size(), _packageName);
|
||||
return true;
|
||||
}
|
||||
if (!FileExist(_modelFilePath)) {
|
||||
_logger.LogError("ANSONNXYOLO::InitOrtEngine",
|
||||
"Model file does not exist: " + _modelFilePath, __FILE__, __LINE__);
|
||||
@@ -1691,6 +1736,16 @@ namespace ANSCENTER {
|
||||
|
||||
bool ANSONNXYOLO::InitOrtEngine(ANSCENTER::EngineType engineType) {
|
||||
try {
|
||||
if (_fromPackage) {
|
||||
if (_packageModel.empty()) {
|
||||
_logger.LogError("ANSONNXYOLO::InitOrtEngine",
|
||||
"The package carries no model", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
m_ortEngine = std::make_shared<class ONNXYOLO>(
|
||||
_packageModel.data(), _packageModel.size(), _packageName, engineType);
|
||||
return true;
|
||||
}
|
||||
if (!FileExist(_modelFilePath)) {
|
||||
_logger.LogError("ANSONNXYOLO::InitOrtEngine",
|
||||
"Model file does not exist: " + _modelFilePath, __FILE__, __LINE__);
|
||||
@@ -1846,21 +1901,30 @@ namespace ANSCENTER {
|
||||
NUM_KPS = _modelConfig.numKPS;
|
||||
KPS_THRESHOLD = _modelConfig.kpsThreshold;
|
||||
|
||||
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
|
||||
if (FileExist(_modelConfigFile)) {
|
||||
ModelType modelType;
|
||||
std::vector<int> inputShape;
|
||||
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
|
||||
if (inputShape.size() == 2) {
|
||||
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
|
||||
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
|
||||
// A package has already supplied the classes and the geometry;
|
||||
// there is no folder to look in.
|
||||
if (!_fromPackage) {
|
||||
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
|
||||
if (FileExist(_modelConfigFile)) {
|
||||
ModelType modelType;
|
||||
std::vector<int> inputShape;
|
||||
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
|
||||
if (inputShape.size() == 2) {
|
||||
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
|
||||
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
|
||||
}
|
||||
}
|
||||
else {
|
||||
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
|
||||
std::ifstream isValid(_classFilePath);
|
||||
if (!isValid) LoadClassesFromString();
|
||||
else LoadClassesFromFile();
|
||||
}
|
||||
}
|
||||
else {
|
||||
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
|
||||
std::ifstream isValid(_classFilePath);
|
||||
if (!isValid) LoadClassesFromString();
|
||||
else LoadClassesFromFile();
|
||||
// The defaults above have just overwritten what the package
|
||||
// said its input size was; put it back.
|
||||
ApplyPackageInputSize();
|
||||
}
|
||||
|
||||
labelMap.clear();
|
||||
@@ -1920,21 +1984,30 @@ namespace ANSCENTER {
|
||||
NUM_KPS = _modelConfig.numKPS;
|
||||
KPS_THRESHOLD = _modelConfig.kpsThreshold;
|
||||
|
||||
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
|
||||
if (FileExist(_modelConfigFile)) {
|
||||
ModelType modelType;
|
||||
std::vector<int> inputShape;
|
||||
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
|
||||
if (inputShape.size() == 2) {
|
||||
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
|
||||
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
|
||||
// A package has already supplied the classes and the geometry;
|
||||
// there is no folder to look in.
|
||||
if (!_fromPackage) {
|
||||
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
|
||||
if (FileExist(_modelConfigFile)) {
|
||||
ModelType modelType;
|
||||
std::vector<int> inputShape;
|
||||
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
|
||||
if (inputShape.size() == 2) {
|
||||
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
|
||||
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
|
||||
}
|
||||
}
|
||||
else {
|
||||
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
|
||||
std::ifstream isValid(_classFilePath);
|
||||
if (!isValid) LoadClassesFromString();
|
||||
else LoadClassesFromFile();
|
||||
}
|
||||
}
|
||||
else {
|
||||
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
|
||||
std::ifstream isValid(_classFilePath);
|
||||
if (!isValid) LoadClassesFromString();
|
||||
else LoadClassesFromFile();
|
||||
// The defaults above have just overwritten what the package
|
||||
// said its input size was; put it back.
|
||||
ApplyPackageInputSize();
|
||||
}
|
||||
|
||||
if (this->_loadEngineOnCreation) {
|
||||
|
||||
Reference in New Issue
Block a user