-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastChwHwcConverter.hpp
215 lines (203 loc) · 8.33 KB
/
FastChwHwcConverter.hpp
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
/*
* This file is part of [https://github.com/whyb/FastChwHwcConverter].
* Copyright (C) [2024] [張小凡](https://github.com/whyb)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include <array>
#include <algorithm>
#include <cmath>
#include <functional>
#include <limits>
#ifdef _OPENMP
#include <omp.h>
#endif
// Check if the compiler supports C++17
#if __cplusplus >= 201703L
// If C++17 is supported, use std::clamp from the standard library
#define CLAMP(value, low, high) std::clamp(value, low, high)
#else
// If C++17 is not supported but C++11 is, implement std::clamp using std::min and std::max
#define CLAMP(value, low, high) (std::max(low, std::min(value, high)))
#endif
namespace whyb {
template <typename T>
T std_clamp(const T& value, const T& low, const T& high) {
return CLAMP(value, low, high);
}
/**
* @brief Determines if two numbers are approximately equal
*
* @tparam Type Type of the numbers
* @param a First number
* @param b Second number
* @return true if the numbers are approximately equal
* @return false if the numbers are not approximately equal
*/
template <typename Type>
inline bool is_number_equal(const Type& a, const Type& b) {
static Type epsilon = std::numeric_limits<Type>::epsilon();
return std::abs(a - b) < epsilon;
}
/**
* @brief Converts image data from HWC format to CHW format
*
* @tparam Stype Source data type
* @tparam Dtype Destination data type
* @param h Height of image
* @param w Width of image
* @param c Number of channels
* @param src Pointer to the source data in HWC format
* @param dst Pointer to the destination data in CHW format
* @param alpha Scaling factor
* @param clamp Whether to clamp the data values
* @param min_v Minimum value for clamping
* @param max_v Maximum value for clamping
* @param normalized_mean_stds Whether to use mean and standard deviation for normalization
* @param mean Array of mean values for normalization
* @param stds Array of standard deviation values for normalization
*/
template <typename Stype, typename Dtype>
inline void hwc2chw(
const size_t h, const size_t w, const size_t c,
const Stype* src, Dtype* dst,
const Dtype alpha = 1,
const bool clamp = false, const Dtype min_v = 0.0, const Dtype max_v = 1.0,
const bool normalized_mean_stds = false,
const std::array<float, 3> mean = { 0.485, 0.456, 0.406 },
const std::array<float, 3> stds = { 0.229, 0.224, 0.225 }) {
std::function<Dtype(const Stype&, const size_t&)> cvt_fun;
if(clamp) {
if(is_number_equal<Dtype>(alpha, 1)) {
if(normalized_mean_stds) {
cvt_fun = [&alpha,&min_v,&max_v,&mean,&stds](const Stype& src_val, const size_t& c){return static_cast<Dtype>(std_clamp<Dtype>((src_val - mean[c]) / stds[c], min_v, max_v));};
} else {
cvt_fun = [&alpha,&min_v,&max_v](const Stype& src_val, const size_t& c){return static_cast<Dtype>(std_clamp<Dtype>(src_val, min_v, max_v));};
}
} else {
if(normalized_mean_stds) {
cvt_fun = [&alpha,&min_v,&max_v,&mean,&stds](const Stype& src_val, const size_t& c){return static_cast<Dtype>(std_clamp<Dtype>((src_val * alpha - mean[c]) / stds[c], min_v, max_v));};
} else {
cvt_fun = [&alpha,&min_v,&max_v](const Stype& src_val, const size_t& c){return static_cast<Dtype>(std_clamp<Dtype>(src_val * alpha, min_v, max_v));};
}
}
} else {
if(is_number_equal<Dtype>(alpha, 1)) {
if(normalized_mean_stds) {
cvt_fun = [&alpha,&mean,&stds](const Stype& src_val, const size_t& c){return static_cast<Dtype>((src_val - mean[c]) / stds[c]);};
} else {
cvt_fun = [&alpha](const Stype& src_val, const size_t& c){return static_cast<Dtype>(src_val);};
}
} else {
if(normalized_mean_stds) {
cvt_fun = [&alpha,&mean,&stds](const Stype& src_val, const size_t& c){return static_cast<Dtype>((src_val * alpha - mean[c]) / stds[c]);};
} else {
cvt_fun = [&alpha](const Stype& src_val, const size_t& c){return static_cast<Dtype>(src_val * alpha);};
}
}
}
#ifdef _OPENMP
const size_t hw_stride = w * h;
const size_t num_threads = omp_get_max_threads();
const size_t chunk_size = hw_stride / num_threads;
#pragma omp parallel
{
const size_t thread_id = omp_get_thread_num();
const size_t start_idx = thread_id * chunk_size;
const size_t end_idx = (thread_id == num_threads - 1) ? hw_stride : (start_idx + chunk_size);
size_t index = start_idx * c;
for (size_t s = start_idx; s < end_idx; ++s) {
size_t stride_index = s;
for (size_t c1 = 0UL; c1 < c; ++c1, stride_index += hw_stride) {
dst[stride_index] = cvt_fun(src[index++], c1);
}
}
}
#else
size_t index = 0UL;
const size_t hw_stride = w * h;
for (size_t s = 0UL; s < hw_stride; ++s) {
size_t stride_index = s;
for (size_t c1 = 0UL; c1 < c; ++c1, stride_index += hw_stride) {
dst[stride_index] = cvt_fun(src[index++], c1);
}
}
#endif
}
/**
* @brief Converts image data from CHW format to HWC format
*
* @tparam Stype Source data type
* @tparam Dtype Destination data type
* @param c Number of channels
* @param h Height of image
* @param w Width of image
* @param src Pointer to the source data in CHW format
* @param dst Pointer to the destination data in HWC format
* @param alpha Scaling factor
* @param clamp Whether to clamp the data values
* @param min_v Minimum value for clamping
* @param max_v Maximum value for clamping
*/
template <typename Stype, typename Dtype>
inline void chw2hwc(
const size_t c, const size_t h, const size_t w,
const Stype* src, Dtype* dst,
const Dtype alpha = 1,
const bool clamp = false, const Dtype min_v = 0, const Dtype max_v = 255) {
std::function<Dtype(const Stype&, const size_t&)> cvt_fun;
if(clamp) {
if(is_number_equal<Dtype>(alpha, 1)) {
cvt_fun = [&alpha,&min_v,&max_v](const Stype& src_val, const size_t& c){return static_cast<Dtype>(std_clamp<Dtype>(src_val * alpha, min_v, max_v));};
} else {
cvt_fun = [&alpha,&min_v,&max_v](const Stype& src_val, const size_t& c){return static_cast<Dtype>(std_clamp<Dtype>(src_val, min_v, max_v));};
}
} else {
if(is_number_equal<Dtype>(alpha, 1)) {
cvt_fun = [&alpha](const Stype& src_val, const size_t& c){return static_cast<Dtype>(src_val);};
} else {
cvt_fun = [&alpha](const Stype& src_val, const size_t& c){return static_cast<Dtype>(src_val * alpha);};
}
}
#ifdef _OPENMP
const size_t hw_stride = w * h;
const size_t num_threads = omp_get_max_threads();
const size_t chunk_size = hw_stride / num_threads;
#pragma omp parallel
{
const size_t thread_id = omp_get_thread_num();
const size_t start_idx = thread_id * chunk_size;
const size_t end_idx = (thread_id == num_threads - 1) ? hw_stride : (start_idx + chunk_size);
size_t index = start_idx * c;
for (size_t s = start_idx; s < end_idx; ++s) {
size_t stride_index = s;
for (size_t c1 = 0UL; c1 < c; ++c1, stride_index += hw_stride) {
dst[index++] = cvt_fun(src[stride_index], c1);
}
}
}
#else
size_t index = 0UL;
const size_t hw_stride = w * h;
for (size_t s = 0UL; s < hw_stride; ++s) {
size_t stride_index = s;
for (size_t c1 = 0UL; c1 < c; ++c1, stride_index += hw_stride) {
dst[index++] = cvt_fun(src[stride_index], c1);
}
}
#endif
}
}