tesseract v5.3.3.20231005
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 Image pix, TRand *randomizer)
 
void FromPixes (const StaticShape &shape, const std::vector< Image > &pixes, TRand *randomizer)
 
void Copy2DImage (int batch, Image pix, float black, float contrast, TRand *randomizer)
 
void Copy1DGreyImage (int batch, Image pix, float black, float contrast, TRand *randomizer)
 
void SetPixel (int t, int f, int pixel, float black, float contrast)
 
Image ToPix () const
 
void Print (int num) const
 
int Width () const
 
int NumFeatures () const
 
float * f (int t)
 
const float * f (int t) const
 
const int8_t * i (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 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 std::vector< int > &labels, int start, int end) const
 
TFloat ScoreOfLabels (const std::vector< 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, TFloat *output) const
 
void AddTimeStep (int t, TFloat *inout) const
 
void AddTimeStepPart (int t, int offset, int num_features, float *inout) const
 
void WriteTimeStep (int t, const TFloat *input)
 
void WriteTimeStepPart (int t, int offset, int num_features, const TFloat *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, TFloat *product)
 
template<class Func >
void FuncMultiply3 (int u_t, const NetworkIO &v_io, int v_t, const TFloat *w, TFloat *product) const
 
template<class Func >
void FuncMultiply3Add (const NetworkIO &v_io, int t, const TFloat *w, TFloat *product) const
 
template<class Func1 , class Func2 >
void Func2Multiply3 (const NetworkIO &v_io, int t, const TFloat *w, TFloat *product) const
 

Static Public Member Functions

static float ProbToCertainty (float prob)
 

Detailed Description

Definition at line 38 of file networkio.h.

Constructor & Destructor Documentation

◆ NetworkIO()

tesseract::NetworkIO::NetworkIO ( )
inline

Definition at line 40 of file networkio.h.

40: int_mode_(false) {}

Member Function Documentation

◆ AddAllToFloat()

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

Definition at line 831 of file networkio.cpp.

831 {
832 ASSERT_HOST(!int_mode_);
833 ASSERT_HOST(!src.int_mode_);
834 f_ += src.f_;
835}
#define ASSERT_HOST(x)
Definition: errcode.h:54

◆ AddTimeStep()

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

Definition at line 625 of file networkio.cpp.

625 {
626 int num_features = NumFeatures();
627 if (int_mode_) {
628 const int8_t *line = i_[t];
629 for (int i = 0; i < num_features; ++i) {
630 inout[i] += static_cast<TFloat>(line[i]) / INT8_MAX;
631 }
632 } else {
633 const float *line = f_[t];
634 for (int i = 0; i < num_features; ++i) {
635 inout[i] += line[i];
636 }
637 }
638}
double TFloat
Definition: tesstypes.h:39
const int8_t * i(int t) const
Definition: networkio.h:118
int NumFeatures() const
Definition: networkio.h:106

◆ AddTimeStepPart()

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

Definition at line 641 of file networkio.cpp.

641 {
642 if (int_mode_) {
643 const int8_t *line = i_[t] + offset;
644 for (int i = 0; i < num_features; ++i) {
645 inout[i] += static_cast<float>(line[i]) / INT8_MAX;
646 }
647 } else {
648 const float *line = f_[t] + offset;
649 for (int i = 0; i < num_features; ++i) {
650 inout[i] += line[i];
651 }
652 }
653}

◆ AnySuspiciousTruth()

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

Definition at line 591 of file networkio.cpp.

591 {
592 int num_features = NumFeatures();
593 for (int t = 0; t < Width(); ++t) {
594 const float *features = f_[t];
595 for (int y = 0; y < num_features; ++y) {
596 float grad = features[y];
597 if (grad < -confidence_thr) {
598 // Correcting strong output. Check for movement.
599 if ((t == 0 || f_[t - 1][y] < confidence_thr / 2) &&
600 (t + 1 == Width() || f_[t + 1][y] < confidence_thr / 2)) {
601 return true; // No strong positive on either side.
602 }
603 }
604 }
605 }
606 return false;
607}
const double y
int Width() const
Definition: networkio.h:102

◆ 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 432 of file networkio.cpp.

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

◆ BestLabel() [1/2]

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

Definition at line 165 of file networkio.h.

165 {
166 return BestLabel(t, -1, -1, score);
167 }
int BestLabel(int t, float *score) const
Definition: networkio.h:165

◆ BestLabel() [2/2]

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

Definition at line 501 of file networkio.cpp.

501 {
502 ASSERT_HOST(!int_mode_);
503 int best_index = -1;
504 float best_score = -FLT_MAX;
505 const float *line = f_[t];
506 for (int i = 0; i < f_.dim2(); ++i) {
507 if (line[i] > best_score && i != not_this && i != not_that) {
508 best_score = line[i];
509 best_index = i;
510 }
511 }
512 if (score != nullptr) {
513 *score = ProbToCertainty(best_score);
514 }
515 return best_index;
516}
static float ProbToCertainty(float prob)
Definition: networkio.cpp:580

