142 lines
5.0 KiB
C++
142 lines
5.0 KiB
C++
// Doi chieu mo ta VPR cua TensorRT (C++, vslam/vpr_encoder.h) voi PyTorch
|
|
// (tools/vpr_export_onnx.py --ref-db). CHI build voi -DVSLAM_WITH_TENSORRT=ON.
|
|
//
|
|
// 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%.
|
|
|
|
#include <rtabmap/core/DBDriver.h>
|
|
#include <rtabmap/core/SensorData.h>
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "vslam/vpr_encoder.h"
|
|
|
|
namespace {
|
|
|
|
// .bin cua vpr_export_onnx.py: int32 N, int32 D, int32 ids[N], float32 desc[N*D].
|
|
bool ReadRef(const std::string& p, std::vector<int>* ids, std::vector<float>* desc, int* dim) {
|
|
FILE* f = std::fopen(p.c_str(), "rb");
|
|
if (f == nullptr) return false;
|
|
int32_t hdr[2] = {0, 0};
|
|
bool ok = std::fread(hdr, sizeof(int32_t), 2, f) == 2 && hdr[0] > 0 && hdr[1] > 0;
|
|
if (ok) {
|
|
ids->resize(hdr[0]);
|
|
desc->resize(static_cast<size_t>(hdr[0]) * hdr[1]);
|
|
ok = std::fread(ids->data(), sizeof(int32_t), hdr[0], f) == static_cast<size_t>(hdr[0]) &&
|
|
std::fread(desc->data(), sizeof(float), desc->size(), f) == desc->size();
|
|
*dim = hdr[1];
|
|
}
|
|
std::fclose(f);
|
|
return ok;
|
|
}
|
|
|
|
float Dot(const float* a, const float* b, int n) {
|
|
double s = 0.0;
|
|
for (int i = 0; i < n; ++i) s += static_cast<double>(a[i]) * b[i];
|
|
return static_cast<float>(s);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
std::string db, ref_path;
|
|
vslam::VprConfig cfg;
|
|
int split = -1;
|
|
for (int i = 1; i < argc; ++i) {
|
|
const std::string a = argv[i];
|
|
auto next = [&]() -> std::string { return i + 1 < argc ? argv[++i] : ""; };
|
|
if (a == "--db") db = next();
|
|
else if (a == "--engine") cfg.engine_path = next();
|
|
else if (a == "--ref") ref_path = next();
|
|
else if (a == "--split-id") split = std::atoi(next().c_str());
|
|
}
|
|
if (db.empty() || cfg.engine_path.empty() || ref_path.empty()) {
|
|
std::fprintf(stderr, "cach dung: %s --db DB --engine PLAN --ref REF.bin [--split-id N]\n", argv[0]);
|
|
return 2;
|
|
}
|
|
std::vector<int> ids;
|
|
std::vector<float> ref;
|
|
int dim = 0;
|
|
if (!ReadRef(ref_path, &ids, &ref, &dim)) {
|
|
std::fprintf(stderr, "khong doc duoc %s\n", ref_path.c_str());
|
|
return 1;
|
|
}
|
|
std::unique_ptr<vslam::VprEncoder> enc;
|
|
const vslam::StatusResponse st = vslam::CreateVprEncoder(cfg, &enc);
|
|
if (!st.ok()) {
|
|
std::fprintf(stderr, "%s\n", st.message.c_str());
|
|
return 1;
|
|
}
|
|
if (enc->dim() != dim) {
|
|
std::fprintf(stderr, "engine ra %d chieu, tham chieu %d chieu\n", enc->dim(), dim);
|
|
return 1;
|
|
}
|
|
std::unique_ptr<rtabmap::DBDriver> driver(rtabmap::DBDriver::create());
|
|
if (!driver->openConnection(db, false)) {
|
|
std::fprintf(stderr, "khong mo duoc %s\n", db.c_str());
|
|
return 1;
|
|
}
|
|
|
|
std::vector<float> trt(ref.size(), 0.0f);
|
|
std::vector<char> have(ids.size(), 0);
|
|
std::vector<float> cosv;
|
|
double ms_sum = 0.0;
|
|
for (size_t k = 0; k < ids.size(); ++k) {
|
|
rtabmap::SensorData data;
|
|
driver->getNodeData(ids[k], data, true, false, false, false);
|
|
data.uncompressData();
|
|
if (data.imageRaw().empty()) continue;
|
|
std::vector<float> d;
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
if (!enc->Describe(data.imageRaw(), &d).ok()) continue;
|
|
ms_sum += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
|
|
std::copy(d.begin(), d.end(), trt.begin() + static_cast<long>(k) * dim);
|
|
have[k] = 1;
|
|
cosv.push_back(Dot(d.data(), ref.data() + k * dim, dim));
|
|
}
|
|
driver->closeConnection(false);
|
|
if (cosv.empty()) {
|
|
std::fprintf(stderr, "khong tinh duoc anh nao\n");
|
|
return 1;
|
|
}
|
|
std::vector<float> s = cosv;
|
|
std::sort(s.begin(), s.end());
|
|
const float cmin = s.front(), cp1 = s[s.size() / 100], cmed = s[s.size() / 2];
|
|
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;
|
|
|
|
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;
|
|
}
|
|
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;
|
|
}
|
|
std::printf("%s\n", pass ? "DAT" : "KHONG DAT");
|
|
return pass ? 0 : 3;
|
|
}
|