tesseract  5.0.0-alpha-619-ge9db
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 
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  ~Plumbing() override = default;
35 
36  // Returns the required shape input to the network.
37  StaticShape InputShape() const override { return stack_[0]->InputShape(); }
38  STRING spec() const override {
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  bool IsPlumbingType() const override { 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  void SetEnableTraining(TrainingState state) override;
49 
50  // Sets flags that control the action of the network. See NetworkFlags enum
51  // for bit values.
52  void SetNetworkFlags(uint32_t flags) override;
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  int InitWeights(float range, TRand* randomizer) override;
60  // Recursively searches the network for softmaxes with old_no outputs,
61  // and remaps their outputs according to code_map. See network.h for details.
62  int RemapOutputs(int old_no, const std::vector<int>& code_map) override;
63 
64  // Converts a float network to an int network.
65  void ConvertToInt() override;
66 
67  // Provides a pointer to a TRand for any networks that care to use it.
68  // Note that randomizer is a borrowed pointer that should outlive the network
69  // and should not be deleted by any of the networks.
70  void SetRandomizer(TRand* randomizer) override;
71 
72  // Adds the given network to the stack.
73  virtual void AddToStack(Network* network);
74 
75  // Sets needs_to_backprop_ to needs_backprop and returns true if
76  // needs_backprop || any weights in this network so the next layer forward
77  // can be told to produce backprop for this layer if needed.
78  bool SetupNeedsBackprop(bool needs_backprop) override;
79 
80  // Returns an integer reduction factor that the network applies to the
81  // time sequence. Assumes that any 2-d is already eliminated. Used for
82  // scaling bounding boxes of truth data.
83  // WARNING: if GlobalMinimax is used to vary the scale, this will return
84  // the last used scale factor. Call it before any forward, and it will return
85  // the minimum scale factor of the paths through the GlobalMinimax.
86  int XScaleFactor() const override;
87 
88  // Provides the (minimum) x scale factor to the network (of interest only to
89  // input units) so they can determine how to scale bounding boxes.
90  void CacheXScaleFactor(int factor) override;
91 
92  // Provides debug output on the weights.
93  void DebugWeights() override;
94 
95  // Returns the current stack.
96  const PointerVector<Network>& stack() const {
97  return stack_;
98  }
99  // Returns a set of strings representing the layer-ids of all layers below.
100  void EnumerateLayers(const STRING* prefix,
101  GenericVector<STRING>* layers) const;
102  // Returns a pointer to the network layer corresponding to the given id.
103  Network* GetLayer(const char* id) const;
104  // Returns the learning rate for a specific layer of the stack.
105  float LayerLearningRate(const char* id) const {
106  const float* lr_ptr = LayerLearningRatePtr(id);
107  ASSERT_HOST(lr_ptr != nullptr);
108  return *lr_ptr;
109  }
110  // Scales the learning rate for a specific layer of the stack.
111  void ScaleLayerLearningRate(const char* id, double factor) {
112  float* lr_ptr = LayerLearningRatePtr(id);
113  ASSERT_HOST(lr_ptr != nullptr);
114  *lr_ptr *= factor;
115  }
116  // Returns a pointer to the learning rate for the given layer id.
117  float* LayerLearningRatePtr(const char* id) const;
118 
119  // Writes to the given file. Returns false in case of error.
120  bool Serialize(TFile* fp) const override;
121  // Reads from the given file. Returns false in case of error.
122  bool DeSerialize(TFile* fp) override;
123 
124  // Updates the weights using the given learning rate, momentum and adam_beta.
125  // num_samples is used in the adam computation iff use_adam_ is true.
126  void Update(float learning_rate, float momentum, float adam_beta,
127  int num_samples) override;
128  // Sums the products of weight updates in *this and other, splitting into
129  // positive (same direction) in *same and negative (different direction) in
130  // *changed.
131  void CountAlternators(const Network& other, double* same,
132  double* changed) const override;
133 
134  protected:
135  // The networks.
137  // Layer-specific learning rate iff network_flags_ & NF_LAYER_SPECIFIC_LR.
138  // One element for each element of stack_.
140 };
141 
142 } // namespace tesseract.
143 
144 #endif // TESSERACT_LSTM_PLUMBING_H_
tesseract::StaticShape
Definition: static_shape.h:38
tesseract::Plumbing::IsPlumbingType
bool IsPlumbingType() const override
Definition: plumbing.h:44
tesseract::Plumbing::learning_rates_
GenericVector< float > learning_rates_
Definition: plumbing.h:139
tesseract::Plumbing::AddToStack
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:82
tesseract::Plumbing::~Plumbing
~Plumbing() override=default
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
tesseract::Plumbing::EnumerateLayers
void EnumerateLayers(const STRING *prefix, GenericVector< STRING > *layers) const
Definition: plumbing.cpp:139
tesseract::Plumbing::Plumbing
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
tesseract::PointerVector
Definition: genericvector.h:417
STRING
Definition: strngs.h:45
network.h
tesseract::Plumbing::CountAlternators
void CountAlternators(const Network &other, double *same, double *changed) const override
Definition: plumbing.cpp:234
tesseract::Plumbing::RemapOutputs
int RemapOutputs(int old_no, const std::vector< int > &code_map) override
Definition: plumbing.cpp:59
tesseract::Plumbing::spec
STRING spec() const override
Definition: plumbing.h:38
tesseract::Plumbing::CacheXScaleFactor
void CacheXScaleFactor(int factor) override
Definition: plumbing.cpp:126
tesseract::Plumbing::stack_
PointerVector< Network > stack_
Definition: plumbing.h:136
tesseract::Plumbing::InitWeights
int InitWeights(float range, TRand *randomizer) override
Definition: plumbing.cpp:50
genericvector.h
tesseract::Plumbing::SetRandomizer
void SetRandomizer(TRand *randomizer) override
Definition: plumbing.cpp:76
tesseract::Plumbing::SetEnableTraining
void SetEnableTraining(TrainingState state) override
Definition: plumbing.cpp:31
matrix.h
tesseract::TFile
Definition: serialis.h:75
tesseract::Plumbing::ConvertToInt
void ConvertToInt() override
Definition: plumbing.cpp:68
tesseract::Plumbing::LayerLearningRatePtr
float * LayerLearningRatePtr(const char *id) const
Definition: plumbing.cpp:168
tesseract::Plumbing::GetLayer
Network * GetLayer(const char *id) const
Definition: plumbing.cpp:155
tesseract::Plumbing
Definition: plumbing.h:30
tesseract
Definition: baseapi.h:65
tesseract::Plumbing::Update
void Update(float learning_rate, float momentum, float adam_beta, int num_samples) override
Definition: plumbing.cpp:216
GenericVector< STRING >
tesseract::Plumbing::SetupNeedsBackprop
bool SetupNeedsBackprop(bool needs_backprop) override
Definition: plumbing.cpp:100
tesseract::Plumbing::DebugWeights
void DebugWeights() override
Definition: plumbing.cpp:133
tesseract::Plumbing::ScaleLayerLearningRate
void ScaleLayerLearningRate(const char *id, double factor)
Definition: plumbing.h:111
tesseract::Network
Definition: network.h:105
tesseract::Plumbing::DeSerialize
bool DeSerialize(TFile *fp) override
Definition: plumbing.cpp:197
tesseract::Network::name
const STRING & name() const
Definition: network.h:138
tesseract::TrainingState
TrainingState
Definition: network.h:92
tesseract::Plumbing::InputShape
StaticShape InputShape() const override
Definition: plumbing.h:37
tesseract::Plumbing::stack
const PointerVector< Network > & stack() const
Definition: plumbing.h:96
tesseract::Plumbing::XScaleFactor
int XScaleFactor() const override
Definition: plumbing.cpp:120
tesseract::Plumbing::LayerLearningRate
float LayerLearningRate(const char *id) const
Definition: plumbing.h:105
tesseract::TRand
Definition: helpers.h:50
tesseract::Plumbing::SetNetworkFlags
void SetNetworkFlags(uint32_t flags) override
Definition: plumbing.cpp:39
tesseract::Plumbing::Serialize
bool Serialize(TFile *fp) const override
Definition: plumbing.cpp:182