Files
ANSCORE/modules/ANSODEngine/ANSONNXCL.h
T
nghianguyenandClaude Opus 5 5820a5e61b feat(ODEngine): load a model from memory, not only from a path
Every engine built its session from a file path, which is why opening an
encrypted model package means inflating it to a directory first: the
unprotected model lands on disk, is optimised, and is zipped back into a
cache. The new package format cannot avoid that from the outside — the
engines have to be able to take bytes.

ANSModelSource.h introduces a ModelSource (a path, or bytes with a name
for logs) and one MakeSession that builds from either. The six ONNX
engines — classification, OBB, pose, segmentation, YOLO12 and YOLO —
each gain a load overload taking it, with the existing string form
delegating so no caller changes. The Windows wide-path conversion and
the from-memory call now live in one place instead of the eleven copies
they were spread across; YOLOOD, which had only the Windows branch,
becomes portable as a side effect.

The factories are named rather than implicit, so a call with a plain
string literal still resolves to the path overload without ambiguity.

Not compiled here: this layer builds only where CUDA is present, so
verification on this machine was the new header compiled and run, plus a
structural check that every engine declares and defines both overloads,
calls MakeSession exactly once, and has no raw session construction
left. It needs a build on a CUDA box before it ships.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 20:29:59 +10:00

100 lines
5.4 KiB
C++

#ifndef ANSONNXCL_H
#define ANSONNXCL_H
#pragma once
#include "ANSEngineCommon.h"
#include "ANSModelSource.h"
#include "engine.h"
#include <memory>
namespace ANSCENTER {
// Forward declaration: avoid pulling ANSONNXYOLO.h into every consumer of
// ANSONNXCL.h. Definition is included in ANSONNXCL.cpp.
class ANSONNXYOLO;
// ────────────────────────────────────────────────────────────────────────
// ANSONNXCL — verification facade over ANSONNXYOLO.
//
// ANSONNXYOLO already auto-detects classification models (single 2D
// output [B, nc]) and routes through its own classification postprocess.
// It also has the safe DML config (DisableMemPattern + ORT_SEQUENTIAL +
// EXTENDED) and the IsDmlFatalError catch-and-rebuild path.
//
// We delegate every public method here to an internal ANSONNXYOLO so we
// can A/B test whether ANSONNXCL's own session/postprocess code is the
// real cause of the AMD same-process DML races we saw with the custom
// helmet model. Public ABI is unchanged — external callers don't move.
// ────────────────────────────────────────────────────────────────────────
class ANSENGINE_API ANSONNXCL :public ANSODBase {
public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXCL(const ANSONNXCL&) = delete;
ANSONNXCL& operator=(const ANSONNXCL&) = delete;
ANSONNXCL(ANSONNXCL&&) = delete;
ANSONNXCL& operator=(ANSONNXCL&&) = delete;
ANSONNXCL() = default;
virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override;
virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override;
virtual bool LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap)override;
virtual bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
std::vector<Object> RunInference(const cv::Mat& input);
std::vector<Object> RunInference(const cv::Mat& input, const std::string& camera_id);
bool Destroy();
~ANSONNXCL();
private:
// The wrapped engine. Lazy-initialised on first public call so we
// don't pay the cost (and pull in the dependency) for callers who
// never use this class.
std::unique_ptr<ANSONNXYOLO> _yoloDelegate;
private:
std::string _modelFilePath;
bool _modelLoadValid;
bool _fp16{ false };
// DML device-lost recovery: when DirectML fails fatally
// (HRESULT 887A0005 device-removed, 80004005 graph/command-recorder
// E_FAIL, etc. — see ANSCENTER::IsDmlFatalError) the Ort::Session
// is permanently broken. We detect once, recreate on CPU via
// Init(_modelFilePath, inputImageShape_, /*useGPU=*/false), and
// suppress further error-log flooding via this flag.
bool _dmlDeviceLost{ false };
size_t vectorProduct(const std::vector<int64_t>& vector);
bool preprocessImageToTensor(const cv::Mat& image, cv::Mat& outImage,
const cv::Size& targetShape,
const cv::Scalar& color = cv::Scalar(0, 0, 0),
bool scaleUp = true,
const std::string& strategy = "resize");
void warmupModel();
bool Init(const std::string& modelPath, const cv::Size& targetInputShape, bool useGPU = true);
// Same load, from a model already in memory (an opened package).
bool Init(const ModelSource& model, const cv::Size& targetInputShape, bool useGPU = true);
bool preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
// outputTensors is non-const because GetTensorMutableData() (the
// ORT API that doesn't hang on AMD DirectML) requires a non-const
// Ort::Value receiver. See comment at the GetTensorMutableData
// call site in postprocess() for the full rationale.
std::vector<Object> postprocess(std::vector<Ort::Value>& outputTensors, const std::string& camera_id);
std::vector<Object> classify(const cv::Mat& image, const std::string& camera_id);
private:
Ort::Env env_{ nullptr };
Ort::SessionOptions sessionOptions_{ nullptr };
Ort::Session session_{ nullptr };
bool isDynamicInputShape_{};
cv::Size inputImageShape_{};
std::vector<Ort::AllocatedStringPtr> inputNodeNameAllocatedStrings_{};
std::vector<const char*> inputNames_{};
std::vector<Ort::AllocatedStringPtr> outputNodeNameAllocatedStrings_{};
std::vector<const char*> outputNames_{};
size_t numInputNodes_{}, numOutputNodes_{};
int numClasses_{ 0 };
float m_imgWidth = 0;
float m_imgHeight = 0;
};
}
#endif
#pragma once