Fix AMD ALPR_OCR and custom model

This commit is contained in:
2026-05-01 07:53:18 +10:00
parent e1f37ec767
commit 91efbb9a8f
25 changed files with 2841 additions and 429 deletions
+113 -197
View File
@@ -1,4 +1,5 @@
#include"ANSONNXCL.h"
#include "ANSONNXYOLO.h" // ANSONNXYOLO is the delegate target — see facade comment in ANSONNXCL.h
#include "EPLoader.h"
#include "OpenVINODeviceConfig.h"
#include <typeinfo> // typeid(e).name() in ANS_DBG diagnostics
@@ -876,9 +877,50 @@ namespace ANSCENTER
);
}
catch (const Ort::Exception& e) {
const std::string msg = e.what();
this->_logger.LogError("ANSONNXCL::classify",
"ONNX Runtime Exception during Run(): " + std::string(e.what()), __FILE__, __LINE__);
"ONNX Runtime Exception during Run(): " + msg, __FILE__, __LINE__);
delete[] blobPtr; // Clean up on error
blobPtr = nullptr;
// DML device-lost / DML graph-execution failure → tear down the
// dead DirectML session and rebuild on CPU. Without this, every
// subsequent classify() call hits the same dead session and
// floods the event log (16k+ entries observed on AMD/RDP).
if (IsDmlFatalError(msg) && !_dmlDeviceLost) {
_dmlDeviceLost = true;
_logger.LogFatal("ANSONNXCL::classify",
std::string("DirectML fatal error — recreating session on CPU. msg=")
+ msg.substr(0, 256),
__FILE__, __LINE__);
ANS_DBG("ANSONNXCL", "DML fatal — rebuild on CPU msg=%s", msg.c_str());
try {
// Order matters: drop AllocatedStringPtrs (which depend
// on the live session's allocator) BEFORE tearing down
// session_, sessionOptions_, env_. Init() will repopulate.
inputNodeNameAllocatedStrings_.clear();
outputNodeNameAllocatedStrings_.clear();
inputNames_.clear();
outputNames_.clear();
session_ = Ort::Session{ nullptr };
sessionOptions_ = Ort::SessionOptions{ nullptr };
env_ = Ort::Env{ nullptr };
if (Init(_modelFilePath, inputImageShape_, /*useGPU=*/false)) {
_logger.LogInfo("ANSONNXCL::classify",
"CPU fallback session created successfully",
__FILE__, __LINE__);
ANS_DBG("ANSONNXCL", "CPU fallback OK");
} else {
_logger.LogFatal("ANSONNXCL::classify",
"CPU fallback session creation failed",
__FILE__, __LINE__);
}
} catch (const std::exception& re) {
_logger.LogFatal("ANSONNXCL::classify",
std::string("CPU fallback exception: ") + re.what(),
__FILE__, __LINE__);
}
}
return {};
}
@@ -922,216 +964,90 @@ namespace ANSCENTER
return {};
}
}
bool ANSONNXCL::OptimizeModel(bool fp16, std::string & optimizedModelFolder) {
if (!ANSODBase::OptimizeModel(fp16, optimizedModelFolder)) {
return false;
}
return true;
}
bool ANSONNXCL::Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
ModelLoadingGuard mlg(_modelLoading);
try {
_modelLoadValid = false;
bool result = ANSODBase::Initialize(licenseKey, modelConfig, modelZipFilePath, modelZipPassword, labelMap);
if (!result) return false;
// Parsing for YOLO only here
_modelConfig = modelConfig;
_modelConfig.detectionType = ANSCENTER::DetectionType::CLASSIFICATION;
_modelConfig.modelType = ModelType::ONNXCL;
_modelConfig.inpHeight = 224;
_modelConfig.inpWidth = 224;
if (_modelConfig.modelMNSThreshold < 0.2)
_modelConfig.modelMNSThreshold = 0.5;
if (_modelConfig.modelConfThreshold < 0.2)
_modelConfig.modelConfThreshold = 0.5;
if (_modelConfig.numKPS <= 0 || _modelConfig.numKPS > 133) // 133 = COCO wholebody max
_modelConfig.numKPS = 17;
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = (modelConfig.precisionType == PrecisionType::FP16);
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];
}
// ────────────────────────────────────────────────────────────────────────
// Public-method facade: delegate everything to the wrapped ANSONNXYOLO.
//
// This is a verification harness — ANSONNXYOLO already covers the
// classification path (auto-detected from model output shape) and ships
// with the safe DML config + DML-fatal-error recovery. By routing every
// ANSONNXCL public call through ANSONNXYOLO we can A/B test whether
// ANSONNXCL's own session/postprocess logic is the actual cause of the
// AMD same-process DML races we saw with the custom helmet model.
//
// The dead code below the facade (Init / preprocess / postprocess /
// classify / Ort::* members in the header) is intentionally preserved
// so the original implementation is one git-revert away if needed.
// ────────────────────────────────────────────────────────────────────────
namespace {
// Lazy-create the delegate on first public call. Not thread-safe in
// the strict sense, but the public methods all take _mutex first via
// the underlying ANSODBase, so concurrent first-time creation is
// serialised in practice.
inline ANSONNXYOLO& GetDelegate(std::unique_ptr<ANSONNXYOLO>& slot) {
if (!slot) {
slot = std::make_unique<ANSONNXYOLO>();
ANS_DBG("ANSONNXCL_Facade",
"delegate created (ANSONNXYOLO=%p)", (void*)slot.get());
}
else {// This is old version of model zip file
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValidFileName(_classFilePath);
if (!isValidFileName)
{
this->_logger.LogDebug("ANSONNXCL::Initialize. Load classes from string", _classFilePath, __FILE__, __LINE__);
LoadClassesFromString();
}
else {
this->_logger.LogDebug("ANSONNXCL::Initialize. Load classes from file", _classFilePath, __FILE__, __LINE__);
LoadClassesFromFile();
}
}
// 1. Load labelMap and engine
labelMap.clear();
if (!_classes.empty())
labelMap = VectorToCommaSeparatedString(_classes);
// 2. Initialize ONNX Runtime session
cv::Size targetInputShape= cv::Size(_modelConfig.inpWidth, _modelConfig.inpHeight);
result = Init(_modelFilePath, targetInputShape,true);
_modelLoadValid = true;
_isInitialized = true;
return result;
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSONNXCL::Initialize", e.what(), __FILE__, __LINE__);
return false;
return *slot;
}
}
bool ANSONNXCL::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
ModelLoadingGuard mlg(_modelLoading);
try {
bool result = ANSODBase::LoadModel(modelZipFilePath, modelZipPassword);
if (!result) return false;
_modelConfig.detectionType = ANSCENTER::DetectionType::CLASSIFICATION;
_modelConfig.modelType = ModelType::TENSORRT;
_modelConfig.inpHeight = 224;
_modelConfig.inpWidth = 224;
if (_modelConfig.modelMNSThreshold < 0.2)
_modelConfig.modelMNSThreshold = 0.5;
if (_modelConfig.modelConfThreshold < 0.2)
_modelConfig.modelConfThreshold = 0.5;
if (_modelConfig.numKPS <= 0 || _modelConfig.numKPS > 133) // 133 = COCO wholebody max
_modelConfig.numKPS = 17;
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
// if (_modelConfig.precisionType == PrecisionType::FP16)_fp16 = true;
_fp16 = true; // Load Model from Here
// 0. Check if the configuration file exist
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 {// This is old version of model zip file
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValidFileName(_classFilePath);
if (!isValidFileName)
{
this->_logger.LogDebug("ANSONNXCL::Initialize. Load classes from string", _classFilePath, __FILE__, __LINE__);
LoadClassesFromString();
}
else {
this->_logger.LogDebug("ANSONNXCL::Initialize. Load classes from file", _classFilePath, __FILE__, __LINE__);
LoadClassesFromFile();
}
}
// Initialize ONNX Runtime session
cv::Size targetInputShape = cv::Size(_modelConfig.inpWidth, _modelConfig.inpHeight);
result = Init(_modelFilePath, targetInputShape,true);
_modelLoadValid = true;
_isInitialized = true;
return result;
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSONNXCL::LoadModel", e.what(), __FILE__, __LINE__);
return false;
}
bool ANSONNXCL::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
ANS_DBG("ANSONNXCL_Facade", "OptimizeModel → ANSONNXYOLO");
return GetDelegate(_yoloDelegate).OptimizeModel(fp16, optimizedModelFolder);
}
bool ANSONNXCL::LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
ModelLoadingGuard mlg(_modelLoading);
try {
bool result = ANSODBase::LoadModelFromFolder(licenseKey, modelConfig, modelName, className, modelFolder, labelMap);
if (!result) return false;
std::string _modelName = modelName;
if (_modelName.empty()) {
_modelName = "train_last";
}
std::string modelFullName = _modelName + ".onnx";
// Parsing for YOLO only here
_modelConfig = modelConfig;
_modelConfig.detectionType = ANSCENTER::DetectionType::CLASSIFICATION;
_modelConfig.modelType = ModelType::TENSORRT;
_modelConfig.inpHeight = 224;
_modelConfig.inpWidth = 224;
if (_modelConfig.modelMNSThreshold < 0.2)
_modelConfig.modelMNSThreshold = 0.5;
if (_modelConfig.modelConfThreshold < 0.2)
_modelConfig.modelConfThreshold = 0.5;
if (_modelConfig.numKPS <= 0 || _modelConfig.numKPS > 133) // 133 = COCO wholebody max
_modelConfig.numKPS = 17;
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = true; // Load Model from Here
// 0. Check if the configuration file exist
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 {// This is old version of model zip file
_modelFilePath = CreateFilePath(_modelFolder, modelFullName);
_classFilePath = CreateFilePath(_modelFolder, className);
std::ifstream isValidFileName(_classFilePath);
if (!isValidFileName)
{
this->_logger.LogDebug("ANSONNXCL::Initialize. Load classes from string", _classFilePath, __FILE__, __LINE__);
LoadClassesFromString();
}
else {
this->_logger.LogDebug("ANSONNXCL::Initialize. Load classes from file", _classFilePath, __FILE__, __LINE__);
LoadClassesFromFile();
}
}
// 1. Load labelMap and engine
labelMap.clear();
if (!_classes.empty())
labelMap = VectorToCommaSeparatedString(_classes);
// 2. Initialize ONNX Runtime session
cv::Size targetInputShape = cv::Size(_modelConfig.inpWidth, _modelConfig.inpHeight);
result = Init(_modelFilePath, targetInputShape,true);
_modelLoadValid = true;
_isInitialized = true;
return result;
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSONNXCL::LoadModelFromFolder", e.what(), __FILE__, __LINE__);
return false;
}
bool ANSONNXCL::Initialize(std::string licenseKey, ModelConfig modelConfig,
const std::string& modelZipFilePath,
const std::string& modelZipPassword,
std::string& labelMap) {
ANS_DBG("ANSONNXCL_Facade", "Initialize → ANSONNXYOLO zip=%s",
modelZipFilePath.c_str());
return GetDelegate(_yoloDelegate).Initialize(
licenseKey, modelConfig, modelZipFilePath, modelZipPassword, labelMap);
}
bool ANSONNXCL::LoadModel(const std::string& modelZipFilePath,
const std::string& modelZipPassword) {
ANS_DBG("ANSONNXCL_Facade", "LoadModel → ANSONNXYOLO zip=%s",
modelZipFilePath.c_str());
return GetDelegate(_yoloDelegate).LoadModel(modelZipFilePath, modelZipPassword);
}
bool ANSONNXCL::LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig,
std::string modelName, std::string className,
const std::string& modelFolder,
std::string& labelMap) {
ANS_DBG("ANSONNXCL_Facade",
"LoadModelFromFolder → ANSONNXYOLO modelName=%s folder=%s",
modelName.c_str(), modelFolder.c_str());
return GetDelegate(_yoloDelegate).LoadModelFromFolder(
licenseKey, modelConfig, modelName, className, modelFolder, labelMap);
}
bool ANSONNXCL::Destroy() {
return true;
if (_yoloDelegate) {
ANS_DBG("ANSONNXCL_Facade", "Destroy → ANSONNXYOLO");
return _yoloDelegate->Destroy();
}
return true;
}
ANSONNXCL::~ANSONNXCL() {
// Explicit Destroy() so the delegate's session is torn down before
// the unique_ptr destructor frees the object. unique_ptr<ANSONNXYOLO>
// still releases the heap memory afterwards.
Destroy();
}
std::vector<Object> ANSONNXCL::RunInference(const cv::Mat& input, const std::string& camera_id) {
if (!PreInferenceCheck("ANSONNXCL::RunInference")) return {};
try {
std::vector<Object> result;
if (input.empty()) return result;
if ((input.cols < 5) || (input.rows < 5)) return result;
return classify(input, camera_id);
}
catch (const std::exception& e) {
this->_logger.LogFatal("ANSONNXCL::RunInference", e.what(), __FILE__, __LINE__);
return {};
}
std::vector<Object> ANSONNXCL::RunInference(const cv::Mat& input,
const std::string& camera_id) {
if (input.empty()) return {};
if (input.cols < 5 || input.rows < 5) return {};
return GetDelegate(_yoloDelegate).RunInference(input, camera_id);
}
std::vector<Object> ANSONNXCL::RunInference(const cv::Mat& inputImgBGR) {
return RunInference(inputImgBGR, "CustomCam");
}