Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
add_subdirectory(cost_matrix_based_matching)
add_subdirectory(similarity_matrix_based_matching)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<loc::relocalizers::LshCvHashing>(
/*onlineDatabase=*/database.get(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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<loc::relocalizers::LshCvHashing>(
/*onlineDatabase=*/database.get(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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);
}
Expand All @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) {
parser.print();

const auto database =
std::make_unique<loc::database::CostMatrixDatabase>(parser.costMatrix);
std::make_unique<loc::database::SimilarityMatrixDatabase>(parser.similarityMatrix);

const auto relocalizer =
std::make_unique<loc::relocalizers::DefaultRelocalizer>(
Expand Down
12 changes: 6 additions & 6 deletions src/localization/database/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)

11 changes: 6 additions & 5 deletions src/localization/database/online_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "database/list_dir.h"
#include "features/feature_buffer.h"
#include "features/ifeature.h"
#include "similarity_matrix.h"

#include <glog/logging.h>

Expand Down Expand Up @@ -54,16 +55,16 @@ 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<features::FeatureBuffer>(
bufferSize)},
queryBuffer_{std::make_unique<features::FeatureBuffer>(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);
}
}

Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/localization/database/online_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand All @@ -66,7 +66,7 @@ class OnlineDatabase : public iDatabase {
std::unique_ptr<features::FeatureBuffer> queryBuffer_{};
std::unordered_map<int, std::unordered_map<int, double>> costs_;

std::optional<CostMatrix> precomputedCosts_ = {};
std::optional<SimilarityMatrix> precomputedScores_ = {};
};
} // namespace localization::database

Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()) << "Similarity 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<std::string> queryFeaturesFiles =
Expand All @@ -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<double> row;
Expand All @@ -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;

Expand All @@ -65,50 +65,50 @@ 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();
rows_ = rows;
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<double>::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;
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<double> 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()) {
costs_.push_back(row);
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.";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
/* 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"

#include <string>
#include <vector>

namespace localization::database {
class CostMatrix {
class SimilarityMatrix {
public:
using Matrix = std::vector<std::vector<double>>;

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_
#endif // SRC_DATABASE_SIMILARITY_MATRIX_H_
Loading