vocabtree  0.0.1
bench_search.cxx
Go to the documentation of this file.
1 #include "bench_config.hpp"
2 
3 #include <fstream>
4 
5 #include <config.hpp>
6 #include <utils/filesystem.hpp>
7 #include <utils/numerics.hpp>
8 #include <utils/dataset.hpp>
9 #include <utils/misc.hpp>
10 #include <utils/vision.hpp>
11 #include <utils/logger.hpp>
12 #include <utils/image.hpp>
13 #include <utils/cycletimer.hpp>
16 #include <vis/matches_page.hpp>
17 
18 #if ENABLE_MULTITHREADING && ENABLE_OPENMP
19 #include <omp.h>
20 #endif
21 #if ENABLE_MULTITHREADING && ENABLE_MPI
22 #include <mpi.h>
23 #endif
24 
25 _INITIALIZE_EASYLOGGINGPP
26 
27 void bench_oxford() {
28 
29  MatchesPage html_output;
30 
31  const uint32_t num_clusters = s_oxford_num_clusters;
32 
34  LINFO << oxford_dataset;
35 
36  std::stringstream index_output_file;
37  index_output_file << oxford_dataset.location() << "/index/" << num_clusters << ".index";
38  InvertedIndex ii(index_output_file.str());
39 
40  double total_time = 0.0;
41  uint32_t num_validate = 16;
42  uint32_t total_iterations = 256;
43  for(uint32_t i=0; i<total_iterations; i++) {
44  double start_time = CycleTimer::currentSeconds();
45  std::shared_ptr<InvertedIndex::MatchResults> matches =
46  std::static_pointer_cast<InvertedIndex::MatchResults>(ii.search(oxford_dataset, nullptr, oxford_dataset.image(i)));
47  if(matches == nullptr) {
48  LERROR << "Error while running search.";
49  continue;
50  }
51  double end_time = CycleTimer::currentSeconds();
52  total_time += (end_time - start_time);
53 
54 
55  // validate matches
56  cv::Mat keypoints_0, descriptors_0;
57  std::shared_ptr<SimpleDataset::SimpleImage> query_image = std::static_pointer_cast<SimpleDataset::SimpleImage>(oxford_dataset.image(i));
58  const std::string &query_keypoints_location = oxford_dataset.location(query_image->feature_path("keypoints"));
59  const std::string &query_descriptors_location = oxford_dataset.location(query_image->feature_path("descriptors"));
60  filesystem::load_cvmat(query_keypoints_location, keypoints_0);
61  filesystem::load_cvmat(query_descriptors_location, descriptors_0);
62  std::vector<int> validated(num_validate, 0);
63 #if ENABLE_MULTITHREADING && ENABLE_OPENMP
64  #pragma omp parallel for schedule(dynamic)
65 #endif
66  for(int32_t j=0; j<(int32_t)num_validate; j++) {
67  cv::Mat keypoints_1, descriptors_1;
68  std::shared_ptr<SimpleDataset::SimpleImage> match_image = std::static_pointer_cast<SimpleDataset::SimpleImage>(oxford_dataset.image(matches->matches[j]));
69  const std::string &match_keypoints_location = oxford_dataset.location(match_image->feature_path("keypoints"));
70  const std::string &match_descriptors_location = oxford_dataset.location(match_image->feature_path("descriptors"));
71  filesystem::load_cvmat(match_keypoints_location, keypoints_1);
72  filesystem::load_cvmat(match_descriptors_location, descriptors_1);
73 
74  cv::detail::MatchesInfo match_info;
75  vision::geo_verify_f(descriptors_0, keypoints_0, descriptors_1, keypoints_1, match_info);
76 
77  validated[j] = vision::is_good_match(match_info) ? 1 : -1;
78  }
79 
80  html_output.add_match(i, matches->matches, oxford_dataset, std::make_shared< std::vector<int> >(validated));
81  html_output.write(oxford_dataset.location() + "/results/matches/");
82  }
83 
84 
85 
86 
87  // Write out the timings
88  std::stringstream timings_file_name;
89  timings_file_name << oxford_dataset.location() + "/results/times.index." << ii.num_clusters() << ".csv";
90  std::ofstream ofs(timings_file_name.str(), std::ios::app);
91  if((size_t)ofs.tellp() == 0) {
92  std::stringstream header;
93  header << "machine\ttime(s)\titerations\tmultithreading\topenmp\tmpi\t" << std::endl;
94  ofs.write(header.str().c_str(), header.str().size());
95  }
96  std::stringstream timing;
97  timing << misc::get_machine_name() << "\t" << total_time << "\t" << total_iterations << "\t" <<
99  "\t" << ENABLE_MPI << "\t" << std::endl;
100  ofs.write(timing.str().c_str(), timing.str().size());
101  ofs.close();
102 
103  // validate matches
104 
105 }
106 
107 int main(int argc, char *argv[]) {
108 #if ENABLE_MULTITHREADING && ENABLE_MPI
109  MPI::Init(argc, argv);
110  int rank = MPI::COMM_WORLD.Get_rank();
111  if(rank == 0) {
112 #endif
113 
114  bench_oxford();
115 
116 #if ENABLE_MULTITHREADING && ENABLE_MPI
117  }
118  MPI::Finalize();
119 #endif
120  return 0;
121 }