SOMHunter Core
scores.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef scores_h
22 #define scores_h
23 
24 #include <map>
25 #include <set>
26 #include <vector>
27 
28 #include "common.h"
29 
30 #include "dataset-features.h"
31 #include "dataset-frames.h"
32 
33 namespace sh {
34 class ScoreModel {
36  std::vector<float> _scores;
37 
39 
47  std::vector<bool> _mask;
48 
49  // *** CACHING VARIABLES ***
50  mutable std::vector<FrameId> _topn_cache;
51  mutable bool _cache_dirty;
52  mutable std::vector<FrameId> _topn_ctx_cache;
53  mutable bool _cache_ctx_dirty;
54 
55 public:
57  : _scores(p.size(), 1.0F),
59  _mask(p.size(), true),
60  _cache_dirty{ true },
61  _cache_ctx_dirty{ true } {}
62 
63  bool operator==(const ScoreModel& other) const;
64  float operator[](FrameId i) const { return _scores[i]; }
65 
66  void reset(float val = 1.0F);
67 
71  float adjust(FrameId i, float prob);
72 
74  float adjust(size_t temp, FrameId i, float prob);
75 
78  float set(FrameId i, float prob);
79 
81  const float* v() const { return _scores.data(); }
82 
83  const float* temp(size_t temp) const { return _temporal_scores[temp].data(); }
84 
86  size_t size() const { return _scores.size(); }
87 
92  void apply_temporals(size_t depth, const DatasetFrames& _dataset_frames, const float power);
93 
95  void normalize(size_t depth = MAX_TEMPORAL_SIZE);
96  void normalize(float* scores, size_t size);
97 
99  _cache_dirty = true;
100  _cache_ctx_dirty = true;
101  }
102 
103  void reset_mask() {
105  std::transform(_mask.begin(), _mask.end(), _mask.begin(), [](const bool&) { return true; });
106  };
107 
109  bool is_masked(FrameId ID) const { return _mask[ID]; }
110 
112  bool set_mask(FrameId ID, bool val) {
114  return _mask[ID] = val;
115  }
116 
120  void apply_bayes(std::set<FrameId> likes, const std::set<FrameId>& screen, const PrimaryFrameFeatures& features);
121 
125  const std::vector<FrameId>& top_n(const DatasetFrames& _dataset_frames, size_t _size, size_t from_vid_limit = 0,
126  size_t from_shot_limit = 0) const;
127 
132  std::vector<FrameId> top_n_with_context(const DatasetFrames& _dataset_frames, size_t _size, size_t from_vid_limit,
133  size_t from_shot_limit) const;
134 
136  std::vector<FrameId> weighted_sample(size_t _size, float pow = 1) const;
137 
139  FrameId weighted_example(const std::vector<FrameId>& subset) const;
140 
142  size_t frame_rank(FrameId i) const;
143 
146 };
147 
148 }; // namespace sh
149 #endif
Definition: dataset-frames.h:162
Definition: scores.h:34
float set(FrameId i, float prob)
Hard-sets the score with the provided value (normalization required).
Definition: scores.cpp:109
std::vector< float > _scores
Current score distribution for the frames.
Definition: scores.h:36
void normalize(size_t depth=MAX_TEMPORAL_SIZE)
Normalizes the score distribution.
Definition: scores.cpp:368
bool operator==(const ScoreModel &other) const
Definition: scores.cpp:86
void invalidate_cache()
Definition: scores.h:98
static StdVector< std::pair< FrameId, float > > sort_by_score(const StdVector< float > &scores)
Sorts images by given score vector.
Definition: scores.cpp:404
std::vector< FrameId > _topn_cache
Definition: scores.h:50
void reset(float val=1.0F)
Definition: scores.cpp:88
size_t size() const
Returns number of scores stored.
Definition: scores.h:86
void apply_bayes(std::set< FrameId > likes, const std::set< FrameId > &screen, const PrimaryFrameFeatures &features)
Applies relevance feedback rescore based on the Bayesian update rule.
Definition: scores.cpp:269
const std::vector< FrameId > & top_n(const DatasetFrames &_dataset_frames, size_t _size, size_t from_vid_limit=0, size_t from_shot_limit=0) const
Gets the images with the highest scores but respecting the provided limits.
Definition: scores.cpp:146
StdMatrix< float > _temporal_scores
Definition: scores.h:38
const float * temp(size_t temp) const
Definition: scores.h:83
std::vector< bool > _mask
Frames mask telling what frames should be placed inside the results.
Definition: scores.h:47
std::vector< FrameId > weighted_sample(size_t _size, float pow=1) const
Samples n random frames from the current scores distribution.
Definition: scores.cpp:197
std::vector< FrameId > top_n_with_context(const DatasetFrames &_dataset_frames, size_t _size, size_t from_vid_limit, size_t from_shot_limit) const
Gets the images with the highest scores while respecting the provided limits and each frame is wrappe...
Definition: scores.cpp:115
bool is_masked(FrameId ID) const
Returns the current value for the frame.
Definition: scores.h:109
bool set_mask(FrameId ID, bool val)
Sets the mask value for the frame.
Definition: scores.h:112
std::vector< FrameId > _topn_ctx_cache
Definition: scores.h:52
size_t frame_rank(FrameId i) const
Returns the current rank of the provided frame (starts from 0).
Definition: scores.cpp:395
bool _cache_dirty
Definition: scores.h:51
ScoreModel(const DatasetFrames &p)
Definition: scores.h:56
const float * v() const
Pointer to the begin of the data.
Definition: scores.h:81
float adjust(FrameId i, float prob)
Multiplies the relevance score with the provided value.
Definition: scores.cpp:97
void reset_mask()
Definition: scores.h:103
float operator[](FrameId i) const
Definition: scores.h:64
void apply_temporals(size_t depth, const DatasetFrames &_dataset_frames, const float power)
Transforms temporal inverse score into temporal scores and aggregates into full image scores.
Definition: scores.cpp:328
bool _cache_ctx_dirty
Definition: scores.h:53
FrameId weighted_example(const std::vector< FrameId > &subset) const
Samples a random frame from the current scores distribution.
Definition: scores.cpp:259
Definition: common-types.h:33
constexpr int MAX_TEMPORAL_SIZE
Maximal size of temporal query.
Definition: static-config.h:94
std::vector< std::vector< T_ > > StdMatrix
Definition: common-types.h:81
std::vector< T_ > StdVector
Definition: common-types.h:84
unsigned long FrameId
Definition: common-types.h:75