tesseract  5.0.0-alpha-619-ge9db
tesseract::Convolve Class Reference

#include <convolve.h>

Inheritance diagram for tesseract::Convolve:
tesseract::Network

Public Member Functions

 Convolve (const STRING &name, int ni, int half_x, int half_y)
 
 ~Convolve () override=default
 
STRING spec () const override
 
bool Serialize (TFile *fp) const override
 
bool DeSerialize (TFile *fp) override
 
void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
 
bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
 
- Public Member Functions inherited from tesseract::Network
 Network ()
 
 Network (NetworkType type, const STRING &name, int ni, int no)
 
virtual ~Network ()=default
 
NetworkType type () const
 
bool IsTraining () const
 
bool needs_to_backprop () const
 
int num_weights () const
 
int NumInputs () const
 
int NumOutputs () const
 
virtual StaticShape InputShape () const
 
virtual StaticShape OutputShape (const StaticShape &input_shape) const
 
const STRINGname () const
 
bool TestFlag (NetworkFlags flag) const
 
virtual bool IsPlumbingType () const
 
virtual void SetEnableTraining (TrainingState state)
 
virtual void SetNetworkFlags (uint32_t flags)
 
virtual int InitWeights (float range, TRand *randomizer)
 
virtual int RemapOutputs (int old_no, const std::vector< int > &code_map)
 
virtual void ConvertToInt ()
 
virtual void SetRandomizer (TRand *randomizer)
 
virtual bool SetupNeedsBackprop (bool needs_backprop)
 
virtual int XScaleFactor () const
 
virtual void CacheXScaleFactor (int factor)
 
virtual void Update (float learning_rate, float momentum, float adam_beta, int num_samples)
 
virtual void CountAlternators (const Network &other, double *same, double *changed) const
 
void DisplayForward (const NetworkIO &matrix)
 
void DisplayBackward (const NetworkIO &matrix)
 

Protected Attributes

int32_t half_x_
 
int32_t half_y_
 
- Protected Attributes inherited from tesseract::Network
NetworkType type_
 
TrainingState training_
 
bool needs_to_backprop_
 
int32_t network_flags_
 
int32_t ni_
 
int32_t no_
 
int32_t num_weights_
 
STRING name_
 
ScrollViewforward_win_
 
ScrollViewbackward_win_
 
TRandrandomizer_
 

Additional Inherited Members

- Static Public Member Functions inherited from tesseract::Network
static NetworkCreateFromFile (TFile *fp)
 
