tesseract v5.3.3.20231005
tesseract::Network Class Referenceabstract

#include <network.h>

Inheritance diagram for tesseract::Network:
tesseract::Convolve tesseract::FullyConnected tesseract::Input tesseract::LSTM tesseract::Plumbing tesseract::Reconfig tesseract::Parallel tesseract::Reversed tesseract::Series tesseract::Maxpool

Public Member Functions

 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)
 

Static Public Member Functions

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

TFloat Random (TFloat range)
 

Protected Attributes

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_
 

Detailed Description

Definition at line 103 of file network.h.

Constructor & Destructor Documentation

◆ Network() [1/2]

tesseract::Network::Network ( )

Definition at line 80 of file network.cpp.

81 : type_(NT_NONE)
83 , needs_to_backprop_(true)
85 , ni_(0)
86 , no_(0)
87 , num_weights_(0)
88 , forward_win_(nullptr)
89 , backward_win_(nullptr)
90 , randomizer_(nullptr) {}
@ TS_ENABLED
Definition: network.h:93
@ NT_NONE
Definition: network.h:42
int32_t network_flags_
Definition: network.h:303
NetworkType type_
Definition: network.h:300
bool needs_to_backprop_
Definition: network.h:302
ScrollView * forward_win_
Definition: network.h:310
ScrollView * backward_win_
Definition: network.h:311
int32_t num_weights_
Definition: network.h:306
TrainingState training_
Definition: network.h:301
TRand * randomizer_
Definition: network.h:312

◆ Network() [2/2]

tesseract::Network::Network ( NetworkType  type,
const std::string &  name,
int  ni,
int  no 
)

Definition at line 91 of file network.cpp.

92 : type_(type)
94 , needs_to_backprop_(true)
96 , ni_(ni)
97 , no_(no)
98 , num_weights_(0)
99 , name_(name)
100 , forward_win_(nullptr)
101 , backward_win_(nullptr)
102 , randomizer_(nullptr) {}
const std::string & name() const
Definition: network.h:140
std::string name_
Definition: network.h:307
NetworkType type() const
Definition: network.h:110

◆ ~Network()

virtual tesseract::Network::~Network ( )
virtualdefault

Member Function Documentation

◆ Backward()

virtual bool tesseract::Network::Backward ( bool  debug,
const NetworkIO fwd_deltas,
NetworkScratch scratch,
NetworkIO back_deltas 
)
pure virtual

◆ CacheXScaleFactor()

virtual void tesseract::Network::CacheXScaleFactor ( int  factor)
inlinevirtual

Reimplemented in tesseract::Input, tesseract::Plumbing, and tesseract::Series.

Definition at line 220 of file network.h.

220{}

◆ ClearWindow()

void tesseract::Network::ClearWindow ( bool  tess_coords,
const char *  window_name,
int  width,
int  height,
ScrollView **  window 
)
static

Definition at line 350 of file network.cpp.

351 {
352 if (*window == nullptr) {
353 int min_size = std::min(width, height);
354 if (min_size < kMinWinSize) {
355 if (min_size < 1) {
356 min_size = 1;
357 }
358 width = width * kMinWinSize / min_size;
359 height = height * kMinWinSize / min_size;
360 }
361 width += kXWinFrameSize;
362 height += kYWinFrameSize;
363 if (width > kMaxWinSize) {
364 width = kMaxWinSize;
365 }
366 if (height > kMaxWinSize) {
367 height = kMaxWinSize;
368 }
369 *window = new ScrollView(window_name, 80, 100, width, height, width, height, tess_coords);
370 tprintf("Created window %s of size %d, %d\n", window_name, width, height);
371 } else {
372 (*window)->Clear();
373 }
374}
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
const int kXWinFrameSize
Definition: network.cpp:54
const int kYWinFrameSize
Definition: network.cpp:55
const int kMinWinSize
Definition: network.cpp:51
const int kMaxWinSize
Definition: network.cpp:52

◆ ConvertToInt()

