SOMHunter Core
canvas-query-ranker.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 COLLAGE_RANKER_H_
22 #define COLLAGE_RANKER_H_
23 
24 #include <torch/script.h>
25 #include <torch/torch.h>
26 #include <cereal/types/complex.hpp>
27 #include <cereal/types/string.hpp>
28 #include <cereal/types/vector.hpp>
29 
30 #include <algorithm>
31 #include <cmath>
32 #include <cstdint>
33 #include <iostream>
34 #include <memory>
35 #include <string>
36 #include <vector>
37 
38 #include "common.h"
39 
40 #include "image-processor.h"
41 #include "keyword-ranker.h"
42 #include "query-types.h"
43 
44 namespace sh {
45 class Somhunter;
46 
47 template <typename DType>
48 std::vector<std::vector<DType>> to_std_matrix(const at::Tensor& tensor_features) {
49  if (tensor_features.sizes().size() != 2) {
50  throw std::runtime_error("Not 2x<dim> matrix.");
51  }
52 
53  size_t num_rows{ size_t(tensor_features.sizes()[0]) };
54  size_t num_cols{ size_t(tensor_features.sizes()[1]) };
55 
56  std::vector<std::vector<DType>> mat;
57  mat.reserve(num_rows);
58 
59  // Iterate over the rows
60  float* data_ptr = static_cast<float*>(tensor_features.data_ptr());
61  for (std::size_t ir = 0; ir < num_rows; ++ir) {
62  std::vector<DType> row;
63  row.assign(data_ptr, data_ptr + num_cols);
64  data_ptr += num_cols;
65 
66  mat.emplace_back(std::move(row));
67  }
68 
69  return mat;
70 }
71 
72 template <c10::ScalarType TensorDType_ = at::kFloat, typename OrigDType_ = float>
73 at::Tensor to_tensor(std::vector<OrigDType_>& orig_vec) {
74  do_assert_debug(orig_vec.size() > 0, "Vector cannot be empty.");
75  // SHLOG_D("shape = (" << orig_vec.size() << ")");
76 
77  return torch::tensor(orig_vec, TensorDType_);
78 }
79 
80 template <c10::ScalarType TensorDType_ = at::kFloat, typename OrigDType_ = float>
81 at::Tensor to_tensor(std::vector<std::vector<OrigDType_>>& orig_mat) {
82  do_assert_cond(orig_mat.size() > 0, "Matrix cannot be empty.");
83  // SHLOG_D("shape = (" << orig_vec.size() << ", " << orig_vec.front(0).size() << ")");
84 
85  std::vector<at::Tensor> meta;
86  meta.reserve(orig_mat.size());
87 
88  for (auto&& vec : orig_mat) {
89  meta.emplace_back(torch::tensor(vec.data(), TensorDType_));
90  }
91 
92  return torch::cat(meta, 0);
93 }
94 
97  bool _loaded;
98 
99  torch::jit::script::Module resnet152;
100  torch::jit::script::Module resnext101;
101  torch::Tensor bias;
102  torch::Tensor weights;
103  torch::Tensor kw_pca_mat;
104  torch::Tensor kw_pca_mean_vec;
105 
106  const std::vector<std::vector<float>> RoIs = {
107  { 0.0, 0.0, 1.0, 1.0 }, { 0.1, 0.2, 0.4, 0.6 }, { 0.3, 0.2, 0.4, 0.6 }, { 0.5, 0.2, 0.4, 0.6 },
108 
109  { 0.0, 0.0, 0.4, 0.6 }, { 0.2, 0.0, 0.4, 0.6 }, { 0.4, 0.0, 0.4, 0.6 }, { 0.6, 0.0, 0.4, 0.6 },
110 
111  { 0.0, 0.4, 0.4, 0.6 }, { 0.2, 0.4, 0.4, 0.6 }, { 0.4, 0.4, 0.4, 0.6 }, { 0.6, 0.4, 0.4, 0.6 },
112  };
113 
114 public:
115  static const size_t models_input_width{ 224 };
116  static const size_t models_input_height{ 224 };
117  static const size_t models_num_channels{ 3 };
118 
119  CanvasQueryRanker(const Settings& _settings, KeywordRanker* p_core);
120  void score(const CanvasQuery&, ScoreModel& model, size_t temporal, UsedTools& used_tools,
121  const PrimaryFrameFeatures& _dataset_features, const DatasetFrames& _dataset_frames);
122 
123 private:
124  std::vector<FeatureMatrix> region_data;
125 
126  at::Tensor get_features(const CanvasQuery&, UsedTools& used_tools);
127  at::Tensor get_L2norm(const at::Tensor& _data) const;
128 
129  std::vector<std::size_t> get_RoIs(const CanvasQuery& collage) const;
130  std::size_t get_RoI(const CanvasSubquery& image) const;
131 
132  std::vector<float> score_image(const std::vector<float>& feature, std::size_t region) const;
133  std::vector<float> average_scores(const std::vector<std::vector<float>>& scores) const;
134 };
135 
136 // This serves for default parameters of type Collage&
137 static CanvasQuery DEFAULT_COLLAGE{};
138 
139 }; // namespace sh
140 
141 #endif // COLLAGE_RANKER_H_
Type representing query related to the canvas (atm text & bitmap) rectangles.
Definition: query-types.h:325
Definition: canvas-query-ranker.h:95
torch::jit::script::Module resnet152
Definition: canvas-query-ranker.h:99
torch::Tensor kw_pca_mat
Definition: canvas-query-ranker.h:103
KeywordRanker * _p_core
Definition: canvas-query-ranker.h:96
void score(const CanvasQuery &, ScoreModel &model, size_t temporal, UsedTools &used_tools, const PrimaryFrameFeatures &_dataset_features, const DatasetFrames &_dataset_frames)
Definition: canvas-query-ranker.cpp:151
static const size_t models_input_height
Definition: canvas-query-ranker.h:116
at::Tensor get_features(const CanvasQuery &, UsedTools &used_tools)
Definition: canvas-query-ranker.cpp:195
std::vector< std::size_t > get_RoIs(const CanvasQuery &collage) const
Definition: canvas-query-ranker.cpp:314
torch::Tensor bias
Definition: canvas-query-ranker.h:101
torch::Tensor kw_pca_mean_vec
Definition: canvas-query-ranker.h:104
std::size_t get_RoI(const CanvasSubquery &image) const
Definition: canvas-query-ranker.cpp:320
torch::jit::script::Module resnext101
Definition: canvas-query-ranker.h:100
CanvasQueryRanker(const Settings &_settings, KeywordRanker *p_core)
Definition: canvas-query-ranker.cpp:37
static const size_t models_input_width
Definition: canvas-query-ranker.h:115
std::vector< FeatureMatrix > region_data
Definition: canvas-query-ranker.h:124
static const size_t models_num_channels
Definition: canvas-query-ranker.h:117
std::vector< float > average_scores(const std::vector< std::vector< float >> &scores) const
Definition: canvas-query-ranker.cpp:352
std::vector< float > score_image(const std::vector< float > &feature, std::size_t region) const
Definition: canvas-query-ranker.cpp:345
bool _loaded
Definition: canvas-query-ranker.h:97
torch::Tensor weights
Definition: canvas-query-ranker.h:102
at::Tensor get_L2norm(const at::Tensor &_data) const
Definition: canvas-query-ranker.cpp:186
const std::vector< std::vector< float > > RoIs
Definition: canvas-query-ranker.h:106
Definition: dataset-frames.h:162
Definition: keyword-ranker.h:52
Definition: scores.h:34
Definition: common-types.h:33
std::vector< std::vector< DType > > to_std_matrix(const at::Tensor &tensor_features)
Definition: canvas-query-ranker.h:48
at::Tensor to_tensor(std::vector< OrigDType_ > &orig_vec)
Definition: canvas-query-ranker.h:73
std::variant< CanvasSubqueryBitmap, CanvasSubqueryText > CanvasSubquery
Definition: query-types.h:320
#define do_assert_debug(assertion, msg)
Assert execuded only if RUN_ASSERTS is true.
Definition: static-logger.hpp:225
Parsed current config of the core.
Definition: settings.h:190
Definition: common-types.h:178