tesseract 4.1.1
Loading...
Searching...
No Matches
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),
81 ni_(0),
82 no_(0),
83 num_weights_(0),
84 forward_win_(nullptr),
85 backward_win_(nullptr),
86 randomizer_(nullptr) {}
@ TS_ENABLED
Definition: network.h:95
@ NT_NONE
Definition: network.h:44
int32_t network_flags_
Definition: network.h:296
NetworkType type_
Definition: network.h:293
bool needs_to_backprop_
Definition: network.h:295
ScrollView * forward_win_
Definition: network.h:303
ScrollView * backward_win_
Definition: network.h:304
int32_t num_weights_
Definition: network.h:299
TrainingState training_
Definition: network.h:294
TRand * randomizer_
Definition: network.h:305

◆ 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),
92 ni_(ni),
93 no_(no),
94 num_weights_(0),
95 name_(name),
96 forward_win_(nullptr),
97 backward_win_(nullptr),
98 randomizer_(nullptr) {}
const STRING & name() const
Definition: network.h:138
NetworkType type() const
Definition: network.h:112

◆ ~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 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}
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:35
const int kXWinFrameSize
Definition: network.cpp:52
const int kYWinFrameSize
Definition: network.cpp:53
const int kMinWinSize
Definition: network.cpp:49
const int kMaxWinSize
Definition: network.cpp:50

◆ ConvertToInt()

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

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

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:
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}
TrainingState
Definition: network.h:92
@ TS_DISABLED
Definition: network.h:94
NetworkType
Definition: network.h:43
@ NT_LINEAR
Definition: network.h:67
@ NT_MAXPOOL
Definition: network.h:48
@ NT_RELU
Definition: network.h:66
@ NT_XREVERSED
Definition: network.h:56
@ NT_LSTM
Definition: network.h:60
@ NT_CONVOLVE
Definition: network.h:47
@ NT_SOFTMAX
Definition: network.h:68
@ NT_LOGISTIC
Definition: network.h:62
@ NT_PAR_UD_LSTM
Definition: network.h:52
@ NT_LSTM_SOFTMAX_ENCODED
Definition: network.h:76
@ NT_PARALLEL
Definition: network.h:49
@ NT_SYMCLIP
Definition: network.h:64
@ NT_PAR_2D_LSTM
Definition: network.h:53
@ NT_LSTM_SUMMARY
Definition: network.h:61
@ NT_YREVERSED
Definition: network.h:57
@ NT_RECONFIG
Definition: network.h:55
@ NT_INPUT
Definition: network.h:45
@ NT_TENSORFLOW
Definition: network.h:78
@ NT_POSCLIP
Definition: network.h:63
@ NT_LSTM_SOFTMAX
Definition: network.h:75
@ NT_XYTRANSPOSE
Definition: network.h:58
@ NT_SERIES
Definition: network.h:54
@ NT_SOFTMAX_NO_CTC
Definition: network.h:69
@ NT_TANH
Definition: network.h:65
@ NT_PAR_RL_LSTM
Definition: network.h:51
@ NT_REPLICATED
Definition: network.h:50
Definition: strngs.h:45
bool DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:159
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 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.string(), pixGetWidth(image),
304 pixGetHeight(image), &backward_win_);
307#endif // GRAPHICS_DISABLED
308}
const char * string() const
Definition: strngs.cpp:194
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
Definition: network.cpp:312
static int DisplayImage(Pix *pix, ScrollView *window)
Definition: network.cpp:335
static void Update()
Definition: scrollview.cpp:709

◆ 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_.string(), pixGetWidth(image),
292 pixGetHeight(image), &forward_win_);
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}
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:765

◆ 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 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::FullyConnected, tesseract::Input, tesseract::LSTM, tesseract::Parallel, tesseract::Reconfig, 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}
#define ASSERT_HOST(x)
Definition: errcode.h:88
double SignedRand(double range)
Definition: helpers.h:55

◆ 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 186 of file network.h.

186 {
187 return 0;
188 }

◆ 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 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}
bool Serialize(FILE *fp) const
Definition: strngs.cpp:146

◆ SetEnableTraining()

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

Reimplemented in tesseract::FullyConnected, tesseract::LSTM, 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}
@ TS_TEMP_DISABLE
Definition: network.h:97
@ TS_RE_ENABLE
Definition: network.h:99

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

Definition at line 230 of file network.h.

231 {}

◆ XScaleFactor()

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

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

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: