Skip to content

Commit e207cab

Browse files
committed
Update RuCLIP code from mainstream
1 parent 2580fc8 commit e207cab

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

thirdparty/ruclip/RuCLIP.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,13 @@ torch::Tensor CLIPImpl :: EncodeText(torch::Tensor input_ids)
207207

208208
torch::Tensor CLIPImpl :: forward(torch::Tensor input_ids, torch::Tensor pixel_values)
209209
{
210+
//std::cout << "pixel_values: " << pixel_values.sizes() << ", input_ids: " << input_ids.sizes() << std::endl;
211+
210212
auto image_features = EncodeImage(pixel_values);
211213
auto text_features = EncodeText(input_ids);
212214

215+
//std::cout << "image_features: " << image_features.sizes() << ", text_features: " << text_features.sizes() << std::endl;
216+
213217
//normalize features
214218
image_features = image_features / image_features.norm(2/*L2*/, -1, true);
215219
text_features = text_features / text_features.norm(2/*L2*/, -1, true);

thirdparty/ruclip/RuCLIPProcessor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ inline cv::Mat TorchTensorToCVMat(const torch::Tensor tensor_image, const bool p
1818
if (perm)
1919
t = t.permute({ 1, 2, 0 });
2020
t = t.mul(255).clamp(0, 255).to(torch::kU8);
21+
t = t.contiguous();
2122
cv::Mat result_img;
2223
cv::Mat(static_cast<int>(t.size(0)), static_cast<int>(t.size(1)), CV_MAKETYPE(CV_8U, t.sizes().size() >= 3 ? static_cast<int>(t.size(2)) : 1), t.data_ptr()).copyTo(result_img);
2324
return result_img;

thirdparty/ruclip/RuCLIPProcessor.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,42 @@ class RuCLIPProcessor
108108
///image_features = image_features / image_features.norm(2/*L2*/, -1, true);
109109
///torch::Tensor rel = Relevancy(image_features, text_features, canon_features);
110110
///float lv = rel.index({0,0}).item<float>();
111+
111112
inline torch::Tensor Relevancy(torch::Tensor embeds, torch::Tensor positives, torch::Tensor negatives)
112113
{
114+
#if 0
113115
std::cout << "Relevancy: 0" << std::endl;
114-
auto embeds2 = torch::cat({positives, negatives});
116+
auto embeds2 = torch::cat({ positives, negatives });
115117
std::cout << "Relevancy: 1" << std::endl;
116118
auto logits = /*scale * */torch::mm(embeds, embeds2.t()); //[batch_size x phrases]
117-
std::cout << "Relevancy: 2" << std::endl;
118-
auto positive_vals = logits.index({"...", torch::indexing::Slice(0, positives.sizes()[0])}); // [batch_size x 1]
119+
std::cout << "Relevancy: 2" << std::endl;
120+
auto positive_vals = logits.index({ "...", torch::indexing::Slice(0, positives.sizes()[0]) }); // [batch_size x 1]
119121
std::cout << "Relevancy: 3" << std::endl;
120-
auto negative_vals = logits.index({"...", torch::indexing::Slice(positives.sizes()[0], torch::indexing::None)}); // [batch_size x negative_phrase_n]
122+
auto negative_vals = logits.index({ "...", torch::indexing::Slice(positives.sizes()[0], torch::indexing::None) }); // [batch_size x negative_phrase_n]
121123
std::cout << "Relevancy: 4" << std::endl;
122-
auto repeated_pos = positive_vals.repeat({1, negatives.sizes()[0]}); //[batch_size x negative_phrase_n]
124+
auto repeated_pos = positive_vals.repeat({ 1, negatives.sizes()[0] }); //[batch_size x negative_phrase_n]
123125
std::cout << "Relevancy: 5: repeated_pos: " << repeated_pos.sizes() << ", negative_vals: " << negative_vals.sizes() << std::endl;
124-
auto sims = torch::stack({repeated_pos, negative_vals}, -1); //[batch_size x negative_phrase_n x 2]
126+
auto sims = torch::stack({ repeated_pos, negative_vals }, -1); //[batch_size x negative_phrase_n x 2]
125127
std::cout << "Relevancy: 6" << std::endl;
126128
auto smx = torch::softmax(10 * sims, -1); // [batch_size x negative_phrase_n x 2]
127129
std::cout << "Relevancy: 7" << std::endl;
128-
auto best_id = smx.index({"...", 0}).argmin(1); // [batch_size x 2]
130+
auto best_id = smx.index({ "...", 0 }).argmin(1); // [batch_size x 2]
129131
std::cout << "Relevancy: 8" << std::endl;
130-
auto result = torch::gather(smx, 1, best_id.index({"...", torch::indexing::None, torch::indexing::None}).expand({best_id.sizes()[0], negatives.sizes()[0], 2})
131-
).index({torch::indexing::Slice(), 0, torch::indexing::Slice()});// [batch_size x 2]
132+
auto result = torch::gather(smx, 1, best_id.index({ "...", torch::indexing::None, torch::indexing::None }).expand({ best_id.sizes()[0], negatives.sizes()[0], 2 })
133+
).index({ torch::indexing::Slice(), 0, torch::indexing::Slice() });// [batch_size x 2]
132134
return result;
135+
#else
136+
auto embeds2 = torch::cat({ positives, negatives }, 0);
137+
auto logits = torch::mm(embeds, embeds2.t()); // [batch_size, 1 + negatives_len]
138+
auto positive_vals = logits.index({ "...", torch::indexing::Slice(0, 1) }); // [batch_size, 1]
139+
auto negative_vals = logits.index({ "...", torch::indexing::Slice(1, torch::indexing::None) }); // [batch_size, negatives_len]
140+
auto repeated_pos = positive_vals.repeat({ 1, negatives.sizes()[0] }); // [batch_size, negatives_len]
141+
auto sims = torch::stack({ repeated_pos, negative_vals }, -1); // [batch_size, negatives_len, 2]
142+
auto smx = torch::softmax(10 * sims, -1); // [batch_size, negatives_len, 2]
143+
//Находим индекс самого сложного негатива (с минимальной вероятностью позитивного класса)
144+
auto best_id = smx.index({ "...", 0 }).argmin(1, /*keepdim=*/true); // [batch_size, 1]
145+
//Собираем результаты для выбранных негативов
146+
auto result = torch::gather(smx, 1, best_id.unsqueeze(-1).expand({ -1, -1, 2 }));
147+
return result.squeeze(1); // [batch_size, 2]
148+
#endif
133149
}

0 commit comments

Comments
 (0)