◆ ClipVector()

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

Definition at line 980 of file networkio.cpp.

980 {
981 ASSERT_HOST(!int_mode_);
982 float *v = f_[t];
983 int dim = f_.dim2();
984 for (int i = 0; i < dim; ++i) {
985 v[i] = ClipToRange<float>(v[i], -range, range);
986 }
987}

◆ CombineOutputs()

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

Definition at line 749 of file networkio.cpp.

749 {
750 int no = base_output.NumFeatures();
751 ASSERT_HOST(combiner_output.NumFeatures() == no + 1);
752 Resize(base_output, no);
753 int width = Width();
754 if (int_mode_) {
755 // Number of outputs from base and final result.
756 for (int t = 0; t < width; ++t) {
757 int8_t *out_line = i_[t];
758 const int8_t *base_line = base_output.i_[t];
759 const int8_t *comb_line = combiner_output.i_[t];
760 float base_weight = static_cast<float>(comb_line[no]) / INT8_MAX;
761 float boost_weight = 1.0f - base_weight;
762 for (int i = 0; i < no; ++i) {
763 out_line[i] = IntCastRounded(base_line[i] * base_weight + comb_line[i] * boost_weight);
764 }
765 }
766 } else {
767 for (int t = 0; t < width; ++t) {
768 float *out_line = f_[t];
769 const float *base_line = base_output.f_[t];
770 const float *comb_line = combiner_output.f_[t];
771 float base_weight = comb_line[no];
772 float boost_weight = 1.0f - base_weight;
773 for (int i = 0; i < no; ++i) {
774 out_line[i] = base_line[i] * base_weight + comb_line[i] * boost_weight;
775 }
776 }
777 }
778}
int IntCastRounded(double x)
Definition: helpers.h:170
void Resize(const NetworkIO &src, int num_features)
Definition: networkio.h:44

◆ ComputeCombinerDeltas()

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

Definition at line 781 of file networkio.cpp.

781 {
782 ASSERT_HOST(!int_mode_);
783 // Compute the deltas for the combiner.
784 int width = Width();
785 int no = NumFeatures() - 1;
786 ASSERT_HOST(fwd_deltas.NumFeatures() == no);
787 ASSERT_HOST(base_output.NumFeatures() == no);
788 // Number of outputs from base and final result.
789 for (int t = 0; t < width; ++t) {
790 const float *delta_line = fwd_deltas.f_[t];
791 const float *base_line = base_output.f_[t];
792 float *comb_line = f_[t];
793 float base_weight = comb_line[no];
794 float boost_weight = 1.0f - base_weight;
795 float max_base_delta = 0.0;
796 for (int i = 0; i < no; ++i) {
797 // What did the combiner actually produce?
798 float output = base_line[i] * base_weight + comb_line[i] * boost_weight;
799 // Reconstruct the target from the delta.
800 float comb_target = delta_line[i] + output;
801 comb_line[i] = comb_target - comb_line[i];
802 float base_delta = std::fabs(comb_target - base_line[i]);
803 if (base_delta > max_base_delta) {
804 max_base_delta = base_delta;
805 }
806 }
807 if (max_base_delta >= 0.5) {
808 // The base network got it wrong. The combiner should output the right
809 // answer and 0 for the base network.
810 comb_line[no] = 0.0 - base_weight;
811 } else {
812 // The base network was right. The combiner should flag that.
813 for (int i = 0; i < no; ++i) {
814 // All other targets are 0.
815 if (comb_line[i] > 0.0) {
816 comb_line[i] -= 1.0;
817 }
818 }
819 comb_line[no] = 1.0 - base_weight;
820 }
821 }
822}

◆ Copy1DGreyImage()

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

Definition at line 257 of file networkio.cpp.

258 {
259 int width = pixGetWidth(pix);
260 int height = pixGetHeight(pix);
261 ASSERT_HOST(height == NumFeatures());
262 int wpl = pixGetWpl(pix);
263 StrideMap::Index index(stride_map_);
264 index.AddOffset(batch, FD_BATCH);
265 int t = index.t();
266 int target_width = stride_map_.Size(FD_WIDTH);
267 if (width > target_width) {
268 width = target_width;
269 }
270 int x;
271 for (x = 0; x < width; ++x, ++t) {
272 for (int y = 0; y < height; ++y) {
273 uint32_t *line = pixGetData(pix) + wpl * y;
274 int pixel = GET_DATA_BYTE(line, x);
275 SetPixel(t, y, pixel, black, contrast);
276 }
277 }
278 for (; x < target_width; ++x) {
279 Randomize(t++, 0, height, randomizer);
280 }
281}
@ FD_WIDTH
Definition: stridemap.h:35
@ FD_BATCH
Definition: stridemap.h:33
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:290
int Size(FlexDimensions dimension) const
Definition: stridemap.h:119

