This engine took the feature count from outputShape[1] unconditionally. For a yolo26 export with end2end on — [1, 300, 6], rows of (x1 y1 x2 y2 score class) with the suppression already inside the graph — that means 296 classes, coordinates read as class scores, and boxes covering the frame. No error, just nonsense, which is exactly what the Labelling Tool produced from the same model before its decoder was fixed. Its sibling ANSONNXYOLO has recognised that head from the tensor shape for a while; this one had not, and the backend marker cannot tell them apart because it names the runtime rather than the head. The branch fires only where the tensor cannot be a grid: the 6 must be the last axis with a detection count before it, hundreds rather than the thousands of anchors a two-class [1, 6, 8400] export carries. The decoder reuses this engine's own scaleCoords rather than repeating the letterbox arithmetic, so both heads undo the padding the same way and agree about where a box is, and the rounding into the integral box type is spelled out instead of left to a narrowing conversion. NOT COMPILED. The engine layer needs CUDA and the ANSLibs tree, neither of which exists on this machine, so this is verified by reading: the shape rule matches the one the other three products now share, every symbol it uses is declared in this translation unit, and the arithmetic is delegated to the function the grid path already trusts. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
104 lines
5.8 KiB
C++
104 lines
5.8 KiB
C++
#ifndef ANSYOLO12OD_H
|
|
#define ANSYOL12OOD_H
|
|
#pragma once
|
|
#include "ANSEngineCommon.h"
|
|
#include "ANSModelSource.h"
|
|
#include <onnxruntime_cxx_api.h>
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <numeric>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <memory>
|
|
#include <chrono>
|
|
#include <random>
|
|
#include <unordered_map>
|
|
#include <thread>
|
|
namespace ANSCENTER {
|
|
// Standard Yolo engine class: yolo12
|
|
class ANSENGINE_API YOLO12OD :public ANSODBase {
|
|
public:
|
|
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();
|
|
~YOLO12OD();
|
|
private:
|
|
Ort::Env env{ nullptr }; // ONNX Runtime environment
|
|
Ort::SessionOptions sessionOptions{ nullptr }; // Session options for ONNX Runtime
|
|
Ort::Session session{ nullptr }; // ONNX Runtime session for running inference
|
|
bool isDynamicInputShape{}; // Flag indicating if input shape is dynamic
|
|
cv::Size inputImageShape; // Expected input image shape for the model
|
|
std::string _modelFilePath;
|
|
|
|
// Vectors to hold allocated input and output node names
|
|
std::vector<Ort::AllocatedStringPtr> inputNodeNameAllocatedStrings;
|
|
std::vector<const char*> inputNames;
|
|
std::vector<Ort::AllocatedStringPtr> outputNodeNameAllocatedStrings;
|
|
std::vector<const char*> outputNames;
|
|
size_t numInputNodes, numOutputNodes; // Number of input and output nodes in the model
|
|
std::vector<std::string> classNames; // Vector of class names loaded from file
|
|
std::vector<cv::Scalar> classColors; // Vector of colors for each class
|
|
float m_imgWidth = 0;
|
|
float m_imgHeight = 0;
|
|
protected:
|
|
bool loadModel(const std::string& modelPath, bool useGPU = true);
|
|
// Same load, from a model already in memory (an opened package).
|
|
bool loadModel(const ModelSource& model, bool useGPU = true);
|
|
std::vector<Object> detect(const cv::Mat& image, float confThreshold = 0.4f, float iouThreshold = 0.45f);
|
|
//cv::Mat preprocess(const cv::Mat& image, float*& blob, std::vector<int64_t>& inputTensorShape);
|
|
cv::Mat preprocess(const cv::Mat& image, std::vector<float>& blob, std::vector<int64_t>& inputTensorShape);
|
|
// outputTensors is non-const because GetTensorMutableData()
|
|
// requires a non-const Ort::Value receiver — see ANSONNXCL.h
|
|
// for full note.
|
|
std::vector<Object> postprocess(const cv::Size& originalImageSize, const cv::Size& resizedImageShape,
|
|
std::vector<Ort::Value>& outputTensors,
|
|
float confThreshold, float iouThreshold);
|
|
|
|
/// The [1, N, 6] head: rows of (x1 y1 x2 y2 score class) whose
|
|
/// non-maximum suppression happened inside the graph. postprocess()
|
|
/// hands the tensor here when it recognises that shape; reading it
|
|
/// as a [1, 4+nc, anchors] grid returns boxes covering the frame.
|
|
std::vector<Object> postprocessEndToEnd(const cv::Size& originalImageSize,
|
|
const cv::Size& resizedImageShape,
|
|
std::vector<Ort::Value>& outputTensors,
|
|
float confThreshold);
|
|
|
|
private:
|
|
template <typename T>
|
|
typename std::enable_if<std::is_arithmetic<T>::value, T>::type
|
|
inline clamp(const T& value, const T& low, const T& high)
|
|
{
|
|
// Ensure the range [low, high] is valid; swap if necessary
|
|
T validLow = low < high ? low : high;
|
|
T validHigh = low < high ? high : low;
|
|
|
|
// Clamp the value to the range [validLow, validHigh]
|
|
if (value < validLow)
|
|
return validLow;
|
|
if (value > validHigh)
|
|
return validHigh;
|
|
return value;
|
|
}
|
|
size_t vectorProduct(const std::vector<int64_t>& vector);
|
|
void letterBox(const cv::Mat& image, cv::Mat& outImage,
|
|
const cv::Size& newShape,
|
|
const cv::Scalar& color = cv::Scalar(114, 114, 114),
|
|
bool auto_ = true,
|
|
bool scaleFill = false,
|
|
bool scaleUp = true,
|
|
int stride = 32);
|
|
BoundingBox scaleCoords(const cv::Size& imageShape, BoundingBox coords,
|
|
const cv::Size& imageOriginalShape, bool p_Clip);
|
|
void NMSBoxes(const std::vector<BoundingBox>& boundingBoxes,
|
|
const std::vector<float>& scores,
|
|
float scoreThreshold,
|
|
float nmsThreshold,
|
|
std::vector<int>& indices);
|
|
};
|
|
}
|
|
#endif |