forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClipAPI.cpp
More file actions
158 lines (129 loc) · 4.03 KB
/
Copy pathClipAPI.cpp
File metadata and controls
158 lines (129 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "ClipAPI.h"
#include "TorchHeader.h"
#include "RuCLIP.h"
#include "RuCLIPProcessor.h"
#include "../../src/common/defines.h"
///
class ClassificationCLIP::ClassificationCLIPImpl
{
public:
ClassificationCLIPImpl() = default;
~ClassificationCLIPImpl() = default;
///
bool Init(const std::string& pathToClip, const std::string& pathToBPE, int inputImgSize, int indGPU, const std::vector<std::string>& labels)
{
bool res = true;
m_pathToClip = pathToClip;
m_indGPU = indGPU;
m_labels = labels;
//torch::manual_seed(24);
std::cout << "Set Torch device (" << m_indGPU << "): " << ((m_indGPU < 0) ? "CPU" : "GPU") << std::endl;
if (m_indGPU >= 0 && torch::cuda::is_available())
{
std::cout << "CUDA is available! Running on GPU." << std::endl;
m_device = torch::Device(torch::kCUDA, m_indGPU);
}
else
{
std::cout << "CUDA is not available! Running on CPU." << std::endl;
}
std::cout << "Load clip from: " << pathToClip << std::endl;
m_clip = FromPretrained(pathToClip);
m_clip->to(m_device);
std::cout << "Load processor from: " << pathToBPE << std::endl;
m_processor = RuCLIPProcessor::FromPretrained(m_pathToClip);
m_processor.CacheText(m_labels);
return res;
}
///
bool ProcessFrame(const cv::Mat& frame, const std::vector<cv::Rect>& rois, std::vector<CLIPResult>& result)
{
bool res = false;
if (rois.empty())
return res;
result.resize(rois.size());
std::map<size_t, size_t> img2roi;
std::cout << "Resizing..." << std::endl;
std::vector<cv::Mat> images;
images.reserve(rois.size());
for (size_t i = 0; i < rois.size(); ++i)
{
cv::Rect r = Clamp(rois[i], frame.size());
if (r.width > m_processor.GetImageSize() / 10 && r.height > m_processor.GetImageSize() / 10)
{
img2roi[images.size()] = i;
images.emplace_back(cv::Mat(frame, r));
}
}
if (images.empty())
{
std::cout << "CLIP::ProcessFrame: empty images" << std::endl;
return res;
}
std::cout << "Running on " << images.size() << "..." << std::endl;
auto dummy_input = m_processor.operator()(images);
try
{
torch::Tensor logits_per_image = m_clip->forward(dummy_input.first.to(m_device), dummy_input.second.to(m_device));
torch::Tensor logits_per_text = logits_per_image.t();
auto probs = logits_per_image.softmax(/*dim = */-1).detach().cpu();
//std::cout << "probs per image: " << probs << std::endl;
const float* tensorData = reinterpret_cast<const float*>(probs.data_ptr());
for (size_t imgInd = 0; imgInd < images.size(); ++imgInd)
{
float bestConf = 0.;
size_t bestInd = 0;
for (size_t labelInd = 0; labelInd < m_labels.size(); ++labelInd)
{
if (bestConf < tensorData[labelInd])
{
bestConf = tensorData[labelInd];
bestInd = labelInd;
}
}
result[img2roi[imgInd]] = CLIPResult(m_labels[bestInd], bestConf);
std::cout << "Object: " << m_labels[bestInd] << " - " << bestConf << std::endl;
tensorData += m_labels.size();
}
res = true;
}
catch (std::exception& e)
{
res = false;
std::cout << "ClassificationCLIP::ProcessFrame: " << e.what() << std::endl;
}
return res;
}
private:
std::string m_pathToClip = "";
int m_indGPU = -1; // -1 - use CPU
torch::Device m_device{ torch::kCPU };
CLIP m_clip = nullptr;
RuCLIPProcessor m_processor;
std::vector<std::string> m_labels{ "human", "pedestrian", "car", "vehicle", "truck", "bus" };
};
///
ClassificationCLIP::ClassificationCLIP() noexcept
{
}
///
ClassificationCLIP::~ClassificationCLIP()
{
if (m_pImpl)
delete m_pImpl;
}
///
bool ClassificationCLIP::Init(const std::string& pathToClip, const std::string& pathToBPE, int inputImgSize, int indGPU, const std::vector<std::string>& labels)
{
if (m_pImpl)
delete m_pImpl;
m_pImpl = new ClassificationCLIPImpl();
bool res = m_pImpl->Init(pathToClip, pathToBPE, inputImgSize, indGPU, labels);
assert(res);
return res;
}
///
bool ClassificationCLIP::ProcessFrame(const cv::Mat& frame, const std::vector<cv::Rect>& rois, std::vector<CLIPResult>& result)
{
return m_pImpl->ProcessFrame(frame, rois, result);
}