◆ Copy2DImage()

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

Definition at line 216 of file networkio.cpp.

216 {
217 int width = pixGetWidth(pix);
218 int height = pixGetHeight(pix);
219 int wpl = pixGetWpl(pix);
220 StrideMap::Index index(stride_map_);
221 index.AddOffset(batch, FD_BATCH);
222 int t = index.t();
223 int target_height = stride_map_.Size(FD_HEIGHT);
224 int target_width = stride_map_.Size(FD_WIDTH);
225 int num_features = NumFeatures();
226 bool color = num_features == 3;
227 if (width > target_width) {
228 width = target_width;
229 }
230 uint32_t *line = pixGetData(pix);
231 for (int y = 0; y < target_height; ++y, line += wpl) {
232 int x = 0;
233 if (y < height) {
234 for (x = 0; x < width; ++x, ++t) {
235 if (color) {
236 int f = 0;
237 for (int c = COLOR_RED; c <= COLOR_BLUE; ++c) {
238 int pixel = GET_DATA_BYTE(line + x, c);
239 SetPixel(t, f++, pixel, black, contrast);
240 }
241 } else {
242 int pixel = GET_DATA_BYTE(line, x);
243 SetPixel(t, 0, pixel, black, contrast);
244 }
245 }
246 }
247 for (; x < target_width; ++x) {
248 Randomize(t++, 0, num_features, randomizer);
249 }
250 }
251}
@ FD_HEIGHT
Definition: stridemap.h:34
float * f(int t)
Definition: networkio.h:110

◆ CopyAll()

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

Definition at line 825 of file networkio.cpp.

825 {
826 ASSERT_HOST(src.int_mode_ == int_mode_);
827 f_ = src.f_;
828}

◆ CopyPacking()

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

Definition at line 929 of file networkio.cpp.

929 {
930 ASSERT_HOST(int_mode_ == src.int_mode_);
931 int width = src.Width();
932 ASSERT_HOST(width <= Width());
933 int num_features = src.NumFeatures();
934 ASSERT_HOST(num_features + feature_offset <= NumFeatures());
935 if (int_mode_) {
936 for (int t = 0; t < width; ++t) {
937 memcpy(i_[t] + feature_offset, src.i_[t], num_features * sizeof(i_[t][0]));
938 }
939 for (int t = width; t < i_.dim1(); ++t) {
940 memset(i_[t], 0, num_features * sizeof(i_[t][0]));
941 }
942 } else {
943 for (int t = 0; t < width; ++t) {
944 memcpy(f_[t] + feature_offset, src.f_[t], num_features * sizeof(f_[t][0]));
945 }
946 for (int t = width; t < f_.dim1(); ++t) {
947 memset(f_[t], 0, num_features * sizeof(f_[t][0]));
948 }
949 }
950 return num_features + feature_offset;
951}

◆ CopyTimeStepFrom()

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

Definition at line 395 of file networkio.cpp.

395 {
396 ASSERT_HOST(int_mode_ == src.int_mode_);
397 if (int_mode_) {
398 memcpy(i_[dest_t], src.i_[src_t], i_.dim2() * sizeof(i_[0][0]));
399 } else {
400 memcpy(f_[dest_t], src.f_[src_t], f_.dim2() * sizeof(f_[0][0]));
401 }
402}

◆ 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 405 of file networkio.cpp.

406 {
407 ASSERT_HOST(int_mode_ == src.int_mode_);
408 if (int_mode_) {
409 memcpy(i_[dest_t] + dest_offset, src.i_[src_t] + src_offset, num_features * sizeof(i_[0][0]));
410 } else {
411 memcpy(f_[dest_t] + dest_offset, src.f_[src_t] + src_offset, num_features * sizeof(f_[0][0]));
412 }
413}

◆ CopyUnpacking()

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

Definition at line 955 of file networkio.cpp.

955 {
956 Resize(src, num_features);
957 int width = src.Width();
958 ASSERT_HOST(num_features + feature_offset <= src.NumFeatures());
959 if (int_mode_) {
960 for (int t = 0; t < width; ++t) {
961 memcpy(i_[t], src.i_[t] + feature_offset, num_features * sizeof(i_[t][0]));
962 }
963 } else {
964 for (int t = 0; t < width; ++t) {
965 memcpy(f_[t], src.f_[t] + feature_offset, num_features * sizeof(f_[t][0]));
966 }
967 }
968}

◆ CopyWithNormalization()

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

Definition at line 845 of file networkio.cpp.

