mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2024-11-30 23:49:05 +08:00
Merge pull request #2740 from stweil/master
Use pre-calculated lookup tables for all C++ compilers
This commit is contained in:
commit
0d96ff3617
File diff suppressed because it is too large
Load Diff
@ -35,40 +35,9 @@ constexpr int kTableSize = 4096;
|
||||
// Scale factor for float arg to int index.
|
||||
constexpr double kScaleFactor = 256.0;
|
||||
|
||||
#if __cplusplus < 201402 || defined(__clang__) || defined(__INTEL_COMPILER) // C++11
|
||||
|
||||
extern double TanhTable[];
|
||||
extern double LogisticTable[];
|
||||
|
||||
#else // C++14 or newer
|
||||
|
||||
typedef double (*LUT_FUNCTION)(int i);
|
||||
|
||||
constexpr double LUTFuncTanh(int i) {
|
||||
return std::tanh(i / kScaleFactor);
|
||||
}
|
||||
|
||||
constexpr double LUTFuncLog(int i) {
|
||||
return 1 / (1 + std::exp(-i / kScaleFactor));
|
||||
}
|
||||
|
||||
template<int n, LUT_FUNCTION f>
|
||||
struct LUTTempl {
|
||||
constexpr LUTTempl() : table_() {
|
||||
for (auto i = 0; i < n; ++i) {
|
||||
table_[i] = f(i);
|
||||
}
|
||||
}
|
||||
const double& operator[](size_t i) const {
|
||||
return table_[i];
|
||||
}
|
||||
double table_[n];
|
||||
};
|
||||
|
||||
extern const LUTTempl<kTableSize, LUTFuncTanh> TanhTable;
|
||||
extern const LUTTempl<kTableSize, LUTFuncLog> LogisticTable;
|
||||
|
||||
#endif
|
||||
// Generated lookup tables.
|
||||
extern const double TanhTable[];
|
||||
extern const double LogisticTable[];
|
||||
|
||||
// Non-linearity (sigmoid) functions with cache tables and clipping.
|
||||
inline double Tanh(double x) {
|
||||
|
25
src/lstm/generate_lut.py
Executable file
25
src/lstm/generate_lut.py
Executable file
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Create C/C++ code for two lookup tables.
|
||||
|
||||
import math
|
||||
|
||||
# Size of static tables.
|
||||
kTableSize = 4096
|
||||
# Scale factor for float arg to int index.
|
||||
kScaleFactor = 256.0
|
||||
|
||||
print("// Generated code with lookup tables")
|
||||
print('#include "functions.h"')
|
||||
print("namespace tesseract {")
|
||||
|
||||
print("const double TanhTable[] = {")
|
||||
for i in range(kTableSize):
|
||||
print(" %a," % math.tanh(i / kScaleFactor))
|
||||
print("};")
|
||||
|
||||
print("const double LogisticTable[] = {")
|
||||
for i in range(kTableSize):
|
||||
print(" %a," % (1 / (1 + math.exp(-i / kScaleFactor))))
|
||||
print("};")
|
||||
print("} // namespace tesseract.")
|
Loading…
Reference in New Issue
Block a user