static void ClearWindow (bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
 
static int DisplayImage (Pix *pix, ScrollView *window)
 
- Protected Member Functions inherited from tesseract::Network
double Random (double range)
 

Detailed Description

Definition at line 32 of file convolve.h.

Constructor & Destructor Documentation

◆ Convolve()

tesseract::Convolve::Convolve ( const STRING name,
int  ni,
int  half_x,
int  half_y 
)

Definition at line 28 of file convolve.cpp.

29  : Network(NT_CONVOLVE, name, ni, ni * (2*half_x + 1) * (2*half_y + 1)),
30  half_x_(half_x), half_y_(half_y) {
31 }

◆ ~Convolve()

tesseract::Convolve::~Convolve ( )
overridedefault

Member Function Documentation

◆ Backward()

bool tesseract::Convolve::Backward ( bool  debug,
const NetworkIO fwd_deltas,
NetworkScratch scratch,
NetworkIO back_deltas 
)
overridevirtual

Implements tesseract::Network.

Definition at line 84 of file convolve.cpp.

86  {
87  back_deltas->Resize(fwd_deltas, ni_);
88  NetworkScratch::IO delta_sum;
89  delta_sum.ResizeFloat(fwd_deltas, ni_, scratch);
90  delta_sum->Zero();
91  int y_scale = 2 * half_y_ + 1;
92  StrideMap::Index src_index(fwd_deltas.stride_map());
93  do {
94  // Stack x_scale groups of y_scale * ni_ inputs together.
95  int t = src_index.t();
96  int out_ix = 0;
97  for (int x = -half_x_; x <= half_x_; ++x, out_ix += y_scale * ni_) {
98  StrideMap::Index x_index(src_index);
99  if (x_index.AddOffset(x, FD_WIDTH)) {
100  int out_iy = out_ix;
101  for (int y = -half_y_; y <= half_y_; ++y, out_iy += ni_) {
102  StrideMap::Index y_index(x_index);
103  if (y_index.AddOffset(y, FD_HEIGHT)) {
104  fwd_deltas.AddTimeStepPart(t, out_iy, ni_,
105  delta_sum->f(y_index.t()));
106  }
107  }
108  }
109  }
110  } while (src_index.Increment());
111  back_deltas->CopyAll(*delta_sum);
112  return true;
113 }

◆ DeSerialize()

bool tesseract::Convolve::DeSerialize ( TFile fp)
overridevirtual

Implements tesseract::Network.

Definition at line 41 of file convolve.cpp.

41  {
42  if (!fp->DeSerialize(&half_x_)) return false;
43  if (!fp->DeSerialize(&half_y_)) return false;
44  no_ = ni_ * (2*half_x_ + 1) * (2*half_y_ + 1);
45  return true;
46 }

◆ Forward()

void tesseract::Convolve::Forward ( bool  debug,
const NetworkIO input,
const TransposedArray input_transpose,
NetworkScratch scratch,
NetworkIO output 
)
overridevirtual

Implements tesseract::Network.

Definition at line 50 of file convolve.cpp.

52  {
53  output->Resize(input, no_);
54  int y_scale = 2 * half_y_ + 1;
55  StrideMap::Index dest_index(output->stride_map());
56  do {
57  // Stack x_scale groups of y_scale * ni_ inputs together.
58  int t = dest_index.t();
59  int out_ix = 0;
60  for (int x = -half_x_; x <= half_x_; ++x, out_ix += y_scale * ni_) {
61  StrideMap::Index x_index(dest_index);
62  if (!x_index.AddOffset(x, FD_WIDTH)) {
63  // This x is outside the image.
64  output->Randomize(t, out_ix, y_scale * ni_, randomizer_);
65  } else {
66  int out_iy = out_ix;
67  for (int y = -half_y_; y <= half_y_; ++y, out_iy += ni_) {
68  StrideMap::Index y_index(x_index);
69  if (!y_index.AddOffset(y, FD_HEIGHT)) {
70  // This y is outside the image.
71  output->Randomize(t, out_iy, ni_, randomizer_);
72  } else {
73  output->CopyTimeStepGeneral(t, out_iy, ni_, input, y_index.t(), 0);
74  }
75  }
76  }
77  }
78  } while (dest_index.Increment());
79  if (debug) DisplayForward(*output);
80 }

◆ Serialize()

bool tesseract::Convolve::Serialize ( TFile fp) const
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 34 of file convolve.cpp.

34  {
35  return Network::Serialize(fp) &&
36  fp->Serialize(&half_x_) &&
37  fp->Serialize(&half_y_);
38 }

◆ spec()

STRING tesseract::Convolve::spec ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Definition at line 39 of file convolve.h.

39  {
40  STRING spec;
41  spec.add_str_int("C", half_x_ * 2 + 1);
42  spec.add_str_int(",", half_y_ * 2 + 1);
43  return spec;
44  }

Member Data Documentation

◆ half_x_

int32_t tesseract::Convolve::half_x_
protected

Definition at line 70 of file convolve.h.

◆ half_y_

int32_t tesseract::Convolve::half_y_
protected

Definition at line 71 of file convolve.h.


The documentation for this class was generated from the following files:
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
tesseract::Network::DisplayForward
void DisplayForward(const NetworkIO &matrix)
Definition: network.cpp:288
STRING
Definition: strngs.h:45
tesseract::Convolve::half_y_
int32_t half_y_
Definition: convolve.h:71
tesseract::FD_WIDTH
Definition: stridemap.h:35
tesseract::Network::randomizer_
TRand * randomizer_
Definition: network.h:305
tesseract::NT_CONVOLVE
Definition: network.h:47
tesseract::Convolve::spec
STRING spec() const override
Definition: convolve.h:39
tesseract::Convolve::half_x_
int32_t half_x_
Definition: convolve.h:70
tesseract::Network::name
const STRING & name() const
Definition: network.h:138
tesseract::FD_HEIGHT
Definition: stridemap.h:34
tesseract::Network::Serialize
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151
tesseract::Network::no_
int32_t no_
Definition: network.h:298
tesseract::Network::ni_
int32_t ni_
Definition: network.h:297
tesseract::Network::Network
Network()
Definition: network.cpp:76