virtual void tesseract::Network::ConvertToInt ( )
inlinevirtual

Reimplemented in tesseract::FullyConnected, tesseract::LSTM, and tesseract::Plumbing.

Definition at line 196 of file network.h.

196{}

◆ CountAlternators()

virtual void tesseract::Network::CountAlternators ( const Network other,
TFloat same,
TFloat changed 
) const
inlinevirtual

Reimplemented in tesseract::FullyConnected, tesseract::LSTM, and tesseract::Plumbing.

Definition at line 242 of file network.h.

244 {}

◆ CreateFromFile()

Network * tesseract::Network::CreateFromFile ( TFile fp)
static

Definition at line 217 of file network.cpp.

217 {
218 NetworkType type; // Type of the derived network class.
219 TrainingState training; // Are we currently training?
220 bool needs_to_backprop; // This network needs to output back_deltas.
221 int32_t network_flags; // Behavior control flags in NetworkFlags.
222 int32_t ni; // Number of input values.
223 int32_t no; // Number of output values.
224 int32_t num_weights; // Number of weights in this and sub-network.
225 std::string name; // A unique name for this layer.
226 int8_t data;
227 Network *network = nullptr;
228 type = getNetworkType(fp);
229 if (!fp->DeSerialize(&data)) {
230 return nullptr;
231 }
232 training = data == TS_ENABLED ? TS_ENABLED : TS_DISABLED;
233 if (!fp->DeSerialize(&data)) {
234 return nullptr;
235 }
236 needs_to_backprop = data != 0;
237 if (!fp->DeSerialize(&network_flags)) {
238 return nullptr;
239 }
240 if (!fp->DeSerialize(&ni)) {
241 return nullptr;
242 }
243 if (!fp->DeSerialize(&no)) {
244 return nullptr;
245 }
246 if (!fp->DeSerialize(&num_weights)) {
247 return nullptr;
248 }
249 if (!fp->DeSerialize(name)) {
250 return nullptr;
251 }
252
253 switch (type) {
254 case NT_CONVOLVE:
255 network = new Convolve(name.c_str(), ni, 0, 0);
256 break;
257 case NT_INPUT:
258 network = new Input(name.c_str(), ni, no);
259 break;
260 case NT_LSTM:
261 case NT_LSTM_SOFTMAX:
263 case NT_LSTM_SUMMARY:
264 network = new LSTM(name.c_str(), ni, no, no, false, type);
265 break;
266 case NT_MAXPOOL:
267 network = new Maxpool(name.c_str(), ni, 0, 0);
268 break;
269 // All variants of Parallel.
270 case NT_PARALLEL:
271 case NT_REPLICATED:
272 case NT_PAR_RL_LSTM:
273 case NT_PAR_UD_LSTM:
274 case NT_PAR_2D_LSTM:
275 network = new Parallel(name.c_str(), type);
276 break;
277 case NT_RECONFIG:
278 network = new Reconfig(name.c_str(), ni, 0, 0);
279 break;
280 // All variants of reversed.
281 case NT_XREVERSED:
282 case NT_YREVERSED:
283 case NT_XYTRANSPOSE:
284 network = new Reversed(name.c_str(), type);
285 break;
286 case NT_SERIES:
287 network = new Series(name.c_str());
288 break;
289 case NT_TENSORFLOW:
290#ifdef INCLUDE_TENSORFLOW
291 network = new TFNetwork(name.c_str());
292#else
293 tprintf("TensorFlow not compiled in! -DINCLUDE_TENSORFLOW\n");
294#endif
295 break;
296 // All variants of FullyConnected.
297 case NT_SOFTMAX:
299 case NT_RELU:
300 case NT_TANH:
301 case NT_LINEAR:
302 case NT_LOGISTIC:
303 case NT_POSCLIP:
304 case NT_SYMCLIP:
305 network = new FullyConnected(name.c_str(), ni, no, type);
306 break;
307 default:
308 break;
309 }
310 if (network) {
311 network->training_ = training;
312 network->needs_to_backprop_ = needs_to_backprop;
313 network->network_flags_ = network_flags;
314 network->num_weights_ = num_weights;
315 if (!network->DeSerialize(fp)) {
316 delete network;
317 network = nullptr;
318 }
319 }
320 return network;
321}
TrainingState
Definition: network.h:90
@ TS_DISABLED
Definition: network.h:92
NetworkType
Definition: network.h:41
@ NT_LINEAR
Definition: network.h:65
@ NT_MAXPOOL
Definition: network.h:46
@ NT_RELU
Definition: network.h:64
@ NT_XREVERSED
Definition: network.h:54
@ NT_LSTM
Definition: network.h:58
@ NT_CONVOLVE
Definition: network.h:45
@ NT_SOFTMAX
Definition: network.h:66
@ NT_LOGISTIC
Definition: network.h:60
@ NT_PAR_UD_LSTM
Definition: network.h:50
@ NT_LSTM_SOFTMAX_ENCODED
Definition: network.h:74
@ NT_PARALLEL
Definition: network.h:47
@ NT_SYMCLIP
Definition: network.h:62
@ NT_PAR_2D_LSTM
Definition: network.h:51
@ NT_LSTM_SUMMARY
Definition: network.h:59
@ NT_YREVERSED
Definition: network.h:55
@ NT_RECONFIG
Definition: network.h:53
@ NT_INPUT
Definition: network.h:43
@ NT_TENSORFLOW
Definition: network.h:76
@ NT_POSCLIP
Definition: network.h:61
@ NT_LSTM_SOFTMAX
Definition: network.h:73
@ NT_XYTRANSPOSE
Definition: network.h:56
@ NT_SERIES
Definition: network.h:52
@ NT_SOFTMAX_NO_CTC
Definition: network.h:67
@ NT_TANH
Definition: network.h:63
@ NT_PAR_RL_LSTM
Definition: network.h:49
@ NT_REPLICATED
Definition: network.h:48
int num_weights() const
Definition: network.h:119
bool needs_to_backprop() const
Definition: network.h:116

