tesseract v5.3.3.20231005
tesseract::Convolve Class Reference

#include <convolve.h>

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

Public Member Functions

TESS_API Convolve (const std::string &name, int ni, int half_x, int half_y)
 
 ~Convolve () override=default
 
std::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 std::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 std::string & name () const
 
virtual std::string spec () 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 DebugWeights ()=0
 
virtual bool Serialize (TFile *fp) const
 
virtual bool DeSerialize (TFile *fp)=0
 
virtual void Update (float learning_rate, float momentum, float adam_beta, int num_samples)
 
virtual void CountAlternators (const Network &other, TFloat *same, TFloat *changed) const
 
virtual void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)=0
 
virtual bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)=0
 
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_
 
std::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 (Image pix, ScrollView *window)
 
- Protected Member Functions inherited from tesseract::Network
TFloat Random (TFloat range)
 

Detailed Description

Definition at line 31 of file convolve.h.

Constructor & Destructor Documentation

◆ Convolve()

tesseract::Convolve::Convolve ( const std::string &  name,
int  ni,
int  half_x,
int  half_y 
)

Definition at line 31 of file convolve.cpp.

32 : Network(NT_CONVOLVE, name, ni, ni * (2 * half_x + 1) * (2 * half_y + 1))
33 , half_x_(half_x)
34 , half_y_(half_y) {}
@ NT_CONVOLVE
Definition: network.h:45
const std::string & name() const
Definition: network.h:140

◆ ~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 92 of file convolve.cpp.

93 {
94 back_deltas->Resize(fwd_deltas, ni_);
95 NetworkScratch::IO delta_sum;
96 delta_sum.ResizeFloat(fwd_deltas, ni_, scratch);
97 delta_sum->Zero();
98 int y_scale = 2 * half_y_ + 1;
99 StrideMap::Index src_index(fwd_deltas.stride_map());
100 do {
101 // Stack x_scale groups of y_scale * ni_ inputs together.
102 int t = src_index.t();
103 int out_ix = 0;
104 for (int x = -half_x_; x <= half_x_; ++x, out_ix += y_scale * ni_) {
105 StrideMap::Index x_index(src_index);
106 if (x_index.AddOffset(x, FD_WIDTH)) {
107 int out_iy = out_ix;
108 for (int y = -half_y_; y <= half_y_; ++y, out_iy += ni_) {
109 StrideMap::Index y_index(x_index);
110 if (y_index.AddOffset(y, FD_HEIGHT)) {
111 fwd_deltas.AddTimeStepPart(t, out_iy, ni_, delta_sum->f(y_index.t()));
112 }
113 }
114 }
115 }
116 } while (src_index.Increment());
117 back_deltas->CopyAll(*delta_sum);
118 return true;
119}
const double y
@ FD_WIDTH
Definition: stridemap.h:35
@ FD_HEIGHT
Definition: stridemap.h:34

◆ DeSerialize()

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

Implements tesseract::Network.

Definition at line 42 of file convolve.cpp.

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

◆ 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 55 of file convolve.cpp.

56 {
57 output->Resize(input, no_);
58 int y_scale = 2 * half_y_ + 1;
59 StrideMap::Index dest_index(output->stride_map());
60 do {
61 // Stack x_scale groups of y_scale * ni_ inputs together.
62 int t = dest_index.t();
63 int out_ix = 0;
64 for (int x = -half_x_; x <= half_x_; ++x, out_ix += y_scale * ni_) {
65 StrideMap::Index x_index(dest_index);
66 if (!x_index.AddOffset(x, FD_WIDTH)) {
67 // This x is outside the image.
68 output->Randomize(t, out_ix, y_scale * ni_, randomizer_);
69 } else {
70 int out_iy = out_ix;
71 for (int y = -half_y_; y <= half_y_; ++y, out_iy += ni_) {
72 StrideMap::Index y_index(x_index);
73 if (!y_index.AddOffset(y, FD_HEIGHT)) {
74 // This y is outside the image.
75 output->Randomize(t, out_iy, ni_, randomizer_);
76 } else {
77 output->CopyTimeStepGeneral(t, out_iy, ni_, input, y_index.t(), 0);
78 }
79 }
80 }
81 }
82 } while (dest_index.Increment());
83#ifndef GRAPHICS_DISABLED
84 if (debug) {
86 }
87#endif
88}
void DisplayForward(const NetworkIO &matrix)
Definition: network.cpp:333
TRand * randomizer_
Definition: network.h:312

◆ Serialize()

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

Reimplemented from tesseract::Network.

Definition at line 37 of file convolve.cpp.

37 {
38 return Network::Serialize(fp) && fp->Serialize(&half_x_) && fp->Serialize(&half_y_);
39}
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:158

◆ spec()

std::string tesseract::Convolve::spec ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Definition at line 39 of file convolve.h.

39 {
40 return "C" + std::to_string(half_y_ * 2 + 1) + "," + std::to_string(half_x_ * 2 + 1);
41 }

Member Data Documentation

◆ half_x_

int32_t tesseract::Convolve::half_x_
protected

Definition at line 65 of file convolve.h.

◆ half_y_

int32_t tesseract::Convolve::half_y_
protected

Definition at line 66 of file convolve.h.


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