Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0baca1b20d | ||
|
|
cb964907f0 | ||
|
|
cd99fcf6d9 | ||
|
|
1e369fdfef | ||
|
|
b69a0fbcba | ||
|
|
e76d3b81e2 | ||
|
|
eb84d48e5c | ||
|
|
3798e3513b | ||
|
|
ea877a61bc | ||
|
|
3c9e0b79f5 |
+13
-3
@@ -1,5 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
cmake_policy(SET CMP0167 NEW) # Suppress FindBoost removal warning
|
||||
# CMP0167 (FindBoost removed from CMake) only exists from 3.30, and this
|
||||
# project asks for 3.28 — so setting it unconditionally makes every older
|
||||
# CMake fail at line two with "policy is not known to this version". Guarded,
|
||||
# a newer CMake still gets the quiet FindBoost it was added for.
|
||||
if(POLICY CMP0167)
|
||||
cmake_policy(SET CMP0167 NEW)
|
||||
endif()
|
||||
|
||||
# ── CUDA is optional (not available on macOS) ───────────────────
|
||||
include(CheckLanguage)
|
||||
@@ -17,10 +23,14 @@ if(CMAKE_CUDA_COMPILER)
|
||||
set(CMAKE_CUDA_ARCHITECTURES
|
||||
75-real 80-real 86-real 87-real 88-real 89-real 90-real 100-real 120-real
|
||||
75-virtual)
|
||||
# --use_fast_math, with underscores: that is nvcc's own spelling. Older
|
||||
# toolkits also accepted the hyphenated form; CUDA 13 does not, and every
|
||||
# .cu file fails with "Unknown option '--use-fast-math'" before it compiles
|
||||
# a line.
|
||||
if(MSVC)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math -Xcompiler=/utf-8")
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use_fast_math -Xcompiler=/utf-8")
|
||||
else()
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math")
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use_fast_math")
|
||||
endif()
|
||||
else()
|
||||
project(ANSCORE LANGUAGES C CXX)
|
||||
|
||||
@@ -1777,10 +1777,19 @@ namespace ANSCENTER
|
||||
registrationName = "ANSTS";
|
||||
productId = 1008;
|
||||
}
|
||||
else {
|
||||
registrationName = productName;
|
||||
else if (productName == "ANSMIND") {//1009
|
||||
registrationName = "ANSMIND";
|
||||
productId = 1009;
|
||||
}
|
||||
else if (productName == "ANSGW") {//1010
|
||||
registrationName = "ANSGW";
|
||||
productId = 1010;
|
||||
}
|
||||
else {
|
||||
// Unknown product: refuse rather than mint a trial under some real product's id.
|
||||
std::cout << "ActivateTrialLicense: unknown product name '" << productName << "'" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string trialLicenseKey = GetTrialLicenseKey(registrationName);
|
||||
if (trialLicenseKey.empty()) {
|
||||
@@ -1849,6 +1858,12 @@ namespace ANSCENTER
|
||||
case ANSCENTER::ANSPRODUCTS::ANS_TS://1008
|
||||
_productName = "ANSTS";
|
||||
break;
|
||||
case ANSCENTER::ANSPRODUCTS::ANSMIND://1009
|
||||
_productName = "ANSMIND";
|
||||
break;
|
||||
case ANSCENTER::ANSPRODUCTS::ANSGW://1010
|
||||
_productName = "ANSGW";
|
||||
break;
|
||||
}
|
||||
if (_productName == productName) {
|
||||
// Delete the license file
|
||||
@@ -1896,6 +1911,12 @@ namespace ANSCENTER
|
||||
else if (productName == "ANSTS") {//1008
|
||||
registrationName = "ANSTS";
|
||||
}
|
||||
else if (productName == "ANSMIND") {//1009
|
||||
registrationName = "ANSMIND";
|
||||
}
|
||||
else if (productName == "ANSGW") {//1010
|
||||
registrationName = "ANSGW";
|
||||
}
|
||||
else {
|
||||
registrationName = productName;
|
||||
}
|
||||
@@ -1936,6 +1957,12 @@ namespace ANSCENTER
|
||||
else if (productName == "ANSTS") {//1008
|
||||
registrationName = "ANSTS";
|
||||
}
|
||||
else if (productName == "ANSMIND") {//1009
|
||||
registrationName = "ANSMIND";
|
||||
}
|
||||
else if (productName == "ANSGW") {//1010
|
||||
registrationName = "ANSGW";
|
||||
}
|
||||
else {
|
||||
registrationName = productName;
|
||||
}
|
||||
@@ -2002,6 +2029,8 @@ namespace ANSCENTER
|
||||
ansLicenseCollection.push_back("ANSALPR");//1006
|
||||
ansLicenseCollection.push_back("ANSCV");//1007
|
||||
ansLicenseCollection.push_back("ANSTS");//1008
|
||||
ansLicenseCollection.push_back("ANSMIND");//1009
|
||||
ansLicenseCollection.push_back("ANSGW");//1010
|
||||
|
||||
for (auto& element : ansLicenseCollection) {
|
||||
boost::property_tree::ptree dataNode;
|
||||
@@ -2064,6 +2093,12 @@ namespace ANSCENTER
|
||||
case ANSCENTER::ANSPRODUCTS::ANS_TS:
|
||||
_productName = "ANSTS";
|
||||
break;
|
||||
case ANSCENTER::ANSPRODUCTS::ANSMIND:
|
||||
_productName = "ANSMIND";
|
||||
break;
|
||||
case ANSCENTER::ANSPRODUCTS::ANSGW:
|
||||
_productName = "ANSGW";
|
||||
break;
|
||||
}
|
||||
if (_productName == element) {
|
||||
_registrationName = ANSUtility::DecodeBase64Utility(_validationData.c_str());// Decode
|
||||
@@ -2141,10 +2176,18 @@ namespace ANSCENTER
|
||||
registrationName = "ANSTS";
|
||||
productId = 1008;
|
||||
}
|
||||
else {
|
||||
registrationName = ansLicenseInfo.productName;
|
||||
else if (ansLicenseInfo.productName == "ANSMIND") {
|
||||
registrationName = "ANSMIND";
|
||||
productId = 1009;
|
||||
}
|
||||
else if (ansLicenseInfo.productName == "ANSGW") {
|
||||
registrationName = "ANSGW";
|
||||
productId = 1010;
|
||||
}
|
||||
else {
|
||||
// Unknown product: skip the row rather than report it under some real product's id.
|
||||
continue;
|
||||
}
|
||||
dataNode.put("product_name", ansLicenseInfo.productName);
|
||||
std::string trialLicenseKey = GetTrialLicenseKey(registrationName);
|
||||
if (trialLicenseKey.empty()) {
|
||||
|
||||
@@ -259,7 +259,9 @@ namespace ANSCENTER
|
||||
ANS_OCR = 1005,
|
||||
ANS_ALPR =1006,
|
||||
ANS_CV = 1007,
|
||||
ANS_TS = 1008
|
||||
ANS_TS = 1008,
|
||||
ANSMIND = 1009,
|
||||
ANSGW = 1010
|
||||
};
|
||||
enum ANSLICENSESTATUS {
|
||||
ACTIVE = 0,
|
||||
|
||||
@@ -13,11 +13,14 @@
|
||||
// package is refused with a sentence that says why. That is what keeps the key
|
||||
// out of a developer's checkout without keeping developers out of the code.
|
||||
|
||||
#if defined(__has_include)
|
||||
#if __has_include("ANSModelKeysGenerated.h")
|
||||
// ANSCORE_HAVE_BUILTIN_MODEL_KEYS is set by CMake when the ceremony's
|
||||
// generated header is there. It is not tested with __has_include on purpose:
|
||||
// that test leaves no dependency for the build system, so a key emitted after
|
||||
// a build would not take effect until someone cleaned — and the failure looks
|
||||
// exactly like a missing key, which on a release machine means shipping an
|
||||
// engine that silently cannot open a package.
|
||||
#if defined(ANSCORE_HAVE_BUILTIN_MODEL_KEYS)
|
||||
#include "ANSModelKeysGenerated.h"
|
||||
#define ANSCORE_HAVE_BUILTIN_MODEL_KEYS 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "ANSModelPackage.h"
|
||||
|
||||
@@ -217,16 +217,28 @@ inline bool OpenModelPackage(const std::string& path, OpenedModel& out,
|
||||
out.name = m.value("project", std::string());
|
||||
if (out.name.empty()) out.name = path;
|
||||
|
||||
// A package written before manifests carried an input size still has to
|
||||
// answer, and the model in hand is the thing that knows. It costs nothing
|
||||
// here: the bytes are already decrypted.
|
||||
if (out.inputWidth <= 0 || out.inputHeight <= 0) {
|
||||
onnxshape::InputShape shape;
|
||||
// WHAT AN OLDER PACKAGE CANNOT TELL US, and its model can. The input
|
||||
// size, when the manifest predates the field; and the head, because a
|
||||
// package whose family is "yolo" may hold an end-to-end export — the box
|
||||
// used to take the family from the backend marker, and the marker names
|
||||
// the runtime rather than the head. It costs nothing to ask here: the
|
||||
// bytes are already decrypted.
|
||||
{
|
||||
onnxshape::GraphShape g;
|
||||
std::string swhy;
|
||||
if (onnxshape::fromMemory(out.model.data(), out.model.size(), shape,
|
||||
if (onnxshape::graphFromMemory(out.model.data(), out.model.size(), g,
|
||||
swhy)) {
|
||||
out.inputWidth = shape.width;
|
||||
out.inputHeight = shape.height;
|
||||
if (out.inputWidth <= 0 || out.inputHeight <= 0) {
|
||||
out.inputWidth = g.input.width;
|
||||
out.inputHeight = g.input.height;
|
||||
}
|
||||
if (g.endToEnd() && out.family == "yolo") {
|
||||
out.family = "yolo10";
|
||||
out.warning = out.warning.empty()
|
||||
? "this package says family yolo, but its head is "
|
||||
"end-to-end; reading it as yolo10"
|
||||
: out.warning;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -3,18 +3,16 @@
|
||||
#pragma once
|
||||
// ANSOnnxShape — the input geometry a model declares, read off the graph.
|
||||
//
|
||||
// WHY A READER NEEDS THIS. An engine has to know what its model expects before
|
||||
// it can preprocess a single frame, and that answer has always come from
|
||||
// whatever the bundle happened to carry: model_config.json's input_size, or an
|
||||
// `input` object in a package manifest. Neither is present on every model —
|
||||
// most of the bundles in the field are classes.names, a marker file and the
|
||||
// export, and nothing else. The graph is always there, and the graph is what
|
||||
// the session is built from, so where the metadata is silent the model itself
|
||||
// is asked instead of every engine falling back to 640x640.
|
||||
// WHY A READER NEEDS THIS. A node has to know what its model expects before it
|
||||
// can preprocess a single frame, and until now that answer came from whatever
|
||||
// the bundle happened to carry: model_config.json's input_size in a folder, an
|
||||
// `input` object in a package manifest. Neither is present on every model. The
|
||||
// graph always is, and the graph is the thing the session is built from — so
|
||||
// where the metadata is silent, ask the model itself rather than assume 640.
|
||||
//
|
||||
// TWO SOURCES, ONE PARSER. A package's model is already in memory, decrypted,
|
||||
// and must never reach the disk; an inflated bundle's is a file that may be
|
||||
// hundreds of megabytes. The template below reads either, and the file case
|
||||
// and must never reach the disk; an imported folder's model is a file that may
|
||||
// be hundreds of megabytes. The template below reads either, and the file case
|
||||
// SEEKS past the nodes and the initializers rather than reading them — the
|
||||
// fields this wants sit after all of the weight on the wire.
|
||||
//
|
||||
@@ -23,12 +21,16 @@
|
||||
// ModelProto.graph field 7, length-delimited
|
||||
// GraphProto.input field 11, repeated (after node and
|
||||
// initializer on the wire)
|
||||
// GraphProto.output field 12, repeated
|
||||
// ValueInfoProto.type field 2
|
||||
// TypeProto.tensor_type field 1
|
||||
// TypeProto.Tensor.shape field 2
|
||||
// TensorShapeProto.dim field 1, repeated
|
||||
// Dimension.dim_value field 1, varint (dim_param means the
|
||||
// axis is dynamic: reported as 0)
|
||||
// ModelProto.metadata_props field 14, repeated StringStringEntryProto
|
||||
// StringStringEntryProto.key field 1, string
|
||||
// StringStringEntryProto.value field 2, string
|
||||
//
|
||||
// An axis the graph leaves open is reported as zero, and a file that cannot be
|
||||
// parsed is reported as a failure. Both mean the same thing to a caller: this
|
||||
@@ -36,6 +38,7 @@
|
||||
// model's input size is worse than being silent about it.
|
||||
|
||||
#include <cstdint>
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
@@ -52,6 +55,32 @@ struct InputShape {
|
||||
bool known() const { return height > 0 && width > 0; }
|
||||
};
|
||||
|
||||
// What the whole graph says about itself: what goes in, and what shape comes
|
||||
// out — which is the difference between a head that needs decoding and one
|
||||
// that has already done it.
|
||||
struct GraphShape {
|
||||
InputShape input;
|
||||
std::vector<int64_t> output; // the FIRST output's dims; empty when unknown
|
||||
bool exportSaysEndToEnd = false; // the model's own metadata, when it says
|
||||
|
||||
// AN END-TO-END HEAD, [1, N, 6]: rows of (x1 y1 x2 y2 score class) with the
|
||||
// NMS already inside the graph. Telling one apart from an ordinary YOLO head
|
||||
// matters more than it looks: feeding [1,N,6] to a v8 decoder yields boxes
|
||||
// everywhere with coordinates read as confidences — plausible-looking
|
||||
// garbage rather than an error.
|
||||
//
|
||||
// The trap is a two-class v8 export, whose [1, 4+nc, N] is [1, 6, 8400] —
|
||||
// a 6 in the shape, and not end-to-end. So the 6 must be the LAST axis, and
|
||||
// the axis before it must be a detection count rather than an anchor count;
|
||||
// an end-to-end head emits max_det rows, which is hundreds, never thousands.
|
||||
// When the export's own metadata says end2end, that settles it outright.
|
||||
bool endToEnd() const {
|
||||
if (exportSaysEndToEnd) return true;
|
||||
return output.size() == 3 && output[0] == 1 && output[2] == 6 &&
|
||||
output[1] > 0 && output[1] <= 1000;
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
// A model already in memory: the decrypted contents of a package.
|
||||
@@ -158,51 +187,105 @@ std::vector<int64_t> dims(Src& s, int64_t end) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// One ValueInfoProto (a graph input or output), given a stream on its payload:
|
||||
// its dims, or empty when it is not a shaped tensor.
|
||||
template <class Src>
|
||||
bool read(Src& s, int64_t end, InputShape& out, std::string& why) {
|
||||
std::vector<int64_t> valueDims(Src& s, int64_t valueEnd) {
|
||||
uint64_t len = 0;
|
||||
if (!seekField(s, valueEnd, 2, len)) return {}; // ValueInfoProto.type
|
||||
const int64_t typeEnd = s.tell() + int64_t(len);
|
||||
if (!seekField(s, typeEnd, 1, len)) return {}; // TypeProto.tensor_type
|
||||
const int64_t tensorEnd = s.tell() + int64_t(len);
|
||||
if (!seekField(s, tensorEnd, 2, len)) return {}; // Tensor.shape
|
||||
return dims(s, s.tell() + int64_t(len));
|
||||
}
|
||||
|
||||
// The graph's first input and first output, and whether the export declared
|
||||
// itself end-to-end. `end` is the whole file; `graphStart` lets the output be
|
||||
// found after the input, since both hang off the same graph.
|
||||
template <class Src>
|
||||
bool readGraph(Src& s, int64_t end, GraphShape& out, std::string& why) {
|
||||
uint64_t len = 0;
|
||||
if (!seekField(s, end, 7, len)) { // ModelProto.graph
|
||||
why = "this does not look like an ONNX model";
|
||||
return false;
|
||||
}
|
||||
const int64_t graphEnd = s.tell() + int64_t(len);
|
||||
const int64_t graphStart = s.tell();
|
||||
const int64_t graphEnd = graphStart + int64_t(len);
|
||||
|
||||
if (!seekField(s, graphEnd, 11, len)) { // GraphProto.input
|
||||
why = "the graph declares no input";
|
||||
return false;
|
||||
}
|
||||
const int64_t inputEnd = s.tell() + int64_t(len);
|
||||
if (!seekField(s, inputEnd, 2, len)) { // ValueInfoProto.type
|
||||
why = "the input declares no type";
|
||||
return false;
|
||||
const std::vector<int64_t> in = valueDims(s, s.tell() + int64_t(len));
|
||||
|
||||
// The output, from the graph's start again: it sits after the input on the
|
||||
// wire, but seeking from the start costs nothing and does not depend on it.
|
||||
s.seek(graphStart);
|
||||
if (seekField(s, graphEnd, 12, len)) // GraphProto.output
|
||||
out.output = valueDims(s, s.tell() + int64_t(len));
|
||||
|
||||
// The export's own word on its head, which beats any shape rule.
|
||||
s.seek(0);
|
||||
while (s.ok() && s.tell() >= 0 && s.tell() < end) {
|
||||
uint32_t field = 0, wire = 0;
|
||||
if (!tag(s, field, wire)) break;
|
||||
if (field != 14 || wire != 2) { // ModelProto.metadata_props
|
||||
if (!skip(s, wire)) break;
|
||||
continue;
|
||||
}
|
||||
const int64_t typeEnd = s.tell() + int64_t(len);
|
||||
if (!seekField(s, typeEnd, 1, len)) { // TypeProto.tensor_type
|
||||
why = "the input is not a tensor";
|
||||
return false;
|
||||
uint64_t entryLen = 0;
|
||||
if (!varint(s, entryLen)) break;
|
||||
const int64_t entryEnd = s.tell() + int64_t(entryLen);
|
||||
std::string key, value;
|
||||
while (s.ok() && s.tell() >= 0 && s.tell() < entryEnd) {
|
||||
uint32_t ef = 0, ew = 0;
|
||||
if (!tag(s, ef, ew)) break;
|
||||
if ((ef == 1 || ef == 2) && ew == 2) {
|
||||
uint64_t n = 0;
|
||||
if (!varint(s, n)) break;
|
||||
std::string text;
|
||||
for (uint64_t i = 0; i < n; ++i) {
|
||||
const int c = s.get();
|
||||
if (c < 0) break;
|
||||
text.push_back(char(c));
|
||||
}
|
||||
(ef == 1 ? key : value) = text;
|
||||
} else if (!skip(s, ew)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
s.seek(entryEnd);
|
||||
if (key == "end2end") {
|
||||
for (char& c : value) c = char(::tolower(static_cast<unsigned char>(c)));
|
||||
out.exportSaysEndToEnd = (value == "true" || value == "1");
|
||||
}
|
||||
const int64_t tensorEnd = s.tell() + int64_t(len);
|
||||
if (!seekField(s, tensorEnd, 2, len)) { // Tensor.shape
|
||||
why = "the input tensor has no shape";
|
||||
return false;
|
||||
}
|
||||
const std::vector<int64_t> d = dims(s, s.tell() + int64_t(len));
|
||||
|
||||
// NCHW is the only layout these engines run.
|
||||
if (d.size() != 4) {
|
||||
if (in.size() != 4) {
|
||||
why = "the input is not a four-dimensional image tensor";
|
||||
return false;
|
||||
}
|
||||
auto axis = [](int64_t v) { return (v > 0 && v < 1000000) ? int(v) : 0; };
|
||||
out.channels = axis(d[1]);
|
||||
out.height = axis(d[2]);
|
||||
out.width = axis(d[3]);
|
||||
if (!out.known()) {
|
||||
out.input.channels = axis(in[1]);
|
||||
out.input.height = axis(in[2]);
|
||||
out.input.width = axis(in[3]);
|
||||
if (!out.input.known()) {
|
||||
why = "the model takes a dynamic input size";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class Src>
|
||||
bool read(Src& s, int64_t end, InputShape& out, std::string& why) {
|
||||
GraphShape g;
|
||||
if (!readGraph(s, end, g, why)) return false;
|
||||
out = g.input;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// What a model already in memory declares.
|
||||
@@ -216,6 +299,17 @@ inline bool fromMemory(const void* data, size_t size, InputShape& out,
|
||||
return detail::read(s, int64_t(size), out, why);
|
||||
}
|
||||
|
||||
// The whole graph, from a model already in memory.
|
||||
inline bool graphFromMemory(const void* data, size_t size, GraphShape& out,
|
||||
std::string& why) {
|
||||
if (!data || size == 0) {
|
||||
why = "there is no model to read";
|
||||
return false;
|
||||
}
|
||||
detail::MemSource s{static_cast<const uint8_t*>(data), int64_t(size), 0};
|
||||
return detail::readGraph(s, int64_t(size), out, why);
|
||||
}
|
||||
|
||||
// What a model on disk declares. The file is walked, not loaded.
|
||||
inline bool fromFile(const std::string& path, InputShape& out,
|
||||
std::string& why) {
|
||||
@@ -235,6 +329,25 @@ inline bool fromFile(const std::string& path, InputShape& out,
|
||||
return detail::read(s, int64_t(size), out, why);
|
||||
}
|
||||
|
||||
// The whole graph, from a model on disk. Walked, not loaded.
|
||||
inline bool graphFromFile(const std::string& path, GraphShape& out,
|
||||
std::string& why) {
|
||||
std::error_code ec;
|
||||
const auto size = std::filesystem::file_size(path, ec);
|
||||
if (ec || size == 0) {
|
||||
why = "the model is not there to read";
|
||||
return false;
|
||||
}
|
||||
detail::FileSource s;
|
||||
s.f.open(path, std::ios::binary);
|
||||
s.size = int64_t(size);
|
||||
if (!s.f) {
|
||||
why = "the model could not be opened";
|
||||
return false;
|
||||
}
|
||||
return detail::readGraph(s, int64_t(size), out, why);
|
||||
}
|
||||
|
||||
} // namespace onnxshape
|
||||
} // namespace ANSCENTER
|
||||
|
||||
|
||||
@@ -14,18 +14,30 @@ target_include_directories(ANSModelPackage PUBLIC
|
||||
${CMAKE_SOURCE_DIR}/3rdparty/monocypher
|
||||
)
|
||||
|
||||
# nlohmann/json comes from vcpkg on Windows and the system elsewhere, the same
|
||||
# way ANSFR already takes it.
|
||||
if(WIN32)
|
||||
target_include_directories(ANSModelPackage PUBLIC ${VCPKG_INCLUDE_DIR})
|
||||
else()
|
||||
find_path(ANSCORE_NLOHMANN_INCLUDE nlohmann/json.hpp
|
||||
HINTS /opt/homebrew/include /usr/local/include /usr/include)
|
||||
if(ANSCORE_NLOHMANN_INCLUDE)
|
||||
# nlohmann/json, wherever this machine keeps it: the ANSLibs tree on Windows,
|
||||
# vcpkg, or the system elsewhere. Asked for by name rather than assumed — the
|
||||
# Windows branch used to point at vcpkg alone, and on a real build machine the
|
||||
# header lives under ANSLibs, so every target that linked this one failed to
|
||||
# find it while the engines, which add ANSLibs themselves, compiled fine.
|
||||
find_path(ANSCORE_NLOHMANN_INCLUDE nlohmann/json.hpp
|
||||
HINTS ${ANSLIBS_DIR} ${VCPKG_INCLUDE_DIR}
|
||||
/opt/homebrew/include /usr/local/include /usr/include)
|
||||
if(ANSCORE_NLOHMANN_INCLUDE)
|
||||
target_include_directories(ANSModelPackage PUBLIC ${ANSCORE_NLOHMANN_INCLUDE})
|
||||
else()
|
||||
message(STATUS "ANSModelPackage: nlohmann/json at ${ANSCORE_NLOHMANN_INCLUDE}")
|
||||
else()
|
||||
message(WARNING "nlohmann/json.hpp not found — ANSModelPackage will not build")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# The model-package key, decided at configure time: see ANSModelKeys.h for why
|
||||
# this is not an __has_include test. PUBLIC so every engine that opens a
|
||||
# package is compiled with the same answer.
|
||||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/ANSModelKeysGenerated.h")
|
||||
target_compile_definitions(ANSModelPackage PUBLIC ANSCORE_HAVE_BUILTIN_MODEL_KEYS=1)
|
||||
message(STATUS "ANSModelPackage: key present — packages can be opened")
|
||||
else()
|
||||
message(STATUS "ANSModelPackage: no key — packages cannot be opened "
|
||||
"(run ansmodel-keys emit --product anscore, then re-run cmake)")
|
||||
endif()
|
||||
|
||||
set_target_properties(ANSModelPackage PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
@@ -316,7 +316,7 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!FileExist(onnxModelPath)) {
|
||||
if (!hasOnnxBytes() && !FileExist(onnxModelPath)) {
|
||||
// ONNX model does not exist, try to find alternative precision engine
|
||||
std::cout << "Searching for alternative precision engine..." << std::endl;
|
||||
|
||||
@@ -390,12 +390,9 @@ bool Engine<T>::buildLoadNetwork(std::string onnxModelPath, const std::array<flo
|
||||
auto tempNetwork = std::unique_ptr<nvinfer1::INetworkDefinition>(TRT_CREATE_NETWORK(tempBuilder));
|
||||
auto tempParser = std::unique_ptr<nvonnxparser::IParser>(nvonnxparser::createParser(*tempNetwork, m_logger));
|
||||
|
||||
std::ifstream onnxFile(onnxModelPath, std::ios::binary | std::ios::ate);
|
||||
std::streamsize onnxSize = onnxFile.tellg();
|
||||
onnxFile.seekg(0, std::ios::beg);
|
||||
std::vector<char> onnxBuffer(onnxSize);
|
||||
if (!onnxFile.read(onnxBuffer.data(), onnxSize)) {
|
||||
std::cout << "Error: Failed to read ONNX file" << std::endl;
|
||||
std::vector<char> onnxBuffer;
|
||||
if (!readOnnxBytes(onnxModelPath, onnxBuffer)) {
|
||||
std::cout << "Error: Failed to read ONNX model" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1465,7 +1462,7 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
|
||||
std::cout << "Engine file already exists: " << engineName << std::endl;
|
||||
return true;
|
||||
}
|
||||
if (!FileExist(onnxModelPath)) {
|
||||
if (!hasOnnxBytes() && !FileExist(onnxModelPath)) {
|
||||
std::cout << "Error: ONNX model file does not exist: " << onnxModelPath << std::endl;
|
||||
return false;
|
||||
}
|
||||
@@ -1502,16 +1499,14 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the onnx file into memory
|
||||
std::ifstream file(onnxModelPath, std::ios::binary | std::ios::ate);
|
||||
std::streamsize size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<char> buffer(size);
|
||||
if (!file.read(buffer.data(), size)) {
|
||||
std::cout << "Error: Unable to read ONNX file" << std::endl;
|
||||
// Get the onnx bytes: from the in-memory buffer of a sealed package, or
|
||||
// from the file on disk.
|
||||
std::vector<char> buffer;
|
||||
if (!readOnnxBytes(onnxModelPath, buffer)) {
|
||||
std::cout << "Error: Unable to read ONNX model" << std::endl;
|
||||
return false;
|
||||
}
|
||||
const std::streamsize size = static_cast<std::streamsize>(buffer.size());
|
||||
|
||||
std::cout << "ONNX model size: " << size / (1024 * 1024) << " MiB" << std::endl;
|
||||
|
||||
@@ -2471,16 +2466,14 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
|
||||
auto tempParser = std::unique_ptr<nvonnxparser::IParser>(
|
||||
nvonnxparser::createParser(*tempNetwork, m_logger));
|
||||
|
||||
std::ifstream onnxFile(onnxModelPath, std::ios::binary | std::ios::ate);
|
||||
if (!onnxFile.good()) {
|
||||
std::vector<char> onnxBuffer;
|
||||
const bool onnxReadable = readOnnxBytes(onnxModelPath, onnxBuffer);
|
||||
if (!onnxReadable) {
|
||||
ANS_DBG("TRT_Build", "buildWithRetry WARN cannot open ONNX for pre-parse path=%s",
|
||||
onnxModelPath.c_str());
|
||||
}
|
||||
if (onnxFile.good()) {
|
||||
std::streamsize onnxSize = onnxFile.tellg();
|
||||
onnxFile.seekg(0, std::ios::beg);
|
||||
std::vector<char> onnxBuffer(onnxSize);
|
||||
if (onnxFile.read(onnxBuffer.data(), onnxSize)) {
|
||||
if (onnxReadable) {
|
||||
{
|
||||
unsigned long sehRetryParse = 0;
|
||||
bool retryParsed = parseOnnxModelSafe(tempParser.get(),
|
||||
onnxBuffer.data(), onnxBuffer.size(), &sehRetryParse);
|
||||
@@ -2508,9 +2501,6 @@ bool Engine<T>::buildWithRetry(std::string onnxModelPath,
|
||||
(int)retryParsed,
|
||||
retryParsed ? tempNetwork->getNbInputs() : -1);
|
||||
}
|
||||
} else {
|
||||
ANS_DBG("TRT_Build", "buildWithRetry WARN ONNX read failed bytes=%zd",
|
||||
(long long)onnxSize);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -88,8 +88,20 @@ public:
|
||||
const std::array<float, 3>& subVals,
|
||||
const std::array<float, 3>& divVals,
|
||||
bool normalize,
|
||||
int maxSlotsPerGpu)
|
||||
int maxSlotsPerGpu,
|
||||
const void* onnxData = nullptr,
|
||||
size_t onnxSize = 0)
|
||||
{
|
||||
// A sealed .ansmodel is decrypted in memory and never written out, so
|
||||
// the caller hands the bytes down here: every Engine this call builds
|
||||
// parses them instead of opening modelPath, which only names the
|
||||
// TensorRT plan that gets cached.
|
||||
auto newEngine = [&]() {
|
||||
auto e = std::make_shared<Engine<T>>(options);
|
||||
if (onnxData != nullptr && onnxSize > 0) e->setOnnxBytes(onnxData, onnxSize);
|
||||
return e;
|
||||
};
|
||||
|
||||
// Optimizer / temporary engines: maxSlotsPerGpu==0 means the caller
|
||||
// only needs a lightweight, non-shared engine (e.g., OptimizeModelStr).
|
||||
// Bypass the pool cache entirely:
|
||||
@@ -99,7 +111,7 @@ public:
|
||||
// Note: maxSlotsPerGpu==1 is now the normal "1 slot per GPU" multi-GPU
|
||||
// round-robin mode, so it goes through the pool path below.
|
||||
if (maxSlotsPerGpu == 0) {
|
||||
auto engine = std::make_shared<Engine<T>>(options);
|
||||
auto engine = newEngine();
|
||||
bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
|
||||
return ok ? engine : nullptr;
|
||||
}
|
||||
@@ -141,13 +153,13 @@ public:
|
||||
}
|
||||
|
||||
// Cache miss — create new Engine pool
|
||||
auto engine = std::make_shared<Engine<T>>(options);
|
||||
auto engine = newEngine();
|
||||
bool ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu);
|
||||
if (!ok) {
|
||||
// Step 1: Force-evict all pools with refcount=0 to reclaim VRAM
|
||||
int evicted = forceEvictPending();
|
||||
if (evicted > 0) {
|
||||
engine = std::make_shared<Engine<T>>(options);
|
||||
engine = newEngine();
|
||||
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize, maxSlotsPerGpu);
|
||||
}
|
||||
|
||||
@@ -156,7 +168,7 @@ public:
|
||||
// consumes ~300-500 MB vs ~50-100 MB for a simple loadNetwork.
|
||||
// Lightweight mode: tasks queue for a single shared slot — slower but works.
|
||||
if (!ok) {
|
||||
engine = std::make_shared<Engine<T>>(options);
|
||||
engine = newEngine();
|
||||
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
|
||||
}
|
||||
|
||||
@@ -183,7 +195,7 @@ public:
|
||||
}
|
||||
|
||||
// Final retry — try lightweight again after delay
|
||||
engine = std::make_shared<Engine<T>>(options);
|
||||
engine = newEngine();
|
||||
ok = engine->buildLoadNetwork(modelPath, subVals, divVals, normalize);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,48 @@
|
||||
#include <NvInferVersion.h> // also defines TRT version macros
|
||||
#include <cudnn_version.h> // CUDNN_MAJOR/MINOR/PATCHLEVEL
|
||||
#include <cuda_runtime.h> // cudaRuntimeGetVersion
|
||||
#include <cstring> // memset for wiping decrypted model bytes
|
||||
#include <fstream>
|
||||
|
||||
// ============================================================================
|
||||
// ONNX bytes in memory
|
||||
//
|
||||
// A sealed .ansmodel is decrypted into RAM and stays there: handing the
|
||||
// parser a buffer is what keeps the plaintext model off the disk. The build
|
||||
// path still takes a path, because the path names the TensorRT plan that is
|
||||
// cached beside it — a plan is a built artefact and carries no model.
|
||||
// ============================================================================
|
||||
template <typename T>
|
||||
void Engine<T>::setOnnxBytes(const void* data, size_t size) {
|
||||
clearOnnxBytes();
|
||||
if (data == nullptr || size == 0) return;
|
||||
const char* p = static_cast<const char*>(data);
|
||||
m_onnxBytes.assign(p, p + size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Engine<T>::clearOnnxBytes() {
|
||||
if (m_onnxBytes.empty()) return;
|
||||
std::memset(m_onnxBytes.data(), 0, m_onnxBytes.size());
|
||||
m_onnxBytes.clear();
|
||||
m_onnxBytes.shrink_to_fit();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Engine<T>::readOnnxBytes(const std::string& onnxModelPath,
|
||||
std::vector<char>& buffer) const {
|
||||
if (!m_onnxBytes.empty()) {
|
||||
buffer = m_onnxBytes;
|
||||
return true;
|
||||
}
|
||||
std::ifstream file(onnxModelPath, std::ios::binary | std::ios::ate);
|
||||
if (!file.good()) return false;
|
||||
const std::streamsize size = file.tellg();
|
||||
if (size <= 0) return false;
|
||||
file.seekg(0, std::ios::beg);
|
||||
buffer.resize(static_cast<size_t>(size));
|
||||
return static_cast<bool>(file.read(buffer.data(), size));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Engine<T>::transformOutput(std::vector<std::vector<std::vector<T>>> &input, std::vector<std::vector<T>> &output) {
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace ANSCENTER
|
||||
{1005, "ANSOCR"},
|
||||
{1006, "ANSALPR"},
|
||||
{1007, "ANSCV"},
|
||||
{1008, "ANSSRT"}
|
||||
{1008, "ANSTS"}
|
||||
};
|
||||
ansllmLicenceValid = false;
|
||||
for (const auto& [productId, productName] : licenseChecks) {
|
||||
|
||||
@@ -816,7 +816,7 @@ namespace ANSCENTER
|
||||
bool _fromPackage{ false };
|
||||
std::vector<unsigned char> _packageModel;
|
||||
std::string _packageTask; // detection, classification, …
|
||||
std::string _packageFamily; // yolo | yolo10 | rfdetr
|
||||
std::string _packageFamily; // yolo | yolo10 | rfdetr | darknet
|
||||
std::string _packageMarker; // what the zip would have carried
|
||||
std::string _packageName; // what logs call this model
|
||||
int _packageInputWidth{ 0 }; // 0: the package does not say
|
||||
|
||||
@@ -24,6 +24,7 @@ namespace ANSCENTER {
|
||||
else {
|
||||
inputImageShape = cv::Size(640, 640);
|
||||
}
|
||||
detectDarknetLayout();
|
||||
}
|
||||
|
||||
ONNXYOLO::ONNXYOLO(const std::string& _onnx_path, EngineType engineType,
|
||||
@@ -39,6 +40,7 @@ namespace ANSCENTER {
|
||||
else {
|
||||
inputImageShape = cv::Size(640, 640);
|
||||
}
|
||||
detectDarknetLayout();
|
||||
}
|
||||
|
||||
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
|
||||
@@ -54,6 +56,7 @@ namespace ANSCENTER {
|
||||
else {
|
||||
inputImageShape = cv::Size(640, 640);
|
||||
}
|
||||
detectDarknetLayout();
|
||||
}
|
||||
|
||||
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
|
||||
@@ -70,6 +73,44 @@ namespace ANSCENTER {
|
||||
else {
|
||||
inputImageShape = cv::Size(640, 640);
|
||||
}
|
||||
detectDarknetLayout();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// detectDarknetLayout — recognise a Darknet/YOLO export (hank-ai
|
||||
// darknet_onnx_export) from its outputs, once, at load time:
|
||||
// confs [B,N,nc] (objectness x class probability)
|
||||
// boxes [B,N,1,4] or [B,N,4] (x1,y1,x2,y2 normalised 0..1)
|
||||
// Matched by name first, then by shape. Must be decided before the
|
||||
// segmentation check in detect(), which would otherwise take the 4-D
|
||||
// boxes tensor for proto masks.
|
||||
// ------------------------------------------------------------------
|
||||
void ONNXYOLO::detectDarknetLayout()
|
||||
{
|
||||
isDarknetLayout = false;
|
||||
if (num_outputs != 2 || output_node_dims.size() != 2) return;
|
||||
|
||||
if (output_node_names_.size() == 2) {
|
||||
const bool byName =
|
||||
(output_node_names_[0] == "confs" && output_node_names_[1] == "boxes") ||
|
||||
(output_node_names_[0] == "boxes" && output_node_names_[1] == "confs");
|
||||
if (byName) { isDarknetLayout = true; return; }
|
||||
}
|
||||
|
||||
// By shape: exactly one output ends in 4 (the boxes), and both
|
||||
// carry the same number of candidates N in dim 1.
|
||||
auto isBoxes = [](const std::vector<int64_t>& d) {
|
||||
return (d.size() == 4 && d[2] == 1 && d[3] == 4) ||
|
||||
(d.size() == 3 && d[2] == 4);
|
||||
};
|
||||
const auto& d0 = output_node_dims[0];
|
||||
const auto& d1 = output_node_dims[1];
|
||||
if (isBoxes(d0) == isBoxes(d1)) return;
|
||||
const auto& boxes = isBoxes(d0) ? d0 : d1;
|
||||
const auto& confs = isBoxes(d0) ? d1 : d0;
|
||||
if (confs.size() != 3 || confs[2] < 1) return;
|
||||
if (confs[1] <= 0 || confs[1] != boxes[1]) return;
|
||||
isDarknetLayout = true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
@@ -143,6 +184,11 @@ namespace ANSCENTER {
|
||||
// Classification: direct resize (no letterbox padding) — matches ANSONNXCL
|
||||
cv::resize(bgrMat, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
|
||||
0, 0, cv::INTER_LINEAR);
|
||||
} else if (isDarknetLayout) {
|
||||
// Darknet: stretch the whole frame to the network size (no
|
||||
// letterbox) — Darknet trains and predicts on a stretched square
|
||||
cv::resize(bgrMat, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
|
||||
0, 0, cv::INTER_LINEAR);
|
||||
} else {
|
||||
// Detection/Seg/Pose/OBB: Ultralytics letterbox
|
||||
letterBox(bgrMat, canvas, inputImageShape);
|
||||
@@ -164,10 +210,10 @@ namespace ANSCENTER {
|
||||
if (s_loggedFirst != this) {
|
||||
s_loggedFirst = this;
|
||||
ANS_DBG("ONNXYOLO",
|
||||
"transformInto[first]: inputImageShape=(%dx%d) canvas=%dx%d cls=%d src=%dx%d dyn=%d",
|
||||
"transformInto[first]: inputImageShape=(%dx%d) canvas=%dx%d cls=%d src=%dx%d dyn=%d darknet=%d",
|
||||
inputImageShape.width, inputImageShape.height, width, height,
|
||||
isClassification ? 1 : 0, mat.cols, mat.rows,
|
||||
isDynamicInputShape ? 1 : 0);
|
||||
isDynamicInputShape ? 1 : 0, isDarknetLayout ? 1 : 0);
|
||||
}
|
||||
|
||||
outDims = { 1, 3, height, width };
|
||||
@@ -222,6 +268,10 @@ namespace ANSCENTER {
|
||||
// Classification: direct resize (no letterbox)
|
||||
cv::resize(bgrImg, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
|
||||
0, 0, cv::INTER_LINEAR);
|
||||
} else if (isDarknetLayout) {
|
||||
// Darknet: stretch resize (no letterbox)
|
||||
cv::resize(bgrImg, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
|
||||
0, 0, cv::INTER_LINEAR);
|
||||
} else {
|
||||
letterBox(bgrImg, canvas, inputImageShape);
|
||||
}
|
||||
@@ -288,6 +338,7 @@ namespace ANSCENTER {
|
||||
// detect — full pipeline with auto task detection
|
||||
//
|
||||
// Decision logic:
|
||||
// darknet layout (load time) → darknet detection
|
||||
// 2 outputs (second 4D) → segmentation
|
||||
// 1 output, 2D → classification
|
||||
// 1 output, 3D end2end dim2=6 → detection
|
||||
@@ -366,6 +417,12 @@ namespace ANSCENTER {
|
||||
|
||||
const size_t numOutputs = outputTensors.size();
|
||||
|
||||
// ── Darknet: confs + boxes (checked before seg — boxes is 4-D) ──
|
||||
if (isDarknetLayout) {
|
||||
return postprocessDarknet(image.size(), outputTensors, classNames,
|
||||
confThreshold, iouThreshold);
|
||||
}
|
||||
|
||||
// ── Segmentation: 2 outputs (detections + proto masks) ──────────
|
||||
if (numOutputs >= 2) {
|
||||
const auto protoShape = outputTensors[1].GetTensorTypeAndShapeInfo().GetShape();
|
||||
@@ -1392,6 +1449,118 @@ namespace ANSCENTER {
|
||||
return results;
|
||||
}
|
||||
|
||||
// ====================================================================
|
||||
// DARKNET — postprocess
|
||||
//
|
||||
// hank-ai darknet_onnx_export (v5.1+): confs [B,N,nc] is already
|
||||
// objectness x class probability; boxes [B,N,1,4] holds corners
|
||||
// x1,y1,x2,y2 normalised 0..1 to the (stretched) network input. The
|
||||
// graph has no NMS. Best class per candidate, threshold, map to the
|
||||
// original image (a stretch, so x*origW / y*origH), clip, then
|
||||
// class-aware NMS.
|
||||
// ====================================================================
|
||||
|
||||
std::vector<Object> ONNXYOLO::postprocessDarknet(
|
||||
const cv::Size& originalImageSize,
|
||||
std::vector<Ort::Value>& outputTensors,
|
||||
const std::vector<std::string>& classNames,
|
||||
float confThreshold, float iouThreshold, int maxDet)
|
||||
{
|
||||
if (outputTensors.size() < 2) return {};
|
||||
|
||||
// Match by name first, then by shape (the boxes tensor ends in 4)
|
||||
int confsIdx = -1, boxesIdx = -1;
|
||||
for (size_t t = 0; t < outputTensors.size() && t < output_node_names_.size(); ++t) {
|
||||
if (output_node_names_[t] == "confs") confsIdx = static_cast<int>(t);
|
||||
else if (output_node_names_[t] == "boxes") boxesIdx = static_cast<int>(t);
|
||||
}
|
||||
if (confsIdx < 0 || boxesIdx < 0) {
|
||||
confsIdx = boxesIdx = -1;
|
||||
for (size_t t = 0; t < outputTensors.size(); ++t) {
|
||||
const auto sh = outputTensors[t].GetTensorTypeAndShapeInfo().GetShape();
|
||||
if (!sh.empty() && sh.back() == 4 && boxesIdx < 0) boxesIdx = static_cast<int>(t);
|
||||
else if (confsIdx < 0) confsIdx = static_cast<int>(t);
|
||||
}
|
||||
}
|
||||
if (confsIdx < 0 || boxesIdx < 0) return {};
|
||||
|
||||
const auto confsShape = outputTensors[confsIdx].GetTensorTypeAndShapeInfo().GetShape();
|
||||
const auto boxesInfo = outputTensors[boxesIdx].GetTensorTypeAndShapeInfo();
|
||||
if (confsShape.size() != 3) return {};
|
||||
|
||||
const int numBoxes = static_cast<int>(confsShape[1]);
|
||||
const int numClasses = static_cast<int>(confsShape[2]);
|
||||
if (numBoxes <= 0 || numClasses <= 0) return {};
|
||||
if (boxesInfo.GetElementCount() != static_cast<size_t>(numBoxes) * 4) return {};
|
||||
|
||||
const float* confs = outputTensors[confsIdx].GetTensorMutableData<float>();
|
||||
const float* boxes = outputTensors[boxesIdx].GetTensorMutableData<float>();
|
||||
|
||||
const float origW = static_cast<float>(originalImageSize.width);
|
||||
const float origH = static_cast<float>(originalImageSize.height);
|
||||
|
||||
struct Candidate { float x1, y1, x2, y2, conf; int classId; };
|
||||
std::vector<Candidate> candidates;
|
||||
|
||||
for (int i = 0; i < numBoxes; ++i) {
|
||||
const float* scoresPtr = confs + static_cast<size_t>(i) * numClasses;
|
||||
float maxScore = scoresPtr[0];
|
||||
int bestClass = 0;
|
||||
for (int c = 1; c < numClasses; ++c) {
|
||||
if (scoresPtr[c] > maxScore) { maxScore = scoresPtr[c]; bestClass = c; }
|
||||
}
|
||||
if (maxScore <= confThreshold) continue;
|
||||
|
||||
const float* b = boxes + static_cast<size_t>(i) * 4;
|
||||
float x1 = clamp(b[0] * origW, 0.f, origW);
|
||||
float y1 = clamp(b[1] * origH, 0.f, origH);
|
||||
float x2 = clamp(b[2] * origW, 0.f, origW);
|
||||
float y2 = clamp(b[3] * origH, 0.f, origH);
|
||||
if (x2 - x1 < 1.f || y2 - y1 < 1.f) continue;
|
||||
candidates.push_back({ x1, y1, x2, y2, maxScore, bestClass });
|
||||
}
|
||||
if (candidates.empty()) return {};
|
||||
|
||||
// Class-aware NMS (boxes are already in image pixels, so compare
|
||||
// class ids rather than offsetting by a fixed MAX_WH)
|
||||
std::vector<int> sortedIdx(candidates.size());
|
||||
std::iota(sortedIdx.begin(), sortedIdx.end(), 0);
|
||||
std::sort(sortedIdx.begin(), sortedIdx.end(),
|
||||
[&](int a, int b) { return candidates[a].conf > candidates[b].conf; });
|
||||
|
||||
std::vector<bool> suppressed(sortedIdx.size(), false);
|
||||
std::vector<Object> results;
|
||||
results.reserve(std::min(static_cast<int>(sortedIdx.size()), maxDet));
|
||||
|
||||
for (size_t i = 0; i < sortedIdx.size() && static_cast<int>(results.size()) < maxDet; ++i) {
|
||||
if (suppressed[i]) continue;
|
||||
const auto& cur = candidates[sortedIdx[i]];
|
||||
const float curArea = (cur.x2 - cur.x1) * (cur.y2 - cur.y1);
|
||||
|
||||
for (size_t j = i + 1; j < sortedIdx.size(); ++j) {
|
||||
if (suppressed[j]) continue;
|
||||
const auto& o = candidates[sortedIdx[j]];
|
||||
if (o.classId != cur.classId) continue;
|
||||
float iw = std::min(cur.x2, o.x2) - std::max(cur.x1, o.x1);
|
||||
float ih = std::min(cur.y2, o.y2) - std::max(cur.y1, o.y1);
|
||||
if (iw <= 0.f || ih <= 0.f) continue;
|
||||
float inter = iw * ih;
|
||||
float ua = curArea + (o.x2 - o.x1) * (o.y2 - o.y1) - inter;
|
||||
if (ua > 0.f && inter / ua > iouThreshold) suppressed[j] = true;
|
||||
}
|
||||
|
||||
Object obj;
|
||||
obj.classId = cur.classId;
|
||||
obj.confidence = cur.conf;
|
||||
obj.box = cv::Rect(static_cast<int>(cur.x1), static_cast<int>(cur.y1),
|
||||
static_cast<int>(cur.x2 - cur.x1), static_cast<int>(cur.y2 - cur.y1));
|
||||
if (cur.classId >= 0 && cur.classId < static_cast<int>(classNames.size()))
|
||||
obj.className = classNames[cur.classId];
|
||||
results.push_back(std::move(obj));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
// ====================================================================
|
||||
// CLASSIFICATION — postprocess
|
||||
// ====================================================================
|
||||
@@ -1569,6 +1738,11 @@ namespace ANSCENTER {
|
||||
}
|
||||
|
||||
// Dispatch to correct postprocess method
|
||||
if (isDarknetLayout) {
|
||||
results[i] = postprocessDarknet(originalSizes[i], perImageOutputs, classNames,
|
||||
confThreshold, iouThreshold);
|
||||
continue;
|
||||
}
|
||||
if (numOutputs >= 2) {
|
||||
const auto protoShape = outputTensors[1].GetTensorTypeAndShapeInfo().GetShape();
|
||||
if (protoShape.size() == 4) {
|
||||
@@ -1718,6 +1892,8 @@ namespace ANSCENTER {
|
||||
}
|
||||
m_ortEngine = std::make_shared<class ONNXYOLO>(
|
||||
_packageModel.data(), _packageModel.size(), _packageName);
|
||||
// The manifest's family outranks what the outputs look like.
|
||||
if (_packageFamily == "darknet") m_ortEngine->setDarknetLayout(true);
|
||||
return true;
|
||||
}
|
||||
if (!FileExist(_modelFilePath)) {
|
||||
@@ -1744,6 +1920,7 @@ namespace ANSCENTER {
|
||||
}
|
||||
m_ortEngine = std::make_shared<class ONNXYOLO>(
|
||||
_packageModel.data(), _packageModel.size(), _packageName, engineType);
|
||||
if (_packageFamily == "darknet") m_ortEngine->setDarknetLayout(true);
|
||||
return true;
|
||||
}
|
||||
if (!FileExist(_modelFilePath)) {
|
||||
|
||||
@@ -19,6 +19,7 @@ namespace ANSCENTER {
|
||||
// - Segmentation: [B,300,38]+protos or [B, nc+36, N]+protos
|
||||
// - Pose : [B,300,6+nk*3] or [B, nc+4+nk*3, N] legacy
|
||||
// - Classification: [B, nc]
|
||||
// - Darknet detect: confs [B,N,nc] + boxes [B,N,1,4] (stretch resize)
|
||||
//
|
||||
// Preprocessing follows the exact Ultralytics LetterBox transform:
|
||||
// - center=True, stride=32, pad_value=114
|
||||
@@ -95,6 +96,14 @@ namespace ANSCENTER {
|
||||
/// True if the ONNX model has dynamic spatial dimensions.
|
||||
bool hasDynamicInputShape() const { return isDynamicInputShape; }
|
||||
|
||||
/// Darknet/YOLO layout (hank-ai darknet_onnx_export): outputs
|
||||
/// confs [B,N,nc] + boxes [B,N,1,4], stretch resize, no letterbox.
|
||||
/// Detected from the output names/shapes at construction; the
|
||||
/// wrapper also forces it when an .ansmodel package says its
|
||||
/// family is "darknet".
|
||||
void setDarknetLayout(bool darknet) { isDarknetLayout = darknet; }
|
||||
bool hasDarknetLayout() const { return isDarknetLayout; }
|
||||
|
||||
private:
|
||||
Ort::Value transform(const cv::Mat& mat) override;
|
||||
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
|
||||
@@ -155,6 +164,13 @@ namespace ANSCENTER {
|
||||
const std::vector<std::string>& classNames,
|
||||
float confThreshold, float iouThreshold, int numKPS, int maxDet = 300);
|
||||
|
||||
// ── Darknet postprocess (confs + boxes, no NMS in the graph) ────
|
||||
std::vector<Object> postprocessDarknet(
|
||||
const cv::Size& originalImageSize,
|
||||
std::vector<Ort::Value>& outputTensors,
|
||||
const std::vector<std::string>& classNames,
|
||||
float confThreshold, float iouThreshold, int maxDet = 300);
|
||||
|
||||
// ── Classification postprocess ──────────────────────────────────
|
||||
std::vector<Object> postprocessClassify(
|
||||
std::vector<Ort::Value>& outputTensors,
|
||||
@@ -189,6 +205,12 @@ namespace ANSCENTER {
|
||||
cv::Size inputImageShape;
|
||||
bool isDynamicInputShape{ false };
|
||||
|
||||
// Darknet layout: decided once at load time (see setDarknetLayout).
|
||||
// Selects the stretch resize in transformInto / transformBatchInto
|
||||
// and postprocessDarknet in detect / detectBatch.
|
||||
bool isDarknetLayout{ false };
|
||||
void detectDarknetLayout();
|
||||
|
||||
// Per-call preprocess that writes into caller-provided buffer + dims
|
||||
// instead of the BasicOrtHandler members (input_values_handler /
|
||||
// input_node_dims). detect() / detectBatch() use these so multiple
|
||||
|
||||
@@ -11,12 +11,37 @@
|
||||
|
||||
namespace ANSCENTER {
|
||||
|
||||
// ====================================================================
|
||||
// Sealed packages (.ansmodel)
|
||||
//
|
||||
// TensorRT still writes a plan to disk and still reads it back on the
|
||||
// next run — that is what makes the second start fast, and a plan is a
|
||||
// built artefact that carries no model. What must never be written is
|
||||
// the ONNX itself, so the builder parses the decrypted bytes from
|
||||
// memory and the path below names nothing that is ever created: it
|
||||
// only tells the plan where to live and what to be called. The folder
|
||||
// is the one the zip flow used, so a package converted from a zip
|
||||
// reuses the plan that zip already built.
|
||||
// ====================================================================
|
||||
std::string ANSRTYOLO::PackagePlanBasePath(const std::string& packagePath) {
|
||||
const std::string folder = GenerateOutputFolder(
|
||||
packagePath, GetFileNameWithoutExtension(packagePath), false);
|
||||
return CreateFilePath(folder, "train_last.onnx");
|
||||
}
|
||||
|
||||
// ====================================================================
|
||||
// ANSODBase interface — OptimizeModel
|
||||
// ====================================================================
|
||||
bool ANSRTYOLO::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
if (!ANSODBase::OptimizeModel(fp16, optimizedModelFolder)) return false;
|
||||
if (_fromPackage) {
|
||||
// The plan is built from the decrypted bytes when the engine
|
||||
// loads; there is no ONNX on disk to pre-optimise. Report the
|
||||
// folder the plan is cached in so the caller sees a real path.
|
||||
optimizedModelFolder = GetParentFolder(_modelFilePath);
|
||||
return true;
|
||||
}
|
||||
if (!FileExist(_modelFilePath)) {
|
||||
_logger.LogFatal("ANSRTYOLO::OptimizeModel", "Raw model file path does not exist", __FILE__, __LINE__);
|
||||
return false;
|
||||
@@ -96,6 +121,17 @@ namespace ANSCENTER {
|
||||
m_options.engineFileDir = _modelFolder;
|
||||
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
|
||||
|
||||
if (_fromPackage) {
|
||||
// The package already carries the classes and the geometry,
|
||||
// and there is no folder to look in. All that is needed here
|
||||
// is where the built plan lives.
|
||||
_modelFilePath = PackagePlanBasePath(modelZipFilePath);
|
||||
m_options.engineFileDir = GetParentFolder(_modelFilePath);
|
||||
// The defaults above have just overwritten the input size the
|
||||
// package asked for; put it back.
|
||||
ApplyPackageInputSize();
|
||||
}
|
||||
else {
|
||||
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
|
||||
if (FileExist(_modelConfigFile)) {
|
||||
ModelType modelType;
|
||||
@@ -112,6 +148,7 @@ namespace ANSCENTER {
|
||||
if (!isValid) LoadClassesFromString();
|
||||
else LoadClassesFromFile();
|
||||
}
|
||||
}
|
||||
|
||||
if (this->_loadEngineOnCreation) {
|
||||
if (!m_trtEngine) {
|
||||
@@ -119,7 +156,9 @@ namespace ANSCENTER {
|
||||
static_cast<int>(m_options.precision), m_options.maxBatchSize };
|
||||
m_trtEngine = EnginePoolManager<float>::instance().acquire(
|
||||
m_poolKey, m_options, _modelFilePath,
|
||||
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu);
|
||||
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu,
|
||||
_fromPackage ? _packageModel.data() : nullptr,
|
||||
_fromPackage ? _packageModel.size() : 0);
|
||||
m_usingSharedPool = (m_trtEngine != nullptr);
|
||||
}
|
||||
if (!m_trtEngine) {
|
||||
@@ -130,6 +169,7 @@ namespace ANSCENTER {
|
||||
}
|
||||
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
|
||||
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
|
||||
DetectDarknetLayout();
|
||||
m_trtEngine->warmUp();
|
||||
}
|
||||
_modelLoadValid = true;
|
||||
@@ -221,7 +261,9 @@ namespace ANSCENTER {
|
||||
static_cast<int>(m_options.precision), m_options.maxBatchSize };
|
||||
m_trtEngine = EnginePoolManager<float>::instance().acquire(
|
||||
m_poolKey, m_options, _modelFilePath,
|
||||
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu);
|
||||
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu,
|
||||
_fromPackage ? _packageModel.data() : nullptr,
|
||||
_fromPackage ? _packageModel.size() : 0);
|
||||
m_usingSharedPool = (m_trtEngine != nullptr);
|
||||
}
|
||||
if (!m_trtEngine) {
|
||||
@@ -232,6 +274,7 @@ namespace ANSCENTER {
|
||||
}
|
||||
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
|
||||
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
|
||||
DetectDarknetLayout();
|
||||
m_trtEngine->warmUp();
|
||||
}
|
||||
_modelLoadValid = true;
|
||||
@@ -295,6 +338,17 @@ namespace ANSCENTER {
|
||||
m_options.engineFileDir = _modelFolder;
|
||||
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
|
||||
|
||||
if (_fromPackage) {
|
||||
// The package already carries the classes and the geometry,
|
||||
// and there is no folder to look in. All that is needed here
|
||||
// is where the built plan lives.
|
||||
_modelFilePath = PackagePlanBasePath(modelZipFilePath);
|
||||
m_options.engineFileDir = GetParentFolder(_modelFilePath);
|
||||
// The defaults above have just overwritten the input size the
|
||||
// package asked for; put it back.
|
||||
ApplyPackageInputSize();
|
||||
}
|
||||
else {
|
||||
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
|
||||
if (FileExist(_modelConfigFile)) {
|
||||
ModelType modelType;
|
||||
@@ -311,6 +365,7 @@ namespace ANSCENTER {
|
||||
if (!isValid) LoadClassesFromString();
|
||||
else LoadClassesFromFile();
|
||||
}
|
||||
}
|
||||
|
||||
labelMap.clear();
|
||||
if (!_classes.empty())
|
||||
@@ -322,7 +377,9 @@ namespace ANSCENTER {
|
||||
static_cast<int>(m_options.precision), m_options.maxBatchSize };
|
||||
m_trtEngine = EnginePoolManager<float>::instance().acquire(
|
||||
m_poolKey, m_options, _modelFilePath,
|
||||
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu);
|
||||
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu,
|
||||
_fromPackage ? _packageModel.data() : nullptr,
|
||||
_fromPackage ? _packageModel.size() : 0);
|
||||
m_usingSharedPool = (m_trtEngine != nullptr);
|
||||
}
|
||||
if (!m_trtEngine) {
|
||||
@@ -333,6 +390,7 @@ namespace ANSCENTER {
|
||||
}
|
||||
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
|
||||
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
|
||||
DetectDarknetLayout();
|
||||
m_trtEngine->warmUp();
|
||||
}
|
||||
_modelLoadValid = true;
|
||||
@@ -456,6 +514,9 @@ namespace ANSCENTER {
|
||||
if (srcImg.rows != inputH || srcImg.cols != inputW) {
|
||||
if (isClassification) {
|
||||
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
|
||||
} else if (_isDarknet) {
|
||||
// Darknet: stretch the whole frame (no letterbox)
|
||||
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
|
||||
} else {
|
||||
cpuResized = Engine<float>::cpuResizeKeepAspectRatioPadRightBottom(srcImg, inputH, inputW);
|
||||
}
|
||||
@@ -866,12 +927,15 @@ namespace ANSCENTER {
|
||||
|
||||
const float scaleW = inputW / static_cast<float>(srcImg.cols);
|
||||
const float scaleH = inputH / static_cast<float>(srcImg.rows);
|
||||
outMetadata.ratios[i] = isClassification ? 1.f : 1.f / std::min(scaleW, scaleH);
|
||||
outMetadata.ratios[i] = (isClassification || _isDarknet) ? 1.f : 1.f / std::min(scaleW, scaleH);
|
||||
|
||||
cv::Mat cpuResized;
|
||||
if (srcImg.rows != inputH || srcImg.cols != inputW) {
|
||||
if (isClassification) {
|
||||
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
|
||||
} else if (_isDarknet) {
|
||||
// Darknet: stretch the whole frame (no letterbox)
|
||||
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
|
||||
} else {
|
||||
cpuResized = Engine<float>::cpuResizeKeepAspectRatioPadRightBottom(srcImg, inputH, inputW);
|
||||
}
|
||||
@@ -1759,6 +1823,117 @@ namespace ANSCENTER {
|
||||
}
|
||||
}
|
||||
|
||||
// ====================================================================
|
||||
// Darknet — layout detection + postprocess
|
||||
//
|
||||
// hank-ai darknet_onnx_export (v5.1+): confs [B,N,nc] is already
|
||||
// objectness x class probability; boxes [B,N,1,4] holds corners
|
||||
// x1,y1,x2,y2 normalised 0..1 to the (stretched) network input. The
|
||||
// graph has no NMS. Best class per candidate, threshold, map to the
|
||||
// original image (a stretch, so x*imgWidth / y*imgHeight), clip, then
|
||||
// class-aware NMS. TensorRT exposes no output names here, so the
|
||||
// boxes tensor is the one whose last dim is 4.
|
||||
// ====================================================================
|
||||
void ANSRTYOLO::DetectDarknetLayout() {
|
||||
_isDarknet = false;
|
||||
if (_fromPackage && _packageFamily == "darknet") {
|
||||
_isDarknet = true;
|
||||
return;
|
||||
}
|
||||
if (!m_trtEngine) return;
|
||||
const auto& outputDims = m_trtEngine->getOutputDims();
|
||||
if (outputDims.size() != 2) return;
|
||||
|
||||
auto isBoxes = [](const nvinfer1::Dims& d) {
|
||||
return (d.nbDims == 4 && d.d[2] == 1 && d.d[3] == 4) ||
|
||||
(d.nbDims == 3 && d.d[2] == 4);
|
||||
};
|
||||
const bool b0 = isBoxes(outputDims[0]);
|
||||
const bool b1 = isBoxes(outputDims[1]);
|
||||
if (b0 == b1) return;
|
||||
const auto& boxes = b0 ? outputDims[0] : outputDims[1];
|
||||
const auto& confs = b0 ? outputDims[1] : outputDims[0];
|
||||
if (confs.nbDims != 3 || confs.d[2] < 1) return;
|
||||
if (confs.d[1] <= 0 || confs.d[1] != boxes.d[1]) return;
|
||||
_isDarknet = true;
|
||||
}
|
||||
|
||||
std::vector<Object> ANSRTYOLO::PostprocessDarknet(
|
||||
std::vector<std::vector<float>>& featureVectors,
|
||||
const std::string& camera_id, const ImageMetadata& meta) {
|
||||
try {
|
||||
const auto& outputDims = m_trtEngine->getOutputDims();
|
||||
if (outputDims.size() < 2 || featureVectors.size() < 2) return {};
|
||||
|
||||
int boxesIdx = -1, confsIdx = -1;
|
||||
for (size_t t = 0; t < outputDims.size(); ++t) {
|
||||
const auto& d = outputDims[t];
|
||||
if (d.nbDims > 0 && d.d[d.nbDims - 1] == 4 && boxesIdx < 0) boxesIdx = static_cast<int>(t);
|
||||
else if (confsIdx < 0) confsIdx = static_cast<int>(t);
|
||||
}
|
||||
if (boxesIdx < 0 || confsIdx < 0) return {};
|
||||
if (outputDims[confsIdx].nbDims != 3) return {};
|
||||
|
||||
const int numClasses = outputDims[confsIdx].d[2];
|
||||
if (numClasses <= 0) return {};
|
||||
const auto& confsVec = featureVectors[confsIdx];
|
||||
const auto& boxesVec = featureVectors[boxesIdx];
|
||||
// Number of candidates from the data (dim 1 may be dynamic)
|
||||
const int numBoxes = static_cast<int>(confsVec.size() / numClasses);
|
||||
if (numBoxes <= 0 || boxesVec.size() < static_cast<size_t>(numBoxes) * 4) return {};
|
||||
|
||||
std::vector<cv::Rect> bboxes;
|
||||
std::vector<float> scores;
|
||||
std::vector<int> labels;
|
||||
std::vector<int> indices;
|
||||
|
||||
for (int i = 0; i < numBoxes; i++) {
|
||||
const float* scoresPtr = confsVec.data() + static_cast<size_t>(i) * numClasses;
|
||||
const float* maxSPtr = std::max_element(scoresPtr, scoresPtr + numClasses);
|
||||
float score = *maxSPtr;
|
||||
if (score > _modelConfig.detectionScoreThreshold) {
|
||||
const float* b = boxesVec.data() + static_cast<size_t>(i) * 4;
|
||||
float x0 = std::clamp(b[0] * meta.imgWidth, 0.f, meta.imgWidth);
|
||||
float y0 = std::clamp(b[1] * meta.imgHeight, 0.f, meta.imgHeight);
|
||||
float x1 = std::clamp(b[2] * meta.imgWidth, 0.f, meta.imgWidth);
|
||||
float y1 = std::clamp(b[3] * meta.imgHeight, 0.f, meta.imgHeight);
|
||||
if (x1 - x0 < 1.f || y1 - y0 < 1.f) continue;
|
||||
cv::Rect_<float> bbox;
|
||||
bbox.x = x0; bbox.y = y0;
|
||||
bbox.width = x1 - x0; bbox.height = y1 - y0;
|
||||
bboxes.push_back(bbox);
|
||||
labels.push_back(static_cast<int>(maxSPtr - scoresPtr));
|
||||
scores.push_back(score);
|
||||
}
|
||||
}
|
||||
|
||||
cv::dnn::NMSBoxesBatched(bboxes, scores, labels, PROBABILITY_THRESHOLD, NMS_THRESHOLD, indices);
|
||||
std::vector<Object> objects;
|
||||
int classNameSize = static_cast<int>(_classes.size());
|
||||
for (auto& chosenIdx : indices) {
|
||||
if (static_cast<int>(objects.size()) >= TOP_K) break;
|
||||
if (scores[chosenIdx] > _modelConfig.detectionScoreThreshold) {
|
||||
Object obj{};
|
||||
obj.confidence = scores[chosenIdx];
|
||||
obj.classId = labels[chosenIdx];
|
||||
obj.box = bboxes[chosenIdx];
|
||||
if (!_classes.empty()) {
|
||||
obj.className = (obj.classId < classNameSize)
|
||||
? _classes[obj.classId] : _classes[classNameSize - 1];
|
||||
}
|
||||
else { obj.className = "Unknown"; }
|
||||
obj.cameraId = camera_id;
|
||||
objects.push_back(obj);
|
||||
}
|
||||
}
|
||||
return objects;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
_logger.LogFatal("ANSRTYOLO::PostprocessDarknet", e.what(), __FILE__, __LINE__);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
// ====================================================================
|
||||
// DetectObjects — single image with auto-detection of task type
|
||||
// ====================================================================
|
||||
@@ -1801,8 +1976,11 @@ namespace ANSCENTER {
|
||||
const int inputW = inputDims[0].d[2];
|
||||
const int inputH = inputDims[0].d[1];
|
||||
|
||||
// Darknet stretches rather than letterboxes: the direct
|
||||
// (no-padding) resize kernel is the classification one.
|
||||
auto nv12 = m_nv12Helper.tryNV12(inputImage, inferenceGpu, inputW, inputH,
|
||||
NV12PreprocessHelper::defaultYOLOLauncher(),
|
||||
_isDarknet ? NV12PreprocessHelper::classificationLauncher()
|
||||
: NV12PreprocessHelper::defaultYOLOLauncher(),
|
||||
_logger, "ANSRTYOLO");
|
||||
if (nv12.succeeded) {
|
||||
meta.imgWidth = nv12.metaWidth;
|
||||
@@ -1857,7 +2035,14 @@ namespace ANSCENTER {
|
||||
const auto& outputDims = m_trtEngine->getOutputDims();
|
||||
const size_t numOutputs = outputDims.size();
|
||||
|
||||
if (numOutputs >= 2) {
|
||||
// Darknet (confs + boxes) — checked before seg, whose test
|
||||
// (2 outputs) it would otherwise pass
|
||||
if (_isDarknet) {
|
||||
std::vector<std::vector<float>> featureVector2d;
|
||||
Engine<float>::transformOutput(featureVectors, featureVector2d);
|
||||
results = PostprocessDarknet(featureVector2d, camera_id, meta);
|
||||
}
|
||||
else if (numOutputs >= 2) {
|
||||
std::vector<std::vector<float>> featureVector2d;
|
||||
Engine<float>::transformOutput(featureVectors, featureVector2d);
|
||||
double msTransform = dbg ? elapsed() : 0;
|
||||
@@ -2157,13 +2342,16 @@ namespace ANSCENTER {
|
||||
int nc = static_cast<int>(_classes.size());
|
||||
|
||||
enum class TaskType { DetLegacy, DetE2E, OBBLegacy, OBBE2E,
|
||||
SegLegacy, SegE2E, PoseLegacy, PoseE2E, Classify };
|
||||
SegLegacy, SegE2E, PoseLegacy, PoseE2E, Classify, Darknet };
|
||||
TaskType taskType = TaskType::DetLegacy; // default
|
||||
|
||||
// E2E: dim1 > dim2 (e.g. [B,300,6]); Legacy: dim1 < dim2 (e.g. [B,84,8400])
|
||||
const bool isEndToEnd = (dim1 > dim2) || (dim2 <= 20);
|
||||
|
||||
if (numOutputs >= 2) {
|
||||
if (_isDarknet) {
|
||||
taskType = TaskType::Darknet;
|
||||
}
|
||||
else if (numOutputs >= 2) {
|
||||
taskType = isEndToEnd ? TaskType::SegE2E : TaskType::SegLegacy;
|
||||
}
|
||||
else if (outputDims[0].nbDims <= 2) {
|
||||
@@ -2244,6 +2432,14 @@ namespace ANSCENTER {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TaskType::Darknet: {
|
||||
std::vector<std::vector<float>> fv2d(batchOutput.begin(), batchOutput.end());
|
||||
postFutures.push_back(std::async(std::launch::async,
|
||||
[this, fv = std::move(fv2d), cid = camera_id, m = imgMeta]() mutable {
|
||||
return PostprocessDarknet(fv, cid, m);
|
||||
}));
|
||||
break;
|
||||
}
|
||||
case TaskType::Classify: {
|
||||
std::vector<float> fv = batchOutput.empty() ? std::vector<float>{} : batchOutput[0];
|
||||
postFutures.push_back(std::async(std::launch::async,
|
||||
|
||||
@@ -22,6 +22,7 @@ namespace ANSCENTER {
|
||||
// - Segmentation: legacy [B, nc+36, N]+protos or end2end [B, 300, 38]+protos
|
||||
// - Pose : legacy [B, nc+4+nk*3, N] or end2end [B, 300, 6+nk*3]
|
||||
// - Classification: [B, nc]
|
||||
// - Darknet detect: confs [B,N,nc] + boxes [B,N,1,4] (stretch resize)
|
||||
//
|
||||
// Uses Engine<float> with GPU preprocessing (cv::cuda::GpuMat),
|
||||
// supports multi-GPU pool via SetMaxSlotsPerGpu.
|
||||
@@ -52,6 +53,11 @@ namespace ANSCENTER {
|
||||
~ANSRTYOLO();
|
||||
|
||||
private:
|
||||
// Where a sealed package's TensorRT plan is cached, and what it is
|
||||
// called. Nothing but the plan is ever written there — the model
|
||||
// itself is parsed from memory.
|
||||
static std::string PackagePlanBasePath(const std::string& packagePath);
|
||||
|
||||
std::string _modelFilePath;
|
||||
bool _modelLoadValid{ false };
|
||||
bool _fp16{ false };
|
||||
@@ -119,6 +125,18 @@ namespace ANSCENTER {
|
||||
std::vector<float>& featureVector,
|
||||
const std::string& camera_id, const ImageMetadata& meta);
|
||||
|
||||
// ── Darknet postprocessor (confs + boxes, no NMS in the graph) ───
|
||||
std::vector<Object> PostprocessDarknet(
|
||||
std::vector<std::vector<float>>& featureVectors,
|
||||
const std::string& camera_id, const ImageMetadata& meta);
|
||||
|
||||
// Darknet/YOLO layout (hank-ai darknet_onnx_export): package family
|
||||
// "darknet", or outputs shaped confs [B,N,nc] + boxes [B,N,1,4].
|
||||
// Decided once, after the engine is loaded; selects the stretch
|
||||
// resize (no letterbox) and PostprocessDarknet.
|
||||
bool _isDarknet{ false };
|
||||
void DetectDarknetLayout();
|
||||
|
||||
// ── OBB NMS helpers (Prob-IoU based) ─────────────────────────────
|
||||
struct OrientedBox {
|
||||
float x, y, width, height, angle;
|
||||
|
||||
@@ -552,6 +552,30 @@ namespace ANSCENTER {
|
||||
const float* rawOutput = outputTensors[0].GetTensorMutableData<float>();
|
||||
const std::vector<int64_t> outputShape = outputTensors[0].GetTensorTypeAndShapeInfo().GetShape();
|
||||
|
||||
if (outputShape.size() < 3) {
|
||||
return detections;
|
||||
}
|
||||
|
||||
// WHICH HEAD THIS MODEL HAS. An export with end2end on has already
|
||||
// done its own non-maximum suppression and answers [1, N, 6] —
|
||||
// rows of (x1 y1 x2 y2 score class) — where an ordinary YOLO
|
||||
// answers [1, 4+nc, anchors]. Its backend marker is still
|
||||
// tensorrt.txt either way, because the marker names the runtime
|
||||
// and not the head, so the tensor is the only thing that knows.
|
||||
//
|
||||
// Reading one as the other does not fail: it takes 300 as a
|
||||
// feature count, decides the model has 296 classes, reads
|
||||
// coordinates as class scores and returns boxes covering the
|
||||
// frame. The trap in the other direction is a two-class export,
|
||||
// whose [1, 4+nc, N] is [1, 6, 8400] — a 6 in the shape and not a
|
||||
// decoded head — so the 6 must be the LAST axis with a detection
|
||||
// count before it: hundreds, never thousands of anchors.
|
||||
if (outputShape[0] == 1 && outputShape[2] == 6 &&
|
||||
outputShape[1] > 0 && outputShape[1] <= 1000) {
|
||||
return postprocessEndToEnd(originalImageSize, resizedImageShape,
|
||||
outputTensors, confThreshold);
|
||||
}
|
||||
|
||||
// Determine the number of features and detections
|
||||
const size_t num_features = outputShape[1];
|
||||
const size_t num_detections = outputShape[2];
|
||||
@@ -675,6 +699,76 @@ namespace ANSCENTER {
|
||||
}
|
||||
}
|
||||
|
||||
// An end-to-end head has already chosen its detections: the rows are boxes
|
||||
// in model-pixel corners with a score and a class, and the suppression
|
||||
// happened inside the graph. None is run here — running one anyway would
|
||||
// hide a broken export instead of showing it.
|
||||
std::vector<Object> YOLO12OD::postprocessEndToEnd(const cv::Size& originalImageSize,
|
||||
const cv::Size& resizedImageShape,
|
||||
std::vector<Ort::Value>& outputTensors,
|
||||
float confThreshold)
|
||||
{
|
||||
std::vector<Object> detections;
|
||||
if (outputTensors.empty()) return detections;
|
||||
|
||||
// GetTensorMutableData (not GetTensorData) on DML — the const variant
|
||||
// hangs on AMD after ~8 calls. Read-only despite the name.
|
||||
const float* rawOutput = outputTensors[0].GetTensorMutableData<float>();
|
||||
const std::vector<int64_t> outputShape =
|
||||
outputTensors[0].GetTensorTypeAndShapeInfo().GetShape();
|
||||
if (outputShape.size() < 3) return detections;
|
||||
|
||||
const int numDets = static_cast<int>(outputShape[1]);
|
||||
const int numFeat = static_cast<int>(outputShape[2]);
|
||||
const int classNameSize = static_cast<int>(_classes.size());
|
||||
|
||||
detections.reserve(static_cast<size_t>(numDets));
|
||||
for (int i = 0; i < numDets; ++i) {
|
||||
const float* det = rawOutput + static_cast<size_t>(i) * numFeat;
|
||||
const float conf = det[4];
|
||||
// The rows are not obliged to be sorted, so every one is read.
|
||||
if (conf <= confThreshold) continue;
|
||||
|
||||
// Corners in model pixels, handed to the same scaler the grid path
|
||||
// uses: it undoes this engine's own letterbox and clips to the
|
||||
// frame, so both heads agree about where a box is.
|
||||
const float left = det[0];
|
||||
const float top = det[1];
|
||||
const float width = det[2] - det[0];
|
||||
const float height = det[3] - det[1];
|
||||
if (width <= 0.0f || height <= 0.0f) continue;
|
||||
|
||||
// BoundingBox is integral and these corners are not, so the
|
||||
// rounding is spelled out rather than left to a narrowing
|
||||
// conversion that would quietly truncate every box by up to a
|
||||
// pixel.
|
||||
const BoundingBox modelBox(static_cast<int>(std::lround(left)),
|
||||
static_cast<int>(std::lround(top)),
|
||||
static_cast<int>(std::lround(width)),
|
||||
static_cast<int>(std::lround(height)));
|
||||
const BoundingBox scaled =
|
||||
scaleCoords(resizedImageShape, modelBox, originalImageSize, true);
|
||||
if (scaled.width <= 0 || scaled.height <= 0) continue;
|
||||
|
||||
Object detection;
|
||||
detection.confidence = conf;
|
||||
detection.classId = static_cast<int>(det[5]);
|
||||
detection.box = cv::Rect(scaled.x, scaled.y, scaled.width, scaled.height);
|
||||
if (classNameSize > 0) {
|
||||
detection.className =
|
||||
(detection.classId >= 0 && detection.classId < classNameSize)
|
||||
? _classes[detection.classId]
|
||||
: _classes[classNameSize - 1];
|
||||
} else {
|
||||
detection.className = "Unknown";
|
||||
}
|
||||
detection.polygon = ANSUtilityHelper::RectToNormalizedPolygon(
|
||||
detection.box, m_imgWidth, m_imgHeight);
|
||||
detections.push_back(std::move(detection));
|
||||
}
|
||||
return detections;
|
||||
}
|
||||
|
||||
std::vector<Object> YOLO12OD::detect(const cv::Mat& image, float confThreshold, float iouThreshold) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
|
||||
@@ -59,6 +59,15 @@ namespace ANSCENTER {
|
||||
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
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <climits> // INT_MIN
|
||||
#include "ANSLicense.h" // ANS_DBG macro for DebugView
|
||||
#include "ANSODVendorGate.h" // ansod_vendor_gate::IsNvidiaGpuAvailable()
|
||||
#include "ANSModelOpen.h" // modelpkg::IsPackagePath (sealed .ansmodel)
|
||||
|
||||
// Process-wide flag: when true, all engines force single-GPU path (no pool, no idle timers).
|
||||
// Defined here, declared extern in EngineBuildLoadNetwork.inl.
|
||||
@@ -433,6 +434,13 @@ extern "C" ANSODENGINE_API std::string CreateANSODHandle(ANSCENTER::ANSODBase**
|
||||
// handler. Unlisted modelType values are left untouched for the switch below.
|
||||
// See CreateANSODHandleEx for the full rationale — three correctness bugs
|
||||
// were fixed in that dispatcher and must be kept in sync across copies.
|
||||
// A sealed package is an ONNX model whatever legacy type the caller names.
|
||||
// YOLOV4 = 1 goes to YOLOOD, which opens only a zip of .cfg/.weights and
|
||||
// cannot read an .ansmodel; a Darknet/YOLO package (family "darknet") runs
|
||||
// in the YOLO engines, which recognise its confs/boxes head. Routed as
|
||||
// detection so the switch below picks TensorRT or ONNX for the hardware.
|
||||
if (modelType == 1 && ANSCENTER::modelpkg::IsPackagePath(modelFilePath ? modelFilePath : ""))
|
||||
modelType = 3;
|
||||
const bool onNvidia = (engineType == ANSCENTER::EngineType::NVIDIA_GPU);
|
||||
switch (modelType) {
|
||||
// ── Detection family: YOLOv8 / V11 / V12 / generic TRT / V10-RTOD ──
|
||||
@@ -815,6 +823,13 @@ extern "C" ANSODENGINE_API int CreateANSODHandleEx(ANSCENTER::ANSODBase** Handl
|
||||
// passing modelType=20 for classification ended up with a YOLO head.
|
||||
// (3) modelType == 22 / 24 (TRT pose / TRT seg) on a non-NVIDIA box fell
|
||||
// back to ONNXYOLO instead of the correct ONNXPOSE / ONNXSEG handler.
|
||||
// A sealed package is an ONNX model whatever legacy type the caller names.
|
||||
// YOLOV4 = 1 goes to YOLOOD, which opens only a zip of .cfg/.weights and
|
||||
// cannot read an .ansmodel; a Darknet/YOLO package (family "darknet") runs
|
||||
// in the YOLO engines, which recognise its confs/boxes head. Routed as
|
||||
// detection so the switch below picks TensorRT or ONNX for the hardware.
|
||||
if (modelType == 1 && ANSCENTER::modelpkg::IsPackagePath(modelFilePath ? modelFilePath : ""))
|
||||
modelType = 3;
|
||||
const bool onNvidia = (engineType == ANSCENTER::EngineType::NVIDIA_GPU);
|
||||
switch (modelType) {
|
||||
// ── Detection family: YOLOv8 / V11 / V12 / generic TRT / V10-RTOD ──
|
||||
@@ -1576,11 +1591,21 @@ ANSODENGINE_API int OptimizeModelStr(const char* modelFilePath, const char* mode
|
||||
modelType, modelDetectionType, fp16, modelFilePath ? modelFilePath : "(null)");
|
||||
try {
|
||||
bool optimizedResult = false;
|
||||
// A sealed .ansmodel is already the artefact: there is no zip to
|
||||
// inflate and no loose ONNX to point an optimiser at. It still goes
|
||||
// through the handle below, which opens it in memory and builds the
|
||||
// TensorRT plan from the decrypted bytes — what it must not go
|
||||
// through is ModelOptimizer, which only knows how to unzip.
|
||||
const bool sealedPackage =
|
||||
ANSCENTER::modelpkg::IsPackagePath(modelFilePath ? modelFilePath : "");
|
||||
// NOTE: odMutex was removed here. OptimizeModelStr creates its own
|
||||
// temporary Engine<float> on the stack — no shared state with running
|
||||
// inference tasks. The global mutex was blocking ALL running tasks'
|
||||
// result delivery for the entire duration of TRT engine building.
|
||||
ANSCENTER::EngineType engineType = ANSCENTER::ANSLicenseHelper::CheckHardwareInformation();
|
||||
// Same rule as CreateANSODHandleEx: a sealed Darknet/YOLO package named
|
||||
// as YOLOV4 = 1 is built in the YOLO engines, not YOLOOD.
|
||||
if (modelType == 1 && sealedPackage) modelType = 3;
|
||||
|
||||
|
||||
// Route detection / pose / segmentation / OBB / classification to the best
|
||||
@@ -1628,7 +1653,7 @@ ANSODENGINE_API int OptimizeModelStr(const char* modelFilePath, const char* mode
|
||||
}
|
||||
|
||||
|
||||
if (modelType == 31) // If modelType is RTYOLO (31), handle separately.
|
||||
if (modelType == 31 && !sealedPackage) // If modelType is RTYOLO (31), handle separately.
|
||||
{
|
||||
if (engineType == ANSCENTER::EngineType::NVIDIA_GPU) {
|
||||
if (modelDetectionType == 0) {
|
||||
@@ -1778,7 +1803,10 @@ ANSODENGINE_API int OptimizeModelStr(const char* modelFilePath, const char* mode
|
||||
Handle.reset(); // Destroy engines BEFORE guard restores cache
|
||||
|
||||
|
||||
if (optimizedResult && !modelFolder.empty()) return 1;
|
||||
// A package reports the folder its plan was cached in; when the
|
||||
// handle declined to name one there is still nothing wrong, because
|
||||
// nothing was ever meant to be written.
|
||||
if (optimizedResult && (!modelFolder.empty() || sealedPackage)) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
|
||||
@@ -188,6 +188,17 @@ public:
|
||||
/// Full path of the engine file resolved by the last buildLoadNetwork().
|
||||
[[nodiscard]] const std::string& engineFilePath() const { return m_engineFilePath; }
|
||||
|
||||
// ── ONNX bytes held in memory (sealed .ansmodel packages) ───────────────
|
||||
// A package is decrypted into memory and the plaintext model must never
|
||||
// reach the disk, so the parser reads these bytes instead of opening
|
||||
// onnxModelPath. The path is still passed in and still matters: it names
|
||||
// the TensorRT plan that gets cached beside it, and a plan is a built
|
||||
// artefact, not the model. Set the bytes before any build call; they are
|
||||
// wiped by clearOnnxBytes() and by the destructor.
|
||||
void setOnnxBytes(const void* data, size_t size);
|
||||
void clearOnnxBytes();
|
||||
[[nodiscard]] bool hasOnnxBytes() const { return !m_onnxBytes.empty(); }
|
||||
|
||||
/// Return the CUDA device index that preprocessing should target.
|
||||
/// In pool mode this returns the device of the first alive slot;
|
||||
/// in single-engine mode it returns m_options.deviceIndex.
|
||||
@@ -244,6 +255,9 @@ public:
|
||||
// retries until build succeeds or all candidates are exhausted.
|
||||
// Fixed-spatial models fall through to a single build() call.
|
||||
bool buildWithRetry(std::string onnxModelPath, const std::array<float, 3>& subVals, const std::array<float, 3>& divVals, bool normalize);
|
||||
// Where the ONNX bytes come from: the in-memory buffer when one was set,
|
||||
// otherwise the file at onnxModelPath.
|
||||
bool readOnnxBytes(const std::string& onnxModelPath, std::vector<char>& buffer) const;
|
||||
void getDeviceNames(std::vector<std::string>& deviceNames);
|
||||
void clearGpuBuffers();
|
||||
int getBindingIndexByName(const std::string& name);
|
||||
@@ -401,6 +415,7 @@ private:
|
||||
bool m_usingCachedEngine = false;
|
||||
bool m_skipEngineCache = false; // true = bypass TRTEngineCache (for OptimizeModelStr)
|
||||
bool m_skipOnnxRebuild = false; // true = don't delete/rebuild corrupt engine files (elastic growth)
|
||||
std::vector<char> m_onnxBytes; // model bytes in memory (package); empty = read from disk
|
||||
bool m_rebuiltFromOnnx = false; // last buildLoadNetwork built from ONNX (no matching cached engine)
|
||||
std::string m_engineFilePath; // engine file resolved by last buildLoadNetwork
|
||||
bool m_skipOnnxBuild = false; // true = skip ONNX→TRT build if no cached engine (demand growth)
|
||||
@@ -575,6 +590,8 @@ template <typename T> Engine<T>::Engine() {
|
||||
m_lastSetInputDims.clear();
|
||||
}
|
||||
template <typename T> Engine<T>::~Engine() {
|
||||
// A decrypted model must not outlive the engine that parsed it.
|
||||
try { clearOnnxBytes(); } catch (...) {}
|
||||
// =========================================================================
|
||||
// CRITICAL: Destructors must NEVER throw. Every CUDA/TRT call below is
|
||||
// wrapped in try/catch so that a failure in one step does not skip the
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace ANSCENTER
|
||||
{1005, "ANSOCR"},
|
||||
{1006, "ANSALPR"},
|
||||
{1007, "ANSCV"},
|
||||
{1008, "ANSSRT"}
|
||||
{1008, "ANSTS"}
|
||||
};
|
||||
ansawss3LicenceValid = false;
|
||||
for (const auto& [productId, productName] : licenseChecks) {
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace ANSCENTER
|
||||
{1005, "ANSOCR"},
|
||||
{1006, "ANSALPR"},
|
||||
{1007, "ANSCV"},
|
||||
{1008, "ANSSRT"}
|
||||
{1008, "ANSTS"}
|
||||
};
|
||||
ansutilityLicenceValid = false;
|
||||
for (const auto& [productId, productName] : licenseChecks) {
|
||||
|
||||
@@ -328,12 +328,15 @@ static std::string pbDim(int64_t v) { // Dimension.dim_value
|
||||
static std::string pbDynamicDim(const std::string& name) {
|
||||
return pbField(1, pbField(2, name)); // dim_param: the axis is open
|
||||
}
|
||||
static std::string fakeOnnx(const std::string& dims) {
|
||||
const std::string tensor = pbField(1, pbField(2, dims));
|
||||
const std::string input = pbField(1, "images") + pbField(2, tensor);
|
||||
const std::string graph = pbField(1, std::string(4096, 'n')) +
|
||||
static std::string fakeOnnx(const std::string& dims,
|
||||
const std::string& outDims = std::string()) {
|
||||
auto value = [](const std::string& name, const std::string& d) {
|
||||
return pbField(1, name) + pbField(2, pbField(1, pbField(2, d)));
|
||||
};
|
||||
std::string graph = pbField(1, std::string(4096, 'n')) +
|
||||
pbField(5, std::string(65536, 'w')) +
|
||||
pbField(11, input);
|
||||
pbField(11, value("images", dims));
|
||||
if (!outDims.empty()) graph += pbField(12, value("output0", outDims));
|
||||
return pbField(7, graph);
|
||||
}
|
||||
|
||||
@@ -405,6 +408,57 @@ static void testShapeFromTheGraph(const fs::path& dir) {
|
||||
CHECK(modelpkg::OpenModelPackage(path, opened, err), ("open: " + err).c_str());
|
||||
CHECK(opened.inputWidth == 224 && opened.inputHeight == 224,
|
||||
"so the engine is told 224 from the graph, not left to assume 640");
|
||||
|
||||
// AN END-TO-END HEAD IN A PACKAGE THAT SAYS "yolo". Boxes used to take the
|
||||
// family from the backend marker, and the marker names the runtime, not the
|
||||
// head — so a yolo26 export with end2end on arrived declared as plain yolo.
|
||||
// Decoding [1,N,6] as a v8 grid reads coordinates as confidences: boxes
|
||||
// everywhere, and no error to notice. The graph settles it.
|
||||
{
|
||||
const std::string graph =
|
||||
fakeOnnx(pbDim(1) + pbDim(3) + pbDim(640) + pbDim(640),
|
||||
pbDim(1) + pbDim(300) + pbDim(6));
|
||||
modelpkg::PackRequest r = req;
|
||||
r.manifest = Json{{"task", "detection"},
|
||||
{"family", "yolo"},
|
||||
{"classes", Json::array({Json{{"id", 1}, {"name", "Tooth"}}})},
|
||||
{"models", Json::array({Json{{"id", "m0"},
|
||||
{"payload", "p0"},
|
||||
{"entry", true}}})}};
|
||||
r.models.clear();
|
||||
r.models.push_back({"p0", std::vector<uint8_t>(graph.begin(), graph.end())});
|
||||
const std::string p = (dir / "e2e.ansmodel").string();
|
||||
CHECK(modelpkg::pack(p, r, err), ("pack e2e: " + err).c_str());
|
||||
modelpkg::OpenedModel m;
|
||||
CHECK(modelpkg::OpenModelPackage(p, m, err) && m.family == "yolo10",
|
||||
"an end-to-end head is read as yolo10 whatever the package says");
|
||||
CHECK(m.warning.find("end-to-end") != std::string::npos,
|
||||
"and the reason is said out loud rather than silently swapped");
|
||||
CHECK(m.legacyMarker.empty() || m.legacyMarker == "tensorrt.txt",
|
||||
"while the legacy marker is left as it was");
|
||||
}
|
||||
|
||||
// A two-class v8 export is [1, 6, 8400]: a 6 in the shape, not a head that
|
||||
// has already decoded itself.
|
||||
{
|
||||
const std::string graph =
|
||||
fakeOnnx(pbDim(1) + pbDim(3) + pbDim(640) + pbDim(640),
|
||||
pbDim(1) + pbDim(6) + pbDim(8400));
|
||||
modelpkg::PackRequest r = req;
|
||||
r.manifest = Json{{"task", "detection"},
|
||||
{"family", "yolo"},
|
||||
{"classes", Json::array({Json{{"id", 1}, {"name", "Tooth"}}})},
|
||||
{"models", Json::array({Json{{"id", "m0"},
|
||||
{"payload", "p0"},
|
||||
{"entry", true}}})}};
|
||||
r.models.clear();
|
||||
r.models.push_back({"p0", std::vector<uint8_t>(graph.begin(), graph.end())});
|
||||
const std::string p = (dir / "twoclass.ansmodel").string();
|
||||
CHECK(modelpkg::pack(p, r, err), ("pack v8: " + err).c_str());
|
||||
modelpkg::OpenedModel m;
|
||||
CHECK(modelpkg::OpenModelPackage(p, m, err) && m.family == "yolo",
|
||||
"a 6 on the wrong axis with thousands of anchors stays plain yolo");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#include "ANSCUSTOMPY.h"
|
||||
#include "ANSMotionDetector.h"
|
||||
#include "ANSONNXSAM3.h"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
@@ -2235,8 +2237,31 @@ int FireNSmokeTest() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
// RUN A MODEL THIS MACHINE ACTUALLY HAS. Every test below names a path from
|
||||
// whichever workstation it was written on, so on any other machine the harness
|
||||
// stops at "Model zip file is not exist" before it ever reaches the GPU. Given
|
||||
// arguments it runs what it is told instead:
|
||||
//
|
||||
// ANSODEngine-UnitTest.exe <model> <video> [engineType] [detectionType]
|
||||
//
|
||||
// `model` is a .zip or a sealed .ansmodel — the engine opens either, and a
|
||||
// package is the only way to see a sealed model run on the GPU. engineType is
|
||||
// the ModelType enum (3 auto-detects a YOLO export, 30 ONNX YOLO, 16 custom,
|
||||
// 32 RTMPose); detectionType 1 is object detection.
|
||||
//
|
||||
// With no arguments it does exactly what it did before.
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc >= 3) {
|
||||
const std::string model = argv[1];
|
||||
const std::string video = argv[2];
|
||||
const int engineType = (argc > 3) ? std::atoi(argv[3]) : 3;
|
||||
const int detectionType = (argc > 4) ? std::atoi(argv[4]) : 1;
|
||||
std::cout << "model: " << model << "\nvideo: " << video
|
||||
<< "\nengineType: " << engineType
|
||||
<< ", detectionType: " << detectionType << std::endl;
|
||||
return VideoDetectorEngine_CPP(model, video, engineType, detectionType);
|
||||
}
|
||||
|
||||
//FallDetectorTest();
|
||||
//ALPROD_Test();
|
||||
|
||||
Reference in New Issue
Block a user