Repository navigation
Expand file tree
/
Copy pathmodels.cpp
More file actions
416 lines (415 loc) · 18.4 KB
/
Copy pathmodels.cpp
File metadata and controls
416 lines (415 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "models.h"
#include "legacy_storage.h"
#include <QCoreApplication>
#include <QCryptographicHash>
#include <QDir>
#include <QDebug>
#include <QEventLoop>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLibrary>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSaveFile>
#include <QScopeGuard>
#include <QStandardPaths>
#include <QTimer>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <opencv2/imgproc.hpp>
#include <stdexcept>
namespace headshotflow {
static QStringList roots() {
QStringList r;
if (!qEnvironmentVariable("HEADSHOTFLOW_MODEL_DIR").isEmpty())
r << qEnvironmentVariable("HEADSHOTFLOW_MODEL_DIR");
r << QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/models"
<< QCoreApplication::applicationDirPath() + "/models"
<< QCoreApplication::applicationDirPath() + "/../share/headshotflow/models"
<< QCoreApplication::applicationDirPath() + "/../Resources/models";
const auto legacy = legacyLocation(QStandardPaths::AppLocalDataLocation);
if (!legacy.isEmpty())
r << legacy + "/models";
if (!qEnvironmentVariableIsSet("HEADSHOTFLOW_DISABLE_SOURCE_MODELS"))
r << QString::fromUtf8(HEADSHOTFLOW_SOURCE_MODELS);
return r;
}
QJsonArray Models::manifest() {
for (const auto &r : roots()) {
QFile f(r + "/manifest.json");
if (f.open(QIODevice::ReadOnly))
return QJsonDocument::fromJson(f.readAll()).object()["models"].toArray();
}
throw std::runtime_error("Model manifest missing. Reinstall HeadshotFlow.");
}
static QJsonObject entry(const QString &id) {
for (const auto &e : Models::manifest())
if (e.toObject()["id"] == id)
return e.toObject();
throw std::runtime_error("Unknown model identifier");
}
QString Models::locate(const QString &id) {
auto e = entry(id);
for (const auto &r : roots()) {
QFile f(r + "/" + e["file"].toString());
if (!f.open(QIODevice::ReadOnly))
continue;
QCryptographicHash hash(QCryptographicHash::Sha256);
hash.addData(&f);
if (hash.result().toHex() == e["sha256"].toString().toLatin1())
return f.fileName();
}
throw std::runtime_error(
("Model '" + id +
"' is missing or damaged. Install it from Models or run: headshotflow models install " + id)
.toStdString());
}
QString Models::install(const QString &id, std::atomic_bool *cancel,
std::function<void(qint64, qint64)> progress) {
auto e = entry(id);
QString dir =
QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/models";
if (!QDir().mkpath(dir))
throw std::runtime_error("Cannot create model directory");
QString path = dir + "/" + e["file"].toString();
QSaveFile file(path);
if (!file.open(QIODevice::WriteOnly))
throw std::runtime_error(file.errorString().toStdString());
QNetworkAccessManager network;
QNetworkRequest request(QUrl(e["url"].toString()));
request.setTransferTimeout(120000);
auto *reply = network.get(request);
QEventLoop loop;
QCryptographicHash hash(QCryptographicHash::Sha256);
bool writeFailed = false;
auto drain = [&] {
auto data = reply->readAll();
hash.addData(data);
if (file.write(data) != data.size()) {
writeFailed = true;
reply->abort();
}
};
QObject::connect(reply, &QNetworkReply::readyRead, &loop, drain);
QObject::connect(reply, &QNetworkReply::downloadProgress, &loop, [&](qint64 a, qint64 b) {
if (progress)
progress(a, b);
});
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, &loop, [&] {
if (cancel && *cancel)
reply->abort();
});
timer.start(100);
loop.exec();
drain();
if (writeFailed || reply->error() != QNetworkReply::NoError)
throw std::runtime_error(("Model download failed: " + reply->errorString()).toStdString());
if (hash.result().toHex() != e["sha256"].toString().toLatin1())
throw std::runtime_error("Model checksum mismatch; download discarded");
if (!file.commit())
throw std::runtime_error("Cannot save model");
return path;
}
static void checkModelCancel(std::atomic_bool *cancel) {
if (cancel && cancel->load()) throw std::runtime_error("Cancelled");
}
QByteArray Models::cacheKey(const cv::Mat &rgb, const QString &id, const QString &purpose) {
session(id); // Resolve the actual device before looking up compatible results.
QCryptographicHash hash(QCryptographicHash::Sha256);
const auto identity = id + ":" + purpose + ":preprocessing-v1:" +
checksums.at(id) + ":" + (cudaSessions.contains(id) ? "cuda" : "cpu") +
":" + QString::number(rgb.cols) + ":" + QString::number(rgb.rows) + ":" + QString::number(rgb.type());
hash.addData(identity.toUtf8());
for (int y = 0; y < rgb.rows; ++y)
hash.addData(QByteArrayView(reinterpret_cast<const char *>(rgb.ptr(y)), rgb.cols * rgb.elemSize()));
return hash.result();
}
void Models::release() {
sessions.clear();
cache.clear();
cudaSessions.clear();
cpuFallback.clear();
checksums.clear();
qualityLoaded.store(false);
std::lock_guard lock(statusMutex);
devices.clear();
fallbackReasons.clear();
diagnostics.clear();
}
void Models::setPreference(const QString &value) {
if (value != "automatic" && value != "cpu") throw std::runtime_error("Invalid processing device");
if (preference == value) return;
release();
preference = value;
}
QString Models::inferenceDetails() const {
std::lock_guard lock(statusMutex);
QStringList lines;
QStringList providers;
for (const auto &provider : Ort::GetAvailableProviders()) providers << QString::fromStdString(provider);
lines << "Available runtime providers: " + providers.join(", ");
if (diagnostics.empty()) lines << "No runtime errors reported.";
for (const auto &[id, message] : diagnostics) lines << id + ": " + message;
return lines.isEmpty() ? "No runtime errors reported." : lines.join("\n");
}
QString Models::inferenceStatus() const {
std::lock_guard lock(statusMutex);
QStringList lines;
for (const auto &[id, device] : devices)
lines << (id == "face" ? "Face" : id == "fast" ? "Fast" : "High Quality") + QString(": ") + device;
return lines.isEmpty() ? (cpuRequested()
? "CPU requested · no models loaded"
: "Automatic GPU selection · no models loaded") : lines.join("\n");
}
void Models::setDevice(const QString &id, const QString &device) {
std::lock_guard lock(statusMutex);
devices[id] = device;
}
Ort::Session &Models::session(const QString &id) {
if (!sessions.contains(id)) {
const auto path = locate(id);
checksums[id] = entry(id)["sha256"].toString();
auto create = [&](bool cuda) {
Ort::SessionOptions options;
options.SetIntraOpNumThreads(2);
options.SetInterOpNumThreads(1);
options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
if (cuda) {
#ifdef __linux__
// Some distribution providers dynamically resolve cuDNN without
// declaring it in DT_NEEDED. Preload its exported symbols first.
static QLibrary cudnn(QStringLiteral("libcudnn.so.9"));
static std::once_flag preload;
std::call_once(preload, [] {
cudnn.setLoadHints(QLibrary::ExportExternalSymbolsHint | QLibrary::PreventUnloadHint);
cudnn.load();
});
#endif
OrtCUDAProviderOptions provider;
// Avoid expensive exhaustive convolution searches during first use.
provider.cudnn_conv_algo_search = OrtCudnnConvAlgoSearchHeuristic;
options.AppendExecutionProvider_CUDA(provider);
}
#ifdef _WIN32
return std::make_unique<Ort::Session>(env, path.toStdWString().c_str(), options);
#else
return std::make_unique<Ort::Session>(env, path.toUtf8().constData(), options);
#endif
};
const auto providers = Ort::GetAvailableProviders();
const bool tryCuda = !cpuRequested() &&
!cpuFallback.contains(id) &&
std::find(providers.begin(), providers.end(), "CUDAExecutionProvider") != providers.end();
if (tryCuda) {
try {
sessions[id] = create(true);
cudaSessions.insert(id);
setDevice(id, "NVIDIA GPU (CUDA; unsupported operations use CPU)");
} catch (const Ort::Exception &e) {
qWarning() << "CUDA initialization failed for" << id << e.what();
cpuFallback.insert(id);
fallbackReasons[id] = "CUDA initialization failed";
std::lock_guard lock(statusMutex);
diagnostics[id] = QString::fromUtf8(e.what());
}
}
if (!sessions.contains(id)) {
sessions[id] = create(false);
setDevice(id, cpuFallback.contains(id) ? "CPU (" + fallbackReasons[id] + "; see Technical details)" :
cpuRequested() ? (qEnvironmentVariable("HEADSHOTFLOW_INFERENCE_DEVICE") == "cpu" ? "CPU (launch override)" : "CPU (selected in Models)") : "CPU (this runtime has no CUDA provider)");
}
if (id == "quality")
qualityLoaded.store(true);
}
return *sessions.at(id);
}
std::vector<Ort::Value> Models::run(const QString &id, const cv::Mat &rgb, int size,
const cv::Vec3f &mean, const cv::Vec3f &scale) {
auto &s = session(id);
cv::Mat resized;
cv::resize(rgb, resized, {size, size}, 0, 0, cv::INTER_AREA);
std::vector<float> input(3 * size * size);
for (int y = 0; y < size; ++y)
for (int x = 0; x < size; ++x)
for (int c = 0; c < 3; ++c)
input[c * size * size + y * size + x] =
(resized.at<cv::Vec3f>(y, x)[c] - mean[c]) * scale[c];
auto memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
std::array<int64_t, 4> shape{1, 3, size, size};
auto tensor = Ort::Value::CreateTensor<float>(memory, input.data(), input.size(), shape.data(),
shape.size());
Ort::AllocatorWithDefaultOptions allocator;
auto inName = s.GetInputNameAllocated(0, allocator);
const char *in = inName.get();
std::vector<Ort::AllocatedStringPtr> names;
std::vector<const char *> outs;
for (size_t i = 0; i < s.GetOutputCount(); ++i) {
names.push_back(s.GetOutputNameAllocated(i, allocator));
outs.push_back(names.back().get());
}
try {
return s.Run(Ort::RunOptions{nullptr}, &in, &tensor, 1, outs.data(), outs.size());
} catch (const Ort::Exception &e) {
if (!cudaSessions.contains(id) || e.GetOrtErrorCode() == ORT_INVALID_ARGUMENT)
throw;
qWarning() << "CUDA inference failed; retrying on CPU for" << id << e.what();
{ std::lock_guard lock(statusMutex); diagnostics[id] = QString::fromUtf8(e.what()); }
cudaSessions.erase(id);
cpuFallback.insert(id);
fallbackReasons[id] = "CUDA inference failed";
sessions.erase(id);
if (id == "quality")
qualityLoaded.store(false);
setDevice(id, "Retrying on CPU after GPU failure");
return run(id, rgb, size, mean, scale);
}
}
std::vector<cv::Rect> Models::faces(const cv::Mat &rgb, std::atomic_bool *cancel, ProcessingTrace *trace) {
const auto previous = trace ? trace->state() : Progress{};
const auto restore = qScopeGuard([&] { if (trace) trace->stage(previous.stage, previous.detail); });
if (trace) trace->stage(sessions.contains("face") ? "Cache lookup" : "Model loading", "face");
checkModelCancel(cancel);
const auto key = cacheKey(rgb, "face", "detection");
if (auto cached = cache.get(key)) {
checkModelCancel(cancel);
if (trace) trace->stage("Cached detection", "face", true);
return std::get<std::vector<cv::Rect>>(*cached);
}
if (trace) trace->stage("Inference", "face");
// YuNet 2023: fixed 640x640, BGR 0..255, outputs cls/obj/bbox/kps at strides 8,16,32.
cv::Mat bgr;
cv::cvtColor(rgb, bgr, cv::COLOR_RGB2BGR);
// Preserve face proportions for landscape and portrait camera originals.
const float scale = 640.f / std::max(rgb.cols, rgb.rows);
cv::Mat resized;
cv::resize(bgr, resized,
{std::max(1, int(std::round(rgb.cols * scale))),
std::max(1, int(std::round(rgb.rows * scale)))},
0, 0, cv::INTER_AREA);
cv::Mat padded(640, 640, CV_32FC3, cv::Scalar(0, 0, 0));
resized.copyTo(padded(cv::Rect(0, 0, resized.cols, resized.rows)));
auto outputs = run("face", padded, 640, {0, 0, 0}, {255, 255, 255});
auto &s = session("face");
Ort::AllocatorWithDefaultOptions allocator;
std::map<std::string, const float *> out;
for (size_t i = 0; i < outputs.size(); ++i)
out[s.GetOutputNameAllocated(i, allocator).get()] = outputs[i].GetTensorData<float>();
struct Candidate {
cv::Rect2f rect;
float score;
};
std::vector<Candidate> candidates;
for (int stride : {8, 16, 32}) {
std::string suffix = std::to_string(stride);
auto cls = out.at("cls_" + suffix);
auto obj = out.at("obj_" + suffix);
auto box = out.at("bbox_" + suffix);
int cols = 640 / stride;
for (int i = 0; i < cols * cols; ++i) {
float score = std::sqrt(std::clamp(cls[i], 0.f, 1.f) * std::clamp(obj[i], 0.f, 1.f));
if (score < .8f)
continue;
float cx = (i % cols + box[4 * i]) * stride, cy = (i / cols + box[4 * i + 1]) * stride;
float w = std::exp(box[4 * i + 2]) * stride, h = std::exp(box[4 * i + 3]) * stride;
candidates.push_back(
{{(cx - w / 2) / scale, (cy - h / 2) / scale, w / scale, h / scale}, score});
}
}
std::sort(candidates.begin(), candidates.end(),
[](auto &a, auto &b) { return a.score > b.score; });
std::vector<cv::Rect> result;
for (auto &c : candidates) {
bool duplicate = false;
for (auto &r : result) {
float intersection = (c.rect & cv::Rect2f(r)).area();
if (intersection / (c.rect.area() + r.area() - intersection) > .3f)
duplicate = true;
}
if (!duplicate) {
cv::Rect r = c.rect;
r &= cv::Rect(0, 0, rgb.cols, rgb.rows);
if (r.area() > 0)
result.push_back(r);
}
}
std::sort(result.begin(), result.end(), [](auto &a, auto &b) { return a.area() > b.area(); });
checkModelCancel(cancel);
cache.put(cacheKey(rgb, "face", "detection"), result);
return result;
}
cv::Mat Models::refineFastMask(const cv::Mat &prediction, const cv::Mat &rgb) {
// Guided reconstruction at crop resolution. Replicated borders avoid inventing
// transparent pixels where a foreground region meets the image boundary.
cv::Mat guide, probability;
cv::cvtColor(rgb, guide, cv::COLOR_RGB2GRAY);
cv::resize(prediction, probability, rgb.size(), 0, 0, cv::INTER_LINEAR);
const int radius = std::max(2, int(std::ceil(double(std::max(rgb.cols, rgb.rows)) / std::max(prediction.cols, prediction.rows))) * 2);
auto mean = [radius](const cv::Mat &input) {
cv::Mat output;
cv::boxFilter(input, output, CV_32F, {radius * 2 + 1, radius * 2 + 1},
{-1, -1}, true, cv::BORDER_REPLICATE);
return output;
};
auto meanGuide = mean(guide), meanProbability = mean(probability);
cv::Mat variance = mean(guide.mul(guide)) - meanGuide.mul(meanGuide);
cv::max(variance, 0, variance);
cv::Mat covariance = mean(guide.mul(probability)) - meanGuide.mul(meanProbability);
cv::Mat a;
cv::divide(covariance, variance + 1e-4f, a);
cv::Mat b = meanProbability - a.mul(meanGuide);
cv::Mat refined = mean(a).mul(guide) + mean(b);
// Remove low-confidence residue in clear background / solid foreground.
// Keep a continuous transition for uncertain boundary pixels.
refined = (refined - .1f) / .8f;
cv::max(refined, 0, refined);
cv::min(refined, 1, refined);
return refined;
}
cv::Mat Models::mask(const cv::Mat &rgb, const QString &method, const QString &purpose,
std::atomic_bool *cancel, ProcessingTrace *trace) {
const auto previous = trace ? trace->state() : Progress{};
const auto restore = qScopeGuard([&] { if (trace) trace->stage(previous.stage, previous.detail); });
const auto detail = (purpose + " · " + method).toStdString();
if (trace) trace->stage(sessions.contains(method) ? "Cache lookup" : "Model loading", detail);
checkModelCancel(cancel);
const auto key = cacheKey(rgb, method, purpose);
if (auto cached = cache.get(key)) {
checkModelCancel(cancel);
if (trace) trace->stage("Cached mask", detail, true);
return std::get<cv::Mat>(*cached);
}
if (trace) trace->stage("Inference", detail);
bool fast = method == "fast";
auto outputs =
fast ? run("fast", rgb, 512, {.5f, .5f, .5f}, {2, 2, 2})
: run("quality", rgb, 1024, {.485f, .456f, .406f}, {1 / .229f, 1 / .224f, 1 / .225f});
if (trace) trace->stage("Mask refinement", detail);
auto shape = outputs[0].GetTensorTypeAndShapeInfo().GetShape();
if (shape.size() != 4)
throw std::runtime_error("Unexpected segmentation output shape");
int h = int(shape[2]), w = int(shape[3]);
const float *p = outputs[0].GetTensorData<float>();
cv::Mat result(h, w, CV_32F);
for (int i = 0; i < h * w; ++i) {
float value = fast ? p[i] : 1.f / (1.f + std::exp(-std::clamp(p[i], -80.f, 80.f)));
result.ptr<float>()[i] = std::clamp(value, 0.f, 1.f);
}
if (!fast) {
double lo, hi;
cv::minMaxLoc(result, &lo, &hi);
if (hi - lo > 1e-6)
result = (result - lo) / (hi - lo);
}
if (fast)
result = refineFastMask(result, rgb);
else
cv::resize(result, result, rgb.size(), 0, 0, cv::INTER_LINEAR);
checkModelCancel(cancel);
cache.put(cacheKey(rgb, method, purpose), result);
return result;
}
} // namespace headshotflow