@@ -19,13 +19,21 @@ class RuCLIPProcessor
1919 const std::vector<double > norm_std = { 0.26862954 , 0.26130258 , 0.27577711 });
2020
2121 // /!!!Локали-юникоды
22- torch::Tensor EncodeText (/* std::vector<*/ std::string &text);
22+ torch::Tensor EncodeText (const /* std::vector<*/ std::string &text);
2323 torch::Tensor PrepareTokens (/* std::vector<*/ std::vector<int32_t > tokens); // Передаю по значению чтобы внутри иметь дело с копией
24+ torch::Tensor EncodeImage (const cv::Mat& img);
2425 std::pair <torch::Tensor, torch::Tensor> operator ()(const std::vector <std::string>& texts, const std::vector <cv::Mat>& images);
2526 std::pair <torch::Tensor, torch::Tensor> operator ()(const std::vector <cv::Mat>& images);
2627
2728 void CacheText (const std::vector <std::string>& texts);
2829
30+ // /
31+ int GetImageSize () const noexcept
32+ {
33+ return ImageSize;
34+ }
35+
36+ // /
2937 static RuCLIPProcessor FromPretrained (const std::filesystem::path &folder)
3038 {
3139 std::filesystem::path tokenizer_path = folder / " bpe.model" ;
@@ -34,13 +42,14 @@ class RuCLIPProcessor
3442 std::ifstream f (folder / " config.json" );
3543 json config = json::parse (f);
3644
37- return RuCLIPProcessor (
38- tokenizer_path.string (),
39- int (config[" image_resolution" ]),
40- int (config[" context_length" ]),
41- { 0.48145466 , 0.4578275 , 0.40821073 }, // config.get("mean"),
42- { 0.26862954 , 0.26130258 , 0.27577711 } // config.get("std")
43- );
45+ auto mean = config[" mean" ].template get <std::vector<double >>();
46+ auto std = config[" std" ].template get <std::vector<double >>();
47+
48+ return RuCLIPProcessor (tokenizer_path.string (),
49+ int (config[" image_resolution" ]),
50+ int (config[" context_length" ]),
51+ mean,
52+ std);
4453 }
4554
4655private:
@@ -52,12 +61,12 @@ class RuCLIPProcessor
5261 const int TextSeqLength{ 77 };
5362 std::vector<double > NormMean;
5463 std::vector<double > NormStd;
55- vkcom::BaseEncoder* Tokenizer = nullptr ;
64+ std::unique_ptr< vkcom::BaseEncoder> Tokenizer;
5665
5766 std::vector<torch::Tensor> m_textsTensors;
5867};
5968
60- // relevancy for batch size == 1 at this moment, float lv = result.index({0,0}).item<float>();
69+ // / relevancy for batch size == 1 at this moment, float lv = result.index({0,0}).item<float>();
6170// /
6271// /std::vector<torch::Tensor> canon_texts_tensors;
6372// /canon_texts_tensors.push_back(ClipProcessor->EncodeText(std::string("объект")));
@@ -75,21 +84,24 @@ class RuCLIPProcessor
7584// /float lv = rel.index({0,0}).item<float>();
7685inline torch::Tensor Relevancy (torch::Tensor embeds, torch::Tensor positives, torch::Tensor negatives)
7786{
78- auto embeds2 = torch::cat ({ positives, negatives });
79- auto logits = /* scale * */ torch::mm (embeds, embeds2.t ()); // [batch_size x phrases]
80- auto positive_vals = logits.index ({ " ..." , torch::indexing::Slice (0 , 1 ) }); // [batch_size x 1]
81- auto negative_vals = logits.index ({ " ..." , torch::indexing::Slice (1 , torch::indexing::None) }); // [batch_size x negative_phrase_n]
82- auto repeated_pos = positive_vals.repeat ({ 1 , negatives.sizes ()[0 ] }); // [batch_size x negative_phrase_n]
83- auto sims = torch::stack ({ repeated_pos, negative_vals }, -1 ); // [batch_size x negative_phrase_n x 2]
84- auto smx = torch::softmax (10 * sims, -1 ); // [batch_size x negative_phrase_n x 2]
85- auto best_id = smx.index ({ " ..." , 0 }).argmin (1 ); // [batch_size x 2]
86- auto result = torch::gather (smx, 1 , best_id.index ({
87- " ..." , torch::indexing::None, torch::indexing::None
88- }).expand (
89- { best_id.sizes ()[0 ], negatives.sizes ()[0 ], 2
90- })
91- ).index (
92- { torch::indexing::Slice (), 0 , torch::indexing::Slice ()
93- });// [batch_size x 2]
87+ std::cout << " Relevancy: 0" << std::endl;
88+ auto embeds2 = torch::cat ({positives, negatives});
89+ std::cout << " Relevancy: 1" << std::endl;
90+ auto logits = /* scale * */ torch::mm (embeds, embeds2.t ()); // [batch_size x phrases]
91+ std::cout << " Relevancy: 2" << std::endl;
92+ auto positive_vals = logits.index ({" ..." , torch::indexing::Slice (0 , positives.sizes ()[0 ])}); // [batch_size x 1]
93+ std::cout << " Relevancy: 3" << std::endl;
94+ auto negative_vals = logits.index ({" ..." , torch::indexing::Slice (positives.sizes ()[0 ], torch::indexing::None)}); // [batch_size x negative_phrase_n]
95+ std::cout << " Relevancy: 4" << std::endl;
96+ auto repeated_pos = positive_vals.repeat ({1 , negatives.sizes ()[0 ]}); // [batch_size x negative_phrase_n]
97+ std::cout << " Relevancy: 5: repeated_pos: " << repeated_pos.sizes () << " , negative_vals: " << negative_vals.sizes () << std::endl;
98+ auto sims = torch::stack ({repeated_pos, negative_vals}, -1 ); // [batch_size x negative_phrase_n x 2]
99+ std::cout << " Relevancy: 6" << std::endl;
100+ auto smx = torch::softmax (10 * sims, -1 ); // [batch_size x negative_phrase_n x 2]
101+ std::cout << " Relevancy: 7" << std::endl;
102+ auto best_id = smx.index ({" ..." , 0 }).argmin (1 ); // [batch_size x 2]
103+ std::cout << " Relevancy: 8" << std::endl;
104+ auto result = torch::gather (smx, 1 , best_id.index ({" ..." , torch::indexing::None, torch::indexing::None}).expand ({best_id.sizes ()[0 ], negatives.sizes ()[0 ], 2 })
105+ ).index ({torch::indexing::Slice (), 0 , torch::indexing::Slice ()});// [batch_size x 2]
94106 return result;
95107}
0 commit comments