tesseract  4.00.00dev
tesseract::NetworkIO Class Reference

#include <networkio.h>

Public Member Functions

 NetworkIO ()
 
void Resize (const NetworkIO &src, int num_features)
 
void Resize2d (bool int_mode, int width, int num_features)
 
void ResizeFloat (const NetworkIO &src, int num_features)
 
void ResizeToMap (bool int_mode, const StrideMap &stride_map, int num_features)
 
void ResizeScaled (const NetworkIO &src, int x_scale, int y_scale, int num_features)
 
void ResizeXTo1 (const NetworkIO &src, int num_features)
 
void Zero ()
 
void ZeroInvalidElements ()
 
void FromPix (const StaticShape &shape, const Pix *pix, TRand *randomizer)
 
void FromPixes (const StaticShape &shape, const std::vector< const Pix *> &pixes, TRand *randomizer)
 
void Copy2DImage (int batch, Pix *pix, float black, float contrast, TRand *randomizer)
 
void Copy1DGreyImage (int batch, Pix *pix, float black, float contrast, TRand *randomizer)
 
void SetPixel (int t, int f, int pixel, float black, float contrast)
 
Pix * ToPix () const
 
void Print (int num) const
 
int Width () const
 
int NumFeatures () const
 
float * f (int t)
 
const float * f (int t) const
 
const inT8i (int t) const
 
bool int_mode () const
 
void set_int_mode (bool is_quantized)
 
const StrideMapstride_map () const
 
void set_stride_map (const StrideMap &map)
 
const GENERIC_2D_ARRAY< float > & float_array () const
 
GENERIC_2D_ARRAY< float > * mutable_float_array ()
 
void CopyTimeStepFrom (int dest_t, const NetworkIO &src, int src_t)
 
void CopyTimeStepGeneral (int dest_t, int dest_offset, int num_features, const NetworkIO &src, int src_t, int src_offset)
 
void ZeroTimeStep (int t)
 
void ZeroTimeStepGeneral (int t, int offset, int num_features)
 
void Randomize (int t, int offset, int num_features, TRand *randomizer)
 
int BestChoiceOverRange (int t_start, int t_end, int not_this, int null_ch, float *rating, float *certainty) const
 
void ScoresOverRange (int t_start, int t_end, int choice, int null_ch, float *rating, float *certainty) const
 
int BestLabel (int t, float *score) const
 
int BestLabel (int t, int not_this, int not_that, float *score) const
 
int PositionOfBestMatch (const GenericVector< int > &labels, int start, int end) const
 
double ScoreOfLabels (const GenericVector< int > &labels, int start) const
 
void SetActivations (int t, int label, float ok_score)
 
void EnsureBestLabel (int t, int label)
 
bool AnySuspiciousTruth (float confidence_thr) const
 
void ReadTimeStep (int t, double *output) const
 
void AddTimeStep (int t, double *inout) const
 
void AddTimeStepPart (int t, int offset, int num_features, float *inout) const
 
void WriteTimeStep (int t, const double *input)
 
void WriteTimeStepPart (int t, int offset, int num_features, const double *input)
 
void MaxpoolTimeStep (int dest_t, const NetworkIO &src, int src_t, int *max_line)
 
void MaxpoolBackward (const NetworkIO &fwd, const GENERIC_2D_ARRAY< int > &maxes)
 
float MinOfMaxes () const
 
float Max () const
 
void CombineOutputs (const NetworkIO &base_output, const NetworkIO &combiner_output)
 
void ComputeCombinerDeltas (const NetworkIO &fwd_deltas, const NetworkIO &base_output)
 
void CopyAll (const NetworkIO &src)
 
void AddAllToFloat (const NetworkIO &src)
 
void SubtractAllFromFloat (const NetworkIO &src)
 
void CopyWithNormalization (const NetworkIO &src, const NetworkIO &scale)
 
void ScaleFloatBy (float factor)
 
void CopyWithYReversal (const NetworkIO &src)
 
void CopyWithXReversal (const NetworkIO &src)
 
void CopyWithXYTranspose (const NetworkIO &src)
 
int CopyPacking (const NetworkIO &src, int feature_offset)
 
void CopyUnpacking (const NetworkIO &src, int feature_offset, int num_features)
 
void Transpose (TransposedArray *dest) const
 
void ClipVector (int t, float range)
 
template<class Func >
void FuncMultiply (const NetworkIO &v_io, int t, double *product)
 
template<class Func >
void FuncMultiply3 (int u_t, const NetworkIO &v_io, int v_t, const double *w, double *product) const
 
template<class Func >
void FuncMultiply3Add (const NetworkIO &v_io, int t, const double *w, double *product) const
 
template<class Func1 , class Func2 >
void Func2Multiply3 (const NetworkIO &v_io, int t, const double *w, double *product) const
 

Static Public Member Functions

static float ProbToCertainty (float prob)
 

Detailed Description

Definition at line 39 of file networkio.h.

Constructor & Destructor Documentation

◆ NetworkIO()

tesseract::NetworkIO::NetworkIO ( )
inline

Definition at line 41 of file networkio.h.

41 : int_mode_(false) {}

Member Function Documentation

◆ AddAllToFloat()

void tesseract::NetworkIO::AddAllToFloat ( const NetworkIO src)

Definition at line 817 of file networkio.cpp.