845 {
846 ASSERT_HOST(!int_mode_);
847 ASSERT_HOST(!src.int_mode_);
848 ASSERT_HOST(!scale.int_mode_);
849 float src_max = src.f_.MaxAbs();
850 ASSERT_HOST(std::isfinite(src_max));
851 float scale_max = scale.f_.MaxAbs();
852 ASSERT_HOST(std::isfinite(scale_max));
853 if (src_max > 0.0f) {
854 float factor = scale_max / src_max;
855 for (int t = 0; t < src.Width(); ++t) {
856 const float *src_ptr = src.f_[t];
857 float *dest_ptr = f_[t];
858 for (int i = 0; i < src.f_.dim2(); ++i) {
859 dest_ptr[i] = src_ptr[i] * factor;
860 }
861 }
862 } else {
863 f_.Clear();
864 }
865}

◆ CopyWithXReversal()

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

Definition at line 888 of file networkio.cpp.

888 {
889 int num_features = src.NumFeatures();
890 Resize(src, num_features);
891 StrideMap::Index b_index(src.stride_map_);
892 do {
893 StrideMap::Index y_index(b_index);
894 do {
895 StrideMap::Index fwd_index(y_index);
896 StrideMap::Index rev_index(y_index);
897 rev_index.AddOffset(rev_index.MaxIndexOfDim(FD_WIDTH), FD_WIDTH);
898 do {
899 CopyTimeStepFrom(rev_index.t(), src, fwd_index.t());
900 } while (fwd_index.AddOffset(1, FD_WIDTH) && rev_index.AddOffset(-1, FD_WIDTH));
901 } while (y_index.AddOffset(1, FD_HEIGHT));
902 } while (b_index.AddOffset(1, FD_BATCH));
903}
void CopyTimeStepFrom(int dest_t, const NetworkIO &src, int src_t)
Definition: networkio.cpp:395

◆ CopyWithXYTranspose()

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

Definition at line 906 of file networkio.cpp.

906 {
907 int num_features = src.NumFeatures();
908 stride_map_ = src.stride_map_;
909 stride_map_.TransposeXY();
910 ResizeToMap(src.int_mode(), stride_map_, num_features);
911 StrideMap::Index src_b_index(src.stride_map_);
912 StrideMap::Index dest_b_index(stride_map_);
913 do {
914 StrideMap::Index src_y_index(src_b_index);
915 StrideMap::Index dest_x_index(dest_b_index);
916 do {
917 StrideMap::Index src_x_index(src_y_index);
918 StrideMap::Index dest_y_index(dest_x_index);
919 do {
920 CopyTimeStepFrom(dest_y_index.t(), src, src_x_index.t());
921 } while (src_x_index.AddOffset(1, FD_WIDTH) && dest_y_index.AddOffset(1, FD_HEIGHT));
922 } while (src_y_index.AddOffset(1, FD_HEIGHT) && dest_x_index.AddOffset(1, FD_WIDTH));
923 } while (src_b_index.AddOffset(1, FD_BATCH) && dest_b_index.AddOffset(1, FD_BATCH));
924}
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:46

◆ CopyWithYReversal()

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

Definition at line 868 of file networkio.cpp.

868 {
869 int num_features = src.NumFeatures();
870 Resize(src, num_features);
871 StrideMap::Index b_index(src.stride_map_);
872 do {
873 int width = b_index.MaxIndexOfDim(FD_WIDTH) + 1;
874 StrideMap::Index fwd_index(b_index);
875 StrideMap::Index rev_index(b_index);
876 rev_index.AddOffset(rev_index.MaxIndexOfDim(FD_HEIGHT), FD_HEIGHT);
877 do {
878 int fwd_t = fwd_index.t();
879 int rev_t = rev_index.t();
880 for (int x = 0; x < width; ++x) {
881 CopyTimeStepFrom(rev_t++, src, fwd_t++);
882 }
883 } while (fwd_index.AddOffset(1, FD_HEIGHT) && rev_index.AddOffset(-1, FD_HEIGHT));
884 } while (b_index.AddOffset(1, FD_BATCH));
885}

◆ EnsureBestLabel()

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

Definition at line 561 of file networkio.cpp.

561 {
562 ASSERT_HOST(!int_mode_);
563 if (BestLabel(t, nullptr) != label) {
564 // Output value needs enhancing. Third all the other elements and add the
565 // remainder to best_label.
566 int num_classes = NumFeatures();
567 float *targets = f_[t];
568 for (int c = 0; c < num_classes; ++c) {
569 if (c == label) {
570 targets[c] += (1.0 - targets[c]) * (2 / 3.0);
571 } else {
572 targets[c] /= 3.0;
573 }
574 }
575 }
576}

◆ f() [1/2]

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

Definition at line 110 of file networkio.h.

110 {
111 ASSERT_HOST(!int_mode_);
112 return f_[t];
113 }

◆ f() [2/2]

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

Definition at line 114 of file networkio.h.

114 {
115 ASSERT_HOST(!int_mode_);
116 return f_[t];
117 }

◆ float_array()

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

Definition at line 134 of file networkio.h.

134 {
135 return f_;
136 }

◆ FromPix()

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

Definition at line 163 of file networkio.cpp.

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

