19 #ifndef TESSERACT_LSTM_NETWORKIO_H_
20 #define TESSERACT_LSTM_NETWORKIO_H_
49 void Resize2d(
bool int_mode,
int width,
int num_features);
53 ResizeToMap(
false, src.
stride_map(), num_features);
56 void ResizeToMap(
bool int_mode,
const StrideMap &stride_map,
int num_features);
58 void ResizeScaled(
const NetworkIO &src,
int x_scale,
int y_scale,
int num_features);
60 void ResizeXTo1(
const NetworkIO &src,
int num_features);
66 void ZeroInvalidElements();
74 void FromPixes(
const StaticShape &shape,
const std::vector<Image> &pixes,
83 void Copy2DImage(
int batch,
Image pix,
float black,
float contrast,
TRand *randomizer);
88 void Copy1DGreyImage(
int batch,
Image pix,
float black,
float contrast,
TRand *randomizer);
96 void SetPixel(
int t,
int f,
int pixel,
float black,
float contrast);
100 void Print(
int num)
const;
104 return int_mode_ ? i_.dim1() : f_.dim1();
108 return int_mode_ ? i_.dim2() : f_.dim2();
115 const float *
f(
int t)
const {
119 const int8_t *
i(
int t)
const {
127 int_mode_ = is_quantized;
143 void CopyTimeStepFrom(
int dest_t,
const NetworkIO &src,
int src_t);
145 void CopyTimeStepGeneral(
int dest_t,
int dest_offset,
int num_features,
const NetworkIO &src,
146 int src_t,
int src_offset);
149 ZeroTimeStepGeneral(t, 0, NumFeatures());
151 void ZeroTimeStepGeneral(
int t,
int offset,
int num_features);
153 void Randomize(
int t,
int offset,
int num_features,
TRand *randomizer);
156 int BestChoiceOverRange(
int t_start,
int t_end,
int not_this,
int null_ch,
float *rating,
157 float *certainty)
const;
159 void ScoresOverRange(
int t_start,
int t_end,
int choice,
int null_ch,
float *rating,
160 float *certainty)
const;
164 return BestLabel(t, -1, -1, score);
169 int BestLabel(
int t,
int not_this,
int not_that,
float *score)
const;
172 int PositionOfBestMatch(
const std::vector<int> &labels,
int start,
int end)
const;
175 TFloat ScoreOfLabels(
const std::vector<int> &labels,
int start)
const;
179 void SetActivations(
int t,
int label,
float ok_score);
183 void EnsureBestLabel(
int t,
int label);
185 static float ProbToCertainty(
float prob);
193 bool AnySuspiciousTruth(
float confidence_thr)
const;
196 void ReadTimeStep(
int t,
TFloat *output)
const;
198 void AddTimeStep(
int t,
TFloat *inout)
const;
200 void AddTimeStepPart(
int t,
int offset,
int num_features,
float *inout)
const;
202 void WriteTimeStep(
int t,
const TFloat *input);
205 void WriteTimeStepPart(
int t,
int offset,
int num_features,
const TFloat *input);
207 void MaxpoolTimeStep(
int dest_t,
const NetworkIO &src,
int src_t,
int *max_line);
211 float MinOfMaxes()
const;
214 return int_mode_ ? i_.Max() : f_.Max();
220 void ComputeCombinerDeltas(
const NetworkIO &fwd_deltas,
const NetworkIO &base_output);
225 void AddAllToFloat(
const NetworkIO &src);
227 void SubtractAllFromFloat(
const NetworkIO &src);
236 void CopyWithYReversal(
const NetworkIO &src);
238 void CopyWithXReversal(
const NetworkIO &src);
240 void CopyWithXYTranspose(
const NetworkIO &src);
244 int CopyPacking(
const NetworkIO &src,
int feature_offset);
247 void CopyUnpacking(
const NetworkIO &src,
int feature_offset,
int num_features);
257 template <
class Func>
264 const int8_t *u = i_[t];
265 const int8_t *v = v_io.i_[t];
266 for (
int i = 0; i < dim; ++i) {
267 product[i] = f(u[i] /
static_cast<TFloat>(INT8_MAX)) * v[i] / INT8_MAX;
270 const float *u = f_[t];
271 const float *v = v_io.f_[t];
272 for (
int i = 0; i < dim; ++i) {
273 product[i] = f(u[i]) * v[i];
280 template <
class Func>
286 const float *u = f_[u_t];
287 const float *v = v_io.f_[v_t];
289 for (
int i = 0; i < dim; ++i) {
290 product[i] = f(u[i]) * v[i] * w[i];
296 template <
class Func>
301 const float *u = f_[t];
302 const float *v = v_io.f_[t];
304 for (
int i = 0; i < dim; ++i) {
305 product[i] += f(u[i]) * v[i] * w[i];
311 template <
class Func1,
class Func2>
317 const float *u = f_[t];
318 const float *v = v_io.f_[t];
320 for (
int i = 0; i < dim; ++i) {
321 product[i] = f(u[i]) * g(v[i]) * w[i];
328 static int GetPadding(
int num_features);
void ClipVector(int n, T lower, T upper, T *vec)
void Resize(const NetworkIO &src, int num_features)
void FuncMultiply(const NetworkIO &v_io, int t, TFloat *product)
const StrideMap & stride_map() const
void ResizeFloat(const NetworkIO &src, int num_features)
GENERIC_2D_ARRAY< float > * mutable_float_array()
const int8_t * i(int t) const
void FuncMultiply3Add(const NetworkIO &v_io, int t, const TFloat *w, TFloat *product) const
void set_stride_map(const StrideMap &map)
void ScaleFloatBy(float factor)
const GENERIC_2D_ARRAY< float > & float_array() const
void FuncMultiply3(int u_t, const NetworkIO &v_io, int v_t, const TFloat *w, TFloat *product) const
const float * f(int t) const
void Func2Multiply3(const NetworkIO &v_io, int t, const TFloat *w, TFloat *product) const
void set_int_mode(bool is_quantized)
int BestLabel(int t, float *score) const