◆ DebugWeights()

virtual void tesseract::Network::DebugWeights ( )
pure virtual

◆ DeSerialize()

virtual bool tesseract::Network::DeSerialize ( TFile fp)
pure virtual

◆ DisplayBackward()

void tesseract::Network::DisplayBackward ( const NetworkIO matrix)

Definition at line 341 of file network.cpp.

341 {
342 Image image = matrix.ToPix();
343 std::string window_name = name_ + "-back";
344 ClearWindow(false, window_name.c_str(), pixGetWidth(image), pixGetHeight(image), &backward_win_);
347}
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
Definition: network.cpp:350
static int DisplayImage(Image pix, ScrollView *window)
Definition: network.cpp:378
static void Update()
Definition: scrollview.cpp:700

◆ DisplayForward()

void tesseract::Network::DisplayForward ( const NetworkIO matrix)

Definition at line 333 of file network.cpp.

333 {
334 Image image = matrix.ToPix();
335 ClearWindow(false, name_.c_str(), pixGetWidth(image), pixGetHeight(image), &forward_win_);
338}

◆ DisplayImage()

int tesseract::Network::DisplayImage ( Image  pix,
ScrollView window 
)
static

Definition at line 378 of file network.cpp.

378 {
379 int height = pixGetHeight(pix);
380 window->Draw(pix, 0, 0);
381 pix.destroy();
382 return height;
383}

◆ Forward()

virtual void tesseract::Network::Forward ( bool  debug,
const NetworkIO input,
const TransposedArray input_transpose,
NetworkScratch scratch,
NetworkIO output 
)
pure virtual

◆ InitWeights()

int tesseract::Network::InitWeights ( float  range,
TRand randomizer 
)
virtual

Reimplemented in tesseract::FullyConnected, tesseract::LSTM, tesseract::Plumbing, and tesseract::Series.

Definition at line 137 of file network.cpp.

137 {
138 randomizer_ = randomizer;
139 return 0;
140}

◆ InputShape()

