18#ifndef TESSERACT_LSTM_NETWORKIO_H_
19#define TESSERACT_LSTM_NETWORKIO_H_
48 void Resize2d(
bool int_mode,
int width,
int num_features);
52 ResizeToMap(
false, src.
stride_map(), num_features);
55 void ResizeToMap(
bool int_mode,
const StrideMap &stride_map,
int num_features);
57 void ResizeScaled(
const NetworkIO &src,
int x_scale,
int y_scale,
int num_features);
59 void ResizeXTo1(
const NetworkIO &src,
int num_features);
65 void ZeroInvalidElements();
73 void FromPixes(
const StaticShape &shape,
const std::vector<Image> &pixes,
82 void Copy2DImage(
int batch,
Image pix,
float black,
float contrast,
TRand *randomizer);
87 void Copy1DGreyImage(
int batch,
Image pix,
float black,
float contrast,
TRand *randomizer);
95 void SetPixel(
int t,
int f,
int pixel,
float black,
float contrast);
99 void Print(
int num)
const;
103 return int_mode_ ? i_.dim1() : f_.dim1();
107 return int_mode_ ? i_.dim2() : f_.dim2();
114 const float *
f(
int t)
const {
118 const int8_t *
i(
int t)
const {
126 int_mode_ = is_quantized;
142 void CopyTimeStepFrom(
int dest_t,
const NetworkIO &src,
int src_t);
144 void CopyTimeStepGeneral(
int dest_t,
int dest_offset,
int num_features,
const NetworkIO &src,
145 int src_t,
int src_offset);
149 memset(i_[t], 0,
sizeof(*i_[t]) * NumFeatures());
151 memset(f_[t], 0,
sizeof(*f_[t]) * NumFeatures());
155 void Randomize(
int t,
int offset,
int num_features,
TRand *randomizer);
158 int BestChoiceOverRange(
int t_start,
int t_end,
int not_this,
int null_ch,
float *rating,
159 float *certainty)
const;
161 void ScoresOverRange(
int t_start,
int t_end,
int choice,
int null_ch,
float *rating,
162 float *certainty)
const;
166 return BestLabel(t, -1, -1, score);
171 int BestLabel(
int t,
int not_this,
int not_that,
float *score)
const;
174 int PositionOfBestMatch(
const std::vector<int> &labels,
int start,
int end)
const;
177 TFloat ScoreOfLabels(
const std::vector<int> &labels,
int start)
const;
181 void SetActivations(
int t,
int label,
float ok_score);
185 void EnsureBestLabel(
int t,
int label);
187 static float ProbToCertainty(
float prob);
195 bool AnySuspiciousTruth(
float confidence_thr)
const;
200 void AddTimeStep(
int t,
TFloat *inout)
const;
202 void AddTimeStepPart(
int t,
int offset,
int num_features,
float *inout)
const;
204 void WriteTimeStep(
int t,
const TFloat *input);
207 void WriteTimeStepPart(
int t,
int offset,
int num_features,
const TFloat *input);
209 void MaxpoolTimeStep(
int dest_t,
const NetworkIO &src,
int src_t,
int *max_line);
213 float MinOfMaxes()
const;
216 return int_mode_ ? i_.Max() : f_.Max();
222 void ComputeCombinerDeltas(
const NetworkIO &fwd_deltas,
const NetworkIO &base_output);
227 void AddAllToFloat(
const NetworkIO &src);
229 void SubtractAllFromFloat(
const NetworkIO &src);
238 void CopyWithYReversal(
const NetworkIO &src);
240 void CopyWithXReversal(
const NetworkIO &src);
242 void CopyWithXYTranspose(
const NetworkIO &src);
246 int CopyPacking(
const NetworkIO &src,
int feature_offset);
249 void CopyUnpacking(
const NetworkIO &src,
int feature_offset,
int num_features);
259 template <
class Func>
266 const int8_t *u = i_[t];
267 const int8_t *v = v_io.i_[t];
268 for (
int i = 0;
i < dim; ++
i) {
269 product[
i] = f(u[
i] /
static_cast<TFloat>(INT8_MAX)) * v[
i] / INT8_MAX;
272 const float *u = f_[t];
273 const float *v = v_io.f_[t];
274 for (
int i = 0;
i < dim; ++
i) {
275 product[
i] = f(u[
i]) * v[
i];
282 template <
class Func>
288 const float *u = f_[u_t];
289 const float *v = v_io.f_[v_t];
291 for (
int i = 0;
i < dim; ++
i) {
292 product[
i] = f(u[
i]) * v[
i] * w[
i];
298 template <
class Func>
303 const float *u = f_[t];
304 const float *v = v_io.f_[t];
306 for (
int i = 0;
i < dim; ++
i) {
307 product[
i] += f(u[
i]) * v[
i] * w[
i];
313 template <
class Func1,
class Func2>
319 const float *u = f_[t];
320 const float *v = v_io.f_[t];
322 for (
int i = 0;
i < dim; ++
i) {
323 product[
i] = f(u[
i]) * g(v[
i]) * w[
i];
330 static int GetPadding(
int num_features);
void ClipVector(int n, T lower, T upper, T *vec)
std::string Print(const T &value)
void Resize(const NetworkIO &src, int num_features)
void FuncMultiply(const NetworkIO &v_io, int t, TFloat *product)
void ResizeFloat(const NetworkIO &src, int num_features)
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 float * f(int t) const
void FuncMultiply3(int u_t, const NetworkIO &v_io, int v_t, const TFloat *w, TFloat *product) const
GENERIC_2D_ARRAY< float > * mutable_float_array()
void Func2Multiply3(const NetworkIO &v_io, int t, const TFloat *w, TFloat *product) const
void set_int_mode(bool is_quantized)
const StrideMap & stride_map() const
const int8_t * i(int t) const
const GENERIC_2D_ARRAY< float > & float_array() const
int BestLabel(int t, float *score) const