◆ FromPixes()

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

Definition at line 171 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 Image var_pix = pix;
178 int width = pixGetWidth(var_pix);
179 if (target_width != 0) {
180 width = target_width;
181 }
182 int height = pixGetHeight(var_pix);
183 if (target_height != 0) {
184 height = target_height;
185 }
186 h_w_pairs.emplace_back(height, width);
187 }
188 stride_map_.SetStride(h_w_pairs);
189 ResizeToMap(int_mode(), stride_map_, shape.depth());
190 // Iterate over the images again to copy the data.
191 for (size_t b = 0; b < pixes.size(); ++b) {
192 Image pix = pixes[b];
193 float black = 0.0f, white = 255.0f;
194 if (shape.depth() != 3) {
195 ComputeBlackWhite(pix, &black, &white);
196 }
197 float contrast = (white - black) / 2.0f;
198 if (contrast <= 0.0f) {
199 contrast = 1.0f;
200 }
201 if (shape.height() == 1) {
202 Copy1DGreyImage(b, pix, black, contrast, randomizer);
203 } else {
204 Copy2DImage(b, pix, black, contrast, randomizer);
205 }
206 }
207}
bool int_mode() const
Definition: networkio.h:122
void Copy1DGreyImage(int batch, Image pix, float black, float contrast, TRand *randomizer)
Definition: networkio.cpp:257
void Copy2DImage(int batch, Image pix, float black, float contrast, TRand *randomizer)
Definition: networkio.cpp:216
void SetStride(const std::vector< std::pair< int, int > > &h_w_pairs)
Definition: stridemap.cpp:131

◆ Func2Multiply3()

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

Definition at line 314 of file networkio.h.

314 {
315 ASSERT_HOST(!int_mode_);
316 ASSERT_HOST(!v_io.int_mode_);
317 Func1 f;
318 Func2 g;
319 const float *u = f_[t];
320 const float *v = v_io.f_[t];
321 int dim = f_.dim2();
322 for (int i = 0; i < dim; ++i) {
323 product[i] = f(u[i]) * g(v[i]) * w[i];
324 }
325 }

◆ FuncMultiply()

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

Definition at line 260 of file networkio.h.

