Files
ANSCORE/modules/ANSFR/ONNXFaceAttributes.cpp
T
nghianguyenandClaude Fable 5 ac4369302a ANSFR: support ANS_FaceRecognizer v1.4 models (AdaFace + ONNX attributes)
- Recognizer: adafacerecognizer.onnx (AdaFace, new default) with automatic
  fallback to ansfacerecognizer.onnx (ArcFace) for pre-v1.4 zips. AdaFace
  consumes BGR: new AdaFace handler in ONNXEngine skips the BGR2RGB swap,
  and the TensorRT paths gate their cvtColor on m_swapRB.
- New C API: SetANSRFRecognizerModel (0=AdaFace, 1=ArcFace),
  GetANSRFRecognizerModel, GetANSRFModelInfo. A live switch reloads the
  recognizer and regenerates all stored embeddings from the enrolled face
  crops; the generating model is persisted in a new SETTINGS table
  (EMBEDDING_MODEL) so upgrading an ArcFace database to the AdaFace
  default migrates automatically on LoadEngine.
- Attributes: prefer agegender.onnx (ViT: gender_logits [male,female] +
  age_pred in years; also handles the single-tensor research export) and
  emotionsrecognition.onnx (YOLOv11-cls, classes from .names) behind new
  IAgeGenderEstimator/IEmotionsClassifier interfaces, falling back to the
  legacy OpenVINO IRs. EP follows hardware: NVIDIA uses TensorRT EP with
  disk-cached engines, AMD DirectML, Intel OpenVINO.
- Emotion JSON mapping: handle the new Fear class and the never-matched
  surprise label (both map to Attentive).
- ANSFR-UnitTest: TestModelSwitchV14 covers default model selection, ONNX
  attribute backends, enrollment, and adaface<->arcface switching.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 11:06:10 +10:00

257 lines
9.7 KiB
C++

