Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1c26c400b |
@@ -0,0 +1,74 @@
|
|||||||
|
#ifndef ANSCSPRNG_H
|
||||||
|
#define ANSCSPRNG_H
|
||||||
|
#pragma once
|
||||||
|
// ANSCsprng — random bytes from the operating system, for keys, salts and
|
||||||
|
// nonces.
|
||||||
|
//
|
||||||
|
// TAKEN FROM ANSMINDCORE (src/core/csprng.hpp). NOT std::random_device or a
|
||||||
|
// seeded generator: fine for an id that only needs to be distinct, and wrong
|
||||||
|
// for anything a guesser must not predict. The OS generator is the only
|
||||||
|
// source used for key material here.
|
||||||
|
//
|
||||||
|
// Windows BCryptGenRandom with the system-preferred RNG (bcrypt.lib)
|
||||||
|
// Linux getrandom(2), looping over short reads and EINTR
|
||||||
|
// macOS arc4random_buf, which cannot fail
|
||||||
|
//
|
||||||
|
// Returns false when the OS refused — callers treat that as "cannot seal",
|
||||||
|
// never as "seal with whatever was in the buffer".
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#ifndef NOMINMAX
|
||||||
|
#define NOMINMAX
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
#include <bcrypt.h>
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#pragma comment(lib, "bcrypt.lib")
|
||||||
|
#endif
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
#include <stdlib.h>
|
||||||
|
#else
|
||||||
|
#include <cerrno>
|
||||||
|
#include <sys/random.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace ANSCENTER {
|
||||||
|
|
||||||
|
inline bool randomBytes(uint8_t* out, size_t n) {
|
||||||
|
if (n == 0) return true;
|
||||||
|
if (!out) return false;
|
||||||
|
#if defined(_WIN32)
|
||||||
|
while (n > 0) {
|
||||||
|
const ULONG chunk = n > 0x7fffffffu ? 0x7fffffffu : ULONG(n);
|
||||||
|
if (BCryptGenRandom(nullptr, out, chunk, BCRYPT_USE_SYSTEM_PREFERRED_RNG) != 0)
|
||||||
|
return false;
|
||||||
|
out += chunk;
|
||||||
|
n -= chunk;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
arc4random_buf(out, n);
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
while (n > 0) {
|
||||||
|
const ssize_t got = getrandom(out, n, 0);
|
||||||
|
if (got < 0) {
|
||||||
|
if (errno == EINTR) continue;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out += size_t(got);
|
||||||
|
n -= size_t(got);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ANSCENTER
|
||||||
|
|
||||||
|
#endif // ANSCSPRNG_H
|
||||||
@@ -75,6 +75,7 @@
|
|||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include "ANSAead.h"
|
#include "ANSAead.h"
|
||||||
|
#include "ANSCsprng.h"
|
||||||
#include "ANSPackageBits.h"
|
#include "ANSPackageBits.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -578,9 +579,15 @@ struct PackRequest {
|
|||||||
Json card; // optional, counts only
|
Json card; // optional, counts only
|
||||||
std::vector<PackModel> models;
|
std::vector<PackModel> models;
|
||||||
uint32_t chunk = kDefaultChunk; // smaller in tests, to exercise chunking
|
uint32_t chunk = kDefaultChunk; // smaller in tests, to exercise chunking
|
||||||
// Deterministic nonces are for TESTS ONLY: a real package draws them from
|
// TESTS ONLY. pack() draws the content key, the ephemeral X25519 key and
|
||||||
// the platform CSPRNG, because a repeated (key, nonce) pair is fatal.
|
// every nonce from the operating system's CSPRNG. Setting this derives them
|
||||||
uint8_t nonceSeed = 0;
|
// from `testSeed` instead, so a fixture can be written byte-for-byte again —
|
||||||
|
// and so anyone holding the package can recover its content key by trying
|
||||||
|
// the 256 seeds, without the base key. It is honoured only in a build that
|
||||||
|
// defines ANSMODEL_DETERMINISTIC_PACK_FOR_TESTS; anywhere else pack() refuses
|
||||||
|
// rather than write a package that only looks sealed.
|
||||||
|
bool insecureDeterministicForTests = false;
|
||||||
|
uint8_t testSeed = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool pack(const std::string& path, const PackRequest& req,
|
inline bool pack(const std::string& path, const PackRequest& req,
|
||||||
@@ -589,11 +596,58 @@ inline bool pack(const std::string& path, const PackRequest& req,
|
|||||||
err = "a package with no model describes nothing";
|
err = "a package with no model describes nothing";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// A content key per package. Tests pass a seed; a real packer must use the
|
// Everything the package's secrecy rests on, drawn up front: the content
|
||||||
// platform CSPRNG here.
|
// key, the ephemeral private key it is wrapped with, the wrap nonce and one
|
||||||
|
// nonce base per sealed part. From the OS CSPRNG — anything a holder of the
|
||||||
|
// file could re-derive would open it without the base key.
|
||||||
Key contentKey{};
|
Key contentKey{};
|
||||||
|
Key esk{};
|
||||||
|
std::array<uint8_t, aead::kNonceBytes> wrapNonce{};
|
||||||
|
std::array<uint8_t, 4> manifestBase{};
|
||||||
|
std::vector<std::array<uint8_t, 4>> bases(req.models.size());
|
||||||
|
struct Wipe {
|
||||||
|
Key& a;
|
||||||
|
Key& b;
|
||||||
|
~Wipe() {
|
||||||
|
crypto_wipe(a.data(), a.size());
|
||||||
|
crypto_wipe(b.data(), b.size());
|
||||||
|
}
|
||||||
|
} wipe{contentKey, esk};
|
||||||
|
if (req.insecureDeterministicForTests) {
|
||||||
|
#if defined(ANSMODEL_DETERMINISTIC_PACK_FOR_TESTS)
|
||||||
for (size_t i = 0; i < contentKey.size(); ++i)
|
for (size_t i = 0; i < contentKey.size(); ++i)
|
||||||
contentKey[i] = uint8_t(req.nonceSeed * 31 + i * 7 + 11);
|
contentKey[i] = uint8_t(req.testSeed * 31 + i * 7 + 11);
|
||||||
|
for (size_t i = 0; i < esk.size(); ++i)
|
||||||
|
esk[i] = uint8_t(req.testSeed * 13 + i * 5 + 3);
|
||||||
|
for (size_t i = 0; i < wrapNonce.size(); ++i)
|
||||||
|
wrapNonce[i] = uint8_t(req.testSeed + 100 + i);
|
||||||
|
manifestBase = {uint8_t(req.testSeed + 1), 2, 3, 4};
|
||||||
|
for (size_t i = 0; i < bases.size(); ++i)
|
||||||
|
bases[i] = {uint8_t(req.testSeed + 10 + i), 5, 6, 7};
|
||||||
|
#else
|
||||||
|
err = "deterministic packing is for tests only, and this build is not one";
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
bool ok = ANSCENTER::randomBytes(contentKey.data(), contentKey.size()) &&
|
||||||
|
ANSCENTER::randomBytes(esk.data(), esk.size()) &&
|
||||||
|
ANSCENTER::randomBytes(wrapNonce.data(), wrapNonce.size()) &&
|
||||||
|
ANSCENTER::randomBytes(manifestBase.data(), manifestBase.size());
|
||||||
|
// The parts share the content key, so their bases must differ: two parts
|
||||||
|
// on one base would reuse every chunk nonce they have in common.
|
||||||
|
for (size_t i = 0; ok && i < bases.size(); ++i) {
|
||||||
|
bool clash = true;
|
||||||
|
while (ok && clash) {
|
||||||
|
ok = ANSCENTER::randomBytes(bases[i].data(), bases[i].size());
|
||||||
|
clash = bases[i] == manifestBase;
|
||||||
|
for (size_t j = 0; j < i; ++j) clash = clash || bases[i] == bases[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!ok) {
|
||||||
|
err = "the operating system gave no random bytes, so nothing was sealed";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto sealChunked = [&](const std::vector<uint8_t>& plain,
|
auto sealChunked = [&](const std::vector<uint8_t>& plain,
|
||||||
const std::string& payloadId,
|
const std::string& payloadId,
|
||||||
@@ -616,7 +670,6 @@ inline bool pack(const std::string& path, const PackRequest& req,
|
|||||||
const std::string manifestText = req.manifest.dump();
|
const std::string manifestText = req.manifest.dump();
|
||||||
const std::vector<uint8_t> manifestBytes(manifestText.begin(),
|
const std::vector<uint8_t> manifestBytes(manifestText.begin(),
|
||||||
manifestText.end());
|
manifestText.end());
|
||||||
std::array<uint8_t, 4> manifestBase{uint8_t(req.nonceSeed + 1), 2, 3, 4};
|
|
||||||
std::string manifestBlob;
|
std::string manifestBlob;
|
||||||
{
|
{
|
||||||
const auto sealed =
|
const auto sealed =
|
||||||
@@ -628,20 +681,14 @@ inline bool pack(const std::string& path, const PackRequest& req,
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> blobs;
|
std::vector<std::string> blobs;
|
||||||
std::vector<std::array<uint8_t, 4>> bases;
|
|
||||||
for (size_t i = 0; i < req.models.size(); ++i) {
|
for (size_t i = 0; i < req.models.size(); ++i) {
|
||||||
std::array<uint8_t, 4> base{uint8_t(req.nonceSeed + 10 + i), 5, 6, 7};
|
|
||||||
std::string blob;
|
std::string blob;
|
||||||
sealChunked(req.models[i].bytes, req.models[i].id, base, blob);
|
sealChunked(req.models[i].bytes, req.models[i].id, bases[i], blob);
|
||||||
blobs.push_back(std::move(blob));
|
blobs.push_back(std::move(blob));
|
||||||
bases.push_back(base);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrap the content key to the base key. One wrap here; the header's shape
|
// Wrap the content key to the base key. One wrap here; the header's shape
|
||||||
// takes several, which is how a package opens on more than one key.
|
// takes several, which is how a package opens on more than one key.
|
||||||
std::array<uint8_t, aead::kNonceBytes> wrapNonce{};
|
|
||||||
for (size_t i = 0; i < wrapNonce.size(); ++i)
|
|
||||||
wrapNonce[i] = uint8_t(req.nonceSeed + 100 + i);
|
|
||||||
const std::vector<uint8_t> keyPlain(contentKey.begin(), contentKey.end());
|
const std::vector<uint8_t> keyPlain(contentKey.begin(), contentKey.end());
|
||||||
|
|
||||||
Key wrapKey = req.key;
|
Key wrapKey = req.key;
|
||||||
@@ -650,9 +697,6 @@ inline bool pack(const std::string& path, const PackRequest& req,
|
|||||||
// An ephemeral key pair per package: its public half travels in the
|
// An ephemeral key pair per package: its public half travels in the
|
||||||
// header, its private half dies here, and the packer never holds anything
|
// header, its private half dies here, and the packer never holds anything
|
||||||
// that opens the result.
|
// that opens the result.
|
||||||
Key esk{};
|
|
||||||
for (size_t i = 0; i < esk.size(); ++i)
|
|
||||||
esk[i] = uint8_t(req.nonceSeed * 13 + i * 5 + 3);
|
|
||||||
epk.assign(32, 0);
|
epk.assign(32, 0);
|
||||||
crypto_x25519_public_key(epk.data(), esk.data());
|
crypto_x25519_public_key(epk.data(), esk.data());
|
||||||
uint8_t shared[32];
|
uint8_t shared[32];
|
||||||
|
|||||||
@@ -40,4 +40,9 @@ else()
|
|||||||
"(run ansmodel-keys emit --product anscore, then re-run cmake)")
|
"(run ansmodel-keys emit --product anscore, then re-run cmake)")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
# BCryptGenRandom, behind the content key and nonces pack() draws (ANSCsprng.h).
|
||||||
|
if(WIN32)
|
||||||
|
target_link_libraries(ANSModelPackage PUBLIC bcrypt)
|
||||||
|
endif()
|
||||||
|
|
||||||
set_target_properties(ANSModelPackage PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
set_target_properties(ANSModelPackage PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||||
|
|||||||
@@ -816,7 +816,7 @@ namespace ANSCENTER
|
|||||||
bool _fromPackage{ false };
|
bool _fromPackage{ false };
|
||||||
std::vector<unsigned char> _packageModel;
|
std::vector<unsigned char> _packageModel;
|
||||||
std::string _packageTask; // detection, classification, …
|
std::string _packageTask; // detection, classification, …
|
||||||
std::string _packageFamily; // yolo | yolo10 | rfdetr | darknet
|
std::string _packageFamily; // yolo | yolo10 | rfdetr
|
||||||
std::string _packageMarker; // what the zip would have carried
|
std::string _packageMarker; // what the zip would have carried
|
||||||
std::string _packageName; // what logs call this model
|
std::string _packageName; // what logs call this model
|
||||||
int _packageInputWidth{ 0 }; // 0: the package does not say
|
int _packageInputWidth{ 0 }; // 0: the package does not say
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ namespace ANSCENTER {
|
|||||||
else {
|
else {
|
||||||
inputImageShape = cv::Size(640, 640);
|
inputImageShape = cv::Size(640, 640);
|
||||||
}
|
}
|
||||||
detectDarknetLayout();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ONNXYOLO::ONNXYOLO(const std::string& _onnx_path, EngineType engineType,
|
ONNXYOLO::ONNXYOLO(const std::string& _onnx_path, EngineType engineType,
|
||||||
@@ -40,7 +39,6 @@ namespace ANSCENTER {
|
|||||||
else {
|
else {
|
||||||
inputImageShape = cv::Size(640, 640);
|
inputImageShape = cv::Size(640, 640);
|
||||||
}
|
}
|
||||||
detectDarknetLayout();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
|
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
|
||||||
@@ -56,7 +54,6 @@ namespace ANSCENTER {
|
|||||||
else {
|
else {
|
||||||
inputImageShape = cv::Size(640, 640);
|
inputImageShape = cv::Size(640, 640);
|
||||||
}
|
}
|
||||||
detectDarknetLayout();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
|
ONNXYOLO::ONNXYOLO(const void* modelData, size_t modelSize,
|
||||||
@@ -73,44 +70,6 @@ namespace ANSCENTER {
|
|||||||
else {
|
else {
|
||||||
inputImageShape = cv::Size(640, 640);
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------
|
// ------------------------------------------------------------------
|
||||||
@@ -184,11 +143,6 @@ namespace ANSCENTER {
|
|||||||
// Classification: direct resize (no letterbox padding) — matches ANSONNXCL
|
// Classification: direct resize (no letterbox padding) — matches ANSONNXCL
|
||||||
cv::resize(bgrMat, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
|
cv::resize(bgrMat, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
|
||||||
0, 0, cv::INTER_LINEAR);
|
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 {
|
} else {
|
||||||
// Detection/Seg/Pose/OBB: Ultralytics letterbox
|
// Detection/Seg/Pose/OBB: Ultralytics letterbox
|
||||||
letterBox(bgrMat, canvas, inputImageShape);
|
letterBox(bgrMat, canvas, inputImageShape);
|
||||||
@@ -210,10 +164,10 @@ namespace ANSCENTER {
|
|||||||
if (s_loggedFirst != this) {
|
if (s_loggedFirst != this) {
|
||||||
s_loggedFirst = this;
|
s_loggedFirst = this;
|
||||||
ANS_DBG("ONNXYOLO",
|
ANS_DBG("ONNXYOLO",
|
||||||
"transformInto[first]: inputImageShape=(%dx%d) canvas=%dx%d cls=%d src=%dx%d dyn=%d darknet=%d",
|
"transformInto[first]: inputImageShape=(%dx%d) canvas=%dx%d cls=%d src=%dx%d dyn=%d",
|
||||||
inputImageShape.width, inputImageShape.height, width, height,
|
inputImageShape.width, inputImageShape.height, width, height,
|
||||||
isClassification ? 1 : 0, mat.cols, mat.rows,
|
isClassification ? 1 : 0, mat.cols, mat.rows,
|
||||||
isDynamicInputShape ? 1 : 0, isDarknetLayout ? 1 : 0);
|
isDynamicInputShape ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
outDims = { 1, 3, height, width };
|
outDims = { 1, 3, height, width };
|
||||||
@@ -268,10 +222,6 @@ namespace ANSCENTER {
|
|||||||
// Classification: direct resize (no letterbox)
|
// Classification: direct resize (no letterbox)
|
||||||
cv::resize(bgrImg, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
|
cv::resize(bgrImg, canvas, cv::Size(inputImageShape.width, inputImageShape.height),
|
||||||
0, 0, cv::INTER_LINEAR);
|
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 {
|
} else {
|
||||||
letterBox(bgrImg, canvas, inputImageShape);
|
letterBox(bgrImg, canvas, inputImageShape);
|
||||||
}
|
}
|
||||||
@@ -338,7 +288,6 @@ namespace ANSCENTER {
|
|||||||
// detect — full pipeline with auto task detection
|
// detect — full pipeline with auto task detection
|
||||||
//
|
//
|
||||||
// Decision logic:
|
// Decision logic:
|
||||||
// darknet layout (load time) → darknet detection
|
|
||||||
// 2 outputs (second 4D) → segmentation
|
// 2 outputs (second 4D) → segmentation
|
||||||
// 1 output, 2D → classification
|
// 1 output, 2D → classification
|
||||||
// 1 output, 3D end2end dim2=6 → detection
|
// 1 output, 3D end2end dim2=6 → detection
|
||||||
@@ -417,12 +366,6 @@ namespace ANSCENTER {
|
|||||||
|
|
||||||
const size_t numOutputs = outputTensors.size();
|
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) ──────────
|
// ── Segmentation: 2 outputs (detections + proto masks) ──────────
|
||||||
if (numOutputs >= 2) {
|
if (numOutputs >= 2) {
|
||||||
const auto protoShape = outputTensors[1].GetTensorTypeAndShapeInfo().GetShape();
|
const auto protoShape = outputTensors[1].GetTensorTypeAndShapeInfo().GetShape();
|
||||||
@@ -1449,118 +1392,6 @@ namespace ANSCENTER {
|
|||||||
return results;
|
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
|
// CLASSIFICATION — postprocess
|
||||||
// ====================================================================
|
// ====================================================================
|
||||||
@@ -1738,11 +1569,6 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dispatch to correct postprocess method
|
// Dispatch to correct postprocess method
|
||||||
if (isDarknetLayout) {
|
|
||||||
results[i] = postprocessDarknet(originalSizes[i], perImageOutputs, classNames,
|
|
||||||
confThreshold, iouThreshold);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (numOutputs >= 2) {
|
if (numOutputs >= 2) {
|
||||||
const auto protoShape = outputTensors[1].GetTensorTypeAndShapeInfo().GetShape();
|
const auto protoShape = outputTensors[1].GetTensorTypeAndShapeInfo().GetShape();
|
||||||
if (protoShape.size() == 4) {
|
if (protoShape.size() == 4) {
|
||||||
@@ -1892,8 +1718,6 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
m_ortEngine = std::make_shared<class ONNXYOLO>(
|
m_ortEngine = std::make_shared<class ONNXYOLO>(
|
||||||
_packageModel.data(), _packageModel.size(), _packageName);
|
_packageModel.data(), _packageModel.size(), _packageName);
|
||||||
// The manifest's family outranks what the outputs look like.
|
|
||||||
if (_packageFamily == "darknet") m_ortEngine->setDarknetLayout(true);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!FileExist(_modelFilePath)) {
|
if (!FileExist(_modelFilePath)) {
|
||||||
@@ -1920,7 +1744,6 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
m_ortEngine = std::make_shared<class ONNXYOLO>(
|
m_ortEngine = std::make_shared<class ONNXYOLO>(
|
||||||
_packageModel.data(), _packageModel.size(), _packageName, engineType);
|
_packageModel.data(), _packageModel.size(), _packageName, engineType);
|
||||||
if (_packageFamily == "darknet") m_ortEngine->setDarknetLayout(true);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!FileExist(_modelFilePath)) {
|
if (!FileExist(_modelFilePath)) {
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ namespace ANSCENTER {
|
|||||||
// - Segmentation: [B,300,38]+protos or [B, nc+36, N]+protos
|
// - 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
|
// - Pose : [B,300,6+nk*3] or [B, nc+4+nk*3, N] legacy
|
||||||
// - Classification: [B, nc]
|
// - Classification: [B, nc]
|
||||||
// - Darknet detect: confs [B,N,nc] + boxes [B,N,1,4] (stretch resize)
|
|
||||||
//
|
//
|
||||||
// Preprocessing follows the exact Ultralytics LetterBox transform:
|
// Preprocessing follows the exact Ultralytics LetterBox transform:
|
||||||
// - center=True, stride=32, pad_value=114
|
// - center=True, stride=32, pad_value=114
|
||||||
@@ -96,14 +95,6 @@ namespace ANSCENTER {
|
|||||||
/// True if the ONNX model has dynamic spatial dimensions.
|
/// True if the ONNX model has dynamic spatial dimensions.
|
||||||
bool hasDynamicInputShape() const { return isDynamicInputShape; }
|
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:
|
private:
|
||||||
Ort::Value transform(const cv::Mat& mat) override;
|
Ort::Value transform(const cv::Mat& mat) override;
|
||||||
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
|
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
|
||||||
@@ -164,13 +155,6 @@ namespace ANSCENTER {
|
|||||||
const std::vector<std::string>& classNames,
|
const std::vector<std::string>& classNames,
|
||||||
float confThreshold, float iouThreshold, int numKPS, int maxDet = 300);
|
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 ──────────────────────────────────
|
// ── Classification postprocess ──────────────────────────────────
|
||||||
std::vector<Object> postprocessClassify(
|
std::vector<Object> postprocessClassify(
|
||||||
std::vector<Ort::Value>& outputTensors,
|
std::vector<Ort::Value>& outputTensors,
|
||||||
@@ -205,12 +189,6 @@ namespace ANSCENTER {
|
|||||||
cv::Size inputImageShape;
|
cv::Size inputImageShape;
|
||||||
bool isDynamicInputShape{ false };
|
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
|
// Per-call preprocess that writes into caller-provided buffer + dims
|
||||||
// instead of the BasicOrtHandler members (input_values_handler /
|
// instead of the BasicOrtHandler members (input_values_handler /
|
||||||
// input_node_dims). detect() / detectBatch() use these so multiple
|
// input_node_dims). detect() / detectBatch() use these so multiple
|
||||||
|
|||||||
@@ -169,7 +169,6 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
|
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
|
||||||
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
|
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
|
||||||
DetectDarknetLayout();
|
|
||||||
m_trtEngine->warmUp();
|
m_trtEngine->warmUp();
|
||||||
}
|
}
|
||||||
_modelLoadValid = true;
|
_modelLoadValid = true;
|
||||||
@@ -274,7 +273,6 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
|
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
|
||||||
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
|
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
|
||||||
DetectDarknetLayout();
|
|
||||||
m_trtEngine->warmUp();
|
m_trtEngine->warmUp();
|
||||||
}
|
}
|
||||||
_modelLoadValid = true;
|
_modelLoadValid = true;
|
||||||
@@ -390,7 +388,6 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
|
m_options.maxBatchSize = m_trtEngine->getOptions().maxBatchSize;
|
||||||
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
|
m_options.optBatchSize = m_trtEngine->getOptions().optBatchSize;
|
||||||
DetectDarknetLayout();
|
|
||||||
m_trtEngine->warmUp();
|
m_trtEngine->warmUp();
|
||||||
}
|
}
|
||||||
_modelLoadValid = true;
|
_modelLoadValid = true;
|
||||||
@@ -514,9 +511,6 @@ namespace ANSCENTER {
|
|||||||
if (srcImg.rows != inputH || srcImg.cols != inputW) {
|
if (srcImg.rows != inputH || srcImg.cols != inputW) {
|
||||||
if (isClassification) {
|
if (isClassification) {
|
||||||
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
|
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 {
|
} else {
|
||||||
cpuResized = Engine<float>::cpuResizeKeepAspectRatioPadRightBottom(srcImg, inputH, inputW);
|
cpuResized = Engine<float>::cpuResizeKeepAspectRatioPadRightBottom(srcImg, inputH, inputW);
|
||||||
}
|
}
|
||||||
@@ -927,15 +921,12 @@ namespace ANSCENTER {
|
|||||||
|
|
||||||
const float scaleW = inputW / static_cast<float>(srcImg.cols);
|
const float scaleW = inputW / static_cast<float>(srcImg.cols);
|
||||||
const float scaleH = inputH / static_cast<float>(srcImg.rows);
|
const float scaleH = inputH / static_cast<float>(srcImg.rows);
|
||||||
outMetadata.ratios[i] = (isClassification || _isDarknet) ? 1.f : 1.f / std::min(scaleW, scaleH);
|
outMetadata.ratios[i] = isClassification ? 1.f : 1.f / std::min(scaleW, scaleH);
|
||||||
|
|
||||||
cv::Mat cpuResized;
|
cv::Mat cpuResized;
|
||||||
if (srcImg.rows != inputH || srcImg.cols != inputW) {
|
if (srcImg.rows != inputH || srcImg.cols != inputW) {
|
||||||
if (isClassification) {
|
if (isClassification) {
|
||||||
cv::resize(srcImg, cpuResized, cv::Size(inputW, inputH), 0, 0, cv::INTER_LINEAR);
|
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 {
|
} else {
|
||||||
cpuResized = Engine<float>::cpuResizeKeepAspectRatioPadRightBottom(srcImg, inputH, inputW);
|
cpuResized = Engine<float>::cpuResizeKeepAspectRatioPadRightBottom(srcImg, inputH, inputW);
|
||||||
}
|
}
|
||||||
@@ -1823,117 +1814,6 @@ 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
|
// DetectObjects — single image with auto-detection of task type
|
||||||
// ====================================================================
|
// ====================================================================
|
||||||
@@ -1976,11 +1856,8 @@ namespace ANSCENTER {
|
|||||||
const int inputW = inputDims[0].d[2];
|
const int inputW = inputDims[0].d[2];
|
||||||
const int inputH = inputDims[0].d[1];
|
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,
|
auto nv12 = m_nv12Helper.tryNV12(inputImage, inferenceGpu, inputW, inputH,
|
||||||
_isDarknet ? NV12PreprocessHelper::classificationLauncher()
|
NV12PreprocessHelper::defaultYOLOLauncher(),
|
||||||
: NV12PreprocessHelper::defaultYOLOLauncher(),
|
|
||||||
_logger, "ANSRTYOLO");
|
_logger, "ANSRTYOLO");
|
||||||
if (nv12.succeeded) {
|
if (nv12.succeeded) {
|
||||||
meta.imgWidth = nv12.metaWidth;
|
meta.imgWidth = nv12.metaWidth;
|
||||||
@@ -2035,14 +1912,7 @@ namespace ANSCENTER {
|
|||||||
const auto& outputDims = m_trtEngine->getOutputDims();
|
const auto& outputDims = m_trtEngine->getOutputDims();
|
||||||
const size_t numOutputs = outputDims.size();
|
const size_t numOutputs = outputDims.size();
|
||||||
|
|
||||||
// Darknet (confs + boxes) — checked before seg, whose test
|
if (numOutputs >= 2) {
|
||||||
// (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;
|
std::vector<std::vector<float>> featureVector2d;
|
||||||
Engine<float>::transformOutput(featureVectors, featureVector2d);
|
Engine<float>::transformOutput(featureVectors, featureVector2d);
|
||||||
double msTransform = dbg ? elapsed() : 0;
|
double msTransform = dbg ? elapsed() : 0;
|
||||||
@@ -2342,16 +2212,13 @@ namespace ANSCENTER {
|
|||||||
int nc = static_cast<int>(_classes.size());
|
int nc = static_cast<int>(_classes.size());
|
||||||
|
|
||||||
enum class TaskType { DetLegacy, DetE2E, OBBLegacy, OBBE2E,
|
enum class TaskType { DetLegacy, DetE2E, OBBLegacy, OBBE2E,
|
||||||
SegLegacy, SegE2E, PoseLegacy, PoseE2E, Classify, Darknet };
|
SegLegacy, SegE2E, PoseLegacy, PoseE2E, Classify };
|
||||||
TaskType taskType = TaskType::DetLegacy; // default
|
TaskType taskType = TaskType::DetLegacy; // default
|
||||||
|
|
||||||
// E2E: dim1 > dim2 (e.g. [B,300,6]); Legacy: dim1 < dim2 (e.g. [B,84,8400])
|
// E2E: dim1 > dim2 (e.g. [B,300,6]); Legacy: dim1 < dim2 (e.g. [B,84,8400])
|
||||||
const bool isEndToEnd = (dim1 > dim2) || (dim2 <= 20);
|
const bool isEndToEnd = (dim1 > dim2) || (dim2 <= 20);
|
||||||
|
|
||||||
if (_isDarknet) {
|
if (numOutputs >= 2) {
|
||||||
taskType = TaskType::Darknet;
|
|
||||||
}
|
|
||||||
else if (numOutputs >= 2) {
|
|
||||||
taskType = isEndToEnd ? TaskType::SegE2E : TaskType::SegLegacy;
|
taskType = isEndToEnd ? TaskType::SegE2E : TaskType::SegLegacy;
|
||||||
}
|
}
|
||||||
else if (outputDims[0].nbDims <= 2) {
|
else if (outputDims[0].nbDims <= 2) {
|
||||||
@@ -2432,14 +2299,6 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
break;
|
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: {
|
case TaskType::Classify: {
|
||||||
std::vector<float> fv = batchOutput.empty() ? std::vector<float>{} : batchOutput[0];
|
std::vector<float> fv = batchOutput.empty() ? std::vector<float>{} : batchOutput[0];
|
||||||
postFutures.push_back(std::async(std::launch::async,
|
postFutures.push_back(std::async(std::launch::async,
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ namespace ANSCENTER {
|
|||||||
// - Segmentation: legacy [B, nc+36, N]+protos or end2end [B, 300, 38]+protos
|
// - 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]
|
// - Pose : legacy [B, nc+4+nk*3, N] or end2end [B, 300, 6+nk*3]
|
||||||
// - Classification: [B, nc]
|
// - 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),
|
// Uses Engine<float> with GPU preprocessing (cv::cuda::GpuMat),
|
||||||
// supports multi-GPU pool via SetMaxSlotsPerGpu.
|
// supports multi-GPU pool via SetMaxSlotsPerGpu.
|
||||||
@@ -125,18 +124,6 @@ namespace ANSCENTER {
|
|||||||
std::vector<float>& featureVector,
|
std::vector<float>& featureVector,
|
||||||
const std::string& camera_id, const ImageMetadata& meta);
|
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) ─────────────────────────────
|
// ── OBB NMS helpers (Prob-IoU based) ─────────────────────────────
|
||||||
struct OrientedBox {
|
struct OrientedBox {
|
||||||
float x, y, width, height, angle;
|
float x, y, width, height, angle;
|
||||||
|
|||||||
@@ -434,13 +434,6 @@ extern "C" ANSODENGINE_API std::string CreateANSODHandle(ANSCENTER::ANSODBase**
|
|||||||
// handler. Unlisted modelType values are left untouched for the switch below.
|
// handler. Unlisted modelType values are left untouched for the switch below.
|
||||||
// See CreateANSODHandleEx for the full rationale — three correctness bugs
|
// See CreateANSODHandleEx for the full rationale — three correctness bugs
|
||||||
// were fixed in that dispatcher and must be kept in sync across copies.
|
// 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);
|
const bool onNvidia = (engineType == ANSCENTER::EngineType::NVIDIA_GPU);
|
||||||
switch (modelType) {
|
switch (modelType) {
|
||||||
// ── Detection family: YOLOv8 / V11 / V12 / generic TRT / V10-RTOD ──
|
// ── Detection family: YOLOv8 / V11 / V12 / generic TRT / V10-RTOD ──
|
||||||
@@ -823,13 +816,6 @@ extern "C" ANSODENGINE_API int CreateANSODHandleEx(ANSCENTER::ANSODBase** Handl
|
|||||||
// passing modelType=20 for classification ended up with a YOLO head.
|
// 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
|
// (3) modelType == 22 / 24 (TRT pose / TRT seg) on a non-NVIDIA box fell
|
||||||
// back to ONNXYOLO instead of the correct ONNXPOSE / ONNXSEG handler.
|
// 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);
|
const bool onNvidia = (engineType == ANSCENTER::EngineType::NVIDIA_GPU);
|
||||||
switch (modelType) {
|
switch (modelType) {
|
||||||
// ── Detection family: YOLOv8 / V11 / V12 / generic TRT / V10-RTOD ──
|
// ── Detection family: YOLOv8 / V11 / V12 / generic TRT / V10-RTOD ──
|
||||||
@@ -1603,9 +1589,6 @@ ANSODENGINE_API int OptimizeModelStr(const char* modelFilePath, const char* mode
|
|||||||
// inference tasks. The global mutex was blocking ALL running tasks'
|
// inference tasks. The global mutex was blocking ALL running tasks'
|
||||||
// result delivery for the entire duration of TRT engine building.
|
// result delivery for the entire duration of TRT engine building.
|
||||||
ANSCENTER::EngineType engineType = ANSCENTER::ANSLicenseHelper::CheckHardwareInformation();
|
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
|
// Route detection / pose / segmentation / OBB / classification to the best
|
||||||
|
|||||||
@@ -162,7 +162,6 @@ static void testRoundTripAndRefusals(const fs::path& dir) {
|
|||||||
req.useX25519 = true;
|
req.useX25519 = true;
|
||||||
req.basePublic = id.basePub;
|
req.basePublic = id.basePub;
|
||||||
req.chunk = 2048;
|
req.chunk = 2048;
|
||||||
req.nonceSeed = 9;
|
|
||||||
req.signerId = "anssig-2026-09";
|
req.signerId = "anssig-2026-09";
|
||||||
req.signerSecret = id.signerSk;
|
req.signerSecret = id.signerSk;
|
||||||
req.signerPublic = id.signerPk;
|
req.signerPublic = id.signerPk;
|
||||||
@@ -252,6 +251,79 @@ static void testRoundTripAndRefusals(const fs::path& dir) {
|
|||||||
// ANSODBase asks these three questions and nothing else: is this path a
|
// ANSODBase asks these three questions and nothing else: is this path a
|
||||||
// package, can this build open one, and what came out of it. The engines
|
// package, can this build open one, and what came out of it. The engines
|
||||||
// themselves need CUDA to compile, so this is where that contract is checked.
|
// themselves need CUDA to compile, so this is where that contract is checked.
|
||||||
|
// ── every package gets its own key ────────────────────────────────────────
|
||||||
|
// pack() used to derive the content key, the ephemeral key and the nonces from
|
||||||
|
// one byte of seed, and the training box passed a seed made of the project's
|
||||||
|
// name and version: anyone holding a package could try the 256 seeds and read
|
||||||
|
// it without the base key. Two packs of the very same inputs must now share
|
||||||
|
// nothing that seals them — and still open with the base key.
|
||||||
|
static void testEachPackIsSealedAfresh(const fs::path& dir) {
|
||||||
|
const Identity id = fixtureIdentity();
|
||||||
|
const modelpkg::Keyring ring = fixtureKeyring(id);
|
||||||
|
|
||||||
|
modelpkg::PackRequest req;
|
||||||
|
req.keyId = "ansk-v1-base";
|
||||||
|
req.useX25519 = true;
|
||||||
|
req.basePublic = id.basePub;
|
||||||
|
req.chunk = 2048;
|
||||||
|
req.manifest = Json{{"task", "detection"},
|
||||||
|
{"family", "yolo"},
|
||||||
|
{"runtime", "onnx"},
|
||||||
|
{"models", Json::array({Json{{"id", "m0"},
|
||||||
|
{"payload", "p0"},
|
||||||
|
{"entry", true}}})}};
|
||||||
|
const auto model = fakeModel(5000, 11);
|
||||||
|
req.models.push_back({"p0", model});
|
||||||
|
req.models.push_back({"p1", model}); // two parts under one key: bases differ
|
||||||
|
|
||||||
|
const std::string pa = (dir / "fresh-a.ansmodel").string();
|
||||||
|
const std::string pb = (dir / "fresh-b.ansmodel").string();
|
||||||
|
std::string err;
|
||||||
|
CHECK(modelpkg::pack(pa, req, err), ("pack a: " + err).c_str());
|
||||||
|
CHECK(modelpkg::pack(pb, req, err), ("pack b: " + err).c_str());
|
||||||
|
modelpkg::Package a, b;
|
||||||
|
CHECK(modelpkg::open(pa, ring, a, err) && modelpkg::open(pb, ring, b, err),
|
||||||
|
("both open with the base key: " + err).c_str());
|
||||||
|
|
||||||
|
CHECK(a.contentKey != b.contentKey,
|
||||||
|
"two packs of identical inputs have different content keys");
|
||||||
|
bool seeded = false;
|
||||||
|
for (int seed = 0; seed < 256; ++seed) {
|
||||||
|
modelpkg::Key guess{};
|
||||||
|
for (size_t i = 0; i < guess.size(); ++i)
|
||||||
|
guess[i] = uint8_t(seed * 31 + i * 7 + 11);
|
||||||
|
seeded = seeded || guess == a.contentKey || guess == b.contentKey;
|
||||||
|
}
|
||||||
|
CHECK(!seeded, "and neither is one of the 256 a seed could have made");
|
||||||
|
|
||||||
|
const Json& wa = a.header["wraps"][0];
|
||||||
|
const Json& wb = b.header["wraps"][0];
|
||||||
|
CHECK(wa.value("epk", std::string()) != wb.value("epk", std::string()),
|
||||||
|
"each has its own ephemeral key");
|
||||||
|
CHECK(wa.value("nonce", std::string()) != wb.value("nonce", std::string()) &&
|
||||||
|
wa.value("ct", std::string()) != wb.value("ct", std::string()),
|
||||||
|
"and its own wrap nonce and wrapped key");
|
||||||
|
CHECK(a.header["payloads"][0].value("sha256", std::string()) !=
|
||||||
|
b.header["payloads"][0].value("sha256", std::string()),
|
||||||
|
"so the same model seals to different ciphertext");
|
||||||
|
CHECK(a.header["payloads"][0].value("nonceBase", std::string()) !=
|
||||||
|
a.header["payloads"][1].value("nonceBase", std::string()) &&
|
||||||
|
a.header["payloads"][0].value("nonceBase", std::string()) !=
|
||||||
|
a.header["manifest"].value("nonceBase", std::string()),
|
||||||
|
"and no two parts of one package share a nonce base");
|
||||||
|
|
||||||
|
std::vector<uint8_t> ga, gb;
|
||||||
|
CHECK(modelpkg::readPayload(a, "p0", ga, err) && ga == model &&
|
||||||
|
modelpkg::readPayload(b, "p1", gb, err) && gb == model,
|
||||||
|
"and both still give back the model that went in");
|
||||||
|
|
||||||
|
// The seeded path is for tests built as such; this one is not.
|
||||||
|
req.insecureDeterministicForTests = true;
|
||||||
|
CHECK(!modelpkg::pack((dir / "seeded.ansmodel").string(), req, err) &&
|
||||||
|
err.find("tests only") != std::string::npos,
|
||||||
|
"a build without the test switch refuses to pack from a seed");
|
||||||
|
}
|
||||||
|
|
||||||
static void testOpenForAnEngine(const fs::path& dataDir) {
|
static void testOpenForAnEngine(const fs::path& dataDir) {
|
||||||
const std::string path = (dataDir / "interop.ansmodel").string();
|
const std::string path = (dataDir / "interop.ansmodel").string();
|
||||||
if (!fs::exists(path)) {
|
if (!fs::exists(path)) {
|
||||||
@@ -376,7 +448,6 @@ static void testShapeFromTheGraph(const fs::path& dir) {
|
|||||||
req.keyId = "ansk-v1-base";
|
req.keyId = "ansk-v1-base";
|
||||||
req.useX25519 = true;
|
req.useX25519 = true;
|
||||||
req.basePublic = id.basePub;
|
req.basePublic = id.basePub;
|
||||||
req.nonceSeed = 21;
|
|
||||||
req.signerId = "anssig-2026-09";
|
req.signerId = "anssig-2026-09";
|
||||||
req.signerSecret = id.signerSk;
|
req.signerSecret = id.signerSk;
|
||||||
req.signerPublic = id.signerPk;
|
req.signerPublic = id.signerPk;
|
||||||
@@ -472,6 +543,7 @@ int main(int argc, char** argv) {
|
|||||||
testVectors();
|
testVectors();
|
||||||
testInterop(dataDir);
|
testInterop(dataDir);
|
||||||
testRoundTripAndRefusals(tmp);
|
testRoundTripAndRefusals(tmp);
|
||||||
|
testEachPackIsSealedAfresh(tmp);
|
||||||
testOpenForAnEngine(dataDir);
|
testOpenForAnEngine(dataDir);
|
||||||
testShapeFromTheGraph(tmp);
|
testShapeFromTheGraph(tmp);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user