260 {
261 Func f;
262 ASSERT_HOST(!int_mode_);
263 ASSERT_HOST(!v_io.int_mode_);
264 int dim = f_.dim2();
265 if (int_mode_) {
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;
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 }

◆ FuncMultiply3()

template<class Func >
void tesseract::NetworkIO::FuncMultiply3 ( int  u_t,
const NetworkIO v_io,
int  v_t,
const TFloat w,
TFloat 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 }

◆ FuncMultiply3Add()

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

Definition at line 299 of file networkio.h.

299 {
300 ASSERT_HOST(!int_mode_);
301 ASSERT_HOST(!v_io.int_mode_);
302 Func f;
303 const float *u = f_[t];
304 const float *v = v_io.f_[t];
305 int dim = f_.dim2();
306 for (int i = 0; i < dim; ++i) {
307 product[i] += f(u[i]) * v[i] * w[i];
308 }
309 }

◆ i()

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

Definition at line 118 of file networkio.h.

118 {
119 ASSERT_HOST(int_mode_);
120 return i_[t];
121 }

◆ int_mode()

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

Definition at line 122 of file networkio.h.

122 {
123 return int_mode_;
124 }

◆ Max()

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

Definition at line 215 of file networkio.h.

215 {
216 return int_mode_ ? i_.Max() : f_.Max();
217 }

◆ MaxpoolBackward()

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

Definition at line 703 of file networkio.cpp.

703 {
704 ASSERT_HOST(!int_mode_);
705 Zero();
706 StrideMap::Index index(fwd.stride_map_);
707 do {
708 int t = index.t();
709 const int *max_line = maxes[t];
710 const float *fwd_line = fwd.f_[t];
711 int num_features = fwd.f_.dim2();
712 for (int i = 0; i < num_features; ++i) {
713 f_[max_line[i]][i] = fwd_line[i];
714 }
715 } while (index.Increment());
716}

◆ MaxpoolTimeStep()

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

Definition at line 677 of file networkio.cpp.

677 {
678 ASSERT_HOST(int_mode_ == src.int_mode_);
679 if (int_mode_) {
680 int dim = i_.dim2();
681 int8_t *dest_line = i_[dest_t];
682 const int8_t *src_line = src.i_[src_t];
683 for (int i = 0; i < dim; ++i) {
684 if (dest_line[i] < src_line[i]) {
685 dest_line[i] = src_line[i];
686 max_line[i] = src_t;
687 }
688 }
689 } else {
690 int dim = f_.dim2();
691 float *dest_line = f_[dest_t];
692 const float *src_line = src.f_[src_t];
693 for (int i = 0; i < dim; ++i) {
694 if (dest_line[i] < src_line[i]) {
695 dest_line[i] = src_line[i];
696 max_line[i] = src_t;
697 }
698 }
699 }
700}

◆ MinOfMaxes()

float tesseract::NetworkIO::MinOfMaxes ( ) const

Definition at line 719 of file networkio.cpp.

719 {
720 float min_max = 0.0f;
721 int width = Width();
722 int num_features = NumFeatures();
723 for (int t = 0; t < width; ++t) {
724 float max_value = -FLT_MAX;
725 if (int_mode_) {
726 const int8_t *column = i_[t];
727 for (int i = 0; i < num_features; ++i) {
728 if (column[i] > max_value) {
729 max_value = column[i];
730 }
731 }
732 } else {
733 const float *column = f_[t];
734 for (int i = 0; i < num_features; ++i) {
735 if (column[i] > max_value) {
736 max_value = column[i];
737 }
738 }
739 }
740 if (t == 0 || max_value < min_max) {
741 min_max = max_value;
742 }
743 }
744 return min_max;
745}

◆ mutable_float_array()

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

Definition at line 137 of file networkio.h.

137 {
138 return &f_;
139 }

◆ NumFeatures()

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

Definition at line 106 of file networkio.h.

106 {
107 return int_mode_ ? i_.dim2() : f_.dim2();
108 }

◆ PositionOfBestMatch()

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

Definition at line 520 of file networkio.cpp.

520 {
521 int length = labels.size();
522 int last_start = end - length;
523 int best_start = -1;
524 TFloat best_score = 0;
525 for (int s = start; s <= last_start; ++s) {
526 TFloat score = ScoreOfLabels(labels, s);
527 if (score > best_score || best_start < 0) {
528 best_score = score;
529 best_start = s;
530 }
531 }
532 return best_start;
533}
TFloat ScoreOfLabels(const std::vector< int > &labels, int start) const
Definition: networkio.cpp:537

◆ Print()

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

Definition at line 378 of file networkio.cpp.

378 {
379 int num_features = NumFeatures();
380 for (int y = 0; y < num_features; ++y) {
381 for (int t = 0; t < Width(); ++t) {
382 if (num == 0 || t < num || t + num >= Width()) {
383 if (int_mode_) {
384 tprintf(" %g", static_cast<float>(i_[t][y]) / INT8_MAX);
385 } else {
386 tprintf(" %g", f_[t][y]);
387 }
388 }
389 }
390 tprintf("\n");
391 }
392}
void tprintf(const char *format,...)
Definition: tprintf.cpp:41

◆ ProbToCertainty()

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

Definition at line 580 of file networkio.cpp.

580 {
581 return prob > kMinProb ? std::log(prob) : kMinCertainty;
582}
const float kMinCertainty
Definition: networkio.cpp:30
const float kMinProb
Definition: networkio.cpp:32

◆ Randomize()

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

Definition at line 416 of file networkio.cpp.

416 {
417 if (int_mode_) {
418 int8_t *line = i_[t] + offset;
419 for (int i = 0; i < num_features; ++i) {
420 line[i] = IntCastRounded(randomizer->SignedRand(INT8_MAX));
421 }
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 }
429}

◆ ReadTimeStep()

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

Definition at line 610 of file networkio.cpp.

610 {
611 if (int_mode_) {
612 const int8_t *line = i_[t];
613 for (int i = 0; i < i_.dim2(); ++i) {
614 output[i] = static_cast<TFloat>(line[i]) / INT8_MAX;
615 }
616 } else {
617 const float *line = f_[t];
618 for (int i = 0; i < f_.dim2(); ++i) {
619 output[i] = static_cast<TFloat>(line[i]);
620 }
621 }
622}

◆ Resize()

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

Definition at line 44 of file networkio.h.

44 {
45 ResizeToMap(src.int_mode(), src.stride_map(), num_features);
46 }

◆ Resize2d()

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

Definition at line 35 of file networkio.cpp.

35 {
36 stride_map_ = StrideMap();
37 int_mode_ = int_mode;
38 if (int_mode_) {
39 i_.ResizeNoInit(width, num_features, GetPadding(num_features));
40 } else {
41 f_.ResizeNoInit(width, num_features);
42 }
43}
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:94

◆ ResizeFloat()

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

Definition at line 51 of file networkio.h.

51 {
52 ResizeToMap(false, src.stride_map(), num_features);
53 }

◆ ResizeScaled()

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

Definition at line 61 of file networkio.cpp.

61 {
62 StrideMap stride_map = src.stride_map_;
63 stride_map.ScaleXY(x_scale, y_scale);
64 ResizeToMap(src.int_mode_, stride_map, num_features);
65}
const StrideMap & stride_map() const
Definition: networkio.h:128
void ScaleXY(int x_factor, int y_factor)
Definition: stridemap.cpp:153

◆ ResizeToMap()

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

Definition at line 46 of file networkio.cpp.

46 {
47 // If this method crashes with this == nullptr,
48 // it most likely got here through an uninitialized scratch element,
49 // ie call NetworkScratch::IO::Resizexxx() not NetworkIO::Resizexxx()!!
50 stride_map_ = stride_map;
51 int_mode_ = int_mode;
52 if (int_mode_) {
53 i_.ResizeNoInit(stride_map.Width(), num_features, GetPadding(num_features));
54 } else {
55 f_.ResizeNoInit(stride_map.Width(), num_features);
56 }
58}
void ZeroInvalidElements()
Definition: networkio.cpp:86
int Width() const
Definition: stridemap.h:123

◆ ResizeXTo1()

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

Definition at line 68 of file networkio.cpp.

68 {
69 StrideMap stride_map = src.stride_map_;
71 ResizeToMap(src.int_mode_, stride_map, num_features);
72}

◆ ScaleFloatBy()

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

Definition at line 234 of file networkio.h.

234 {
235 f_ *= factor;
236 }

◆ ScoreOfLabels()

TFloat tesseract::NetworkIO::ScoreOfLabels ( const std::vector< int > &  labels,
int  start 
) const

Definition at line 537 of file networkio.cpp.

537 {
538 int length = labels.size();
539 TFloat score = 0;
540 for (int i = 0; i < length; ++i) {
541 score += f_(start + i, labels[i]);
542 }
543 return score;
544}

◆ ScoresOverRange()

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

Definition at line 454 of file networkio.cpp.

455 {
456 ASSERT_HOST(!int_mode_);
457 *rating = 0.0f;
458 *certainty = 0.0f;
459 if (t_end <= t_start || t_end <= 0) {
460 return;
461 }
462 float ratings[3] = {0.0f, 0.0f, 0.0f};
463 float certs[3] = {0.0f, 0.0f, 0.0f};
464 for (int t = t_start; t < t_end; ++t) {
465 const float *line = f_[t];
466 float score = ProbToCertainty(line[choice]);
467 float zero = ProbToCertainty(line[null_ch]);
468 if (t == t_start) {
469 ratings[2] = FLT_MAX;
470 ratings[1] = -score;
471 certs[1] = score;
472 } else {
473 for (int i = 2; i >= 1; --i) {
474 if (ratings[i] > ratings[i - 1]) {
475 ratings[i] = ratings[i - 1];
476 certs[i] = certs[i - 1];
477 }
478 }
479 ratings[2] -= zero;
480 if (zero < certs[2]) {
481 certs[2] = zero;
482 }
483 ratings[1] -= score;
484 if (score < certs[1]) {
485 certs[1] = score;
486 }
487 }
488 ratings[0] -= zero;
489 if (zero < certs[0]) {
490 certs[0] = zero;
491 }
492 }
493 int best_i = ratings[2] < ratings[1] ? 2 : 1;
494 *rating = ratings[best_i] + t_end - t_start;
495 *certainty = certs[best_i];
496}

◆ set_int_mode()

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

Definition at line 125 of file networkio.h.

125 {
126 int_mode_ = is_quantized;
127 }

◆ set_stride_map()

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

Definition at line 131 of file networkio.h.

131 {
132 stride_map_ = map;
133 }

◆ SetActivations()

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

Definition at line 548 of file networkio.cpp.

548 {
549 ASSERT_HOST(!int_mode_);
550 int num_classes = NumFeatures();
551 float bad_score = (1.0f - ok_score) / (num_classes - 1);
552 float *targets = f_[t];
553 for (int i = 0; i < num_classes; ++i) {
554 targets[i] = bad_score;
555 }
556 targets[label] = ok_score;
557}

◆ SetPixel()

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

Definition at line 290 of file networkio.cpp.

290 {
291 float float_pixel = (pixel - black) / contrast - 1.0f;
292 if (int_mode_) {
293 i_[t][f] = ClipToRange<int>(IntCastRounded((INT8_MAX + 1) * float_pixel), -INT8_MAX, INT8_MAX);
294 } else {
295 f_[t][f] = float_pixel;
296 }
297}

◆ stride_map()

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

Definition at line 128 of file networkio.h.

128 {
129 return stride_map_;
130 }

◆ SubtractAllFromFloat()

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

Definition at line 838 of file networkio.cpp.

838 {
839 ASSERT_HOST(!int_mode_);
840 ASSERT_HOST(!src.int_mode_);
841 f_ -= src.f_;
842}

◆ ToPix()

Image tesseract::NetworkIO::ToPix ( ) const

Definition at line 300 of file networkio.cpp.

300 {
301 // Count the width of the image, and find the max multiplication factor.
302 int im_width = stride_map_.Size(FD_WIDTH);
303 int im_height = stride_map_.Size(FD_HEIGHT);
304 int num_features = NumFeatures();
305 int feature_factor = 1;
306 if (num_features == 3) {
307 // Special hack for color.
308 num_features = 1;
309 feature_factor = 3;
310 }
311 Image pix = pixCreate(im_width, im_height * num_features, 32);
312 StrideMap::Index index(stride_map_);
313 do {
314 int im_x = index.index(FD_WIDTH);
315 int top_im_y = index.index(FD_HEIGHT);
316 int im_y = top_im_y;
317 int t = index.t();
318 if (int_mode_) {
319 const int8_t *features = i_[t];
320 for (int y = 0; y < num_features; ++y, im_y += im_height) {
321 int pixel = features[y * feature_factor];
322 // 1 or 2 features use greyscale.
323 int red = ClipToRange<int>(pixel + 128, 0, 255);
324 int green = red, blue = red;
325 if (feature_factor == 3) {
326 // With 3 features assume RGB color.
327 green = ClipToRange<int>(features[y * feature_factor + 1] + 128, 0, 255);
328 blue = ClipToRange<int>(features[y * feature_factor + 2] + 128, 0, 255);
329 } else if (num_features > 3) {
330 // More than 3 features use false yellow/blue color, assuming a signed
331 // input in the range [-1,1].
332 red = abs(pixel) * 2;
333 if (pixel >= 0) {
334 green = red;
335 blue = 0;
336 } else {
337 blue = red;
338 green = red = 0;
339 }
340 }
341 pixSetPixel(pix, im_x, im_y,
342 (red << L_RED_SHIFT) | (green << L_GREEN_SHIFT) | (blue << L_BLUE_SHIFT));
343 }
344 } else {
345 const float *features = f_[t];
346 for (int y = 0; y < num_features; ++y, im_y += im_height) {
347 float pixel = features[y * feature_factor];
348 // 1 or 2 features use greyscale.
349 int red = ClipToRange<int>(IntCastRounded((pixel + 1.0f) * 127.5f), 0, 255);
350 int green = red, blue = red;
351 if (feature_factor == 3) {
352 // With 3 features assume RGB color.
353 pixel = features[y * feature_factor + 1];
354 green = ClipToRange<int>(IntCastRounded((pixel + 1.0f) * 127.5f), 0, 255);
355 pixel = features[y * feature_factor + 2];
356 blue = ClipToRange<int>(IntCastRounded((pixel + 1.0f) * 127.5f), 0, 255);
357 } else if (num_features > 3) {
358 // More than 3 features use false yellow/blue color, assuming a signed
359 // input in the range [-1,1].
360 red = ClipToRange<int>(IntCastRounded(std::fabs(pixel) * 255), 0, 255);
361 if (pixel >= 0) {
362 green = red;
363 blue = 0;
364 } else {
365 blue = red;
366 green = red = 0;
367 }
368 }
369 pixSetPixel(pix, im_x, im_y,
370 (red << L_RED_SHIFT) | (green << L_GREEN_SHIFT) | (blue << L_BLUE_SHIFT));
371 }
372 }
373 } while (index.Increment());
374 return pix;
375}

◆ Transpose()

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

Definition at line 971 of file networkio.cpp.

971 {
972 int width = Width();
973 dest->ResizeNoInit(NumFeatures(), width);
974 for (int t = 0; t < width; ++t) {
975 dest->WriteStrided(t, f_[t]);
976 }
977}
dest
Definition: upload.py:409

◆ Width()

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

Definition at line 102 of file networkio.h.

102 {
103 return int_mode_ ? i_.dim1() : f_.dim1();
104 }

◆ WriteTimeStep()

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

Definition at line 656 of file networkio.cpp.

656 {
657 WriteTimeStepPart(t, 0, NumFeatures(), input);
658}
void WriteTimeStepPart(int t, int offset, int num_features, const TFloat *input)
Definition: networkio.cpp:662

◆ WriteTimeStepPart()

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

Definition at line 662 of file networkio.cpp.

662 {
663 if (int_mode_) {
664 int8_t *line = i_[t] + offset;
665 for (int i = 0; i < num_features; ++i) {
666 line[i] = ClipToRange<int>(IntCastRounded(input[i] * INT8_MAX), -INT8_MAX, INT8_MAX);
667 }
668 } else {
669 float *line = f_[t] + offset;
670 for (int i = 0; i < num_features; ++i) {
671 line[i] = static_cast<float>(input[i]);
672 }
673 }
674}

◆ Zero()

void tesseract::NetworkIO::Zero ( )

Definition at line 75 of file networkio.cpp.

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

◆ ZeroInvalidElements()

void tesseract::NetworkIO::ZeroInvalidElements ( )

Definition at line 86 of file networkio.cpp.

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

◆ ZeroTimeStep()

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

Definition at line 147 of file networkio.h.

147 {
148 if (int_mode_) {
149 memset(i_[t], 0, sizeof(*i_[t]) * NumFeatures());
150 } else {
151 memset(f_[t], 0, sizeof(*f_[t]) * NumFeatures());
152 }
153 }

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