tesseract  5.0.0-alpha-619-ge9db
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 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
 
virtual 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, double *same, double *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 (Pix *pix, ScrollView *window)
 

Protected Member Functions

double Random (double 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_
 
STRING name_
 
ScrollViewforward_win_
 
ScrollViewbackward_win_
 
TRandrandomizer_
 

Detailed Description

Definition at line 105 of file network.h.

Constructor & Destructor Documentation

◆ Network() [1/2]

tesseract::Network::Network ( )

Definition at line 76 of file network.cpp.

77  : type_(NT_NONE),
79  needs_to_backprop_(true),
80  network_flags_(0),
81  ni_(0),
82  no_(0),
83  num_weights_(0),
84  forward_win_(nullptr),
85  backward_win_(nullptr),
86  randomizer_(nullptr) {}

◆ Network() [2/2]

tesseract::Network::Network ( NetworkType  type,
const STRING name,
int  ni,
int  no 
)

Definition at line 87 of file network.cpp.

88  : type_(type),
90  needs_to_backprop_(true),
91  network_flags_(0),
92  ni_(ni),
93  no_(no),
94  num_weights_(0),
95  name_(name),
96  forward_win_(nullptr),
97  backward_win_(nullptr),
98  randomizer_(nullptr) {}

◆ ~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::Plumbing, tesseract::Series, and tesseract::Input.

Definition at line 215 of file network.h.

215 {}

◆ ClearWindow()

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

Definition at line 312 of file network.cpp.

313  {
314  if (*window == nullptr) {
315  int min_size = std::min(width, height);
316  if (min_size < kMinWinSize) {
317  if (min_size < 1) min_size = 1;
318  width = width * kMinWinSize / min_size;
319  height = height * kMinWinSize / min_size;
320  }
321  width += kXWinFrameSize;
322  height += kYWinFrameSize;
323  if (width > kMaxWinSize) width = kMaxWinSize;
324  if (height > kMaxWinSize) height = kMaxWinSize;
325  *window = new ScrollView(window_name, 80, 100, width, height, width, height,
326  tess_coords);
327  tprintf("Created window %s of size %d, %d\n", window_name, width, height);
328  } else {
329  (*window)->Clear();
330  }
331 }

◆ ConvertToInt()

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

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

Definition at line 191 of file network.h.

191 {}

◆ CountAlternators()

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

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

Definition at line 235 of file network.h.

236  {}

◆ CreateFromFile()

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

Definition at line 187 of file network.cpp.

187  {
188  NetworkType type; // Type of the derived network class.
189  TrainingState training; // Are we currently training?
190  bool needs_to_backprop; // This network needs to output back_deltas.
191  int32_t network_flags; // Behavior control flags in NetworkFlags.
192  int32_t ni; // Number of input values.
193  int32_t no; // Number of output values.
194  int32_t num_weights; // Number of weights in this and sub-network.
195  STRING name; // A unique name for this layer.
196  int8_t data;
197  Network* network = nullptr;
198  type = getNetworkType(fp);
199  if (!fp->DeSerialize(&data)) return nullptr;
200  training = data == TS_ENABLED ? TS_ENABLED : TS_DISABLED;
201  if (!fp->DeSerialize(&data)) return nullptr;
202  needs_to_backprop = data != 0;
203  if (!fp->DeSerialize(&network_flags)) return nullptr;
204  if (!fp->DeSerialize(&ni)) return nullptr;
205  if (!fp->DeSerialize(&no)) return nullptr;
206  if (!fp->DeSerialize(&num_weights)) return nullptr;
207  if (!name.DeSerialize(fp)) return nullptr;
208 
209  switch (type) {
210  case NT_CONVOLVE:
211  network = new Convolve(name, ni, 0, 0);
212  break;
213  case NT_INPUT:
214  network = new Input(name, ni, no);
215  break;
216  case NT_LSTM:
217  case NT_LSTM_SOFTMAX:
219  case NT_LSTM_SUMMARY:
220  network =
221  new LSTM(name, ni, no, no, false, type);
222  break;
223  case NT_MAXPOOL:
224  network = new Maxpool(name, ni, 0, 0);
225  break;
226  // All variants of Parallel.
227  case NT_PARALLEL:
228  case NT_REPLICATED:
229  case NT_PAR_RL_LSTM:
230  case NT_PAR_UD_LSTM:
231  case NT_PAR_2D_LSTM:
232  network = new Parallel(name, type);
233  break;
234  case NT_RECONFIG:
235  network = new Reconfig(name, ni, 0, 0);
236  break;
237  // All variants of reversed.
238  case NT_XREVERSED:
239  case NT_YREVERSED:
240  case NT_XYTRANSPOSE:
241  network = new Reversed(name, type);
242  break;
243  case NT_SERIES:
244  network = new Series(name);
245  break;
246  case NT_TENSORFLOW:
247 #ifdef INCLUDE_TENSORFLOW
248  network = new TFNetwork(name);
249 #else
250  tprintf("TensorFlow not compiled in! -DINCLUDE_TENSORFLOW\n");
251 #endif
252  break;
253  // All variants of FullyConnected.
254  case NT_SOFTMAX:
255  case NT_SOFTMAX_NO_CTC:
256  case NT_RELU:
257  case NT_TANH:
258  case NT_LINEAR:
259  case NT_LOGISTIC:
260  case NT_POSCLIP:
261  case NT_SYMCLIP:
262  network = new FullyConnected(name, ni, no, type);
263  break;
264  default:
265  break;
266  }
267  if (network) {
268  network->training_ = training;
269  network->needs_to_backprop_ = needs_to_backprop;
270  network->network_flags_ = network_flags;
271  network->num_weights_ = num_weights;
272  if (!network->DeSerialize(fp)) {
273  delete network;
274  network = nullptr;
275  }
276  }
277  return network;
278 }

◆ 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 299 of file network.cpp.

299  {
300 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics
301  Pix* image = matrix.ToPix();
302  STRING window_name = name_ + "-back";
303  ClearWindow(false, window_name.c_str(), pixGetWidth(image),
304  pixGetHeight(image), &backward_win_);
305  DisplayImage(image, backward_win_);
307 #endif // GRAPHICS_DISABLED
308 }

◆ DisplayForward()

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

Definition at line 288 of file network.cpp.

288  {
289 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics
290  Pix* image = matrix.ToPix();
291  ClearWindow(false, name_.c_str(), pixGetWidth(image),
292  pixGetHeight(image), &forward_win_);
293  DisplayImage(image, forward_win_);
294  forward_win_->Update();
295 #endif // GRAPHICS_DISABLED
296 }

◆ DisplayImage()

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

Definition at line 335 of file network.cpp.

335  {
336  int height = pixGetHeight(pix);
337  window->Image(pix, 0, 0);
338  pixDestroy(&pix);
339  return height;
340 }

◆ 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::LSTM, tesseract::FullyConnected, tesseract::Plumbing, and tesseract::Series.

Definition at line 130 of file network.cpp.

130  {
131  randomizer_ = randomizer;
132  return 0;
133 }

◆ InputShape()

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

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

Definition at line 127 of file network.h.

127  {
128  StaticShape result;
129  return result;
130  }

◆ IsPlumbingType()

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

Reimplemented in tesseract::Plumbing.

Definition at line 152 of file network.h.

152 { return false; }

◆ IsTraining()

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

Definition at line 115 of file network.h.

115 { return training_ == TS_ENABLED; }

◆ name()

const STRING& tesseract::Network::name ( ) const
inline

Definition at line 138 of file network.h.

138  {
139  return name_;
140  }

◆ 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 { return num_weights_; }

◆ NumInputs()

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

Definition at line 120 of file network.h.

120  {
121  return ni_;
122  }

◆ NumOutputs()

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

Definition at line 123 of file network.h.

123  {
124  return no_;
125  }

◆ OutputShape()

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

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

Definition at line 133 of file network.h.

133  {
134  StaticShape result(input_shape);
135  result.set_depth(no_);
136  return result;
137  }

◆ Random()

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

Definition at line 281 of file network.cpp.

281  {
282  ASSERT_HOST(randomizer_ != nullptr);
283  return randomizer_->SignedRand(range);
284 }

◆ RemapOutputs()

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

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

Definition at line 186 of file network.h.

186  {
187  return 0;
188  }

◆ Serialize()

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

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

Definition at line 151 of file network.cpp.

151  {
152  int8_t data = NT_NONE;
153  if (!fp->Serialize(&data)) return false;
154  STRING type_name = kTypeNames[type_];
155  if (!type_name.Serialize(fp)) return false;
156  data = training_;
157  if (!fp->Serialize(&data)) return false;
158  data = needs_to_backprop_;
159  if (!fp->Serialize(&data)) return false;
160  if (!fp->Serialize(&network_flags_)) return false;
161  if (!fp->Serialize(&ni_)) return false;
162  if (!fp->Serialize(&no_)) return false;
163  if (!fp->Serialize(&num_weights_)) return false;
164  if (!name_.Serialize(fp)) return false;
165  return true;
166 }

◆ SetEnableTraining()

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

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

Definition at line 110 of file network.cpp.

110  {
111  if (state == TS_RE_ENABLE) {
112  // Enable only from temp disabled.
114  } else if (state == TS_TEMP_DISABLE) {
115  // Temp disable only from enabled.
116  if (training_ == TS_ENABLED) training_ = state;
117  } else {
118  training_ = state;
119  }
120 }

◆ SetNetworkFlags()

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

Reimplemented in tesseract::Plumbing.

Definition at line 124 of file network.cpp.

124  {
125  network_flags_ = flags;
126 }

◆ SetRandomizer()

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

Reimplemented in tesseract::Plumbing.

Definition at line 138 of file network.cpp.

138  {
139  randomizer_ = randomizer;
140 }

◆ SetupNeedsBackprop()

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

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

Definition at line 145 of file network.cpp.

145  {
146  needs_to_backprop_ = needs_backprop;
147  return needs_backprop || num_weights_ > 0;
148 }

◆ spec()

virtual STRING tesseract::Network::spec ( ) const
inlinevirtual

◆ TestFlag()

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

Definition at line 144 of file network.h.

144  {
145  return (network_flags_ & flag) != 0;
146  }

◆ type()

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

Definition at line 112 of file network.h.

112  {
113  return type_;
114  }

◆ Update()

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

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

Definition at line 230 of file network.h.

231  {}

◆ XScaleFactor()

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

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

Definition at line 209 of file network.h.

209  {
210  return 1;
211  }

Member Data Documentation

◆ backward_win_

ScrollView* tesseract::Network::backward_win_
protected

Definition at line 304 of file network.h.

◆ forward_win_

ScrollView* tesseract::Network::forward_win_
protected

Definition at line 303 of file network.h.

◆ name_

STRING tesseract::Network::name_
protected

Definition at line 300 of file network.h.

◆ needs_to_backprop_

bool tesseract::Network::needs_to_backprop_
protected

Definition at line 295 of file network.h.

◆ network_flags_

int32_t tesseract::Network::network_flags_
protected

Definition at line 296 of file network.h.

◆ ni_

int32_t tesseract::Network::ni_
protected

Definition at line 297 of file network.h.

◆ no_

int32_t tesseract::Network::no_
protected

Definition at line 298 of file network.h.

◆ num_weights_

int32_t tesseract::Network::num_weights_
protected

Definition at line 299 of file network.h.

◆ randomizer_

TRand* tesseract::Network::randomizer_
protected

Definition at line 305 of file network.h.

◆ training_

TrainingState tesseract::Network::training_
protected

Definition at line 294 of file network.h.

◆ type_

NetworkType tesseract::Network::type_
protected

Definition at line 293 of file network.h.


The documentation for this class was generated from the following files:
tesseract::TS_ENABLED
Definition: network.h:95
ScrollView
Definition: scrollview.h:97
tesseract::NT_PARALLEL
Definition: network.h:49
tesseract::NT_POSCLIP
Definition: network.h:63
tesseract::NT_PAR_2D_LSTM
Definition: network.h:53
tesseract::NT_XYTRANSPOSE
Definition: network.h:58
tesseract::NT_SOFTMAX_NO_CTC
Definition: network.h:69
tesseract::NT_PAR_RL_LSTM
Definition: network.h:51
tesseract::Network::needs_to_backprop
bool needs_to_backprop() const
Definition: network.h:116
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
tesseract::Network::backward_win_
ScrollView * backward_win_
Definition: network.h:304
STRING
Definition: strngs.h:45
ScrollView::Image
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:763
tesseract::Network::type
NetworkType type() const
Definition: network.h:112
tesseract::NT_REPLICATED
Definition: network.h:50
tesseract::NetworkType
NetworkType
Definition: network.h:43
tesseract::Network::needs_to_backprop_
bool needs_to_backprop_
Definition: network.h:295
tesseract::NT_LSTM
Definition: network.h:60
tesseract::NT_SYMCLIP
Definition: network.h:64
STRING::DeSerialize
bool DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:157
STRING::Serialize
bool Serialize(FILE *fp) const
Definition: strngs.cpp:144
tesseract::kMinWinSize
const int kMinWinSize
Definition: network.cpp:49
tesseract::Network::name_
STRING name_
Definition: network.h:300
tesseract::NT_SERIES
Definition: network.h:54
tesseract::Network::type_
NetworkType type_
Definition: network.h:293
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
tesseract::NT_PAR_UD_LSTM
Definition: network.h:52
tesseract::NT_YREVERSED
Definition: network.h:57
tesseract::Network::forward_win_
ScrollView * forward_win_
Definition: network.h:303
tesseract::NT_TANH
Definition: network.h:65
tesseract::Network::randomizer_
TRand * randomizer_
Definition: network.h:305
tesseract::Network::training_
TrainingState training_
Definition: network.h:294
tesseract::NT_CONVOLVE
Definition: network.h:47
tesseract::TS_RE_ENABLE
Definition: network.h:99
tesseract::NT_INPUT
Definition: network.h:45
tesseract::NT_TENSORFLOW
Definition: network.h:78
tesseract::TRand::SignedRand
double SignedRand(double range)
Definition: helpers.h:85
tesseract::NT_XREVERSED
Definition: network.h:56
tesseract::TS_DISABLED
Definition: network.h:94
tesseract::NT_LSTM_SOFTMAX_ENCODED
Definition: network.h:76
tesseract::kXWinFrameSize
const int kXWinFrameSize
Definition: network.cpp:52
tesseract::Network::num_weights_
int32_t num_weights_
Definition: network.h:299
tesseract::Network::name
const STRING & name() const
Definition: network.h:138
tesseract::Network::ClearWindow
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
Definition: network.cpp:312
tesseract::NT_RELU
Definition: network.h:66
tesseract::TrainingState
TrainingState
Definition: network.h:92
tesseract::kMaxWinSize
const int kMaxWinSize
Definition: network.cpp:50
tesseract::NT_NONE
Definition: network.h:44
tesseract::TS_TEMP_DISABLE
Definition: network.h:97
tesseract::NT_LSTM_SOFTMAX
Definition: network.h:75
tesseract::NT_LSTM_SUMMARY
Definition: network.h:61
tesseract::Network::DisplayImage
static int DisplayImage(Pix *pix, ScrollView *window)
Definition: network.cpp:335
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
ScrollView::Update
static void Update()
Definition: scrollview.cpp:708
tesseract::NT_LINEAR
Definition: network.h:67
tesseract::NT_LOGISTIC
Definition: network.h:62
tesseract::kYWinFrameSize
const int kYWinFrameSize
Definition: network.cpp:53
tesseract::Network::no_
int32_t no_
Definition: network.h:298
tesseract::Network::ni_
int32_t ni_
Definition: network.h:297
tesseract::Network::num_weights
int num_weights() const
Definition: network.h:119
tesseract::NT_MAXPOOL
Definition: network.h:48
tesseract::Network::network_flags_
int32_t network_flags_
Definition: network.h:296
tesseract::NT_RECONFIG
Definition: network.h:55
tesseract::NT_SOFTMAX
Definition: network.h:68
tesseract::Network::Network
Network()
Definition: network.cpp:76