vslam_vpr_check: cham theo ty le TIM DUNG CHO (top-1/top-5) thay vi giong het PyTorch
This commit is contained in:
@@ -3,16 +3,26 @@
|
||||
//
|
||||
// vslam_vpr_check --db DB --engine PLAN --ref REF.bin [--split-id N]
|
||||
//
|
||||
// DAT khi: cosine voi PyTorch trung vi >= 0.999 va nho nhat >= 0.99; va (neu co
|
||||
// --split-id) anh ban do giong nhat (id < N) cua moi anh hoi (id >= N) trung PyTorch >= 98%.
|
||||
// DAT khi (co --split-id): tim dung CHO (anh ban do id < N trong 2 m & lech huong < 45 do)
|
||||
// bang mo ta TensorRT KHONG kem PyTorch qua 1 diem % o top-1 va top-5, va cosine voi
|
||||
// PyTorch nho nhat >= 0.98 (kiem tien xu ly khong hong).
|
||||
//
|
||||
// ============ VI SAO KHONG DOI "GIONG HET PYTORCH" ============
|
||||
// Do 2026-09-30: TensorRT fp16 va fp32 THAT cho CUNG cosine 0.9973 (trung vi) -> khong phai
|
||||
// do lam tron cua mo hinh. Lech den tu giai nen JPEG / tien xu ly khac phien ban OpenCV giua
|
||||
// may dev (x86, 4.2) va Jetson (ARM): anh IR toi + CLAHE khuech dai rat manh -- lech +-1 muc
|
||||
// sang o 10% pixel da ha cosine xuong 0.964, nen JPEG lai mot lan xuong 0.9935. Trong node
|
||||
// ca anh ban do lan anh hoi deu tinh tren Jetson, nen cai can do la TIM DUNG CHO.
|
||||
|
||||
#include <rtabmap/core/DBDriver.h>
|
||||
#include <rtabmap/core/SensorData.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -102,6 +112,7 @@ int main(int argc, char** argv) {
|
||||
have[k] = 1;
|
||||
cosv.push_back(Dot(d.data(), ref.data() + k * dim, dim));
|
||||
}
|
||||
const std::map<int, rtabmap::Transform> opt = driver->loadOptimizedPoses();
|
||||
driver->closeConnection(false);
|
||||
if (cosv.empty()) {
|
||||
std::fprintf(stderr, "khong tinh duoc anh nao\n");
|
||||
@@ -113,28 +124,59 @@ int main(int argc, char** argv) {
|
||||
std::printf("%zu/%zu anh: cosine TensorRT vs PyTorch min %.5f p1 %.5f trung vi %.5f;"
|
||||
" %.1f ms/anh (ca tien xu ly)\n",
|
||||
cosv.size(), ids.size(), cmin, cp1, cmed, ms_sum / cosv.size());
|
||||
bool pass = cmed >= 0.999f && cmin >= 0.99f;
|
||||
bool pass = cmin >= 0.98f;
|
||||
|
||||
if (split > 0) {
|
||||
int nq = 0, same = 0;
|
||||
for (size_t q = 0; q < ids.size(); ++q) {
|
||||
if (ids[q] < split || !have[q]) continue;
|
||||
int best_r = -1, best_t = -1;
|
||||
float sr = -2.0f, stt = -2.0f;
|
||||
for (size_t m = 0; m < ids.size(); ++m) {
|
||||
if (ids[m] >= split || !have[m]) continue;
|
||||
const float a = Dot(ref.data() + q * dim, ref.data() + m * dim, dim);
|
||||
const float b = Dot(trt.data() + q * dim, trt.data() + m * dim, dim);
|
||||
if (a > sr) { sr = a; best_r = ids[m]; }
|
||||
if (b > stt) { stt = b; best_t = ids[m]; }
|
||||
}
|
||||
++nq;
|
||||
if (best_r == best_t) ++same;
|
||||
// Anh ban do DUNG CHO: trong 2 m va lech huong < 45 do (nhu tools/vpr_eval.py).
|
||||
auto near = [&](int a, int b) {
|
||||
const auto ia = opt.find(a), ib = opt.find(b);
|
||||
if (ia == opt.end() || ib == opt.end()) return false;
|
||||
double dth = std::fabs(ia->second.theta() - ib->second.theta());
|
||||
dth = std::fmod(dth, 2.0 * M_PI);
|
||||
if (dth > M_PI) dth = 2.0 * M_PI - dth;
|
||||
return std::hypot(ia->second.x() - ib->second.x(), ia->second.y() - ib->second.y()) < 2.0 &&
|
||||
dth < 45.0 * M_PI / 180.0;
|
||||
};
|
||||
std::vector<size_t> maps;
|
||||
for (size_t m = 0; m < ids.size(); ++m) {
|
||||
if (ids[m] < split && have[m]) maps.push_back(m);
|
||||
}
|
||||
const double agree = nq > 0 ? 100.0 * same / nq : 0.0;
|
||||
std::printf("anh giong nhat trong ban do (id < %d): trung PyTorch %d/%d = %.1f%%\n", split,
|
||||
same, nq, agree);
|
||||
pass = pass && agree >= 98.0;
|
||||
int nq = 0, same = 0, r1[2] = {0, 0}, r5[2] = {0, 0};
|
||||
std::vector<std::pair<float, size_t>> sc(maps.size());
|
||||
for (size_t q = 0; q < ids.size() && !maps.empty(); ++q) {
|
||||
if (ids[q] < split || !have[q]) continue;
|
||||
bool any = false;
|
||||
for (size_t m : maps) any = any || near(ids[q], ids[m]);
|
||||
if (!any) continue; // cho chua tung chup cung huong trong ban do: khong cham
|
||||
++nq;
|
||||
int top1[2] = {-1, -1};
|
||||
for (int w = 0; w < 2; ++w) {
|
||||
const float* D = (w == 0 ? ref : trt).data();
|
||||
for (size_t j = 0; j < maps.size(); ++j) {
|
||||
sc[j] = {Dot(D + q * dim, D + maps[j] * dim, dim), maps[j]};
|
||||
}
|
||||
const size_t k = std::min<size_t>(5, sc.size());
|
||||
std::partial_sort(sc.begin(), sc.begin() + k, sc.end(),
|
||||
[](const std::pair<float, size_t>& a, const std::pair<float, size_t>& b) {
|
||||
return a.first > b.first;
|
||||
});
|
||||
top1[w] = ids[sc[0].second];
|
||||
if (near(ids[q], top1[w])) ++r1[w];
|
||||
for (size_t j = 0; j < k; ++j) {
|
||||
if (near(ids[q], ids[sc[j].second])) {
|
||||
++r5[w];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (top1[0] == top1[1]) ++same;
|
||||
}
|
||||
auto pct = [&](int v) { return nq > 0 ? 100.0 * v / nq : 0.0; };
|
||||
std::printf("%d anh hoi (id >= %d) co anh ban do dung cho; tim dung cho:\n", nq, split);
|
||||
std::printf(" PyTorch : top-1 %.1f%% top-5 %.1f%%\n", pct(r1[0]), pct(r5[0]));
|
||||
std::printf(" TensorRT : top-1 %.1f%% top-5 %.1f%% (anh dung dau trung PyTorch %.1f%%)\n",
|
||||
pct(r1[1]), pct(r5[1]), pct(same));
|
||||
pass = pass && pct(r1[1]) >= pct(r1[0]) - 1.0 && pct(r5[1]) >= pct(r5[0]) - 1.0;
|
||||
}
|
||||
std::printf("%s\n", pass ? "DAT" : "KHONG DAT");
|
||||
return pass ? 0 : 3;
|
||||
|
||||
Reference in New Issue
Block a user