#include "ONNXFaceAttributes.h"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <filesystem>
namespace {
// The exported classifier may or may not end with a softmax node
// (Ultralytics kept flipping this across versions). Treat the output
// as probabilities only when it already looks like a distribution.
void SoftmaxIfNeeded(std::vector<float>& v) {
if (v.empty()) return;
float sum = 0.0f;
bool inRange = true;
for (float x : v) {
if (x < 0.0f || x > 1.0f) { inRange = false; break; }
sum += x;
}
if (inRange && std::fabs(sum - 1.0f) < 0.05f) return;
float maxVal = *std::max_element(v.begin(), v.end());
float denom = 0.0f;
for (float& x : v) {
x = std::exp(x - maxVal);
denom += x;
}
if (denom > 0.0f) {
for (float& x : v) x /= denom;
}
}
void ResolveInputSize(const std::vector<int64_t>& dims, int& width, int& height) {
// NCHW; dynamic dims come back as -1 — the shipped models are 224x224.
width = 224;
height = 224;
if (dims.size() >= 4) {
if (dims[2] > 0) height = static_cast<int>(dims[2]);
if (dims[3] > 0) width = static_cast<int>(dims[3]);
}
}
}
namespace ANSCENTER {
// ================================================================
// OnnxAgeGenderEstimator
// ================================================================
OnnxAgeGenderEstimator::OnnxAgeGenderEstimator(const std::string& onnxPath,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads)
: BasicOrtHandler(onnxPath, engineType, options, numThreads)
{
_ready = true;
}
Ort::Value OnnxAgeGenderEstimator::transform(const cv::Mat& mat)
{
if (mat.empty())
throw std::runtime_error("OnnxAgeGenderEstimator::transform — input is empty.");
int width = 0, height = 0;
ResolveInputSize(input_node_dims, width, height);
cv::Mat canvas = mat;
if (canvas.channels() == 1)
cv::cvtColor(canvas, canvas, cv::COLOR_GRAY2BGR);
cv::Mat resized;
cv::resize(canvas, resized, cv::Size(width, height), 0, 0, cv::INTER_LINEAR);
cv::cvtColor(resized, resized, cv::COLOR_BGR2RGB);
resized.convertTo(resized, CV_32FC3, 1.0 / 255.0);
// ImageNet normalization (ViTFeatureExtractor defaults)
static const float mean_vals[3] = { 0.485f, 0.456f, 0.406f };
static const float scale_vals[3] = { 1.f / 0.229f, 1.f / 0.224f, 1.f / 0.225f };
utils::transform::normalize_inplace(resized, mean_vals, scale_vals);
std::vector<int64_t> shape = input_node_dims;
if (shape.size() < 4) shape = { 1, 3, height, width };
if (shape[0] <= 0) shape[0] = 1;
if (shape[2] <= 0) shape[2] = height;
if (shape[3] <= 0) shape[3] = width;
return utils::transform::create_tensor(
resized, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
Ort::Value OnnxAgeGenderEstimator::transformBatch(const std::vector<cv::Mat>&)
{
throw std::runtime_error("OnnxAgeGenderEstimator::transformBatch — not supported.");
}
AgeGenderResults OnnxAgeGenderEstimator::runInfer(const cv::Mat& frame)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!_ready || frame.empty()) return { -1, -1 };
Ort::Value input_tensor = transform(frame);
DmlInferenceGuard dmlGuard(m_engineType, "OnnxAgeGenderEstimator::runInfer");
auto output_tensors = ort_session->Run(
Ort::RunOptions{ nullptr },
input_node_names.data(), &input_tensor, 1,
output_node_names.data(), num_outputs);
// Two known export layouts, distinguished by tensor sizes:
// (a) v1.4 pack: age_pred [1] (years) + gender_logits [1,2]
// with class order [male, female]
// (b) research export: a single [1,2] tensor of
// [age_years, gender_logit] where sigmoid >= 0.5 -> Female
const float* agePtr = nullptr;
const float* genderLogits = nullptr;
for (auto& tensor : output_tensors) {
const size_t count =
tensor.GetTensorTypeAndShapeInfo().GetElementCount();
if (count == 1 && !agePtr)
agePtr = tensor.GetTensorMutableData<float>();
else if (count >= 2 && !genderLogits)
genderLogits = tensor.GetTensorMutableData<float>();
}
if (agePtr && genderLogits) { // layout (a)
const float age = std::clamp(agePtr[0], 0.0f, 100.0f);
const float m = genderLogits[0], f = genderLogits[1];
const float mx = std::max(m, f);
const float em = std::exp(m - mx), ef = std::exp(f - mx);
return { age, em / (em + ef) };
}
if (genderLogits) { // layout (b)
const float age = std::clamp(genderLogits[0], 0.0f, 100.0f);
const float femaleProb = 1.0f / (1.0f + std::exp(-genderLogits[1]));
return { age, 1.0f - femaleProb };
}
return { -1, -1 };
}
catch (const std::exception& error) {
std::cerr << "OnnxAgeGenderEstimator error: " << error.what() << std::endl;
return { -1, -1 };
}
}
// ================================================================
// OnnxEmotionsClassifier
// ================================================================
std::vector<std::string> OnnxEmotionsClassifier::LoadClassNames(const std::string& namesFilePath)
{
std::vector<std::string> names;
try {
std::ifstream file(namesFilePath);
std::string line;
while (std::getline(file, line)) {
while (!line.empty() && (line.back() == '\r' || line.back() == '\n'
|| line.back() == ' ' || line.back() == '\t'))
line.pop_back();
if (!line.empty()) names.push_back(line);
}
}
catch (const std::exception&) {
names.clear();
}
if (names.empty()) {
names = { "neutral", "happy", "sad", "surprise", "anger" };
}
return names;
}
OnnxEmotionsClassifier::OnnxEmotionsClassifier(const std::string& onnxPath,
std::vector<std::string> classNames,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads)
: BasicOrtHandler(onnxPath, engineType, options, numThreads)
, _classNames(std::move(classNames))
{
if (_classNames.empty())
_classNames = { "neutral", "happy", "sad", "surprise", "anger" };
_ready = true;
}
Ort::Value OnnxEmotionsClassifier::transform(const cv::Mat& mat)
{
if (mat.empty())
throw std::runtime_error("OnnxEmotionsClassifier::transform — input is empty.");
int width = 0, height = 0;
ResolveInputSize(input_node_dims, width, height);
cv::Mat canvas = mat;
if (canvas.channels() == 1)
cv::cvtColor(canvas, canvas, cv::COLOR_GRAY2BGR);
cv::Mat resized;
cv::resize(canvas, resized, cv::Size(width, height), 0, 0, cv::INTER_LINEAR);
cv::cvtColor(resized, resized, cv::COLOR_BGR2RGB);
// Ultralytics classifier preprocessing: RGB, scaled to [0, 1]
resized.convertTo(resized, CV_32FC3, 1.0 / 255.0);
std::vector<int64_t> shape = input_node_dims;
if (shape.size() < 4) shape = { 1, 3, height, width };
if (shape[0] <= 0) shape[0] = 1;
if (shape[2] <= 0) shape[2] = height;
if (shape[3] <= 0) shape[3] = width;
return utils::transform::create_tensor(
resized, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
Ort::Value OnnxEmotionsClassifier::transformBatch(const std::vector<cv::Mat>&)
{
throw std::runtime_error("OnnxEmotionsClassifier::transformBatch — not supported.");
}
std::map<std::string, float> OnnxEmotionsClassifier::runInfer(const cv::Mat& frame)
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
if (!_ready || frame.empty()) return { };
Ort::Value input_tensor = transform(frame);
DmlInferenceGuard dmlGuard(m_engineType, "OnnxEmotionsClassifier::runInfer");
auto output_tensors = ort_session->Run(
Ort::RunOptions{ nullptr },
input_node_names.data(), &input_tensor, 1,
output_node_names.data(), num_outputs);
auto info = output_tensors.at(0).GetTensorTypeAndShapeInfo();
const size_t count = info.GetElementCount();
if (count == 0) return { };
const float* vals = output_tensors.at(0).GetTensorMutableData<float>();
std::vector<float> scores(vals, vals + count);
SoftmaxIfNeeded(scores);
std::map<std::string, float> emotions;
for (size_t i = 0; i < scores.size(); ++i) {
const std::string name = (i < _classNames.size())
? _classNames[i]
: ("class_" + std::to_string(i));
emotions[name] = scores[i];
}
return emotions;
}
catch (const std::exception& error) {
std::cerr << "OnnxEmotionsClassifier error: " << error.what() << std::endl;
return { };
}
}
}