virtual StaticShape tesseract::Network::InputShape ( ) const
inlinevirtual

Reimplemented in tesseract::Input, and tesseract::Plumbing.

Definition at line 129 of file network.h.

129 {
130 StaticShape result;
131 return result;
132 }

◆ IsPlumbingType()

virtual bool tesseract::Network::IsPlumbingType ( ) const
inlinevirtual

Reimplemented in tesseract::Plumbing.

Definition at line 154 of file network.h.

154 {
155 return false;
156 }

◆ IsTraining()

bool tesseract::Network::IsTraining ( ) const
inline

Definition at line 113 of file network.h.

113 {
114 return training_ == TS_ENABLED;
115 }

◆ name()

const std::string & tesseract::Network::name ( ) const
inline

Definition at line 140 of file network.h.

140 {
141 return name_;
142 }

◆ needs_to_backprop()

bool tesseract::Network::needs_to_backprop ( ) const
inline

Definition at line 116 of file network.h.

116 {
117 return needs_to_backprop_;
118 }

◆ num_weights()

int tesseract::Network::num_weights ( ) const
inline

Definition at line 119 of file network.h.

119 {
120 return num_weights_;
121 }

◆ NumInputs()

int tesseract::Network::NumInputs ( ) const
inline

Definition at line 122 of file network.h.

122 {
123 return ni_;
124 }

◆ NumOutputs()

int tesseract::Network::NumOutputs ( ) const
inline

Definition at line 125 of file network.h.

125 {
126 return no_;
127 }

◆ OutputShape()

virtual StaticShape tesseract::Network::OutputShape ( const StaticShape input_shape) const
inlinevirtual

Reimplemented in tesseract::FullyConnected, tesseract::Input, tesseract::LSTM, tesseract::Parallel, tesseract::Reconfig, tesseract::Reversed, and tesseract::Series.

Definition at line 135 of file network.h.

135 {
136 StaticShape result(input_shape);
137 result.set_depth(no_);
138 return result;
139 }

◆ Random()

TFloat tesseract::Network::Random ( TFloat  range)
protected

Definition at line 324 of file network.cpp.

324 {
325 ASSERT_HOST(randomizer_ != nullptr);
326 return randomizer_->SignedRand(range);
327}
#define ASSERT_HOST(x)
Definition: errcode.h:54
double SignedRand(double range)
Definition: helpers.h:78

◆ RemapOutputs()

virtual int tesseract::Network::RemapOutputs ( int  old_no,
const std::vector< int > &  code_map 
)
inlinevirtual

Reimplemented in tesseract::FullyConnected, tesseract::LSTM, tesseract::Plumbing, and tesseract::Series.

Definition at line 190 of file network.h.

191 {
192 return 0;
193 }

◆ Serialize()

bool tesseract::Network::Serialize ( TFile fp) const
virtual

Reimplemented in tesseract::Convolve, tesseract::FullyConnected, tesseract::Input, tesseract::LSTM, tesseract::Plumbing, and tesseract::Reconfig.

Definition at line 158 of file network.cpp.

158 {
159 int8_t data = NT_NONE;
160 if (!fp->Serialize(&data)) {
161 return false;
162 }
163 std::string type_name = kTypeNames[type_];
164 if (!fp->Serialize(type_name)) {
165 return false;
166 }
167 data = training_;
168 if (!fp->Serialize(&data)) {
169 return false;
170 }
171 data = needs_to_backprop_;
172 if (!fp->Serialize(&data)) {
173 return false;
174 }
175 if (!fp->Serialize(&network_flags_)) {
176 return false;
177 }
178 if (!fp->Serialize(&ni_)) {
179 return false;
180 }
181 if (!fp->Serialize(&no_)) {
182 return false;
183 }
184 if (!fp->Serialize(&num_weights_)) {
185 return false;
186 }
187 uint32_t length = name_.length();
188 if (!fp->Serialize(&length)) {
189 return false;
190 }
191 return fp->Serialize(name_.c_str(), length);
192}