817  {
818  ASSERT_HOST(!int_mode_);
819  ASSERT_HOST(!src.int_mode_);
820  f_ += src.f_;
821 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ AddTimeStep()

void tesseract::NetworkIO::AddTimeStep ( int  t,
double *  inout 
) const

Definition at line 613 of file networkio.cpp.

613  {
614  int num_features = NumFeatures();
615  if (int_mode_) {
616  const inT8* line = i_[t];
617  for (int i = 0; i < num_features; ++i) {
618  inout[i] += static_cast<double>(line[i]) / MAX_INT8;
619  }
620  } else {
621  const float* line = f_[t];
622  for (int i = 0; i < num_features; ++i) {
623  inout[i] += line[i];
624  }
625  }
626 }
int8_t inT8
Definition: host.h:34
int NumFeatures() const
Definition: networkio.h:111
const inT8 * i(int t) const
Definition: networkio.h:123
#define MAX_INT8
Definition: host.h:60

◆ AddTimeStepPart()

void tesseract::NetworkIO::AddTimeStepPart ( int  t,
int  offset,
int  num_features,
float *  inout 
) const

Definition at line 629 of file networkio.cpp.

630  {
631  if (int_mode_) {
632  const inT8* line = i_[t] + offset;
633  for (int i = 0; i < num_features; ++i) {
634  inout[i] += static_cast<float>(line[i]) / MAX_INT8;
635  }
636  } else {
637  const float* line = f_[t] + offset;
638  for (int i = 0; i < num_features; ++i) {
639  inout[i] += line[i];
640  }
641  }
642 }
voidpf uLong offset
Definition: ioapi.h:42
int8_t inT8
Definition: host.h:34
const inT8 * i(int t) const
Definition: networkio.h:123
#define MAX_INT8
Definition: host.h:60

◆ AnySuspiciousTruth()

bool tesseract::NetworkIO::AnySuspiciousTruth ( float  confidence_thr) const

Definition at line 579 of file networkio.cpp.

579  {
580  int num_features = NumFeatures();
581  for (int t = 0; t < Width(); ++t) {
582  const float* features = f_[t];
583  for (int y = 0; y < num_features; ++y) {
584  float grad = features[y];
585  if (grad < -confidence_thr) {
586  // Correcting strong output. Check for movement.
587  if ((t == 0 || f_[t - 1][y] < confidence_thr / 2) &&
588  (t + 1 == Width() || f_[t + 1][y] < confidence_thr / 2)) {
589  return true; // No strong positive on either side.
590  }
591  }
592  }
593  }
594  return false;
595 }
int Width() const
Definition: networkio.h:107
int NumFeatures() const
Definition: networkio.h:111
const char features[]
Definition: feature_tests.c:2

◆ BestChoiceOverRange()

int tesseract::NetworkIO::BestChoiceOverRange ( int  t_start,
int  t_end,
int  not_this,
int  null_ch,
float *  rating,
float *  certainty 
) const

Definition at line 431 of file networkio.cpp.

433  {
434  if (t_end <= t_start) return -1;
435  int max_char = -1;
436  float min_score = 0.0f;
437  for (int c = 0; c < NumFeatures(); ++c) {
438  if (c == not_this || c == null_ch) continue;
439  ScoresOverRange(t_start, t_end, c, null_ch, rating, certainty);
440  if (max_char < 0 || *rating < min_score) {
441  min_score = *rating;
442  max_char = c;
443  }
444  }
445  ScoresOverRange(t_start, t_end, max_char, null_ch, rating, certainty);
446  return max_char;
447 }
void ScoresOverRange(int t_start, int t_end, int choice, int null_ch, float *rating, float *certainty) const
Definition: networkio.cpp:450
int NumFeatures() const
Definition: networkio.h:111

◆ BestLabel() [1/2]

int tesseract::NetworkIO::BestLabel ( int  t,
float *  score 
) const
inline

Definition at line 161 of file networkio.h.

161  {
162  return BestLabel(t, -1, -1, score);
163  }
int BestLabel(int t, float *score) const
Definition: networkio.h:161

◆ BestLabel() [2/2]

int tesseract::NetworkIO::BestLabel ( int  t,
int  not_this,
int  not_that,
float *  score 
) const

Definition at line 489 of file networkio.cpp.

490  {
491  ASSERT_HOST(!int_mode_);
492  int best_index = -1;
493  float best_score = -MAX_FLOAT32;
494  const float* line = f_[t];
495  for (int i = 0; i < f_.dim2(); ++i) {
496  if (line[i] > best_score && i != not_this && i != not_that) {
497  best_score = line[i];
498  best_index = i;
499  }
500  }
501  if (score != NULL) *score = ProbToCertainty(best_score);
502  return best_index;
503 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim2() const
Definition: matrix.h:202
#define MAX_FLOAT32
Definition: host.h:66
static float ProbToCertainty(float prob)
Definition: networkio.cpp:568
const inT8 * i(int t) const
Definition: networkio.h:123

◆ ClipVector()

void tesseract::NetworkIO::ClipVector ( int  t,
float  range 
)

Definition at line 971 of file networkio.cpp.

971  {
972  ASSERT_HOST(!int_mode_);
973  float* v = f_[t];
974  int dim = f_.dim2();
975  for (int i = 0; i < dim; ++i)
976  v[i] = ClipToRange(v[i], -range, range);
977 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim2() const
Definition: matrix.h:202
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
const inT8 * i(int t) const
Definition: networkio.h:123
double v[max]

◆ CombineOutputs()

void tesseract::NetworkIO::CombineOutputs ( const NetworkIO base_output,
const NetworkIO combiner_output 
)

Definition at line 736 of file networkio.cpp.

737  {
738  int no = base_output.NumFeatures();
739  ASSERT_HOST(combiner_output.NumFeatures() == no + 1);
740  Resize(base_output, no);
741  int width = Width();
742  if (int_mode_) {
743  // Number of outputs from base and final result.
744  for (int t = 0; t < width; ++t) {
745  inT8* out_line = i_[t];
746  const inT8* base_line = base_output.i_[t];
747  const inT8* comb_line = combiner_output.i_[t];
748  float base_weight = static_cast<float>(comb_line[no]) / MAX_INT8;
749  float boost_weight = 1.0f - base_weight;
750  for (int i = 0; i < no; ++i) {
751  out_line[i] = IntCastRounded(base_line[i] * base_weight +
752  comb_line[i] * boost_weight);
753  }
754  }
755  } else {
756  for (int t = 0; t < width; ++t) {
757  float* out_line = f_[t];
758  const float* base_line = base_output.f_[t];
759  const float* comb_line = combiner_output.f_[t];
760  float base_weight = comb_line[no];
761  float boost_weight = 1.0f - base_weight;
762  for (int i = 0; i < no; ++i) {
763  out_line[i] = base_line[i] * base_weight + comb_line[i] * boost_weight;
764  }
765  }
766  }
767 }
int Width() const
Definition: networkio.h:107
void Resize(const NetworkIO &src, int num_features)
Definition: networkio.h:45
int IntCastRounded(double x)
Definition: helpers.h:179
#define ASSERT_HOST(x)
Definition: errcode.h:84
int8_t inT8
Definition: host.h:34
const inT8 * i(int t) const
Definition: networkio.h:123
#define MAX_INT8
Definition: host.h:60

◆ ComputeCombinerDeltas()

void tesseract::NetworkIO::ComputeCombinerDeltas ( const NetworkIO fwd_deltas,
const NetworkIO base_output 
)

Definition at line 770 of file networkio.cpp.

771  {
772  ASSERT_HOST(!int_mode_);
773  // Compute the deltas for the combiner.
774  int width = Width();
775  int no = NumFeatures() - 1;
776  ASSERT_HOST(fwd_deltas.NumFeatures() == no);
777  ASSERT_HOST(base_output.NumFeatures() == no);
778  // Number of outputs from base and final result.
779  for (int t = 0; t < width; ++t) {
780  const float* delta_line = fwd_deltas.f_[t];
781  const float* base_line = base_output.f_[t];
782  float* comb_line = f_[t];
783  float base_weight = comb_line[no];
784  float boost_weight = 1.0f - base_weight;
785  float max_base_delta = 0.0;
786  for (int i = 0; i < no; ++i) {
787  // What did the combiner actually produce?
788  float output = base_line[i] * base_weight + comb_line[i] * boost_weight;
789  // Reconstruct the target from the delta.
790  float comb_target = delta_line[i] + output;
791  comb_line[i] = comb_target - comb_line[i];
792  float base_delta = fabs(comb_target - base_line[i]);
793  if (base_delta > max_base_delta) max_base_delta = base_delta;
794  }
795  if (max_base_delta >= 0.5) {
796  // The base network got it wrong. The combiner should output the right
797  // answer and 0 for the base network.
798  comb_line[no] = 0.0 - base_weight;
799  } else {
800  // The base network was right. The combiner should flag that.
801  for (int i = 0; i < no; ++i) {
802  // All other targets are 0.
803  if (comb_line[i] > 0.0) comb_line[i] -= 1.0;
804  }
805  comb_line[no] = 1.0 - base_weight;
806  }
807  }
808 }
int Width() const
Definition: networkio.h:107
#define ASSERT_HOST(x)
Definition: errcode.h:84
int NumFeatures() const
Definition: networkio.h:111
const inT8 * i(int t) const
Definition: networkio.h:123

◆ Copy1DGreyImage()

void tesseract::NetworkIO::Copy1DGreyImage ( int  batch,
Pix *  pix,
float  black,
float  contrast,
TRand randomizer 
)

Definition at line 246 of file networkio.cpp.

247  {
248  int width = pixGetWidth(pix);
249  int height = pixGetHeight(pix);
250  ASSERT_HOST(height == NumFeatures());
251  int wpl = pixGetWpl(pix);
252  StrideMap::Index index(stride_map_);
253  index.AddOffset(batch, FD_BATCH);
254  int t = index.t();
255  int target_width = stride_map_.Size(FD_WIDTH);
256  if (width > target_width) width = target_width;
257  int x;
258  for (x = 0; x < width; ++x, ++t) {
259  for (int y = 0; y < height; ++y) {
260  uinT32* line = pixGetData(pix) + wpl * y;
261  int pixel = GET_DATA_BYTE(line, x);
262  SetPixel(t, y, pixel, black, contrast);
263  }
264  }
265  for (; x < target_width; ++x) Randomize(t++, 0, height, randomizer);
266 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
int Size(FlexDimensions dimension) const
Definition: stridemap.h:116
uint32_t uinT32
Definition: host.h:39
int NumFeatures() const
Definition: networkio.h:111
void Randomize(int t, int offset, int num_features, TRand *randomizer)
Definition: networkio.cpp:416
void SetPixel(int t, int f, int pixel, float black, float contrast)
Definition: networkio.cpp:275

◆ Copy2DImage()

void tesseract::NetworkIO::Copy2DImage ( int  batch,
Pix *  pix,
float  black,
float  contrast,
TRand randomizer 
)

Definition at line 208 of file networkio.cpp.

209  {
210  int width = pixGetWidth(pix);
211  int height = pixGetHeight(pix);
212  int wpl = pixGetWpl(pix);
213  StrideMap::Index index(stride_map_);
214  index.AddOffset(batch, FD_BATCH);
215  int t = index.t();
216  int target_height = stride_map_.Size(FD_HEIGHT);
217  int target_width = stride_map_.Size(FD_WIDTH);
218  int num_features = NumFeatures();
219  bool color = num_features == 3;
220  if (width > target_width) width = target_width;
221  uinT32* line = pixGetData(pix);
222  for (int y = 0; y < target_height; ++y, line += wpl) {
223  int x = 0;
224  if (y < height) {
225  for (x = 0; x < width; ++x, ++t) {
226  if (color) {
227  int f = 0;
228  for (int c = COLOR_RED; c <= COLOR_BLUE; ++c) {
229  int pixel = GET_DATA_BYTE(line + x, c);
230  SetPixel(t, f++, pixel, black, contrast);
231  }
232  } else {
233  int pixel = GET_DATA_BYTE(line, x);
234  SetPixel(t, 0, pixel, black, contrast);
235  }
236  }
237  }
238  for (; x < target_width; ++x) Randomize(t++, 0, num_features, randomizer);
239  }
240 }
float * f(int t)
Definition: networkio.h:115
int Size(FlexDimensions dimension) const
Definition: stridemap.h:116
uint32_t uinT32
Definition: host.h:39
int NumFeatures() const
Definition: networkio.h:111
void Randomize(int t, int offset, int num_features, TRand *randomizer)
Definition: networkio.cpp:416
void SetPixel(int t, int f, int pixel, float black, float contrast)
Definition: networkio.cpp:275

◆ CopyAll()

void tesseract::NetworkIO::CopyAll ( const NetworkIO src)

Definition at line 811 of file networkio.cpp.

811  {
812  ASSERT_HOST(src.int_mode_ == int_mode_);
813  f_ = src.f_;
814 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ CopyPacking()

int tesseract::NetworkIO::CopyPacking ( const NetworkIO src,
int  feature_offset 
)

Definition at line 917 of file networkio.cpp.

917  {
918  ASSERT_HOST(int_mode_ == src.int_mode_);
919  int width = src.Width();
920  ASSERT_HOST(width <= Width());
921  int num_features = src.NumFeatures();
922  ASSERT_HOST(num_features + feature_offset <= NumFeatures());
923  if (int_mode_) {
924  for (int t = 0; t < width; ++t) {
925  memcpy(i_[t] + feature_offset, src.i_[t],
926  num_features * sizeof(i_[t][0]));
927  }
928  for (int t = width; t < i_.dim1(); ++t) {
929  memset(i_[t], 0, num_features * sizeof(i_[t][0]));
930  }
931  } else {
932  for (int t = 0; t < width; ++t) {
933  memcpy(f_[t] + feature_offset, src.f_[t],
934  num_features * sizeof(f_[t][0]));
935  }
936  for (int t = width; t < f_.dim1(); ++t) {
937  memset(f_[t], 0, num_features * sizeof(f_[t][0]));
938  }
939  }
940  return num_features + feature_offset;
941 }
int Width() const
Definition: networkio.h:107
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim1() const
Definition: matrix.h:201
int NumFeatures() const
Definition: networkio.h:111

◆ CopyTimeStepFrom()

void tesseract::NetworkIO::CopyTimeStepFrom ( int  dest_t,
const NetworkIO src,
int  src_t 
)

Definition at line 383 of file networkio.cpp.

383  {
384  ASSERT_HOST(int_mode_ == src.int_mode_);
385  if (int_mode_) {
386  memcpy(i_[dest_t], src.i_[src_t], i_.dim2() * sizeof(i_[0][0]));
387  } else {
388  memcpy(f_[dest_t], src.f_[src_t], f_.dim2() * sizeof(f_[0][0]));
389  }
390 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim2() const
Definition: matrix.h:202

◆ CopyTimeStepGeneral()

void tesseract::NetworkIO::CopyTimeStepGeneral ( int  dest_t,
int  dest_offset,
int  num_features,
const NetworkIO src,
int  src_t,
int  src_offset 
)

Definition at line 393 of file networkio.cpp.

395  {
396  ASSERT_HOST(int_mode_ == src.int_mode_);
397  if (int_mode_) {
398  memcpy(i_[dest_t] + dest_offset, src.i_[src_t] + src_offset,
399  num_features * sizeof(i_[0][0]));
400  } else {
401  memcpy(f_[dest_t] + dest_offset, src.f_[src_t] + src_offset,
402  num_features * sizeof(f_[0][0]));
403  }
404 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ CopyUnpacking()

void tesseract::NetworkIO::CopyUnpacking ( const NetworkIO src,
int  feature_offset,
int  num_features 
)

Definition at line 945 of file networkio.cpp.

946  {
947  Resize(src, num_features);
948  int width = src.Width();
949  ASSERT_HOST(num_features + feature_offset <= src.NumFeatures());
950  if (int_mode_) {
951  for (int t = 0; t < width; ++t) {
952  memcpy(i_[t], src.i_[t] + feature_offset,
953  num_features * sizeof(i_[t][0]));
954  }
955  } else {
956  for (int t = 0; t < width; ++t) {
957  memcpy(f_[t], src.f_[t] + feature_offset,
958  num_features * sizeof(f_[t][0]));
959  }
960  }
961 }
void Resize(const NetworkIO &src, int num_features)
Definition: networkio.h:45
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ CopyWithNormalization()

void tesseract::NetworkIO::CopyWithNormalization ( const NetworkIO src,
const NetworkIO scale 
)

Definition at line 831 of file networkio.cpp.

832  {
833  ASSERT_HOST(!int_mode_);
834  ASSERT_HOST(!src.int_mode_);
835  ASSERT_HOST(!scale.int_mode_);
836  float src_max = src.f_.MaxAbs();
837  ASSERT_HOST(std::isfinite(src_max));
838  float scale_max = scale.f_.MaxAbs();
839  ASSERT_HOST(std::isfinite(scale_max));
840  if (src_max > 0.0f) {
841  float factor = scale_max / src_max;
842  for (int t = 0; t < src.Width(); ++t) {
843  const float* src_ptr = src.f_[t];
844  float* dest_ptr = f_[t];
845  for (int i = 0; i < src.f_.dim2(); ++i) dest_ptr[i] = src_ptr[i] * factor;
846  }
847  } else {
848  f_.Clear();
849  }
850 }
void Clear()
Definition: matrix.h:129
float * f(int t)
Definition: networkio.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84
const inT8 * i(int t) const
Definition: networkio.h:123

◆ CopyWithXReversal()

void tesseract::NetworkIO::CopyWithXReversal ( const NetworkIO src)

Definition at line 872 of file networkio.cpp.

872  {
873  int num_features = src.NumFeatures();
874  Resize(src, num_features);
875  StrideMap::Index b_index(src.stride_map_);
876  do {
877  StrideMap::Index y_index(b_index);
878  do {
879  StrideMap::Index fwd_index(y_index);
880  StrideMap::Index rev_index(y_index);
881  rev_index.AddOffset(rev_index.MaxIndexOfDim(FD_WIDTH), FD_WIDTH);
882  do {
883  CopyTimeStepFrom(rev_index.t(), src, fwd_index.t());
884  } while (fwd_index.AddOffset(1, FD_WIDTH) &&
885  rev_index.AddOffset(-1, FD_WIDTH));
886  } while (y_index.AddOffset(1, FD_HEIGHT));
887  } while (b_index.AddOffset(1, FD_BATCH));
888 }
void CopyTimeStepFrom(int dest_t, const NetworkIO &src, int src_t)
Definition: networkio.cpp:383
void Resize(const NetworkIO &src, int num_features)
Definition: networkio.h:45

◆ CopyWithXYTranspose()

void tesseract::NetworkIO::CopyWithXYTranspose ( const NetworkIO src)

Definition at line 891 of file networkio.cpp.

891  {
892  int num_features = src.NumFeatures();
893  stride_map_ = src.stride_map_;
894  stride_map_.TransposeXY();
895  ResizeToMap(src.int_mode(), stride_map_, num_features);
896  StrideMap::Index src_b_index(src.stride_map_);
897  StrideMap::Index dest_b_index(stride_map_);
898  do {
899  StrideMap::Index src_y_index(src_b_index);
900  StrideMap::Index dest_x_index(dest_b_index);
901  do {
902  StrideMap::Index src_x_index(src_y_index);
903  StrideMap::Index dest_y_index(dest_x_index);
904  do {
905  CopyTimeStepFrom(dest_y_index.t(), src, src_x_index.t());
906  } while (src_x_index.AddOffset(1, FD_WIDTH) &&
907  dest_y_index.AddOffset(1, FD_HEIGHT));
908  } while (src_y_index.AddOffset(1, FD_HEIGHT) &&
909  dest_x_index.AddOffset(1, FD_WIDTH));
910  } while (src_b_index.AddOffset(1, FD_BATCH) &&
911  dest_b_index.AddOffset(1, FD_BATCH));
912 }
void CopyTimeStepFrom(int dest_t, const NetworkIO &src, int src_t)
Definition: networkio.cpp:383
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:45

◆ CopyWithYReversal()

void tesseract::NetworkIO::CopyWithYReversal ( const NetworkIO src)

Definition at line 853 of file networkio.cpp.

853  {
854  int num_features = src.NumFeatures();
855  Resize(src, num_features);
856  StrideMap::Index b_index(src.stride_map_);
857  do {
858  int width = b_index.MaxIndexOfDim(FD_WIDTH) + 1;
859  StrideMap::Index fwd_index(b_index);
860  StrideMap::Index rev_index(b_index);
861  rev_index.AddOffset(rev_index.MaxIndexOfDim(FD_HEIGHT), FD_HEIGHT);
862  do {
863  int fwd_t = fwd_index.t();
864  int rev_t = rev_index.t();
865  for (int x = 0; x < width; ++x) CopyTimeStepFrom(rev_t++, src, fwd_t++);
866  } while (fwd_index.AddOffset(1, FD_HEIGHT) &&
867  rev_index.AddOffset(-1, FD_HEIGHT));
868  } while (b_index.AddOffset(1, FD_BATCH));
869 }
void CopyTimeStepFrom(int dest_t, const NetworkIO &src, int src_t)
Definition: networkio.cpp:383
void Resize(const NetworkIO &src, int num_features)
Definition: networkio.h:45

◆ EnsureBestLabel()

void tesseract::NetworkIO::EnsureBestLabel ( int  t,
int  label 
)

Definition at line 549 of file networkio.cpp.

549  {
550  ASSERT_HOST(!int_mode_);
551  if (BestLabel(t, NULL) != label) {
552  // Output value needs enhancing. Third all the other elements and add the
553  // remainder to best_label.
554  int num_classes = NumFeatures();
555  float* targets = f_[t];
556  for (int c = 0; c < num_classes; ++c) {
557  if (c == label) {
558  targets[c] += (1.0 - targets[c]) * (2 / 3.0);
559  } else {
560  targets[c] /= 3.0;
561  }
562  }
563  }
564 }
int BestLabel(int t, float *score) const
Definition: networkio.h:161
#define ASSERT_HOST(x)
Definition: errcode.h:84
int NumFeatures() const
Definition: networkio.h:111

◆ f() [1/2]

float* tesseract::NetworkIO::f ( int  t)
inline

Definition at line 115 of file networkio.h.

115  {
116  ASSERT_HOST(!int_mode_);
117  return f_[t];
118  }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ f() [2/2]

const float* tesseract::NetworkIO::f ( int  t) const
inline

Definition at line 119 of file networkio.h.

119  {
120  ASSERT_HOST(!int_mode_);
121  return f_[t];
122  }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ float_array()

const GENERIC_2D_ARRAY<float>& tesseract::NetworkIO::float_array ( ) const
inline

Definition at line 139 of file networkio.h.

139 { return f_; }

◆ FromPix()

void tesseract::NetworkIO::FromPix ( const StaticShape shape,
const Pix *  pix,
TRand randomizer 
)

Definition at line 161 of file networkio.cpp.

162  {
163  std::vector<const Pix*> pixes(1, pix);
164  FromPixes(shape, pixes, randomizer);
165 }
void FromPixes(const StaticShape &shape, const std::vector< const Pix *> &pixes, TRand *randomizer)
Definition: networkio.cpp:170

◆ FromPixes()

void tesseract::NetworkIO::FromPixes ( const StaticShape shape,
const std::vector< const Pix *> &  pixes,
TRand randomizer 
)

Definition at line 170 of file networkio.cpp.

172  {
173  int target_height = shape.height();
174  int target_width = shape.width();
175  std::vector<std::pair<int, int>> h_w_pairs;
176  for (auto pix : pixes) {
177  Pix* var_pix = const_cast<Pix*>(pix);
178  int width = pixGetWidth(var_pix);
179  if (target_width != 0) width = target_width;
180  int height = pixGetHeight(var_pix);
181  if (target_height != 0) height = target_height;
182  h_w_pairs.emplace_back(height, width);
183  }
184  stride_map_.SetStride(h_w_pairs);
185  ResizeToMap(int_mode(), stride_map_, shape.depth());
186  // Iterate over the images again to copy the data.
187  for (size_t b = 0; b < pixes.size(); ++b) {
188  Pix* pix = const_cast<Pix*>(pixes[b]);
189  float black = 0.0f, white = 255.0f;
190  if (shape.depth() != 3) ComputeBlackWhite(pix, &black, &white);
191  float contrast = (white - black) / 2.0f;
192  if (contrast <= 0.0f) contrast = 1.0f;
193  if (shape.height() == 1) {
194  Copy1DGreyImage(b, pix, black, contrast, randomizer);
195  } else {
196  Copy2DImage(b, pix, black, contrast, randomizer);
197  }
198  }
199 }
float * f(int t)
Definition: networkio.h:115
bool int_mode() const
Definition: networkio.h:127
void Copy2DImage(int batch, Pix *pix, float black, float contrast, TRand *randomizer)
Definition: networkio.cpp:208
void Copy1DGreyImage(int batch, Pix *pix, float black, float contrast, TRand *randomizer)
Definition: networkio.cpp:246
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:45
void SetStride(const std::vector< std::pair< int, int >> &h_w_pairs)
Definition: stridemap.cpp:126

◆ Func2Multiply3()

template<class Func1 , class Func2 >
void tesseract::NetworkIO::Func2Multiply3 ( const NetworkIO v_io,
int  t,
const double *  w,
double *  product 
) const
inline

Definition at line 315 of file networkio.h.

316  {
317  ASSERT_HOST(!int_mode_);
318  ASSERT_HOST(!v_io.int_mode_);
319  Func1 f;
320  Func2 g;
321  const float* u = f_[t];
322  const float* v = v_io.f_[t];
323  int dim = f_.dim2();
324  for (int i = 0; i < dim; ++i) {
325  product[i] = f(u[i]) * g(v[i]) * w[i];
326  }
327  }
double u[max]
float * f(int t)
Definition: networkio.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim2() const
Definition: matrix.h:202
const inT8 * i(int t) const
Definition: networkio.h:123
double v[max]

◆ FuncMultiply()

template<class Func >
void tesseract::NetworkIO::FuncMultiply ( const NetworkIO v_io,
int  t,
double *  product 
)
inline

Definition at line 259 of file networkio.h.

259  {
260  Func f;
261  ASSERT_HOST(!int_mode_);
262  ASSERT_HOST(!v_io.int_mode_);
263  int dim = f_.dim2();
264  if (int_mode_) {
265  const inT8* u = i_[t];
266  const inT8* v = v_io.i_[t];
267  for (int i = 0; i < dim; ++i) {
268  product[i] = f(u[i] / static_cast<double>(MAX_INT8)) * v[i] /
269  static_cast<double>(MAX_INT8);
270  }
271  } else {
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];
276  }
277  }
278  }
double u[max]
float * f(int t)
Definition: networkio.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim2() const
Definition: matrix.h:202
int8_t inT8
Definition: host.h:34
const inT8 * i(int t) const
Definition: networkio.h:123
#define MAX_INT8
Definition: host.h:60
double v[max]

◆ FuncMultiply3()

template<class Func >
void tesseract::NetworkIO::FuncMultiply3 ( int  u_t,
const NetworkIO v_io,
int  v_t,
const double *  w,
double *  product 
) const
inline

Definition at line 283 of file networkio.h.

284  {
285  ASSERT_HOST(!int_mode_);
286  ASSERT_HOST(!v_io.int_mode_);
287  Func f;
288  const float* u = f_[u_t];
289  const float* v = v_io.f_[v_t];
290  int dim = f_.dim2();
291  for (int i = 0; i < dim; ++i) {
292  product[i] = f(u[i]) * v[i] * w[i];
293  }
294  }
double u[max]
float * f(int t)
Definition: networkio.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim2() const
Definition: matrix.h:202
const inT8 * i(int t) const
Definition: networkio.h:123
double v[max]

◆ FuncMultiply3Add()

template<class Func >
void tesseract::NetworkIO::FuncMultiply3Add ( const NetworkIO v_io,
int  t,
const double *  w,
double *  product 
) const
inline

Definition at line 299 of file networkio.h.

300  {
301  ASSERT_HOST(!int_mode_);
302  ASSERT_HOST(!v_io.int_mode_);
303  Func f;
304  const float* u = f_[t];
305  const float* v = v_io.f_[t];
306  int dim = f_.dim2();
307  for (int i = 0; i < dim; ++i) {
308  product[i] += f(u[i]) * v[i] * w[i];
309  }
310  }
double u[max]
float * f(int t)
Definition: networkio.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim2() const
Definition: matrix.h:202
const inT8 * i(int t) const
Definition: networkio.h:123
double v[max]

◆ i()

const inT8* tesseract::NetworkIO::i ( int  t) const
inline

Definition at line 123 of file networkio.h.

123  {
124  ASSERT_HOST(int_mode_);
125  return i_[t];
126  }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ int_mode()

bool tesseract::NetworkIO::int_mode ( ) const
inline

Definition at line 127 of file networkio.h.

127  {
128  return int_mode_;
129  }

◆ Max()

float tesseract::NetworkIO::Max ( ) const
inline

Definition at line 215 of file networkio.h.

215 { return int_mode_ ? i_.Max() : f_.Max(); }
T Max() const
Definition: matrix.h:337

◆ MaxpoolBackward()

void tesseract::NetworkIO::MaxpoolBackward ( const NetworkIO fwd,
const GENERIC_2D_ARRAY< int > &  maxes 
)

Definition at line 695 of file networkio.cpp.

696  {
697  ASSERT_HOST(!int_mode_);
698  Zero();
699  StrideMap::Index index(fwd.stride_map_);
700  do {
701  int t = index.t();
702  const int* max_line = maxes[t];
703  const float* fwd_line = fwd.f_[t];
704  int num_features = fwd.f_.dim2();
705  for (int i = 0; i < num_features; ++i) {
706  f_[max_line[i]][i] = fwd_line[i];
707  }
708  } while (index.Increment());
709 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim2() const
Definition: matrix.h:202
const inT8 * i(int t) const
Definition: networkio.h:123

◆ MaxpoolTimeStep()

void tesseract::NetworkIO::MaxpoolTimeStep ( int  dest_t,
const NetworkIO src,
int  src_t,
int max_line 
)

Definition at line 668 of file networkio.cpp.

669  {
670  ASSERT_HOST(int_mode_ == src.int_mode_);
671  if (int_mode_) {
672  int dim = i_.dim2();
673  inT8* dest_line = i_[dest_t];
674  const inT8* src_line = src.i_[src_t];
675  for (int i = 0; i < dim; ++i) {
676  if (dest_line[i] < src_line[i]) {
677  dest_line[i] = src_line[i];
678  max_line[i] = src_t;
679  }
680  }
681  } else {
682  int dim = f_.dim2();
683  float* dest_line = f_[dest_t];
684  const float* src_line = src.f_[src_t];
685  for (int i = 0; i < dim; ++i) {
686  if (dest_line[i] < src_line[i]) {
687  dest_line[i] = src_line[i];
688  max_line[i] = src_t;
689  }
690  }
691  }
692 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
int dim2() const
Definition: matrix.h:202
int8_t inT8
Definition: host.h:34
const inT8 * i(int t) const
Definition: networkio.h:123

◆ MinOfMaxes()

float tesseract::NetworkIO::MinOfMaxes ( ) const

Definition at line 712 of file networkio.cpp.

712  {
713  float min_max = 0.0f;
714  int width = Width();
715  int num_features = NumFeatures();
716  for (int t = 0; t < width; ++t) {
717  float max_value = -MAX_FLOAT32;
718  if (int_mode_) {
719  const inT8* column = i_[t];
720  for (int i = 0; i < num_features; ++i) {
721  if (column[i] > max_value) max_value = column[i];
722  }
723  } else {
724  const float* column = f_[t];
725  for (int i = 0; i < num_features; ++i) {
726  if (column[i] > max_value) max_value = column[i];
727  }
728  }
729  if (t == 0 || max_value < min_max) min_max = max_value;
730  }
731  return min_max;
732 }
int Width() const
Definition: networkio.h:107
#define MAX_FLOAT32
Definition: host.h:66
int8_t inT8
Definition: host.h:34
int NumFeatures() const
Definition: networkio.h:111
const inT8 * i(int t) const
Definition: networkio.h:123

◆ mutable_float_array()

GENERIC_2D_ARRAY<float>* tesseract::NetworkIO::mutable_float_array ( )
inline

Definition at line 140 of file networkio.h.

140 { return &f_; }

◆ NumFeatures()

int tesseract::NetworkIO::NumFeatures ( ) const
inline

Definition at line 111 of file networkio.h.

111  {
112  return int_mode_ ? i_.dim2() : f_.dim2();
113  }
int dim2() const
Definition: matrix.h:202

◆ PositionOfBestMatch()

int tesseract::NetworkIO::PositionOfBestMatch ( const GenericVector< int > &  labels,
int  start,
int  end 
) const

Definition at line 507 of file networkio.cpp.

508  {
509  int length = labels.size();
510  int last_start = end - length;
511  int best_start = -1;
512  double best_score = 0.0;
513  for (int s = start; s <= last_start; ++s) {
514  double score = ScoreOfLabels(labels, s);
515  if (score > best_score || best_start < 0) {
516  best_score = score;
517  best_start = s;
518  }
519  }
520  return best_start;
521 }
double ScoreOfLabels(const GenericVector< int > &labels, int start) const
Definition: networkio.cpp:525
int size() const
Definition: genericvector.h:72

◆ Print()

void tesseract::NetworkIO::Print ( int  num) const

Definition at line 366 of file networkio.cpp.

366  {
367  int num_features = NumFeatures();
368  for (int y = 0; y < num_features; ++y) {
369  for (int t = 0; t < Width(); ++t) {
370  if (num == 0 || t < num || t + num >= Width()) {
371  if (int_mode_) {
372  tprintf(" %g", static_cast<float>(i_[t][y]) / MAX_INT8);
373  } else {
374  tprintf(" %g", f_[t][y]);
375  }
376  }
377  }
378  tprintf("\n");
379  }
380 }
int Width() const
Definition: networkio.h:107
#define tprintf(...)
Definition: tprintf.h:31
int NumFeatures() const
Definition: networkio.h:111
#define MAX_INT8
Definition: host.h:60

◆ ProbToCertainty()

float tesseract::NetworkIO::ProbToCertainty ( float  prob)
static

Definition at line 568 of file networkio.cpp.

568  {
569  return prob > kMinProb ? log(prob) : kMinCertainty;
570 }
const float kMinProb
Definition: networkio.cpp:31
const float kMinCertainty
Definition: networkio.cpp:29

◆ Randomize()

void tesseract::NetworkIO::Randomize ( int  t,
int  offset,
int  num_features,
TRand randomizer 
)

Definition at line 416 of file networkio.cpp.

417  {
418  if (int_mode_) {
419  inT8* line = i_[t] + offset;
420  for (int i = 0; i < num_features; ++i)
421  line[i] = IntCastRounded(randomizer->SignedRand(MAX_INT8));
422  } else {
423  // float mode.
424  float* line = f_[t] + offset;
425  for (int i = 0; i < num_features; ++i)
426  line[i] = randomizer->SignedRand(1.0);
427  }
428 }
voidpf uLong offset
Definition: ioapi.h:42
int IntCastRounded(double x)
Definition: helpers.h:179
int8_t inT8
Definition: host.h:34
const inT8 * i(int t) const
Definition: networkio.h:123
#define MAX_INT8
Definition: host.h:60

◆ ReadTimeStep()

void tesseract::NetworkIO::ReadTimeStep ( int  t,
double *  output 
) const

Definition at line 598 of file networkio.cpp.

598  {
599  if (int_mode_) {
600  const inT8* line = i_[t];
601  for (int i = 0; i < i_.dim2(); ++i) {
602  output[i] = static_cast<double>(line[i]) / MAX_INT8;
603  }
604  } else {
605  const float* line = f_[t];
606  for (int i = 0; i < f_.dim2(); ++i) {
607  output[i] = static_cast<double>(line[i]);
608  }
609  }
610 }
int dim2() const
Definition: matrix.h:202
int8_t inT8
Definition: host.h:34
const inT8 * i(int t) const
Definition: networkio.h:123
#define MAX_INT8
Definition: host.h:60

◆ Resize()

void tesseract::NetworkIO::Resize ( const NetworkIO src,
int  num_features 
)
inline

Definition at line 45 of file networkio.h.

45  {
46  ResizeToMap(src.int_mode(), src.stride_map(), num_features);
47  }
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:45

◆ Resize2d()

void tesseract::NetworkIO::Resize2d ( bool  int_mode,
int  width,
int  num_features 
)

Definition at line 34 of file networkio.cpp.

34  {
35  stride_map_ = StrideMap();
36  int_mode_ = int_mode;
37  if (int_mode_) {
38  i_.ResizeNoInit(width, num_features);
39  } else {
40  f_.ResizeNoInit(width, num_features);
41  }
42 }
bool int_mode() const
Definition: networkio.h:127
void ResizeNoInit(int size1, int size2)
Definition: matrix.h:86

◆ ResizeFloat()

void tesseract::NetworkIO::ResizeFloat ( const NetworkIO src,
int  num_features 
)
inline

Definition at line 52 of file networkio.h.

52  {
53  ResizeToMap(false, src.stride_map(), num_features);
54  }
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:45

◆ ResizeScaled()

void tesseract::NetworkIO::ResizeScaled ( const NetworkIO src,
int  x_scale,
int  y_scale,
int  num_features 
)

Definition at line 62 of file networkio.cpp.

63  {
64  StrideMap stride_map = src.stride_map_;
65  stride_map.ScaleXY(x_scale, y_scale);
66  ResizeToMap(src.int_mode_, stride_map, num_features);
67 }
const StrideMap & stride_map() const
Definition: networkio.h:133
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:45
void ScaleXY(int x_factor, int y_factor)
Definition: stridemap.cpp:144

◆ ResizeToMap()

void tesseract::NetworkIO::ResizeToMap ( bool  int_mode,
const StrideMap stride_map,
int  num_features 
)

Definition at line 45 of file networkio.cpp.

46  {
47  // If this assert fails, it most likely got here through an uninitialized
48  // scratch element, ie call NetworkScratch::IO::Resizexxx() not
49  // NetworkIO::Resizexxx()!!
50  ASSERT_HOST(this != NULL);
51  stride_map_ = stride_map;
52  int_mode_ = int_mode;
53  if (int_mode_) {
54  i_.ResizeNoInit(stride_map.Width(), num_features);
55  } else {
56  f_.ResizeNoInit(stride_map.Width(), num_features);
57  }
59 }
int Width() const
Definition: stridemap.h:118
void ZeroInvalidElements()
Definition: networkio.cpp:88
bool int_mode() const
Definition: networkio.h:127
#define ASSERT_HOST(x)
Definition: errcode.h:84
void ResizeNoInit(int size1, int size2)
Definition: matrix.h:86
const StrideMap & stride_map() const
Definition: networkio.h:133

◆ ResizeXTo1()

void tesseract::NetworkIO::ResizeXTo1 ( const NetworkIO src,
int  num_features 
)

Definition at line 70 of file networkio.cpp.

70  {
71  StrideMap stride_map = src.stride_map_;
72  stride_map.ReduceWidthTo1();
73  ResizeToMap(src.int_mode_, stride_map, num_features);
74 }
const StrideMap & stride_map() const
Definition: networkio.h:133
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:45

◆ ScaleFloatBy()

void tesseract::NetworkIO::ScaleFloatBy ( float  factor)
inline

Definition at line 234 of file networkio.h.

234 { f_ *= factor; }

◆ ScoreOfLabels()

double tesseract::NetworkIO::ScoreOfLabels ( const GenericVector< int > &  labels,
int  start 
) const

Definition at line 525 of file networkio.cpp.

526  {
527  int length = labels.size();
528  double score = 0.0;
529  for (int i = 0; i < length; ++i) {
530  score += f_(start + i, labels[i]);
531  }
532  return score;
533 }
int size() const
Definition: genericvector.h:72
const inT8 * i(int t) const
Definition: networkio.h:123

◆ ScoresOverRange()

void tesseract::NetworkIO::ScoresOverRange ( int  t_start,
int  t_end,
int  choice,
int  null_ch,
float *  rating,
float *  certainty 
) const

Definition at line 450 of file networkio.cpp.

451  {
452  ASSERT_HOST(!int_mode_);
453  *rating = 0.0f;
454  *certainty = 0.0f;
455  if (t_end <= t_start || t_end <= 0) return;
456  float ratings[3] = {0.0f, 0.0f, 0.0f};
457  float certs[3] = {0.0f, 0.0f, 0.0f};
458  for (int t = t_start; t < t_end; ++t) {
459  const float* line = f_[t];
460  float score = ProbToCertainty(line[choice]);
461  float zero = ProbToCertainty(line[null_ch]);
462  if (t == t_start) {
463  ratings[2] = MAX_FLOAT32;
464  ratings[1] = -score;
465  certs[1] = score;
466  } else {
467  for (int i = 2; i >= 1; --i) {
468  if (ratings[i] > ratings[i - 1]) {
469  ratings[i] = ratings[i - 1];
470  certs[i] = certs[i - 1];
471  }
472  }
473  ratings[2] -= zero;
474  if (zero < certs[2]) certs[2] = zero;
475  ratings[1] -= score;
476  if (score < certs[1]) certs[1] = score;
477  }
478  ratings[0] -= zero;
479  if (zero < certs[0]) certs[0] = zero;
480  }
481  int best_i = ratings[2] < ratings[1] ? 2 : 1;
482  *rating = ratings[best_i] + t_end - t_start;
483  *certainty = certs[best_i];
484 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
#define MAX_FLOAT32
Definition: host.h:66
static float ProbToCertainty(float prob)
Definition: networkio.cpp:568
const inT8 * i(int t) const
Definition: networkio.h:123

◆ set_int_mode()

void tesseract::NetworkIO::set_int_mode ( bool  is_quantized)
inline

Definition at line 130 of file networkio.h.

130  {
131  int_mode_ = is_quantized;
132  }

◆ set_stride_map()

void tesseract::NetworkIO::set_stride_map ( const StrideMap map)
inline

Definition at line 136 of file networkio.h.

136  {
137  stride_map_ = map;
138  }

◆ SetActivations()

void tesseract::NetworkIO::SetActivations ( int  t,
int  label,
float  ok_score 
)

Definition at line 537 of file networkio.cpp.

537  {
538  ASSERT_HOST(!int_mode_);
539  int num_classes = NumFeatures();
540  float bad_score = (1.0f - ok_score) / (num_classes - 1);
541  float* targets = f_[t];
542  for (int i = 0; i < num_classes; ++i)
543  targets[i] = bad_score;
544  targets[label] = ok_score;
545 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
int NumFeatures() const
Definition: networkio.h:111
const inT8 * i(int t) const
Definition: networkio.h:123

◆ SetPixel()

void tesseract::NetworkIO::SetPixel ( int  t,
int  f,
int  pixel,
float  black,
float  contrast 
)

Definition at line 275 of file networkio.cpp.

275  {
276  float float_pixel = (pixel - black) / contrast - 1.0f;
277  if (int_mode_) {
278  i_[t][f] = ClipToRange(IntCastRounded((MAX_INT8 + 1) * float_pixel),
279  -MAX_INT8, MAX_INT8);
280  } else {
281  f_[t][f] = float_pixel;
282  }
283 }
float * f(int t)
Definition: networkio.h:115
int IntCastRounded(double x)
Definition: helpers.h:179
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
#define MAX_INT8
Definition: host.h:60

◆ stride_map()

const StrideMap& tesseract::NetworkIO::stride_map ( ) const
inline

Definition at line 133 of file networkio.h.

133  {
134  return stride_map_;
135  }

◆ SubtractAllFromFloat()

void tesseract::NetworkIO::SubtractAllFromFloat ( const NetworkIO src)

Definition at line 824 of file networkio.cpp.

824  {
825  ASSERT_HOST(!int_mode_);
826  ASSERT_HOST(!src.int_mode_);
827  f_ -= src.f_;
828 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ ToPix()

Pix * tesseract::NetworkIO::ToPix ( ) const

Definition at line 286 of file networkio.cpp.

286  {
287  // Count the width of the image, and find the max multiplication factor.
288  int im_width = stride_map_.Size(FD_WIDTH);
289  int im_height = stride_map_.Size(FD_HEIGHT);
290  int num_features = NumFeatures();
291  int feature_factor = 1;
292  if (num_features == 3) {
293  // Special hack for color.
294  num_features = 1;
295  feature_factor = 3;
296  }
297  Pix* pix = pixCreate(im_width, im_height * num_features, 32);
298  StrideMap::Index index(stride_map_);
299  do {
300  int im_x = index.index(FD_WIDTH);
301  int top_im_y = index.index(FD_HEIGHT);
302  int im_y = top_im_y;
303  int t = index.t();
304  if (int_mode_) {
305  const inT8* features = i_[t];
306  for (int y = 0; y < num_features; ++y, im_y += im_height) {
307  int pixel = features[y * feature_factor];
308  // 1 or 2 features use greyscale.
309  int red = ClipToRange(pixel + 128, 0, 255);
310  int green = red, blue = red;
311  if (feature_factor == 3) {
312  // With 3 features assume RGB color.
313  green = ClipToRange(features[y * feature_factor + 1] + 128, 0, 255);
314  blue = ClipToRange(features[y * feature_factor + 2] + 128, 0, 255);
315  } else if (num_features > 3) {
316  // More than 3 features use false yellow/blue color, assuming a signed
317  // input in the range [-1,1].
318  red = abs(pixel) * 2;
319  if (pixel >= 0) {
320  green = red;
321  blue = 0;
322  } else {
323  blue = red;
324  green = red = 0;
325  }
326  }
327  pixSetPixel(pix, im_x, im_y, (red << L_RED_SHIFT) |
328  (green << L_GREEN_SHIFT) |
329  (blue << L_BLUE_SHIFT));
330  }
331  } else {
332  const float* features = f_[t];
333  for (int y = 0; y < num_features; ++y, im_y += im_height) {
334  float pixel = features[y * feature_factor];
335  // 1 or 2 features use greyscale.
336  int red = ClipToRange(IntCastRounded((pixel + 1.0f) * 127.5f), 0, 255);
337  int green = red, blue = red;
338  if (feature_factor == 3) {
339  // With 3 features assume RGB color.
340  pixel = features[y * feature_factor + 1];
341  green = ClipToRange(IntCastRounded((pixel + 1.0f) * 127.5f), 0, 255);
342  pixel = features[y * feature_factor + 2];
343  blue = ClipToRange(IntCastRounded((pixel + 1.0f) * 127.5f), 0, 255);
344  } else if (num_features > 3) {
345  // More than 3 features use false yellow/blue color, assuming a signed
346  // input in the range [-1,1].
347  red = ClipToRange(IntCastRounded(fabs(pixel) * 255), 0, 255);
348  if (pixel >= 0) {
349  green = red;
350  blue = 0;
351  } else {
352  blue = red;
353  green = red = 0;
354  }
355  }
356  pixSetPixel(pix, im_x, im_y, (red << L_RED_SHIFT) |
357  (green << L_GREEN_SHIFT) |
358  (blue << L_BLUE_SHIFT));
359  }
360  }
361  } while (index.Increment());
362  return pix;
363 }
float * f(int t)
Definition: networkio.h:115
int IntCastRounded(double x)
Definition: helpers.h:179
int Size(FlexDimensions dimension) const
Definition: stridemap.h:116
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
int8_t inT8
Definition: host.h:34
int NumFeatures() const
Definition: networkio.h:111
const char features[]
Definition: feature_tests.c:2

◆ Transpose()

void tesseract::NetworkIO::Transpose ( TransposedArray dest) const

Definition at line 964 of file networkio.cpp.

964  {
965  int width = Width();
966  dest->ResizeNoInit(NumFeatures(), width);
967  for (int t = 0; t < width; ++t) dest->WriteStrided(t, f_[t]);
968 }
int Width() const
Definition: networkio.h:107
int NumFeatures() const
Definition: networkio.h:111

◆ Width()

int tesseract::NetworkIO::Width ( ) const
inline

Definition at line 107 of file networkio.h.

107  {
108  return int_mode_ ? i_.dim1() : f_.dim1();
109  }
int dim1() const
Definition: matrix.h:201

◆ WriteTimeStep()

void tesseract::NetworkIO::WriteTimeStep ( int  t,
const double *  input 
)

Definition at line 645 of file networkio.cpp.

645  {
646  WriteTimeStepPart(t, 0, NumFeatures(), input);
647 }
int NumFeatures() const
Definition: networkio.h:111
void WriteTimeStepPart(int t, int offset, int num_features, const double *input)
Definition: networkio.cpp:651

◆ WriteTimeStepPart()

void tesseract::NetworkIO::WriteTimeStepPart ( int  t,
int  offset,
int  num_features,
const double *  input 
)

Definition at line 651 of file networkio.cpp.

652  {
653  if (int_mode_) {
654  inT8* line = i_[t] + offset;
655  for (int i = 0; i < num_features; ++i) {
656  line[i] = ClipToRange(IntCastRounded(input[i] * MAX_INT8),
657  -MAX_INT8, MAX_INT8);
658  }
659  } else {
660  float* line = f_[t] + offset;
661  for (int i = 0; i < num_features; ++i) {
662  line[i] = static_cast<float>(input[i]);
663  }
664  }
665 }
voidpf uLong offset
Definition: ioapi.h:42
int IntCastRounded(double x)
Definition: helpers.h:179
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
int8_t inT8
Definition: host.h:34
const inT8 * i(int t) const
Definition: networkio.h:123
#define MAX_INT8
Definition: host.h:60

◆ Zero()

void tesseract::NetworkIO::Zero ( )

Definition at line 77 of file networkio.cpp.

77  {
78  int width = Width();
79  // Zero out the everything. Column-by-column in case it is aligned.
80  for (int t = 0; t < width; ++t) {
81  ZeroTimeStep(t);
82  }
83 }
int Width() const
Definition: networkio.h:107
void ZeroTimeStep(int t)
Definition: networkio.h:148

◆ ZeroInvalidElements()

void tesseract::NetworkIO::ZeroInvalidElements ( )

Definition at line 88 of file networkio.cpp.

88  {
89  int num_features = NumFeatures();
90  int full_width = stride_map_.Size(FD_WIDTH);
91  int full_height = stride_map_.Size(FD_HEIGHT);
92  StrideMap::Index b_index(stride_map_);
93  do {
94  int end_x = b_index.MaxIndexOfDim(FD_WIDTH) + 1;
95  if (end_x < full_width) {
96  // The width is small, so fill for every valid y.
97  StrideMap::Index y_index(b_index);
98  int fill_size = num_features * (full_width - end_x);
99  do {
100  StrideMap::Index z_index(y_index);
101  z_index.AddOffset(end_x, FD_WIDTH);
102  if (int_mode_) {
103  ZeroVector(fill_size, i_[z_index.t()]);
104  } else {
105  ZeroVector(fill_size, f_[z_index.t()]);
106  }
107  } while (y_index.AddOffset(1, FD_HEIGHT));
108  }
109  int end_y = b_index.MaxIndexOfDim(FD_HEIGHT) + 1;
110  if (end_y < full_height) {
111  // The height is small, so fill in the space in one go.
112  StrideMap::Index y_index(b_index);
113  y_index.AddOffset(end_y, FD_HEIGHT);
114  int fill_size = num_features * full_width * (full_height - end_y);
115  if (int_mode_) {
116  ZeroVector(fill_size, i_[y_index.t()]);
117  } else {
118  ZeroVector(fill_size, f_[y_index.t()]);
119  }
120  }
121  } while (b_index.AddOffset(1, FD_BATCH));
122 }
int Size(FlexDimensions dimension) const
Definition: stridemap.h:116
int NumFeatures() const
Definition: networkio.h:111
void ZeroVector(int n, T *vec)
Definition: functions.h:219

◆ ZeroTimeStep()

void tesseract::NetworkIO::ZeroTimeStep ( int  t)
inline

Definition at line 148 of file networkio.h.

148 { ZeroTimeStepGeneral(t, 0, NumFeatures()); }
void ZeroTimeStepGeneral(int t, int offset, int num_features)
Definition: networkio.cpp:407
int NumFeatures() const
Definition: networkio.h:111

◆ ZeroTimeStepGeneral()

void tesseract::NetworkIO::ZeroTimeStepGeneral ( int  t,
int  offset,
int  num_features 
)

Definition at line 407 of file networkio.cpp.

407  {
408  if (int_mode_) {
409  ZeroVector(num_features, i_[t] + offset);
410  } else {
411  ZeroVector(num_features, f_[t] + offset);
412  }
413 }
voidpf uLong offset
Definition: ioapi.h:42
void ZeroVector(int n, T *vec)
Definition: functions.h:219

The documentation for this class was generated from the following files: