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
+196 -23
View File
@@ -2,6 +2,7 @@
#include "include/clipper.h"
#include "ANSGpuFrameRegistry.h"
#include "NV12PreprocessHelper.h"
#include "ANSLicense.h" // ANS_DBG
#include <opencv2/imgproc.hpp>
#include <iostream>
@@ -12,6 +13,130 @@
namespace ANSCENTER {
namespace onnxocr {
// AMD/DirectML latency-based recreate (Path B) — record helper. See
// ONNXOCRRecognizer for the rationale. Arms _slowRecreatePending if
// a Run() exceeded the slow threshold while past warmup and outside
// the cooldown window.
void ONNXOCRDetector::RecordRunLatency(const char* context, double ms) {
if (ms <= kSlowRecreateThresholdMs) return;
if (!_warmedUp) return;
const auto now = std::chrono::steady_clock::now();
const auto sinceLast = std::chrono::duration_cast<std::chrono::milliseconds>(
now - _lastRecreateAt).count();
if (_lastRecreateAt.time_since_epoch().count() != 0 &&
sinceLast < kRecreateCooldownMs) {
ANS_DBG("ANSONNXDet_run",
"%s: slow Run (%.0f ms) but in cooldown (%lldms)",
context, ms, static_cast<long long>(sinceLast));
return;
}
_slowRecreatePending.store(true, std::memory_order_release);
std::cerr << "[ONNXOCRDetector] " << context
<< ": slow Run (" << ms
<< " ms > threshold) — flagged for proactive recreate\n";
ANS_DBG("ANSONNXDet_run",
"%s: slow Run %.0fms > %.0fms — armed proactive recreate",
context, ms, kSlowRecreateThresholdMs);
}
// AMD/DirectML latency-based recreate (Path B) — apply helper. Called
// at entry of public methods. If the slow flag was armed by a prior
// call's RecordRunLatency, swap the session in here so this call uses
// the fresh one.
void ONNXOCRDetector::ApplyPendingSlowRecreate(const char* context) {
if (!_slowRecreatePending.exchange(false, std::memory_order_acquire)) {
return;
}
const EngineType currentEp = getEngineType();
std::cerr << "[ONNXOCRDetector] " << context
<< ": proactive recreate on EP=" << static_cast<int>(currentEp)
<< " (latency-triggered)\n";
ANS_DBG("ANSONNXDet_run",
"%s: proactive recreate on EP=%d", context,
static_cast<int>(currentEp));
try {
Reinitialize(currentEp);
_warmedUp = false;
_lastRecreateAt = std::chrono::steady_clock::now();
std::cerr << "[ONNXOCRDetector] " << context
<< ": proactive recreate OK\n";
ANS_DBG("ANSONNXDet_run", "%s: proactive recreate OK", context);
} catch (const std::exception& e) {
std::cerr << "[ONNXOCRDetector] " << context
<< ": proactive recreate failed: " << e.what() << "\n";
ANS_DBG("ANSONNXDet_run",
"%s: proactive recreate failed (%s)", context, e.what());
} catch (...) {
std::cerr << "[ONNXOCRDetector] " << context
<< ": proactive recreate failed (unknown)\n";
}
}
// AMD/DirectML self-heal helper. See ONNXOCRRecognizer for the full
// rationale. On any Run() failure: recreate session on the same EP
// first (transient driver hiccup recovery — the common case for AMD
// DML 0x80004005 E_FAIL); on a second failure, fall back to CPU EP.
// The current call's result is already lost — caller returns empty;
// the recovered session is used by the NEXT Detect call.
void ONNXOCRDetector::HandleRunFailureAndRecreate(const char* context,
const std::exception& e) {
const EngineType currentEp = getEngineType();
std::cerr << "[ONNXOCRDetector] " << context
<< ": Run() failed on EP=" << static_cast<int>(currentEp)
<< ": " << e.what() << " — recreating session\n";
ANS_DBG("ANSONNXDet_run",
"%s: Run failed EP=%d msg=%.200s — recreating",
context, static_cast<int>(currentEp), e.what());
// Option A: trip process-wide DML serial mode on AMD exception
// (idempotent, no-op on Intel/NVIDIA/CPU).
if (currentEp == EngineType::AMD_GPU) {
ANSCENTER::DmlSerialState::TripFromException(
context, e.what());
}
if (currentEp != EngineType::CPU) {
try {
Reinitialize(currentEp);
_warmedUp = false;
std::cerr << "[ONNXOCRDetector] " << context
<< ": session recreated on EP=" << static_cast<int>(currentEp) << "\n";
ANS_DBG("ANSONNXDet_run",
"%s: session recreated on EP=%d", context,
static_cast<int>(currentEp));
return;
} catch (const std::exception& reErr) {
std::cerr << "[ONNXOCRDetector] " << context
<< ": same-EP recreate failed: " << reErr.what()
<< " — falling back to CPU\n";
ANS_DBG("ANSONNXDet_run",
"%s: same-EP recreate failed (%s); falling back to CPU",
context, reErr.what());
} catch (...) {
std::cerr << "[ONNXOCRDetector] " << context
<< ": same-EP recreate failed (unknown) — falling back to CPU\n";
}
}
try {
Reinitialize(EngineType::CPU);
_warmedUp = false;
std::cerr << "[ONNXOCRDetector] " << context
<< ": fallback session created on CPU\n";
ANS_DBG("ANSONNXDet_run", "%s: CPU fallback OK", context);
} catch (const std::exception& cpuErr) {
std::cerr << "[ONNXOCRDetector] " << context
<< ": CPU fallback ALSO failed: " << cpuErr.what()
<< " — ort_session may be null, future calls will return empty\n";
ANS_DBG("ANSONNXDet_run",
"%s: CPU fallback failed (%s); ort_session may be null",
context, cpuErr.what());
} catch (...) {
std::cerr << "[ONNXOCRDetector] " << context
<< ": CPU fallback ALSO failed (unknown)\n";
}
}
ONNXOCRDetector::ONNXOCRDetector(const std::string& onnx_path, unsigned int num_threads)
: BasicOrtHandler(onnx_path, num_threads) {
}
@@ -55,6 +180,11 @@ std::vector<TextBox> ONNXOCRDetector::Detect(const cv::Mat& srcImage,
return {};
}
// Path B (AMD/DML): apply pending proactive recreate before constructing
// any tensors. See ONNXOCRRecognizer for the full rationale.
ApplyPendingSlowRecreate("Detect");
if (!ort_session) return {}; // recreate may have failed; bail safely
// Try to get full-resolution image from NV12 frame (same pattern as ANSONNXYOLO)
cv::Mat inferenceImage = srcImage;
float bgrScaleX = 1.0f, bgrScaleY = 1.0f;
@@ -89,36 +219,60 @@ std::vector<TextBox> ONNXOCRDetector::Detect(const cv::Mat& srcImage,
*memory_info_handler, inputData.data(), inputData.size(),
inputShape.data(), inputShape.size());
// Run inference
auto outputTensors = ort_session->Run(
Ort::RunOptions{ nullptr },
input_node_names.data(), &inputTensor, 1,
output_node_names.data(), num_outputs);
// Run inference + post-process inside try/catch. AMD/DirectML
// self-heal: on Run() failure (typically 0x80004005 E_FAIL from a
// transient DML driver hiccup) we recreate the session for the next
// call (same EP first, CPU as last resort). This call's result is
// lost — return empty boxes; ALPR pipeline tolerates a dropped
// detection frame, the tracker recovers. Latency is also recorded
// for the Path B proactive-recreate trigger.
try {
const auto t0 = std::chrono::steady_clock::now();
std::vector<Ort::Value> outputTensors;
{
// Option A: serialize DML inference once per-process
// serial mode has been tripped (no-op on Intel/NVIDIA/CPU).
ANSCENTER::DmlInferenceGuard dmlGuard(
m_engineType, "ONNXOCRDetector::Detect");
outputTensors = ort_session->Run(
Ort::RunOptions{ nullptr },
input_node_names.data(), &inputTensor, 1,
output_node_names.data(), num_outputs);
}
const auto t1 = std::chrono::steady_clock::now();
const double runMs =
std::chrono::duration<double, std::milli>(t1 - t0).count();
RecordRunLatency("Detect", runMs);
// Get output data
float* outputData = outputTensors[0].GetTensorMutableData<float>();
auto outputShape = outputTensors[0].GetTensorTypeAndShapeInfo().GetShape();
// Get output data
float* outputData = outputTensors[0].GetTensorMutableData<float>();
auto outputShape = outputTensors[0].GetTensorTypeAndShapeInfo().GetShape();
int outH = static_cast<int>(outputShape[2]);
int outW = static_cast<int>(outputShape[3]);
int outH = static_cast<int>(outputShape[2]);
int outW = static_cast<int>(outputShape[3]);
// Postprocess — detection coords are relative to inferenceImage (full-res),
// then scaled back to srcImage (display-res) coordinates
auto boxes = Postprocess(outputData, outH, outW, ratioH, ratioW,
inferenceImage.rows, inferenceImage.cols,
dbThresh, boxThresh, unclipRatio, useDilation);
// Postprocess — detection coords are relative to inferenceImage (full-res),
// then scaled back to srcImage (display-res) coordinates
auto boxes = Postprocess(outputData, outH, outW, ratioH, ratioW,
inferenceImage.rows, inferenceImage.cols,
dbThresh, boxThresh, unclipRatio, useDilation);
// Rescale box coordinates from full-res to display-res
if (bgrScaleX != 1.0f || bgrScaleY != 1.0f) {
for (auto& box : boxes) {
for (auto& pt : box.points) {
pt.x *= bgrScaleX;
pt.y *= bgrScaleY;
// Rescale box coordinates from full-res to display-res
if (bgrScaleX != 1.0f || bgrScaleY != 1.0f) {
for (auto& box : boxes) {
for (auto& pt : box.points) {
pt.x *= bgrScaleX;
pt.y *= bgrScaleY;
}
}
}
}
return boxes;
return boxes;
}
catch (const Ort::Exception& e) {
HandleRunFailureAndRecreate("Detect", e);
return {};
}
}
std::vector<float> ONNXOCRDetector::Preprocess(const cv::Mat& srcImage, int maxSideLen,
@@ -323,6 +477,8 @@ void ONNXOCRDetector::Warmup() {
// multiple-of-32 rounding. cuDNN caches the algorithm for this
// shape so the first real inference doesn't pay the picker cost.
constexpr int kWarmupSide = 320;
bool warmupFailed = false;
std::string warmupErr;
try {
cv::Mat dummy(kWarmupSide, kWarmupSide, CV_8UC3, cv::Scalar(128, 128, 128));
cv::Mat dummyF;
@@ -347,6 +503,23 @@ void ONNXOCRDetector::Warmup() {
}
catch (const Ort::Exception& e) {
std::cerr << "[ONNXOCRDetector] Warmup failed: " << e.what() << std::endl;
warmupFailed = true;
warmupErr = e.what();
}
// AMD/DML self-heal: if warmup itself threw, the session is dead and
// every Detect() will fail. Recreate now (same EP first, CPU as
// fallback) so production traffic can still serve. Leave _warmedUp
// = false so the recovered session warms up on its first real call.
if (warmupFailed) {
ANS_DBG("ANSONNXDet_run",
"Warmup failed (%.200s); triggering self-heal",
warmupErr.c_str());
try {
std::runtime_error proxy(warmupErr);
HandleRunFailureAndRecreate("Warmup", proxy);
} catch (...) {}
return; // do NOT set _warmedUp; recovered session warms on first real Detect
}
_warmedUp = true;
}