Fix ANSONNXYOLO:

1. core/ANSLicensingSystem/ANSLicense.h:268 — added default initializers to ModelConfig POD members (inpWidth = 0, inpHeight = 0, etc.). This was the actual bug: CreateANSODHandle declares a local ModelConfig and only sets a few fields, leaving inpWidth/inpHeight as indeterminate stack memory. On your machine that memory consistently held ~80, slipping past the <=0 → 640 guard and driving the ORT session at 80×80 — which crashes YOLOv11's /model.12/Concat because 2·ceil(80/32)=6 ≠ ceil(80/16)=5.

2. modules/ANSODEngine/ANSONNXYOLO.h:75 — ONNXYOLO::setInputShape() now snaps width/height up to the next multiple of 32 (with a 64-pixel floor). Defense in depth: even if a future caller hands in a non-stride-32 size, the FPN concat alignment is preserved.

3. modules/ANSODEngine/ANSONNXYOLO.cpp — two diagnostic additions:

WarmUpEngine bails with a clear log line if the warm-up dims aren't a multiple of 32, instead of letting the model.12 error fire and mask the real cause.
transform() logs the input shape, canvas shape, source size, and dynamic flag once per session in DebugView — so any future shape-mismatch crash points at the offending HxW directly.
This commit is contained in:
2026-05-01 20:24:08 +10:00
parent 1c8abf2a88
commit fc9cf9671b
4 changed files with 80 additions and 16 deletions
+29 -2
View File
@@ -116,6 +116,20 @@ namespace ANSCENTER {
const int width = canvas.cols;
const size_t imageSize = static_cast<size_t>(height) * width;
// Diagnostics — log the very first transform() per ONNXYOLO so a
// future ORT shape-mismatch failure (e.g. /model.12/Concat for
// non-stride-32 inputs) points at the offending HxW directly.
// One line per session — does not flood DebugView.
static thread_local const ONNXYOLO* s_loggedFirst = nullptr;
if (s_loggedFirst != this) {
s_loggedFirst = this;
ANS_DBG("ONNXYOLO",
"transform[first]: inputImageShape=(%dx%d) canvas=%dx%d cls=%d src=%dx%d dyn=%d",
inputImageShape.width, inputImageShape.height, width, height,
isClassification ? 1 : 0, mat.cols, mat.rows,
isDynamicInputShape ? 1 : 0);
}
input_node_dims = { 1, 3, height, width };
input_tensor_size = 1 * 3 * imageSize;
input_values_handler.resize(input_tensor_size);
@@ -1702,13 +1716,26 @@ namespace ANSCENTER {
const int w = std::clamp(rawW, kMinDim, kMaxDim);
const int h = std::clamp(rawH, kMinDim, kMaxDim);
// Stride alignment guard — YOLOv8/v11/v12 FPN concats only line
// up when H,W are multiples of 32. A non-aligned warm-up size
// would crash inside /model.12/Concat with a 5-vs-6 shape error
// and the warning would mask any real engine load failure.
if ((w % 32) != 0 || (h % 32) != 0) {
_logger.LogWarn("ANSONNXYOLO::WarmUpEngine",
"Warm-up skipped — dims not multiple of stride 32 ("
+ std::to_string(w) + "x" + std::to_string(h) + ")",
__FILE__, __LINE__);
return;
}
try {
// Mid-gray BGR image matches the letterbox fill colour used in
// preprocessing (114,114,114 ~ 128) and avoids degenerate inputs.
cv::Mat dummy(h, w, CV_8UC3, cv::Scalar(128, 128, 128));
ANS_DBG("ONNXYOLO", "Warm-up: running 1 dummy %s inference (%dx%d)",
(et == EngineType::NVIDIA_GPU) ? "CUDA" : "DirectML", w, h);
ANS_DBG("ONNXYOLO", "Warm-up: running 1 dummy %s inference (%dx%d) cfg=(%dx%d) raw=(%dx%d)",
(et == EngineType::NVIDIA_GPU) ? "CUDA" : "DirectML",
w, h, _modelConfig.inpWidth, _modelConfig.inpHeight, rawW, rawH);
auto t0 = std::chrono::steady_clock::now();
(void)m_ortEngine->detect(dummy, _classes,
+17 -1
View File
@@ -62,8 +62,24 @@ namespace ANSCENTER {
/// Override the input image shape for dynamic-input models.
/// Call after construction when the model config specifies a
/// different resolution than the default 640x640.
///
/// Width/height are snapped UP to the nearest multiple of 32
/// (with a 64-pixel floor). Ultralytics YOLO necks (v8/v11/v12)
/// concat upsampled stride-32 features with backbone stride-16
/// features in /model.12/Concat — that op requires
/// 2*ceil(H/32) == ceil(H/16), which only holds when H is a
/// multiple of 32. Feeding e.g. H=80 fails at runtime with
/// "Axis 2 has mismatched dimensions of 5 and 6". The 64-pixel
/// floor avoids degenerate sub-stride-32 inputs that would
/// produce 1x1 P5 maps.
void setInputShape(int width, int height) {
inputImageShape = cv::Size(width, height);
constexpr int kStride = 32;
constexpr int kMinDim = 64;
auto snap = [](int v) {
if (v < kMinDim) v = kMinDim;
return ((v + kStride - 1) / kStride) * kStride;
};
inputImageShape = cv::Size(snap(width), snap(height));
}
/// True if the ONNX model has dynamic spatial dimensions.