vocabtree  0.0.1
inverted_index.hpp
Go to the documentation of this file.
1 #pragma once
2 
5 
6 /// Implements a Bag of Words based (BoW) image search using an inverted index. The inverted
7 /// index keeps track of a list of images associated with each visual word. The images are
8 /// represented as an unsigned long long which must then be translated back to an actual image
9 /// using the appropriate Dataset class implementation.
10 class InvertedIndex : public SearchBase {
11 public:
12 
13  /// Subclass of train params base which specifies inverted index training parameters.
14  struct TrainParams : public TrainParamsBase {
15  std::shared_ptr<BagOfWords> bag_of_words; /// bag of words to index on
16  };
17 
18  /// Subclass of train params base which specifies inverted index training parameters.
19  struct SearchParams : public SearchParamsBase {
20 
21  };
22 
23  /// Subclass of match results base which also returns scores
24  struct MatchResults : public MatchResultsBase {
25  std::vector<float> tfidf_scores;
26  };
27 
28  InvertedIndex();
29  InvertedIndex(const std::string &file_name);
30 
31  /// Given a set of training parameters, list of images, trains. Returns true if successful, false
32  /// if not successful.
33  bool train(Dataset &dataset, const std::shared_ptr<const TrainParamsBase> &params,
34  const std::vector< std::shared_ptr<const Image > > &examples);
35 
36  /// Loads a trained search structure from the input filepath
37  bool load (const std::string &file_path);
38 
39  /// Saves a trained search structure to the input filepath
40  bool save (const std::string &file_path) const;
41 
42  /// Returns the number of clusters used in the inverted index descriptors
43  uint32_t num_clusters() const;
44 
45  /// Given a set of search parameters, a query image, searches for matching images and returns the match. If the match is nullptr, then the search failed
46  /// (it will fail if the example image has missing features).
47  std::shared_ptr<MatchResultsBase> search(Dataset &dataset, const std::shared_ptr<const SearchParamsBase> &params, const std::shared_ptr<const Image > &example);
48 
49 protected:
50 
51  std::vector< std::vector<uint64_t> > inverted_index; /// Stores the inverted index, dimension one is the cluster index, dimension two holds a list of ids containing that word.
52  std::vector<float> idf_weights; /// Stores the idf weights, one element per cluster
53 
54 };
55 
56 /// Prints out information about the match results.
57 std::ostream& operator<< (std::ostream &out, const InvertedIndex::MatchResults &match_results);