From e2ddfd99ea0589e8bca1da948b20e8b26eb8623d Mon Sep 17 00:00:00 2001 From: Olga Date: Mon, 30 Jun 2025 16:42:30 +0200 Subject: [PATCH 1/4] renamed c++ src and tests --- src/apps/CMakeLists.txt | 2 +- .../CMakeLists.txt | 10 +- .../localization_by_hashing.cpp | 2 +- .../online_localizer_lsh.cpp | 2 +- .../similarity_matrix_no_hashing.cpp} | 4 +- src/localization/database/CMakeLists.txt | 12 +-- src/localization/database/online_database.cpp | 11 ++- src/localization/database/online_database.h | 6 +- ...{cost_matrix.cpp => similarity_matrix.cpp} | 44 ++++----- .../{cost_matrix.h => similarity_matrix.h} | 23 ++--- ...ase.cpp => similarity_matrix_database.cpp} | 16 ++- ...atabase.h => similarity_matrix_database.h} | 16 +-- src/localization/features/ifeature.h | 7 +- .../tools/config_parser/config_parser.cpp | 19 +--- .../tools/config_parser/config_parser.h | 12 +-- src/test/CMakeLists.txt | 4 +- src/test/cost_matrix_test.cpp | 98 ------------------- src/test/database_test.cpp | 2 +- src/test/online_localizer_test.cpp | 1 - src/test/similarity_matrix_test.cpp | 98 +++++++++++++++++++ src/test/test_utils.h | 2 +- 21 files changed, 186 insertions(+), 205 deletions(-) rename src/apps/{cost_matrix_based_matching => similarity_matrix_based_matching}/CMakeLists.txt (74%) rename src/apps/{cost_matrix_based_matching => similarity_matrix_based_matching}/localization_by_hashing.cpp (98%) rename src/apps/{cost_matrix_based_matching => similarity_matrix_based_matching}/online_localizer_lsh.cpp (98%) rename src/apps/{cost_matrix_based_matching/cost_matrix_no_hashing.cpp => similarity_matrix_based_matching/similarity_matrix_no_hashing.cpp} (91%) rename src/localization/database/{cost_matrix.cpp => similarity_matrix.cpp} (68%) rename src/localization/database/{cost_matrix.h => similarity_matrix.h} (52%) rename src/localization/database/{cost_matrix_database.cpp => similarity_matrix_database.cpp} (78%) rename src/localization/database/{cost_matrix_database.h => similarity_matrix_database.h} (78%) delete mode 100644 src/test/cost_matrix_test.cpp create mode 100644 src/test/similarity_matrix_test.cpp diff --git a/src/apps/CMakeLists.txt b/src/apps/CMakeLists.txt index ab7df28..41d4f23 100644 --- a/src/apps/CMakeLists.txt +++ b/src/apps/CMakeLists.txt @@ -1 +1 @@ -add_subdirectory(cost_matrix_based_matching) \ No newline at end of file +add_subdirectory(similarity_matrix_based_matching) \ No newline at end of file diff --git a/src/apps/cost_matrix_based_matching/CMakeLists.txt b/src/apps/similarity_matrix_based_matching/CMakeLists.txt similarity index 74% rename from src/apps/cost_matrix_based_matching/CMakeLists.txt rename to src/apps/similarity_matrix_based_matching/CMakeLists.txt index 0e62f93..00ac4e6 100644 --- a/src/apps/cost_matrix_based_matching/CMakeLists.txt +++ b/src/apps/similarity_matrix_based_matching/CMakeLists.txt @@ -5,7 +5,7 @@ target_link_libraries(online_localizer_lsh glog::glog path_element online_localizer - cost_matrix_database + similarity_matrix_database successor_manager config_parser lsh_cv_hashing @@ -14,12 +14,12 @@ target_link_libraries(online_localizer_lsh ) -add_executable(cost_matrix_no_hashing cost_matrix_no_hashing.cpp) -target_link_libraries(cost_matrix_no_hashing +add_executable(similarity_matrix_no_hashing similarity_matrix_no_hashing.cpp) +target_link_libraries(similarity_matrix_no_hashing glog::glog path_element online_localizer - cost_matrix_database + similarity_matrix_database successor_manager config_parser default_relocalizer @@ -31,7 +31,7 @@ target_link_libraries(localization_by_hashing glog::glog cnn_feature path_element - cost_matrix_database + similarity_matrix_database config_parser lsh_cv_hashing ${OpenCV_LIBS} diff --git a/src/apps/cost_matrix_based_matching/localization_by_hashing.cpp b/src/apps/similarity_matrix_based_matching/localization_by_hashing.cpp similarity index 98% rename from src/apps/cost_matrix_based_matching/localization_by_hashing.cpp rename to src/apps/similarity_matrix_based_matching/localization_by_hashing.cpp index 255d5c3..958c899 100644 --- a/src/apps/cost_matrix_based_matching/localization_by_hashing.cpp +++ b/src/apps/similarity_matrix_based_matching/localization_by_hashing.cpp @@ -56,7 +56,7 @@ int main(int argc, char *argv[]) { /*refFeaturesDir=*/parser.path2ref, /*type=*/loc::features::FeatureType::Cnn_Feature, /*bufferSize=*/parser.bufferSize, - /*costMatrixFile=*/parser.costMatrix); + /*similarityMatrixFile=*/parser.similarityMatrix); auto relocalizer = std::make_unique( /*onlineDatabase=*/database.get(), diff --git a/src/apps/cost_matrix_based_matching/online_localizer_lsh.cpp b/src/apps/similarity_matrix_based_matching/online_localizer_lsh.cpp similarity index 98% rename from src/apps/cost_matrix_based_matching/online_localizer_lsh.cpp rename to src/apps/similarity_matrix_based_matching/online_localizer_lsh.cpp index d2e3a44..7fbd9be 100644 --- a/src/apps/cost_matrix_based_matching/online_localizer_lsh.cpp +++ b/src/apps/similarity_matrix_based_matching/online_localizer_lsh.cpp @@ -77,7 +77,7 @@ int main(int argc, char *argv[]) { /*refFeaturesDir=*/parser.path2ref, /*type=*/loc::features::FeatureType::Cnn_Feature, /*bufferSize=*/parser.bufferSize, - /*costMatrixFile=*/parser.costMatrix); + /*similarityMatrixFile=*/parser.similarityMatrix); auto relocalizer = std::make_unique( /*onlineDatabase=*/database.get(), diff --git a/src/apps/cost_matrix_based_matching/cost_matrix_no_hashing.cpp b/src/apps/similarity_matrix_based_matching/similarity_matrix_no_hashing.cpp similarity index 91% rename from src/apps/cost_matrix_based_matching/cost_matrix_no_hashing.cpp rename to src/apps/similarity_matrix_based_matching/similarity_matrix_no_hashing.cpp index e8f9673..c737406 100644 --- a/src/apps/cost_matrix_based_matching/cost_matrix_no_hashing.cpp +++ b/src/apps/similarity_matrix_based_matching/similarity_matrix_no_hashing.cpp @@ -1,6 +1,6 @@ /* By O. Vysotska in 2023 */ -#include "database/cost_matrix_database.h" +#include "database/similarity_matrix_database.h" #include "database/idatabase.h" #include "online_localizer/online_localizer.h" #include "online_localizer/path_element.h" @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) { parser.print(); const auto database = - std::make_unique(parser.costMatrix); + std::make_unique(parser.similarityMatrix); const auto relocalizer = std::make_unique( diff --git a/src/localization/database/CMakeLists.txt b/src/localization/database/CMakeLists.txt index d154c48..5815d6b 100644 --- a/src/localization/database/CMakeLists.txt +++ b/src/localization/database/CMakeLists.txt @@ -4,8 +4,8 @@ target_link_libraries(list_dir glog::glog ) -add_library(cost_matrix cost_matrix.cpp) -target_link_libraries(cost_matrix +add_library(similarity_matrix similarity_matrix.cpp) +target_link_libraries(similarity_matrix feature_factory list_dir protos @@ -19,16 +19,16 @@ target_link_libraries(online_database feature_buffer feature_factory glog::glog - cost_matrix + similarity_matrix ) -add_library(cost_matrix_database cost_matrix_database.cpp) -target_link_libraries(cost_matrix_database +add_library(similarity_matrix_database similarity_matrix_database.cpp) +target_link_libraries(similarity_matrix_database timer list_dir feature_buffer feature_factory glog::glog - cost_matrix + similarity_matrix ) diff --git a/src/localization/database/online_database.cpp b/src/localization/database/online_database.cpp index 094c966..0916a95 100644 --- a/src/localization/database/online_database.cpp +++ b/src/localization/database/online_database.cpp @@ -27,6 +27,7 @@ #include "database/list_dir.h" #include "features/feature_buffer.h" #include "features/ifeature.h" +#include "similarity_matrix.h" #include @@ -54,7 +55,7 @@ addFeatureIfNeeded(features::FeatureBuffer &featureBuffer, OnlineDatabase::OnlineDatabase(const std::string &queryFeaturesDir, const std::string &refFeaturesDir, features::FeatureType type, int bufferSize, - const std::string &costMatrixFile) + const std::string &similarityMatrixFile) : quFeaturesNames_{listProtoDir(queryFeaturesDir, ".Feature")}, refFeaturesNames_{listProtoDir(refFeaturesDir, ".Feature")}, featureType_{type}, refBuffer_{std::make_unique( @@ -62,8 +63,8 @@ OnlineDatabase::OnlineDatabase(const std::string &queryFeaturesDir, queryBuffer_{std::make_unique(bufferSize)} { LOG_IF(FATAL, quFeaturesNames_.empty()) << "Query features are not set."; LOG_IF(FATAL, refFeaturesNames_.empty()) << "Reference features are not set."; - if (!costMatrixFile.empty()) { - precomputedCosts_ = CostMatrix(costMatrixFile); + if (!similarityMatrixFile.empty()) { + precomputedScores_ = SimilarityMatrix(similarityMatrixFile); } } @@ -82,8 +83,8 @@ double OnlineDatabase::computeMatchingCost(int quId, int refId) { } double OnlineDatabase::getCost(int quId, int refId) { - if (precomputedCosts_) { - return precomputedCosts_->getInverseCost(quId, refId); + if (precomputedScores_) { + return precomputedScores_->getCost(quId, refId); } // Check if the cost was computed before. auto rowIter = costs_.find(quId); diff --git a/src/localization/database/online_database.h b/src/localization/database/online_database.h index bf16b62..1fc9ca3 100644 --- a/src/localization/database/online_database.h +++ b/src/localization/database/online_database.h @@ -26,7 +26,7 @@ #ifndef SRC_DATABASE_ONLINE_DATABASE_H_ #define SRC_DATABASE_ONLINE_DATABASE_H_ -#include "database/cost_matrix.h" +#include "database/similarity_matrix.h" #include "database/idatabase.h" #include "features/feature_buffer.h" #include "features/feature_factory.h" @@ -46,7 +46,7 @@ class OnlineDatabase : public iDatabase { public: OnlineDatabase(const std::string &queryFeaturesDir, const std::string &refFeaturesDir, features::FeatureType type, - int bufferSize, const std::string &costMatrixFile = ""); + int bufferSize, const std::string &similarityMatrixFile = ""); inline int refSize() override { return refFeaturesNames_.size(); } double getCost(int quId, int refId) override; @@ -66,7 +66,7 @@ class OnlineDatabase : public iDatabase { std::unique_ptr queryBuffer_{}; std::unordered_map> costs_; - std::optional precomputedCosts_ = {}; + std::optional precomputedScores_ = {}; }; } // namespace localization::database diff --git a/src/localization/database/cost_matrix.cpp b/src/localization/database/similarity_matrix.cpp similarity index 68% rename from src/localization/database/cost_matrix.cpp rename to src/localization/database/similarity_matrix.cpp index 929d334..74088b7 100644 --- a/src/localization/database/cost_matrix.cpp +++ b/src/localization/database/similarity_matrix.cpp @@ -1,6 +1,6 @@ /* By O. Vysotska in 2023 */ -#include "cost_matrix.h" +#include "similarity_matrix.h" #include "database/list_dir.h" #include "features/feature_factory.h" #include "localization_protos.pb.h" @@ -15,12 +15,12 @@ namespace { constexpr auto kEpsilon = 1e-09; } // namespace -CostMatrix::CostMatrix(const std::string &costMatrixFile) { - CHECK(!costMatrixFile.empty()) << "Cost matrix file is not set"; - loadFromProto(costMatrixFile); +SimilarityMatrix::SimilarityMatrix(const std::string &similarityMatrixFile) { + CHECK(!similarityMatrixFile.empty()) << "Cost matrix file is not set"; + loadFromProto(similarityMatrixFile); } -CostMatrix::CostMatrix(const std::string &queryFeaturesDir, +SimilarityMatrix::SimilarityMatrix(const std::string &queryFeaturesDir, const std::string &refFeaturesDir, const features::FeatureType &type) { const std::vector queryFeaturesFiles = @@ -31,7 +31,7 @@ CostMatrix::CostMatrix(const std::string &queryFeaturesDir, std::cerr << "Query features" << queryFeaturesFiles.size() << std::endl; std::cerr << "ref features" << refFeaturesFiles.size() << std::endl; - costs_.reserve(queryFeaturesFiles.size()); + scores_.reserve(queryFeaturesFiles.size()); for (const auto &queryFile : queryFeaturesFiles) { auto queryFeature = createFeature(type, queryFile); std::vector row; @@ -40,21 +40,21 @@ CostMatrix::CostMatrix(const std::string &queryFeaturesDir, const auto refFeature = createFeature(type, refFile); row.push_back(queryFeature->computeSimilarityScore(*refFeature)); } - costs_.push_back(row); + scores_.push_back(row); } - rows_ = costs_.size(); - if (costs_.size() > 0) { - cols_ = costs_[0].size(); + rows_ = scores_.size(); + if (scores_.size() > 0) { + cols_ = scores_[0].size(); } } -CostMatrix::CostMatrix(const Matrix &costs) { - costs_ = costs; - rows_ = costs.size(); - cols_ = rows_ > 0 ? costs[0].size() : 0; +SimilarityMatrix::SimilarityMatrix(const Matrix &scores) { + scores_ = scores; + rows_ = scores.size(); + cols_ = rows_ > 0 ? scores[0].size() : 0; } -void CostMatrix::loadFromTxt(const std::string &filename, int rows, int cols) { +void SimilarityMatrix::loadFromTxt(const std::string &filename, int rows, int cols) { std::ifstream in(filename); LOG_IF(FATAL, !in) << "The file cannot be opened " << filename; @@ -65,7 +65,7 @@ void CostMatrix::loadFromTxt(const std::string &filename, int rows, int cols) { float value; in >> row[c]; } - costs_.push_back(row); + scores_.push_back(row); } LOG(INFO) << "Matrix was read"; in.close(); @@ -73,25 +73,25 @@ void CostMatrix::loadFromTxt(const std::string &filename, int rows, int cols) { cols_ = cols; } -double CostMatrix::at(int row, int col) const { +double SimilarityMatrix::at(int row, int col) const { CHECK(row >= 0 && row < rows_) << "Row outside range " << row; CHECK(col >= 0 && col < cols_) << "Col outside range " << col; - return costs_[row][col]; + return scores_[row][col]; } -double CostMatrix::getInverseCost(int row, int col) const { +double SimilarityMatrix::getCost(int row, int col) const { const double value = this->at(row, col); if (std::abs(value) < kEpsilon) { return std::numeric_limits::max(); } if (value < 0){ - LOG(WARNING) << "The cost value for row:" << row << " and col:" << col <<" is < 0: " << value<< ". This should not be like this. I will make a positive value of it for now. But please check your values"; + LOG(WARNING) << "The score value for row:" << row << " and col:" << col <<" is < 0: " << value<< ". This should not be like this. I will make a positive value of it for now. But please check your values"; } return 1. / std::abs(value); } -void CostMatrix::loadFromProto(const std::string &filename) { +void SimilarityMatrix::loadFromProto(const std::string &filename) { GOOGLE_PROTOBUF_VERIFY_VERSION; image_sequence_localizer::CostMatrix cost_matrix_proto; std::fstream input(filename, std::ios::in | std::ios::binary); @@ -103,7 +103,7 @@ void CostMatrix::loadFromProto(const std::string &filename) { for (int idx = 0; idx < cost_matrix_proto.values_size(); ++idx) { row.push_back(cost_matrix_proto.values(idx)); if (row.size() == cost_matrix_proto.cols()) { - costs_.push_back(row); + scores_.push_back(row); row.clear(); } } diff --git a/src/localization/database/cost_matrix.h b/src/localization/database/similarity_matrix.h similarity index 52% rename from src/localization/database/cost_matrix.h rename to src/localization/database/similarity_matrix.h index d2a2db3..b989b50 100644 --- a/src/localization/database/cost_matrix.h +++ b/src/localization/database/similarity_matrix.h @@ -1,7 +1,7 @@ /* By O. Vysotska in 2023 */ -#ifndef SRC_DATABASE_COST_MATRIX_H_ -#define SRC_DATABASE_COST_MATRIX_H_ +#ifndef SRC_DATABASE_SIMILARITY_MATRIX_H_ +#define SRC_DATABASE_SIMILARITY_MATRIX_H_ #include "features/feature_factory.h" @@ -9,32 +9,33 @@ #include namespace localization::database { -class CostMatrix { +class SimilarityMatrix { public: using Matrix = std::vector>; - explicit CostMatrix(const std::string &costMatrixFile); - CostMatrix(const std::string &queryFeaturesDir, + explicit SimilarityMatrix(const std::string &similarityMatrixFile); + SimilarityMatrix(const std::string &queryFeaturesDir, const std::string &refFeaturesDir, const features::FeatureType &type); - explicit CostMatrix(const Matrix &costs); + explicit SimilarityMatrix(const Matrix &costs); void loadFromTxt(const std::string &filename, int rows, int cols); void loadFromProto(const std::string &filename); - const Matrix &getCosts() const { return costs_; } + const Matrix &getScores() const { return scores_; } double at(int row, int col) const; - // Computes 1/value. - double getInverseCost(int row, int col) const; + // Cost is the value opposite to a score. + // This function computes cost as inverse score cost = 1/score. + double getCost(int row, int col) const; int rows() const { return rows_; } int cols() const { return cols_; } private: - Matrix costs_; + Matrix scores_; int rows_ = 0; int cols_ = 0; }; } // namespace localization::database -#endif // SRC_DATABASE_COST_MATRIX_H_ \ No newline at end of file +#endif // SRC_DATABASE_SIMILARITY_MATRIX_H_ \ No newline at end of file diff --git a/src/localization/database/cost_matrix_database.cpp b/src/localization/database/similarity_matrix_database.cpp similarity index 78% rename from src/localization/database/cost_matrix_database.cpp rename to src/localization/database/similarity_matrix_database.cpp index f31da53..78da35b 100644 --- a/src/localization/database/cost_matrix_database.cpp +++ b/src/localization/database/similarity_matrix_database.cpp @@ -23,21 +23,19 @@ /* Updated by O. Vysotska in 2022 */ -#include "database/cost_matrix_database.h" -#include "database/cost_matrix.h" - -#include -#include +#include "database/similarity_matrix_database.h" +#include "database/similarity_matrix.h" +#include "similarity_matrix.h" #include namespace localization::database { -CostMatrixDatabase::CostMatrixDatabase(const std::string &costMatrixFile) - : costMatrix_(CostMatrix(costMatrixFile)) {} +SimilarityMatrixDatabase::SimilarityMatrixDatabase(const std::string &similarityMatrixFile) + : similarityMatrix_(SimilarityMatrix(similarityMatrixFile)) {} -double CostMatrixDatabase::getCost(int quId, int refId) { - return costMatrix_.getInverseCost(quId, refId); +double SimilarityMatrixDatabase::getCost(int quId, int refId) { + return similarityMatrix_.getCost(quId, refId); } } // namespace localization::database diff --git a/src/localization/database/cost_matrix_database.h b/src/localization/database/similarity_matrix_database.h similarity index 78% rename from src/localization/database/cost_matrix_database.h rename to src/localization/database/similarity_matrix_database.h index 10094ef..aee03f6 100644 --- a/src/localization/database/cost_matrix_database.h +++ b/src/localization/database/similarity_matrix_database.h @@ -23,28 +23,28 @@ /* Updated by O. Vysotska in 2022 */ -#ifndef SRC_DATABASE_COST_MATRIX_DATABASE_H_ -#define SRC_DATABASE_COST_MATRIX_DATABASE_H_ +#ifndef SRC_DATABASE_SIMILARITY_MATRIX_DATABASE_H_ +#define SRC_DATABASE_SIMILARITY_MATRIX_DATABASE_H_ -#include "database/cost_matrix.h" #include "database/idatabase.h" +#include "database/similarity_matrix.h" #include #include namespace localization::database { -class CostMatrixDatabase : public iDatabase { +class SimilarityMatrixDatabase : public iDatabase { public: - explicit CostMatrixDatabase(const std::string &costMatrixFile); + explicit SimilarityMatrixDatabase(const std::string &costMatrixFile); - int refSize() override { return costMatrix_.cols(); } + int refSize() override { return similarityMatrix_.cols(); } double getCost(int quId, int refId) override; private: - CostMatrix costMatrix_; + SimilarityMatrix similarityMatrix_; }; } // namespace localization::database -#endif // SRC_DATABASE_COST_MATRIX_DATABASE_H_ +#endif // SRC_DATABASE_SIMILARITY_MATRIX_DATABASE_H_ diff --git a/src/localization/features/ifeature.h b/src/localization/features/ifeature.h index 318d825..45f7148 100644 --- a/src/localization/features/ifeature.h +++ b/src/localization/features/ifeature.h @@ -67,12 +67,9 @@ class iFeature { std::vector dimensions; }; /** - \fn iFeature::score2weight + \fn iFeature::score2cost * Intuitively, the bigger the similarity between the feature the smaller - * should be the weight on the corresponding edges in the graph. - * If you already have an "inverted similarity" score, this function should - just return the same value it takes as an input. - + * should be the cost on the corresponding edges in the graph. */ } // namespace localization::features diff --git a/src/localization/tools/config_parser/config_parser.cpp b/src/localization/tools/config_parser/config_parser.cpp index d2cb637..77bc0fc 100644 --- a/src/localization/tools/config_parser/config_parser.cpp +++ b/src/localization/tools/config_parser/config_parser.cpp @@ -103,14 +103,9 @@ bool ConfigParser::parse(const std::string &iniFile) { ss >> imgExt; continue; } - if (header == "costMatrix") { + if (header == "similarityMatrix") { ss >> header; // reads "=" - ss >> costMatrix; - continue; - } - if (header == "costOutputName") { - ss >> header; // reads "=" - ss >> costOutputName; + ss >> similarityMatrix; continue; } if (header == "simPlaces") { @@ -138,8 +133,7 @@ void ConfigParser::print() const { printf("== Image extension: %s\n", imgExt.c_str()); printf("== Buffer size: %d\n", bufferSize); - printf("== CostMatrix: %s\n", costMatrix.c_str()); - printf("== costOutputName: %s\n", costOutputName.c_str()); + printf("== similarityMatrix: %s\n", similarityMatrix.c_str()); printf("== matchingResult: %s\n", matchingResult.c_str()); printf("== simPlaces: %s\n", simPlaces.c_str()); } @@ -184,11 +178,8 @@ bool ConfigParser::parseYaml(const std::string &yamlFile) { if (config["bufferSize"]) { bufferSize = config["bufferSize"].as(); } - if (config["costMatrix"]) { - costMatrix = config["costMatrix"].as(); - } - if (config["costOutputName"]) { - costOutputName = config["costOutputName"].as(); + if (config["similarityMatrix"]) { + similarityMatrix = config["similarityMatrix"].as(); } if (config["simPlaces"]) { simPlaces = config["simPlaces"].as(); diff --git a/src/localization/tools/config_parser/config_parser.h b/src/localization/tools/config_parser/config_parser.h index b9316b5..a54b1e8 100644 --- a/src/localization/tools/config_parser/config_parser.h +++ b/src/localization/tools/config_parser/config_parser.h @@ -41,8 +41,7 @@ class ConfigParser { std::string path2quImg = ""; std::string path2refImg = ""; std::string imgExt = ""; - std::string costMatrix = ""; - std::string costOutputName = ""; + std::string similarityMatrix = ""; std::string simPlaces = ""; std::string hashTable = ""; std::string matchingResult = "matches.MatchingResult.pb"; @@ -70,14 +69,9 @@ class ConfigParser { /*! \var std::string ConfigParser::imgExt \brief stores the extension of the provided images (".jpg", ".png", etc). */ -/*! \var std::string ConfigParser::costMatrix - \brief stores path to precomputed cost/similarity matrix. +/*! \var std::string ConfigParser::similarityMatrix + \brief stores path to precomputed similarity matrix. */ -/*! \var std::string ConfigParser::costOutputName - \brief stores the name of the produced result for the cost_matrix_based - matching. -*/ - /*! \var std::string ConfigParser::hashTable \brief stores the name of the file to read hash table from. matching. diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 6273007..6146f59 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -9,13 +9,13 @@ set(TESTNAME test_${PROJECT_NAME}) # ) add_executable(${TESTNAME} - cost_matrix_test.cpp + similarity_matrix_test.cpp database_test.cpp feature_buffer_test.cpp online_localizer_test.cpp ) target_link_libraries(${TESTNAME} - cost_matrix + similarity_matrix cnn_feature feature_buffer online_database diff --git a/src/test/cost_matrix_test.cpp b/src/test/cost_matrix_test.cpp deleted file mode 100644 index 303095a..0000000 --- a/src/test/cost_matrix_test.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* By O. Vysotska in 2023 */ - -#include "database/cost_matrix.h" -#include "localization_protos.pb.h" -#include "test_utils.h" - -#include "gtest/gtest.h" -#include - -namespace test { - -class CostMatrixTest : public ::testing::Test { -public: - void SetUp() { - - tmp_dir = std::filesystem::temp_directory_path() / "costMatrixTest"; - std::filesystem::create_directories(tmp_dir); - image_sequence_localizer::CostMatrix costMatrixProto; - for (int r = 0; r < 2; ++r) { - for (int c = 0; c < 3; ++c) { - costMatrixProto.add_values(costMatrixValues[r][c]); - } - } - - costMatrixProto.set_cols(3); - costMatrixProto.set_rows(2); - std::string testName = - ::testing::UnitTest::GetInstance()->current_test_info()->name(); - - costMatrixFile = tmp_dir / (testName + "_test.CostMatrix.pb"); - std::cout << "Saving to" << costMatrixFile; - std::fstream out(costMatrixFile, - std::ios::out | std::ios::trunc | std::ios::binary); - costMatrixProto.SerializeToOstream(&out); - out.close(); - } - - void TearDown() { - std::filesystem::remove(costMatrixFile); - std::filesystem::remove(tmp_dir); - tmp_dir = ""; - } - - std::filesystem::path tmp_dir = ""; - std::string costMatrixFile = ""; - const std::vector> costMatrixValues = {{1, 2, 3}, - {4, 5, 6}}; -}; - -TEST_F(CostMatrixTest, ConstructFromProto) { - auto costMatrix = localization::database::CostMatrix(costMatrixFile); - EXPECT_EQ(costMatrix.rows(), this->costMatrixValues.size()); - EXPECT_EQ(costMatrix.cols(), this->costMatrixValues[0].size()); -} - -TEST_F(CostMatrixTest, FailedToConstruct) { - ASSERT_DEATH(localization::database::CostMatrix(""), - "Cost matrix file is not set"); -} - -TEST_F(CostMatrixTest, at) { - auto costMatrix = localization::database::CostMatrix(costMatrixFile); - localization::database::CostMatrix::Matrix expectedMatrix = - this->costMatrixValues; - EXPECT_EQ(costMatrix.rows(), expectedMatrix.size()); - EXPECT_EQ(costMatrix.cols(), expectedMatrix[0].size()); - - for (int r = 0; r < costMatrix.rows(); ++r) { - for (int c = 0; c < costMatrix.cols(); ++c) { - EXPECT_NEAR(costMatrix.at(r, c), expectedMatrix[r][c], 1e-06); - } - } - ASSERT_DEATH(costMatrix.at(-1, 0), "Row outside range -1"); - ASSERT_DEATH(costMatrix.at(4, 0), "Row outside range 4"); - ASSERT_DEATH(costMatrix.at(0, -1), "Col outside range -1"); - ASSERT_DEATH(costMatrix.at(0, 4), "Col outside range 4"); -} - -TEST_F(CostMatrixTest, getInverseCost) { - auto costMatrix = localization::database::CostMatrix(costMatrixFile); - EXPECT_DOUBLE_EQ(costMatrix.getInverseCost(0, 0), 1); - EXPECT_NEAR(costMatrix.getInverseCost(1, 0), 1 / this->costMatrixValues[1][0], - 1e-06); -} - -TEST(CostMatrixComputation, createCostMatrixFromFeatures) { - const fs::path tmp_dir = test::createFeatures(); - - auto costMatrix = localization::database::CostMatrix( - tmp_dir, tmp_dir, localization::features::Cnn_Feature); - - for (int r = 0; r < costMatrix.rows(); ++r) { - for (int c = 0; c < costMatrix.cols(); ++c) { - EXPECT_NEAR(costMatrix.at(r, c), kCostMatrix[r][c], kTestEpsilon); - } - } -} -} // namespace test \ No newline at end of file diff --git a/src/test/database_test.cpp b/src/test/database_test.cpp index d0b699c..f7d252c 100644 --- a/src/test/database_test.cpp +++ b/src/test/database_test.cpp @@ -23,7 +23,7 @@ /* Updated by O. Vysotska in 2022 */ -#include "database/cost_matrix_database.h" +#include "database/similarity_matrix_database.h" #include "database/online_database.h" #include "localization_protos.pb.h" #include "test_utils.h" diff --git a/src/test/online_localizer_test.cpp b/src/test/online_localizer_test.cpp index ad8c38a..36f831c 100644 --- a/src/test/online_localizer_test.cpp +++ b/src/test/online_localizer_test.cpp @@ -1,5 +1,4 @@ -#include "database/cost_matrix_database.h" #include "database/online_database.h" #include "features/ifeature.h" #include "localization_protos.pb.h" diff --git a/src/test/similarity_matrix_test.cpp b/src/test/similarity_matrix_test.cpp new file mode 100644 index 0000000..f884e32 --- /dev/null +++ b/src/test/similarity_matrix_test.cpp @@ -0,0 +1,98 @@ +/* By O. Vysotska in 2023 */ + +#include "database/similarity_matrix.h" +#include "localization_protos.pb.h" +#include "test_utils.h" + +#include "gtest/gtest.h" +#include + +namespace test { + +class SimilarityMatrixTest : public ::testing::Test { +public: + void SetUp() { + + tmp_dir = std::filesystem::temp_directory_path() / "SimilarityMatrixTest"; + std::filesystem::create_directories(tmp_dir); + image_sequence_localizer::CostMatrix costMatrixProto; + for (int r = 0; r < 2; ++r) { + for (int c = 0; c < 3; ++c) { + costMatrixProto.add_values(similarityMatrixValues[r][c]); + } + } + + costMatrixProto.set_cols(3); + costMatrixProto.set_rows(2); + std::string testName = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + + similarityMatrixFile = tmp_dir / (testName + "_test.CostMatrix.pb"); + std::cout << "Saving to" << similarityMatrixFile; + std::fstream out(similarityMatrixFile, + std::ios::out | std::ios::trunc | std::ios::binary); + costMatrixProto.SerializeToOstream(&out); + out.close(); + } + + void TearDown() { + std::filesystem::remove(similarityMatrixFile); + std::filesystem::remove(tmp_dir); + tmp_dir = ""; + } + + std::filesystem::path tmp_dir = ""; + std::string similarityMatrixFile = ""; + const std::vector> similarityMatrixValues = {{1, 2, 3}, + {4, 5, 6}}; +}; + +TEST_F(SimilarityMatrixTest, ConstructFromProto) { + auto similarityMatrix = localization::database::SimilarityMatrix(similarityMatrixFile); + EXPECT_EQ(similarityMatrix.rows(), this->similarityMatrixValues.size()); + EXPECT_EQ(similarityMatrix.cols(), this->similarityMatrixValues[0].size()); +} + +TEST_F(SimilarityMatrixTest, FailedToConstruct) { + ASSERT_DEATH(localization::database::SimilarityMatrix(""), + "Similarity matrix file is not set"); +} + +TEST_F(SimilarityMatrixTest, at) { + auto similarityMatrix = localization::database::SimilarityMatrix(similarityMatrixFile); + localization::database::SimilarityMatrix::Matrix expectedMatrix = + this->similarityMatrixValues; + EXPECT_EQ(similarityMatrix.rows(), expectedMatrix.size()); + EXPECT_EQ(similarityMatrix.cols(), expectedMatrix[0].size()); + + for (int r = 0; r < similarityMatrix.rows(); ++r) { + for (int c = 0; c < similarityMatrix.cols(); ++c) { + EXPECT_NEAR(similarityMatrix.at(r, c), expectedMatrix[r][c], 1e-06); + } + } + ASSERT_DEATH(similarityMatrix.at(-1, 0), "Row outside range -1"); + ASSERT_DEATH(similarityMatrix.at(4, 0), "Row outside range 4"); + ASSERT_DEATH(similarityMatrix.at(0, -1), "Col outside range -1"); + ASSERT_DEATH(similarityMatrix.at(0, 4), "Col outside range 4"); +} + +TEST_F(SimilarityMatrixTest, getCost) { + auto similarityMatrix = localization::database::SimilarityMatrix(similarityMatrixFile); + EXPECT_DOUBLE_EQ(similarityMatrix.getCost(0, 0), 1); + EXPECT_NEAR(similarityMatrix.getCost(1, 0), 1 / this->similarityMatrixValues[1][0], + 1e-06); +} + +TEST(CostMatrixComputation, createCostMatrixFromFeatures) { + const fs::path tmp_dir = test::createFeatures(); + + auto similarityMatrix = localization::database::SimilarityMatrix( + tmp_dir, tmp_dir, localization::features::Cnn_Feature); + + for (int r = 0; r < similarityMatrix.rows(); ++r) { + for (int c = 0; c < similarityMatrix.cols(); ++c) { + EXPECT_NEAR(similarityMatrix.at(r, c), kSimilarityMatrix[r][c], kTestEpsilon); + } + } +} +} // namespace test \ No newline at end of file diff --git a/src/test/test_utils.h b/src/test/test_utils.h index 3a9e322..ed2aabf 100644 --- a/src/test/test_utils.h +++ b/src/test/test_utils.h @@ -62,7 +62,7 @@ inline void clearDataUnderPath(const std::filesystem::path &path) { std::filesystem::remove_all(path); } -const std::vector> kCostMatrix = { +const std::vector> kSimilarityMatrix = { {1, 0.922225, 0.285714, 0.99449}, {0.922225, 1, 0.634029, 0.922876}, {0.285714, 0.634029, 1, 0.298347}, From b41dbb7fb1593b7c538ec76ef7a9d7f5f4f8381f Mon Sep 17 00:00:00 2001 From: Olga Date: Mon, 30 Jun 2025 17:39:27 +0200 Subject: [PATCH 2/4] renamed python + proto part. Crashing for now --- .../database/similarity_matrix.cpp | 14 ++++---- src/localization_protos.proto | 2 +- ...matrix.py => compute_similarity_matrix.py} | 26 +++++++-------- src/python/matching_scripts.py | 12 +++---- src/python/protos_io.py | 32 +++++++++---------- src/python/run_matching_from_config.py | 2 +- src/python/run_matching_from_features.py | 6 ++-- src/python/run_matching_from_images.py | 9 ++---- src/python/visualize_cost_matrix.py | 19 ++++++----- src/python/visualize_localization_result.py | 17 +++++----- src/test/database_test.cpp | 18 +++++------ src/test/online_localizer_test.cpp | 8 ++--- src/test/similarity_matrix_test.cpp | 12 +++---- src/test/test_utils.h | 14 ++++---- 14 files changed, 93 insertions(+), 98 deletions(-) rename src/python/{compute_cost_matrix.py => compute_similarity_matrix.py} (63%) diff --git a/src/localization/database/similarity_matrix.cpp b/src/localization/database/similarity_matrix.cpp index 74088b7..21045bf 100644 --- a/src/localization/database/similarity_matrix.cpp +++ b/src/localization/database/similarity_matrix.cpp @@ -93,22 +93,22 @@ double SimilarityMatrix::getCost(int row, int col) const { void SimilarityMatrix::loadFromProto(const std::string &filename) { GOOGLE_PROTOBUF_VERIFY_VERSION; - image_sequence_localizer::CostMatrix cost_matrix_proto; + image_sequence_localizer::SimilarityMatrix similarity_matrix_proto; std::fstream input(filename, std::ios::in | std::ios::binary); - if (!cost_matrix_proto.ParseFromIstream(&input)) { + if (!similarity_matrix_proto.ParseFromIstream(&input)) { LOG(FATAL) << "Failed to parse cost_matrix file: " << filename; } std::vector row; - for (int idx = 0; idx < cost_matrix_proto.values_size(); ++idx) { - row.push_back(cost_matrix_proto.values(idx)); - if (row.size() == cost_matrix_proto.cols()) { + for (int idx = 0; idx < similarity_matrix_proto.values_size(); ++idx) { + row.push_back(similarity_matrix_proto.values(idx)); + if (row.size() == similarity_matrix_proto.cols()) { scores_.push_back(row); row.clear(); } } - cols_ = cost_matrix_proto.cols(); - rows_ = cost_matrix_proto.rows(); + cols_ = similarity_matrix_proto.cols(); + rows_ = similarity_matrix_proto.rows(); LOG(INFO) << "Read cost matrix with " << rows_ << " rows and " << cols_ << " cols."; } diff --git a/src/localization_protos.proto b/src/localization_protos.proto index 0bc3152..ae91d11 100644 --- a/src/localization_protos.proto +++ b/src/localization_protos.proto @@ -2,7 +2,7 @@ syntax = "proto2"; package image_sequence_localizer; -message CostMatrix { +message SimilarityMatrix { optional int32 rows = 20; optional int32 cols = 21; repeated double values = 1; diff --git a/src/python/compute_cost_matrix.py b/src/python/compute_similarity_matrix.py similarity index 63% rename from src/python/compute_cost_matrix.py rename to src/python/compute_similarity_matrix.py index dc89942..fbbbe0c 100644 --- a/src/python/compute_cost_matrix.py +++ b/src/python/compute_similarity_matrix.py @@ -10,17 +10,17 @@ def cosine_similarity(qu, db): return dist -def compute_cost_matrix(qu_features, db_features): - cost_matrix = np.zeros((len(qu_features), len(db_features))) +def compute_similarity_matrix(qu_features, db_features): + similarity_matrix = np.zeros((len(qu_features), len(db_features))) - print("Computing cost matrix...") + print("Computing similarity matrix...") for q, query_feature in enumerate(qu_features): for d, db_feature in enumerate(db_features): # Compute cosine distance - cost_matrix[q, d] = cosine_similarity(query_feature, db_feature) + similarity_matrix[q, d] = cosine_similarity(query_feature, db_feature) print("Finished.") - return cost_matrix + return similarity_matrix def main(): @@ -32,17 +32,17 @@ def main(): "--db_features", required=True, type=Path, help="Path to db directory." ) parser.add_argument( - "--cost_matrix_file", + "--similarity_matrix_file", required=False, - default="cost_matrix.CostMatrix.pb", + default="similarity_matrix.SimilarityMatrix.pb", type=Path, - help="File name for cost matrix output with extension CostMatrix.pb", + help="File name for similarity matrix output with extension SimilarityMatrix.pb", ) args = parser.parse_args() query_files = list(args.query_features.glob("*.Feature.pb")) db_files = list(args.db_features.glob("*.Feature.pb")) - cost_matrix_file = args.cost_matrix_file + similarity_matrix_file = args.similarity_matrix_file if len(query_files) == 0: print("WARNING: no query features were read") @@ -62,11 +62,11 @@ def main(): for db_file in db_files: db_features.append(protos_io.read_feature(db_file)) - cost_matrix = compute_cost_matrix(query_features, db_features) + similarity_matrix = compute_similarity_matrix(query_features, db_features) - print("Matrix size", cost_matrix.shape) - protos_io.write_cost_matrix(cost_matrix, cost_matrix_file) - print("The cost matrix was save to", cost_matrix_file) + print("Matrix size", similarity_matrix.shape) + protos_io.write_similarity_matrix(similarity_matrix, similarity_matrix_file) + print("The similarity matrix was save to", similarity_matrix_file) if __name__ == "__main__": diff --git a/src/python/matching_scripts.py b/src/python/matching_scripts.py index 9beeabc..64419f7 100644 --- a/src/python/matching_scripts.py +++ b/src/python/matching_scripts.py @@ -9,7 +9,7 @@ class RunParameters: path2ref_images: str = None path2qu: str = None path2ref: str = None - costMatrix: str = None + similarityMatrix: str = None matchingResult: str = None matchingResultImage: str = None expansionRate: float = 0.3 @@ -96,17 +96,17 @@ def computeFeatures(image_dir, output_folder, dataset_name, feature_type): ) -def computeCostMatrix(run_params): +def computeSimilarityMatrix(run_params): params = "--query_features {query_features} ".format( query_features=run_params.path2qu ) params += "--db_features {reference_features} ".format( reference_features=run_params.path2ref ) - params += "--cost_matrix_file {cost_matrix_file} ".format( - cost_matrix_file=run_params.costMatrix + params += "--similarity_matrix_file {cost_matrix_file} ".format( + cost_matrix_file=run_params.similarityMatrix ) - command = "python compute_cost_matrix.py " + params + command = "python compute_similarity_matrix.py " + params print("Calling:", command) os.system(command) @@ -119,7 +119,7 @@ def runMatching(config_yaml_file): def runLocalizationResultVisualization(run_params): - params = "--cost_matrix {cost_matrix} ".format(cost_matrix=run_params.costMatrix) + params = "--similarity_matrix {similarity_matrix} ".format(cost_matrix=run_params.similarityMatrix) params += "--matching_result {matching_result} ".format( matching_result=run_params.matchingResult ) diff --git a/src/python/protos_io.py b/src/python/protos_io.py index 9643194..e83ca88 100644 --- a/src/python/protos_io.py +++ b/src/python/protos_io.py @@ -17,29 +17,29 @@ def write_feature(filename, proto): f.close() -def write_cost_matrix(cost_matrix, cost_matrix_file): +def write_similarity_matrix(simlarity_matrix, similarity_matrix_file): - cost_matrix_proto = loc_protos.CostMatrix() - cost_matrix_proto.rows = cost_matrix.shape[0] - cost_matrix_proto.cols = cost_matrix.shape[1] - for row in cost_matrix: - cost_matrix_proto.values.extend(row.tolist()) + similarity_matrix_proto = loc_protos.SimilarityMatrix() + similarity_matrix_proto.rows = simlarity_matrix.shape[0] + similarity_matrix_proto.cols = simlarity_matrix.shape[1] + for row in simlarity_matrix: + similarity_matrix_proto.values.extend(row.tolist()) - f = open(cost_matrix_file, "wb") - f.write(cost_matrix_proto.SerializeToString()) + f = open(similarity_matrix_file, "wb") + f.write(similarity_matrix_proto.SerializeToString()) f.close() -def read_cost_matrix(cost_matrix_file): - f = open(cost_matrix_file, "rb") - cost_matrix_proto = loc_protos.CostMatrix() - cost_matrix_proto.ParseFromString(f.read()) +def read_similarity_matrix(similarity_matrix_file): + f = open(similarity_matrix_file, "rb") + similarity_matrix_proto = loc_protos.CostMatrix() + similarity_matrix_proto.ParseFromString(f.read()) f.close() - cost_matrix = np.array(cost_matrix_proto.values) - cost_matrix = np.reshape( - cost_matrix, (cost_matrix_proto.rows, cost_matrix_proto.cols) + similarity_matrix = np.array(similarity_matrix_proto.values) + similarity_matrix = np.reshape( + similarity_matrix, (similarity_matrix_proto.rows, similarity_matrix_proto.cols) ) - return cost_matrix + return similarity_matrix def read_matching_result(filename): diff --git a/src/python/run_matching_from_config.py b/src/python/run_matching_from_config.py index bd997b1..a99a288 100644 --- a/src/python/run_matching_from_config.py +++ b/src/python/run_matching_from_config.py @@ -28,7 +28,7 @@ def main(): run_params = matching.RunParameters() run_params = matching.initializeFromDict(run_params, config) - matching.computeCostMatrix(run_params) + matching.computeSimilarityMatrix(run_params) matching.runMatching(args.yaml_config_file) matching.runLocalizationResultVisualization(run_params) diff --git a/src/python/run_matching_from_features.py b/src/python/run_matching_from_features.py index e522423..e4db806 100644 --- a/src/python/run_matching_from_features.py +++ b/src/python/run_matching_from_features.py @@ -41,8 +41,8 @@ def setRunParameters(args): run_parameters = matching.RunParameters() run_parameters.path2qu = args.query_features.as_posix() run_parameters.path2ref = args.reference_features.as_posix() - run_parameters.costMatrix = ( - args.output_dir / (args.dataset_name + ".CostMatrix.pb") + run_parameters.similarityMatrix = ( + args.output_dir / (args.dataset_name + ".SimilarityMatrix.pb") ).as_posix() run_parameters.matchingResult = ( args.output_dir / (args.dataset_name + ".MatchingResult.pb") @@ -75,7 +75,7 @@ def main(): with open(yaml_config_file, "w") as file: yaml.dump(param_as_dict, file) - matching.computeCostMatrix(run_params) + matching.computeSimilarityMatrix(run_params) matching.runMatching(yaml_config_file) matching.runLocalizationResultVisualization(run_params) diff --git a/src/python/run_matching_from_images.py b/src/python/run_matching_from_images.py index af7e617..976cd0e 100644 --- a/src/python/run_matching_from_images.py +++ b/src/python/run_matching_from_images.py @@ -60,8 +60,8 @@ def setRunParameters(args): run_parameters.path2ref_images = args.reference_images.as_posix() run_parameters.path2qu = (args.output_dir / "query_features").as_posix() run_parameters.path2ref = (args.output_dir / "reference_features").as_posix() - run_parameters.costMatrix = ( - args.output_dir / (args.dataset_name + ".CostMatrix.pb") + run_parameters.similarityMatrix = ( + args.output_dir / (args.dataset_name + ".SimilarityMatrix.pb") ).as_posix() run_parameters.matchingResult = ( args.output_dir / (args.dataset_name + ".MatchingResult.pb") @@ -69,9 +69,6 @@ def setRunParameters(args): run_parameters.matchingResultImage = ( args.output_dir / (args.dataset_name + "_result.png") ).as_posix() - run_parameters.debugProto = ( - args.output_dir / (args.dataset_name + ".OnlineLocalizerDebug.png") - ).as_posix() queriesNum = len(list(Path(run_parameters.path2qu).glob("*Feature.pb"))) run_parameters.querySize = queriesNum @@ -113,7 +110,7 @@ def main(): with open(yaml_config_file, "w") as file: yaml.dump(params_as_dict, file) - matching.computeCostMatrix(run_params) + matching.computeSimilarityMatrix(run_params) matching.runMatching(yaml_config_file) matching.runLocalizationResultVisualization(run_params) if args.write_image_matches: diff --git a/src/python/visualize_cost_matrix.py b/src/python/visualize_cost_matrix.py index c2027d2..5581c14 100644 --- a/src/python/visualize_cost_matrix.py +++ b/src/python/visualize_cost_matrix.py @@ -8,38 +8,37 @@ def main(): parser = argparse.ArgumentParser() parser.add_argument( - "--cost_matrix", + "--similarity_matrix", required=True, type=Path, - help="Path to the cost_matrix .CostMatrix.pb file", + help="Path to the similarity matrix .SimilarityMatrix.pb file", ) parser.add_argument( "--image_name", required=False, default=None, type=Path, - help="Image name to save the cost matrix to.", + help="Image name to save the similarity matrix to.", ) args = parser.parse_args() - cost_matrix_file = args.cost_matrix - cost_matrix = protos_io.read_cost_matrix(cost_matrix_file) + similarity_matrix = protos_io.read_cost_matrix(args.similarity_matrix) - max_value = np.max(np.max(cost_matrix)) - min_value = np.min(np.min(cost_matrix)) + max_value = np.max(np.max(similarity_matrix)) + min_value = np.min(np.min(similarity_matrix)) print("Max value", max_value) print("Min value", min_value) if args.image_name: print(args.image_name) - img = np.array(cost_matrix, dtype=float) * float(255) + img = np.array(similarity_matrix, dtype=float) * float(255) cv2.imwrite(str(args.image_name), img) - window_name = f"cost_matrix {max_value:.4f}:{min_value:.4f}" + window_name = f"similarity_matrix {max_value:.4f}:{min_value:.4f}" cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) - cv2.imshow(window_name, cost_matrix) + cv2.imshow(window_name, similarity_matrix) cv2.waitKey(0) cv2.destroyAllWindows() diff --git a/src/python/visualize_localization_result.py b/src/python/visualize_localization_result.py index 48c9e0f..a91c61b 100644 --- a/src/python/visualize_localization_result.py +++ b/src/python/visualize_localization_result.py @@ -33,10 +33,10 @@ def create_combined_image(matching_result, cost_matrix, expanded_mask=None): def main(): parser = argparse.ArgumentParser() parser.add_argument( - "--cost_matrix", + "--similarity_matrix", required=True, type=Path, - help="Path to the cost_matrix .CostMatrix.pb file", + help="Path to the similarity matrix .SimilarityMatrix.pb file", ) parser.add_argument( "--matching_result", @@ -55,7 +55,7 @@ def main(): required=False, default=None, type=Path, - help="Image name to save the cost matrix to.", + help="Image name to save the result to.", ) parser.add_argument( "--show_image", @@ -65,11 +65,10 @@ def main(): args = parser.parse_args() - cost_matrix_file = args.cost_matrix - cost_matrix = protos_io.read_cost_matrix(cost_matrix_file) + similarity_matrix = protos_io.read_cost_matrix(args.similarity_matrix) - max_value = np.max(np.max(cost_matrix)) - min_value = np.min(np.min(cost_matrix)) + max_value = np.max(np.max(similarity_matrix)) + min_value = np.min(np.min(similarity_matrix)) print("Max value", max_value) print("Min value", min_value) @@ -81,7 +80,7 @@ def main(): else: expanded_mask = None - image_bgr = create_combined_image(matching_result, cost_matrix, expanded_mask) + image_bgr = create_combined_image(matching_result, similarity_matrix, expanded_mask) if args.image_name: image_bgr_char = np.array(image_bgr, dtype=float) * float(255) @@ -89,7 +88,7 @@ def main(): print("Image is written to", args.image_name) if args.show_image: - window_name = f"cost_matrix {max_value:.4f}:{min_value:.4f}" + window_name = f"similarity matrix {max_value:.4f}:{min_value:.4f}" cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) cv2.imshow(window_name, image_bgr) diff --git a/src/test/database_test.cpp b/src/test/database_test.cpp index f7d252c..b2cf90f 100644 --- a/src/test/database_test.cpp +++ b/src/test/database_test.cpp @@ -44,20 +44,20 @@ using FeatureType = localization::features::FeatureType; class OnlineDatabaseTest : public ::testing::Test { protected: - std::string createCostMatrixProto(const std::filesystem::path &dir) { - image_sequence_localizer::CostMatrix cost_matrix; + std::string createSimilarityMatrixProto(const std::filesystem::path &dir) { + image_sequence_localizer::SimilarityMatrix similarity_matrix; for (double value : {1, 2, 3, 4, 5, 6}) { - cost_matrix.add_values(value); + similarity_matrix.add_values(value); } - cost_matrix.set_cols(3); - cost_matrix.set_rows(2); + similarity_matrix.set_cols(3); + similarity_matrix.set_rows(2); std::string cost_matrix_name = - (dir / "test_cost_matrix.CostMatrix.pb").string(); + (dir / "test_similarity_matrix.SimilarityMatrix.pb").string(); std::fstream out(cost_matrix_name, std::ios::out | std::ios::trunc | std::ios::binary); - cost_matrix.SerializeToOstream(&out); + similarity_matrix.SerializeToOstream(&out); out.close(); return cost_matrix_name; } @@ -108,7 +108,7 @@ TEST_F(OnlineDatabaseTest, NoCostMatrixFile) { } TEST_F(OnlineDatabaseTest, CostMatrixDatabaseConstructor) { - std::string cost_matrix_name = createCostMatrixProto(tmp_dir); + std::string cost_matrix_name = createSimilarityMatrixProto(tmp_dir); loc_database::OnlineDatabase database(/*queryFeaturesDir=*/tmp_dir, /*refFeaturesDir=*/tmp_dir, /*type=*/FeatureType::Cnn_Feature, @@ -120,7 +120,7 @@ TEST_F(OnlineDatabaseTest, CostMatrixDatabaseConstructor) { } TEST_F(OnlineDatabaseTest, CostMatrixDatabaseGetCost) { - std::string cost_matrix_name = createCostMatrixProto(tmp_dir); + std::string cost_matrix_name = createSimilarityMatrixProto(tmp_dir); loc_database::OnlineDatabase database(/*queryFeaturesDir=*/tmp_dir, /*refFeaturesDir=*/tmp_dir, /*type=*/FeatureType::Cnn_Feature, diff --git a/src/test/online_localizer_test.cpp b/src/test/online_localizer_test.cpp index 36f831c..f585d3a 100644 --- a/src/test/online_localizer_test.cpp +++ b/src/test/online_localizer_test.cpp @@ -27,12 +27,12 @@ class OnlineLocalizerTest : public ::testing::Test { void SetUp() { tmp_dir = test::createFeatures(); std::filesystem::path featureDir = tmp_dir; - image_sequence_localizer::CostMatrix cost_matrix = - test::computeCostMatrixProto(featureDir, featureDir); - std::string costMatrixFile = tmp_dir / "test.CostMatrix.pb"; + image_sequence_localizer::SimilarityMatrix similarity_matrix = + test::computeSimilarityMatrixProto(featureDir, featureDir); + std::string costMatrixFile = tmp_dir / "test.SimilarityMatrix.pb"; std::fstream out(costMatrixFile, std::ios::out | std::ios::trunc | std::ios::binary); - cost_matrix.SerializeToOstream(&out); + similarity_matrix.SerializeToOstream(&out); out.close(); database = std::make_unique( diff --git a/src/test/similarity_matrix_test.cpp b/src/test/similarity_matrix_test.cpp index f884e32..623e87b 100644 --- a/src/test/similarity_matrix_test.cpp +++ b/src/test/similarity_matrix_test.cpp @@ -15,23 +15,23 @@ class SimilarityMatrixTest : public ::testing::Test { tmp_dir = std::filesystem::temp_directory_path() / "SimilarityMatrixTest"; std::filesystem::create_directories(tmp_dir); - image_sequence_localizer::CostMatrix costMatrixProto; + image_sequence_localizer::SimilarityMatrix similarityMatrixProto; for (int r = 0; r < 2; ++r) { for (int c = 0; c < 3; ++c) { - costMatrixProto.add_values(similarityMatrixValues[r][c]); + similarityMatrixProto.add_values(similarityMatrixValues[r][c]); } } - costMatrixProto.set_cols(3); - costMatrixProto.set_rows(2); + similarityMatrixProto.set_cols(3); + similarityMatrixProto.set_rows(2); std::string testName = ::testing::UnitTest::GetInstance()->current_test_info()->name(); - similarityMatrixFile = tmp_dir / (testName + "_test.CostMatrix.pb"); + similarityMatrixFile = tmp_dir / (testName + "_test.SimilarityMatrix.pb"); std::cout << "Saving to" << similarityMatrixFile; std::fstream out(similarityMatrixFile, std::ios::out | std::ios::trunc | std::ios::binary); - costMatrixProto.SerializeToOstream(&out); + similarityMatrixProto.SerializeToOstream(&out); out.close(); } diff --git a/src/test/test_utils.h b/src/test/test_utils.h index ed2aabf..8804d83 100644 --- a/src/test/test_utils.h +++ b/src/test/test_utils.h @@ -68,26 +68,26 @@ const std::vector> kSimilarityMatrix = { {0.285714, 0.634029, 1, 0.298347}, {0.99449, 0.922876, 0.298347, 1}}; -inline image_sequence_localizer::CostMatrix -computeCostMatrixProto(const fs::path &queryDir, const fs::path &refDir) { +inline image_sequence_localizer::SimilarityMatrix +computeSimilarityMatrixProto(const fs::path &queryDir, const fs::path &refDir) { const std::vector queryFiles = localization::database::listProtoDir(queryDir, ".Feature"); const std::vector refFiles = localization::database::listProtoDir(refDir, ".Feature"); - image_sequence_localizer::CostMatrix cost_matrix; + image_sequence_localizer::SimilarityMatrix similarity_matrix; for (const auto &query : queryFiles) { const auto queryFeature = localization::features::CnnFeature(query); for (const auto &ref : refFiles) { - cost_matrix.add_values(queryFeature.computeSimilarityScore( + similarity_matrix.add_values(queryFeature.computeSimilarityScore( localization::features::CnnFeature(ref))); } } - cost_matrix.set_cols(refFiles.size()); - cost_matrix.set_rows(queryFiles.size()); - return cost_matrix; + similarity_matrix.set_cols(refFiles.size()); + similarity_matrix.set_rows(queryFiles.size()); + return similarity_matrix; } } // namespace test From a3dce3e573e416a4a8f9912ad5054e8082bd7957 Mon Sep 17 00:00:00 2001 From: Olga Date: Wed, 2 Jul 2025 17:02:36 +0200 Subject: [PATCH 3/4] fixed introduced bugs --- .../similarity_matrix_based_matching/online_localizer_lsh.cpp | 2 +- .../similarity_matrix_no_hashing.cpp | 2 +- src/python/matching_scripts.py | 4 ++-- src/python/protos_io.py | 2 +- src/python/visualize_localization_result.py | 2 +- ...isualize_cost_matrix.py => visualize_similarity_matrix.py} | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) rename src/python/{visualize_cost_matrix.py => visualize_similarity_matrix.py} (93%) diff --git a/src/apps/similarity_matrix_based_matching/online_localizer_lsh.cpp b/src/apps/similarity_matrix_based_matching/online_localizer_lsh.cpp index 7fbd9be..fc15835 100644 --- a/src/apps/similarity_matrix_based_matching/online_localizer_lsh.cpp +++ b/src/apps/similarity_matrix_based_matching/online_localizer_lsh.cpp @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) { if (argc < 2) { printf("[ERROR] Not enough input parameters.\n"); - printf("Proper usage: ./cost_matrix_based_matching_lsh config_file.yaml\n"); + printf("Proper usage: ./online_localizer_lsh config_file.yaml\n"); exit(0); } diff --git a/src/apps/similarity_matrix_based_matching/similarity_matrix_no_hashing.cpp b/src/apps/similarity_matrix_based_matching/similarity_matrix_no_hashing.cpp index c737406..3d8e953 100644 --- a/src/apps/similarity_matrix_based_matching/similarity_matrix_no_hashing.cpp +++ b/src/apps/similarity_matrix_based_matching/similarity_matrix_no_hashing.cpp @@ -20,7 +20,7 @@ int main(int argc, char *argv[]) { if (argc < 2) { LOG(ERROR) << "Not enough input parameters."; - LOG(INFO) << "Proper usage: ./cost_matrix_based_matching_no_hashing " + LOG(INFO) << "Proper usage: ./similarity_matrix_based_matching_no_hashing " "config_file.yaml"; exit(0); } diff --git a/src/python/matching_scripts.py b/src/python/matching_scripts.py index 64419f7..5f6fdcc 100644 --- a/src/python/matching_scripts.py +++ b/src/python/matching_scripts.py @@ -112,14 +112,14 @@ def computeSimilarityMatrix(run_params): def runMatching(config_yaml_file): - binary = "../../build/src/apps/cost_matrix_based_matching/online_localizer_lsh" + binary = "../../build/src/apps/similarity_matrix_based_matching/online_localizer_lsh" command = binary + " " + str(config_yaml_file) print("Calling:", command) os.system(command) def runLocalizationResultVisualization(run_params): - params = "--similarity_matrix {similarity_matrix} ".format(cost_matrix=run_params.similarityMatrix) + params = "--similarity_matrix {similarity_matrix} ".format(similarity_matrix=run_params.similarityMatrix) params += "--matching_result {matching_result} ".format( matching_result=run_params.matchingResult ) diff --git a/src/python/protos_io.py b/src/python/protos_io.py index e83ca88..f452686 100644 --- a/src/python/protos_io.py +++ b/src/python/protos_io.py @@ -32,7 +32,7 @@ def write_similarity_matrix(simlarity_matrix, similarity_matrix_file): def read_similarity_matrix(similarity_matrix_file): f = open(similarity_matrix_file, "rb") - similarity_matrix_proto = loc_protos.CostMatrix() + similarity_matrix_proto = loc_protos.SimilarityMatrix() similarity_matrix_proto.ParseFromString(f.read()) f.close() similarity_matrix = np.array(similarity_matrix_proto.values) diff --git a/src/python/visualize_localization_result.py b/src/python/visualize_localization_result.py index a91c61b..cb6e563 100644 --- a/src/python/visualize_localization_result.py +++ b/src/python/visualize_localization_result.py @@ -65,7 +65,7 @@ def main(): args = parser.parse_args() - similarity_matrix = protos_io.read_cost_matrix(args.similarity_matrix) + similarity_matrix = protos_io.read_similarity_matrix(args.similarity_matrix) max_value = np.max(np.max(similarity_matrix)) min_value = np.min(np.min(similarity_matrix)) diff --git a/src/python/visualize_cost_matrix.py b/src/python/visualize_similarity_matrix.py similarity index 93% rename from src/python/visualize_cost_matrix.py rename to src/python/visualize_similarity_matrix.py index 5581c14..91f13dd 100644 --- a/src/python/visualize_cost_matrix.py +++ b/src/python/visualize_similarity_matrix.py @@ -22,7 +22,7 @@ def main(): ) args = parser.parse_args() - similarity_matrix = protos_io.read_cost_matrix(args.similarity_matrix) + similarity_matrix = protos_io.read_similarity_matrix(args.similarity_matrix) max_value = np.max(np.max(similarity_matrix)) min_value = np.min(np.min(similarity_matrix)) From 7d47730be82e8ef4aca16aff3aa6ab1ff2cd6459 Mon Sep 17 00:00:00 2001 From: Olga Date: Wed, 2 Jul 2025 17:33:42 +0200 Subject: [PATCH 4/4] fixed the build --- src/localization/database/similarity_matrix.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/localization/database/similarity_matrix.cpp b/src/localization/database/similarity_matrix.cpp index 21045bf..ad5f5f8 100644 --- a/src/localization/database/similarity_matrix.cpp +++ b/src/localization/database/similarity_matrix.cpp @@ -16,7 +16,7 @@ constexpr auto kEpsilon = 1e-09; } // namespace SimilarityMatrix::SimilarityMatrix(const std::string &similarityMatrixFile) { - CHECK(!similarityMatrixFile.empty()) << "Cost matrix file is not set"; + CHECK(!similarityMatrixFile.empty()) << "Similarity matrix file is not set"; loadFromProto(similarityMatrixFile); }