-
Notifications
You must be signed in to change notification settings - Fork 706
/
Copy pathort_utils.h
227 lines (179 loc) · 7.45 KB
/
ort_utils.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// Created by DefTruth on 2021/3/28.
//
#ifndef LITE_AI_ORT_CORE_ORT_UTILS_H
#define LITE_AI_ORT_CORE_ORT_UTILS_H
#include "ort_config.h"
#include "ort_types.h"
namespace ortcv
{
namespace utils
{
static constexpr const float _PI = 3.1415926f;
LITE_EXPORTS std::wstring to_wstring(const std::string &str);
LITE_EXPORTS std::string to_string(const std::wstring &wstr);
LITE_EXPORTS cv::Mat draw_axis(const cv::Mat &mat, const types::EulerAngles &euler_angles, float size = 50.f, int thickness = 2);
LITE_EXPORTS cv::Mat draw_boxes(const cv::Mat &mat, const std::vector<types::Boxf> &boxes);
LITE_EXPORTS cv::Mat draw_landmarks(const cv::Mat &mat, types::Landmarks &landmarks);
LITE_EXPORTS cv::Mat draw_age(const cv::Mat &mat, types::Age &age);
LITE_EXPORTS cv::Mat draw_gender(const cv::Mat &mat, types::Gender &gender);
LITE_EXPORTS cv::Mat draw_emotion(const cv::Mat &mat, types::Emotions &emotions);
LITE_EXPORTS void draw_boxes_inplace(cv::Mat &mat_inplace, const std::vector<types::Boxf> &boxes);
LITE_EXPORTS void
draw_axis_inplace(cv::Mat &mat_inplace, const types::EulerAngles &euler_angles, float size = 50.f, int thickness = 2);
LITE_EXPORTS void draw_landmarks_inplace(cv::Mat &mat, types::Landmarks &landmarks);
LITE_EXPORTS void draw_age_inplace(cv::Mat &mat_inplace, types::Age &age);
LITE_EXPORTS void draw_gender_inplace(cv::Mat &mat_inplace, types::Gender &gender);
LITE_EXPORTS void draw_emotion_inplace(cv::Mat &mat_inplace, types::Emotions &emotions);
LITE_EXPORTS void
hard_nms(std::vector<types::Boxf> &input, std::vector<types::Boxf> &output, float iou_threshold, unsigned int topk);
LITE_EXPORTS void
blending_nms(std::vector<types::Boxf> &input, std::vector<types::Boxf> &output, float iou_threshold, unsigned int topk);
LITE_EXPORTS void
offset_nms(std::vector<types::Boxf> &input, std::vector<types::Boxf> &output, float iou_threshold, unsigned int topk);
namespace transform
{
enum
{
CHW = 0, HWC = 1
};
/**
* @param mat CV:Mat with type 'CV_32FC3|2|1'
* @param tensor_dims e.g {1,C,H,W} | {1,H,W,C}
* @param memory_info It needs to be a global variable in a class
* @param tensor_value_handler It needs to be a global variable in a class
* @param data_format CHW | HWC
* @return
*/
LITE_EXPORTS Ort::Value create_tensor(const cv::Mat &mat, const std::vector<int64_t> &tensor_dims,
const Ort::MemoryInfo &memory_info_handler,
std::vector<float> &tensor_value_handler,
unsigned int data_format = CHW) throw(std::runtime_error);
LITE_EXPORTS cv::Mat normalize(const cv::Mat &mat, float mean, float scale);
LITE_EXPORTS cv::Mat normalize(const cv::Mat &mat, const float mean[3], const float scale[3]);
LITE_EXPORTS void normalize(const cv::Mat &inmat, cv::Mat &outmat, float mean, float scale);
LITE_EXPORTS void normalize_inplace(cv::Mat &mat_inplace, float mean, float scale);
LITE_EXPORTS void normalize_inplace(cv::Mat &mat_inplace, const float mean[3], const float scale[3]);
}
namespace math
{
template<typename T=float>
std::vector<T> softmax(const std::vector<T> &logits, unsigned int &max_id);
template LITE_EXPORTS std::vector<float> softmax(const std::vector<float> &logits, unsigned int &max_id);
template<typename T=float>
std::vector<T> softmax(const T *logits, unsigned int _size, unsigned int &max_id);
template LITE_EXPORTS std::vector<float> softmax(const float *logits, unsigned int _size, unsigned int &max_id);
template<typename T=float>
std::vector<unsigned int> argsort(const std::vector<T> &arr);
template LITE_EXPORTS std::vector<unsigned int> argsort(const std::vector<float> &arr);
template<typename T=float>
std::vector<unsigned int> argsort(const T *arr, unsigned int _size);
template LITE_EXPORTS std::vector<unsigned int> argsort(const float *arr, unsigned int _size);
template<typename T=float>
T cosine_similarity(const std::vector<T> &a, const std::vector<T> &b);
template LITE_EXPORTS float cosine_similarity(const std::vector<float> &a, const std::vector<float> &b);
}
} // NAMESPACE UTILS
} // NAMESPACE ORTCV
namespace ortnlp
{
namespace utils
{
} // NAMESPACE UTILS
} // NAMESPACE ORTNLP
namespace ortasr
{
namespace utils
{
} // NAMESPACE UTILS
} // NAMESPACE ORTASR
template<typename T>
std::vector<T> ortcv::utils::math::softmax(const T *logits, unsigned int _size, unsigned int &max_id)
{
types::__assert_type<T>();
if (_size == 0 or logits == nullptr) return {};
T max_prob = static_cast<T>(0), total_exp = static_cast<T>(0);
std::vector<float> softmax_probs(_size);
for (unsigned int i = 0; i < _size; ++i)
{
softmax_probs[i] = std::expf(logits[i]);
total_exp += softmax_probs[i];
}
for (unsigned int i = 0; i < _size; ++i)
{
softmax_probs[i] = softmax_probs[i] / total_exp;
if (softmax_probs[i] > max_prob)
{
max_id = i;
max_prob = softmax_probs[i];
}
}
return softmax_probs;
}
template<typename T>
std::vector<T> ortcv::utils::math::softmax(const std::vector<T> &logits, unsigned int &max_id)
{
types::__assert_type<T>();
if (logits.empty()) return {};
const unsigned int _size = logits.size();
T max_prob = static_cast<T>(0), total_exp = static_cast<T>(0);
std::vector<float> softmax_probs(_size);
for (unsigned int i = 0; i < _size; ++i)
{
softmax_probs[i] = std::expf(logits[i]);
total_exp += softmax_probs[i];
}
for (unsigned int i = 0; i < _size; ++i)
{
softmax_probs[i] = softmax_probs[i] / total_exp;
if (softmax_probs[i] > max_prob)
{
max_id = i;
max_prob = softmax_probs[i];
}
}
return softmax_probs;
}
template<typename T>
std::vector<unsigned int> ortcv::utils::math::argsort(const std::vector<T> &arr)
{
types::__assert_type<T>();
if (arr.empty()) return {};
const unsigned int _size = arr.size();
std::vector<unsigned int> indices;
for (unsigned int i = 0; i < _size; ++i) indices.push_back(i);
std::sort(indices.begin(), indices.end(),
[&arr](const unsigned int a, const unsigned int b)
{ return arr[a] > arr[b]; });
return indices;
}
template<typename T>
std::vector<unsigned int> ortcv::utils::math::argsort(const T *arr, unsigned int _size)
{
types::__assert_type<T>();
if (_size == 0 || arr == nullptr) return {};
std::vector<unsigned int> indices;
for (unsigned int i = 0; i < _size; ++i) indices.push_back(i);
std::sort(indices.begin(), indices.end(),
[arr](const unsigned int a, const unsigned int b)
{ return arr[a] > arr[b]; });
return indices;
}
template<typename T>
T ortcv::utils::math::cosine_similarity(const std::vector<T> &a, const std::vector<T> &b)
{
types::__assert_type<T>();
T zero_vale = static_cast<T>(0);
if (a.empty() || b.empty() || (a.size() != b.size())) return zero_vale;
const unsigned int _size = a.size();
T mul_a = zero_vale, mul_b = zero_vale, mul_ab = zero_vale;
for (unsigned int i = 0; i < _size; ++i)
{
mul_a += a[i] * a[i];
mul_b += b[i] * b[i];
mul_ab += a[i] * b[i];
}
if (mul_a == zero_vale || mul_b == zero_vale) return zero_vale;
return static_cast<T>(mul_ab / (std::sqrt(mul_a) * std::sqrt(mul_b)));
}
#endif //LITE_AI_ORT_CORE_ORT_UTILS_H