◆ SetEnableTraining()

void tesseract::Network::SetEnableTraining ( TrainingState  state)
virtual

Reimplemented in tesseract::FullyConnected, tesseract::LSTM, and tesseract::Plumbing.

Definition at line 113 of file network.cpp.

113 {
114 if (state == TS_RE_ENABLE) {
115 // Enable only from temp disabled.
116 if (training_ == TS_TEMP_DISABLE) {
118 }
119 } else if (state == TS_TEMP_DISABLE) {
120 // Temp disable only from enabled.
121 if (training_ == TS_ENABLED) {
122 training_ = state;
123 }
124 } else {
125 training_ = state;
126 }
127}
@ TS_TEMP_DISABLE
Definition: network.h:95
@ TS_RE_ENABLE
Definition: network.h:97

◆ SetNetworkFlags()

void tesseract::Network::SetNetworkFlags ( uint32_t  flags)
virtual

Reimplemented in tesseract::Plumbing.

Definition at line 131 of file network.cpp.

131 {
132 network_flags_ = flags;
133}

◆ SetRandomizer()

void tesseract::Network::SetRandomizer ( TRand randomizer)
virtual

Reimplemented in tesseract::Plumbing.

Definition at line 145 of file network.cpp.

145 {
146 randomizer_ = randomizer;
147}

◆ SetupNeedsBackprop()

bool tesseract::Network::SetupNeedsBackprop ( bool  needs_backprop)
virtual

Reimplemented in tesseract::Plumbing, and tesseract::Series.

Definition at line 152 of file network.cpp.

152 {
153 needs_to_backprop_ = needs_backprop;
154 return needs_backprop || num_weights_ > 0;
155}

◆ spec()

virtual std::string tesseract::Network::spec ( ) const
inlinevirtual

◆ TestFlag()

bool tesseract::Network::TestFlag ( NetworkFlags  flag) const
inline

Definition at line 146 of file network.h.

146 {
147 return (network_flags_ & flag) != 0;
148 }

◆ type()

NetworkType tesseract::Network::type ( ) const
inline

Definition at line 110 of file network.h.

110 {
111 return type_;
112 }

◆ Update()

virtual void tesseract::Network::Update ( float  learning_rate,
float  momentum,
float  adam_beta,
int  num_samples 
)
inlinevirtual

Reimplemented in tesseract::FullyConnected, tesseract::LSTM, and tesseract::Plumbing.

Definition at line 235 of file network.h.

238 {}

◆ XScaleFactor()

virtual int tesseract::Network::XScaleFactor ( ) const
inlinevirtual

Reimplemented in tesseract::Input, tesseract::Plumbing, tesseract::Reconfig, and tesseract::Series.

Definition at line 214 of file network.h.

214 {
215 return 1;
216 }

Member Data Documentation

◆ backward_win_

ScrollView* tesseract::Network::backward_win_
protected

Definition at line 311 of file network.h.

◆ forward_win_

ScrollView* tesseract::Network::forward_win_
protected

Definition at line 310 of file network.h.

◆ name_

std::string tesseract::Network::name_
protected

Definition at line 307 of file network.h.

◆ needs_to_backprop_

bool tesseract::Network::needs_to_backprop_
protected

Definition at line 302 of file network.h.

◆ network_flags_

int32_t tesseract::Network::network_flags_
protected

Definition at line 303 of file network.h.

◆ ni_

int32_t tesseract::Network::ni_
protected

Definition at line 304 of file network.h.

◆ no_

int32_t tesseract::Network::no_
protected

Definition at line 305 of file network.h.

◆ num_weights_

int32_t tesseract::Network::num_weights_
protected

Definition at line 306 of file network.h.

◆ randomizer_

TRand* tesseract::Network::randomizer_
protected

Definition at line 312 of file network.h.

◆ training_

TrainingState tesseract::Network::training_
protected

Definition at line 301 of file network.h.

◆ type_

NetworkType tesseract::Network::type_
protected

Definition at line 300 of file network.h.


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