tesseract  4.00.00dev
plumbing.h
Go to the documentation of this file.
1 // File: plumbing.h
3 // Description: Base class for networks that organize other networks
4 // eg series or parallel.
5 // Author: Ray Smith
6 // Created: Mon May 12 08:11:36 PST 2014
7 //
8 // (C) Copyright 2014, Google Inc.
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 
20 #ifndef TESSERACT_LSTM_PLUMBING_H_
21 #define TESSERACT_LSTM_PLUMBING_H_
22 
23 #include "genericvector.h"
24 #include "matrix.h"
25 #include "network.h"
26 
27 namespace tesseract {
28 
29 // Holds a collection of other networks and forwards calls to each of them.
30 class Plumbing : public Network {
31  public:
32  // ni_ and no_ will be set by AddToStack.
33  explicit Plumbing(const STRING& name);
34  virtual ~Plumbing();
35 
36  // Returns the required shape input to the network.
37  virtual StaticShape InputShape() const { return stack_[0]->InputShape(); }
38  virtual STRING spec() const {
39  return "Sub-classes of Plumbing must implement spec()!";
40  }
41 
42  // Returns true if the given type is derived from Plumbing, and thus contains
43  // multiple sub-networks that can have their own learning rate.
44  virtual bool IsPlumbingType() const { return true; }
45 
46  // Suspends/Enables training by setting the training_ flag. Serialize and
47  // DeSerialize only operate on the run-time data if state is false.
48  virtual void SetEnableTraining(TrainingState state);
49 
50  // Sets flags that control the action of the network. See NetworkFlags enum
51  // for bit values.
52  virtual void SetNetworkFlags(uinT32 flags);
53 
54  // Sets up the network for training. Initializes weights using weights of
55  // scale `range` picked according to the random number generator `randomizer`.
56  // Note that randomizer is a borrowed pointer that should outlive the network
57  // and should not be deleted by any of the networks.
58  // Returns the number of weights initialized.
59  virtual int InitWeights(float range, TRand* randomizer);
60 
61  // Converts a float network to an int network.
62  virtual void ConvertToInt();
63 
64  // Provides a pointer to a TRand for any networks that care to use it.
65  // Note that randomizer is a borrowed pointer that should outlive the network
66  // and should not be deleted by any of the networks.
67  virtual void SetRandomizer(TRand* randomizer);
68 
69  // Adds the given network to the stack.
70  virtual void AddToStack(Network* network);
71 
72  // Sets needs_to_backprop_ to needs_backprop and returns true if
73  // needs_backprop || any weights in this network so the next layer forward
74  // can be told to produce backprop for this layer if needed.
75  virtual bool SetupNeedsBackprop(bool needs_backprop);
76 
77  // Returns an integer reduction factor that the network applies to the
78  // time sequence. Assumes that any 2-d is already eliminated. Used for
79  // scaling bounding boxes of truth data.
80  // WARNING: if GlobalMinimax is used to vary the scale, this will return
81  // the last used scale factor. Call it before any forward, and it will return
82  // the minimum scale factor of the paths through the GlobalMinimax.
83  virtual int XScaleFactor() const;
84 
85  // Provides the (minimum) x scale factor to the network (of interest only to
86  // input units) so they can determine how to scale bounding boxes.
87  virtual void CacheXScaleFactor(int factor);
88 
89  // Provides debug output on the weights.
90  virtual void DebugWeights();
91 
92  // Returns the current stack.
93  const PointerVector<Network>& stack() const {
94  return stack_;
95  }
96  // Returns a set of strings representing the layer-ids of all layers below.
97  void EnumerateLayers(const STRING* prefix,
98  GenericVector<STRING>* layers) const;
99  // Returns a pointer to the network layer corresponding to the given id.
100  Network* GetLayer(const char* id) const;
101  // Returns the learning rate for a specific layer of the stack.
102  float LayerLearningRate(const char* id) const {
103  const float* lr_ptr = LayerLearningRatePtr(id);
104  ASSERT_HOST(lr_ptr != NULL);
105  return *lr_ptr;
106  }
107  // Scales the learning rate for a specific layer of the stack.
108  void ScaleLayerLearningRate(const char* id, double factor) {
109  float* lr_ptr = LayerLearningRatePtr(id);
110  ASSERT_HOST(lr_ptr != NULL);
111  *lr_ptr *= factor;
112  }
113  // Returns a pointer to the learning rate for the given layer id.
114  float* LayerLearningRatePtr(const char* id) const;
115 
116  // Writes to the given file. Returns false in case of error.
117  virtual bool Serialize(TFile* fp) const;
118  // Reads from the given file. Returns false in case of error.
119  virtual bool DeSerialize(TFile* fp);
120 
121  // Updates the weights using the given learning rate and momentum.
122  // num_samples is the quotient to be used in the adagrad computation iff
123  // use_ada_grad_ is true.
124  virtual void Update(float learning_rate, float momentum, int num_samples);
125  // Sums the products of weight updates in *this and other, splitting into
126  // positive (same direction) in *same and negative (different direction) in
127  // *changed.
128  virtual void CountAlternators(const Network& other, double* same,
129  double* changed) const;
130 
131  protected:
132  // The networks.
134  // Layer-specific learning rate iff network_flags_ & NF_LAYER_SPECIFIC_LR.
135  // One element for each element of stack_.
137 };
138 
139 } // namespace tesseract.
140 
141 #endif // TESSERACT_LSTM_PLUMBING_H_
142 
const PointerVector< Network > & stack() const
Definition: plumbing.h:93
float * LayerLearningRatePtr(const char *id) const
Definition: plumbing.cpp:161
virtual void Update(float learning_rate, float momentum, int num_samples)
Definition: plumbing.cpp:210
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
virtual int InitWeights(float range, TRand *randomizer)
Definition: plumbing.cpp:53
Network * GetLayer(const char *id) const
Definition: plumbing.cpp:148
virtual void SetNetworkFlags(uinT32 flags)
Definition: plumbing.cpp:42
virtual void CacheXScaleFactor(int factor)
Definition: plumbing.cpp:119
virtual STRING spec() const
Definition: plumbing.h:38
TrainingState
Definition: network.h:92
virtual bool DeSerialize(TFile *fp)
Definition: plumbing.cpp:190
#define ASSERT_HOST(x)
Definition: errcode.h:84
virtual void SetEnableTraining(TrainingState state)
Definition: plumbing.cpp:34
virtual bool SetupNeedsBackprop(bool needs_backprop)
Definition: plumbing.cpp:93
uint32_t uinT32
Definition: host.h:39
PointerVector< Network > stack_
Definition: plumbing.h:133
Definition: strngs.h:45
virtual int XScaleFactor() const
Definition: plumbing.cpp:113
virtual void SetRandomizer(TRand *randomizer)
Definition: plumbing.cpp:69
virtual void ConvertToInt()
Definition: plumbing.cpp:61
virtual StaticShape InputShape() const
Definition: plumbing.h:37
virtual bool IsPlumbingType() const
Definition: plumbing.h:44
void ScaleLayerLearningRate(const char *id, double factor)
Definition: plumbing.h:108
virtual void DebugWeights()
Definition: plumbing.cpp:126
const STRING & name() const
Definition: network.h:138
GenericVector< float > learning_rates_
Definition: plumbing.h:136
void EnumerateLayers(const STRING *prefix, GenericVector< STRING > *layers) const
Definition: plumbing.cpp:132
virtual void CountAlternators(const Network &other, double *same, double *changed) const
Definition: plumbing.cpp:227
virtual ~Plumbing()
Definition: plumbing.cpp:29
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:75
virtual bool Serialize(TFile *fp) const
Definition: plumbing.cpp:175
float LayerLearningRate(const char *id) const
Definition: plumbing.h:102