tesseract  5.0.0-alpha-619-ge9db
tesseract::Plumbing Class Reference

#include <plumbing.h>

Inheritance diagram for tesseract::Plumbing:
tesseract::Network tesseract::Parallel tesseract::Reversed tesseract::Series

Public Member Functions

 Plumbing (const STRING &name)
 
 ~Plumbing () override=default
 
StaticShape InputShape () const override
 
STRING spec () const override
 
bool IsPlumbingType () const override
 
void SetEnableTraining (TrainingState state) override
 
void SetNetworkFlags (uint32_t flags) override
 
int InitWeights (float range, TRand *randomizer) override
 
int RemapOutputs (int old_no, const std::vector< int > &code_map) override
 
void ConvertToInt () override
 
void SetRandomizer (TRand *randomizer) override
 
virtual void AddToStack (Network *network)
 
bool SetupNeedsBackprop (bool needs_backprop) override
 
int XScaleFactor () const override
 
void CacheXScaleFactor (int factor) override
 
void DebugWeights () override
 
const PointerVector< Network > & stack () const
 
void EnumerateLayers (const STRING *prefix, GenericVector< STRING > *layers) const
 
NetworkGetLayer (const char *id) const
 
float LayerLearningRate (const char *id) const
 
void ScaleLayerLearningRate (const char *id, double factor)
 
float * LayerLearningRatePtr (const char *id) const
 
bool Serialize (TFile *fp) const override
 
bool DeSerialize (TFile *fp) override
 
void Update (float learning_rate, float momentum, float adam_beta, int num_samples) override
 
void CountAlternators (const Network &other, double *same, double *changed) const override
 
- Public Member Functions inherited from tesseract::Network
 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 OutputShape (const StaticShape &input_shape) const
 
const STRINGname () const
 
bool TestFlag (NetworkFlags flag) 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)
 

Protected Attributes

PointerVector< Networkstack_
 
GenericVector< float > learning_rates_
 
- Protected Attributes inherited from tesseract::Network
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_
 

Additional Inherited Members

- Static Public Member Functions inherited from tesseract::Network
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 inherited from tesseract::Network
double Random (double range)
 

Detailed Description

Definition at line 30 of file plumbing.h.

Constructor & Destructor Documentation

◆ Plumbing()

tesseract::Plumbing::Plumbing ( const STRING name)
explicit

Definition at line 25 of file plumbing.cpp.

26  : Network(NT_PARALLEL, name, 0, 0) {
27 }

◆ ~Plumbing()

tesseract::Plumbing::~Plumbing ( )
overridedefault

Member Function Documentation

◆ AddToStack()

void tesseract::Plumbing::AddToStack ( Network network)
virtual

Definition at line 82 of file plumbing.cpp.

82  {
83  if (stack_.empty()) {
84  ni_ = network->NumInputs();
85  no_ = network->NumOutputs();
86  } else if (type_ == NT_SERIES) {
87  // ni is input of first, no output of last, others match output to input.
88  ASSERT_HOST(no_ == network->NumInputs());
89  no_ = network->NumOutputs();
90  } else {
91  // All parallel types. Output is sum of outputs, inputs all match.
92  ASSERT_HOST(ni_ == network->NumInputs());
93  no_ += network->NumOutputs();
94  }
95  stack_.push_back(network);
96 }

◆ CacheXScaleFactor()

void tesseract::Plumbing::CacheXScaleFactor ( int  factor)
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 126 of file plumbing.cpp.

126  {
127  for (int i = 0; i < stack_.size(); ++i) {
128  stack_[i]->CacheXScaleFactor(factor);
129  }
130 }

◆ ConvertToInt()

void tesseract::Plumbing::ConvertToInt ( )
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 68 of file plumbing.cpp.

68  {
69  for (int i = 0; i < stack_.size(); ++i)
70  stack_[i]->ConvertToInt();
71 }

◆ CountAlternators()

void tesseract::Plumbing::CountAlternators ( const Network other,
double *  same,
double *  changed 
) const
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 234 of file plumbing.cpp.

235  {
236  ASSERT_HOST(other.type() == type_);
237  const auto* plumbing = static_cast<const Plumbing*>(&other);
238  ASSERT_HOST(plumbing->stack_.size() == stack_.size());
239  for (int i = 0; i < stack_.size(); ++i)
240  stack_[i]->CountAlternators(*plumbing->stack_[i], same, changed);
241 }

◆ DebugWeights()

void tesseract::Plumbing::DebugWeights ( )
overridevirtual

Implements tesseract::Network.

Definition at line 133 of file plumbing.cpp.

133  {
134  for (int i = 0; i < stack_.size(); ++i)
135  stack_[i]->DebugWeights();
136 }

◆ DeSerialize()

bool tesseract::Plumbing::DeSerialize ( TFile fp)
overridevirtual

Implements tesseract::Network.

Definition at line 197 of file plumbing.cpp.

197  {
198  stack_.truncate(0);
199  no_ = 0; // We will be modifying this as we AddToStack.
200  uint32_t size;
201  if (!fp->DeSerialize(&size)) return false;
202  for (uint32_t i = 0; i < size; ++i) {
203  Network* network = CreateFromFile(fp);
204  if (network == nullptr) return false;
205  AddToStack(network);
206  }
209  return false;
210  }
211  return true;
212 }

◆ EnumerateLayers()

void tesseract::Plumbing::EnumerateLayers ( const STRING prefix,
GenericVector< STRING > *  layers 
) const

Definition at line 139 of file plumbing.cpp.

140  {
141  for (int i = 0; i < stack_.size(); ++i) {
142  STRING layer_name;
143  if (prefix) layer_name = *prefix;
144  layer_name.add_str_int(":", i);
145  if (stack_[i]->IsPlumbingType()) {
146  auto* plumbing = static_cast<Plumbing*>(stack_[i]);
147  plumbing->EnumerateLayers(&layer_name, layers);
148  } else {
149  layers->push_back(layer_name);
150  }
151  }
152 }

◆ GetLayer()

Network * tesseract::Plumbing::GetLayer ( const char *  id) const

Definition at line 155 of file plumbing.cpp.

155  {
156  char* next_id;
157  int index = strtol(id, &next_id, 10);
158  if (index < 0 || index >= stack_.size()) return nullptr;
159  if (stack_[index]->IsPlumbingType()) {
160  auto* plumbing = static_cast<Plumbing*>(stack_[index]);
161  ASSERT_HOST(*next_id == ':');
162  return plumbing->GetLayer(next_id + 1);
163  }
164  return stack_[index];
165 }

◆ InitWeights()

int tesseract::Plumbing::InitWeights ( float  range,
TRand randomizer 
)
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 50 of file plumbing.cpp.

50  {
51  num_weights_ = 0;
52  for (int i = 0; i < stack_.size(); ++i)
53  num_weights_ += stack_[i]->InitWeights(range, randomizer);
54  return num_weights_;
55 }

◆ InputShape()

StaticShape tesseract::Plumbing::InputShape ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Definition at line 37 of file plumbing.h.

37 { return stack_[0]->InputShape(); }

◆ IsPlumbingType()

bool tesseract::Plumbing::IsPlumbingType ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Definition at line 44 of file plumbing.h.

44 { return true; }

◆ LayerLearningRate()

float tesseract::Plumbing::LayerLearningRate ( const char *  id) const
inline

Definition at line 105 of file plumbing.h.

105  {
106  const float* lr_ptr = LayerLearningRatePtr(id);
107  ASSERT_HOST(lr_ptr != nullptr);
108  return *lr_ptr;
109  }

◆ LayerLearningRatePtr()

float * tesseract::Plumbing::LayerLearningRatePtr ( const char *  id) const

Definition at line 168 of file plumbing.cpp.

168  {
169  char* next_id;
170  int index = strtol(id, &next_id, 10);
171  if (index < 0 || index >= stack_.size()) return nullptr;
172  if (stack_[index]->IsPlumbingType()) {
173  auto* plumbing = static_cast<Plumbing*>(stack_[index]);
174  ASSERT_HOST(*next_id == ':');
175  return plumbing->LayerLearningRatePtr(next_id + 1);
176  }
177  if (index >= learning_rates_.size()) return nullptr;
178  return &learning_rates_[index];
179 }

◆ RemapOutputs()

int tesseract::Plumbing::RemapOutputs ( int  old_no,
const std::vector< int > &  code_map 
)
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 59 of file plumbing.cpp.

59  {
60  num_weights_ = 0;
61  for (int i = 0; i < stack_.size(); ++i) {
62  num_weights_ += stack_[i]->RemapOutputs(old_no, code_map);
63  }
64  return num_weights_;
65 }

◆ ScaleLayerLearningRate()

void tesseract::Plumbing::ScaleLayerLearningRate ( const char *  id,
double  factor 
)
inline

Definition at line 111 of file plumbing.h.

111  {
112  float* lr_ptr = LayerLearningRatePtr(id);
113  ASSERT_HOST(lr_ptr != nullptr);
114  *lr_ptr *= factor;
115  }

◆ Serialize()

bool tesseract::Plumbing::Serialize ( TFile fp) const
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 182 of file plumbing.cpp.

182  {
183  if (!Network::Serialize(fp)) return false;
184  uint32_t size = stack_.size();
185  // Can't use PointerVector::Serialize here as we need a special DeSerialize.
186  if (!fp->Serialize(&size)) return false;
187  for (uint32_t i = 0; i < size; ++i)
188  if (!stack_[i]->Serialize(fp)) return false;
190  !learning_rates_.Serialize(fp)) {
191  return false;
192  }
193  return true;
194 }

◆ SetEnableTraining()

void tesseract::Plumbing::SetEnableTraining ( TrainingState  state)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 31 of file plumbing.cpp.

31  {
33  for (int i = 0; i < stack_.size(); ++i)
34  stack_[i]->SetEnableTraining(state);
35 }

◆ SetNetworkFlags()

void tesseract::Plumbing::SetNetworkFlags ( uint32_t  flags)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 39 of file plumbing.cpp.

39  {
41  for (int i = 0; i < stack_.size(); ++i)
42  stack_[i]->SetNetworkFlags(flags);
43 }

◆ SetRandomizer()

void tesseract::Plumbing::SetRandomizer ( TRand randomizer)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 76 of file plumbing.cpp.

76  {
77  for (int i = 0; i < stack_.size(); ++i)
78  stack_[i]->SetRandomizer(randomizer);
79 }

◆ SetupNeedsBackprop()

bool tesseract::Plumbing::SetupNeedsBackprop ( bool  needs_backprop)
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 100 of file plumbing.cpp.

100  {
101  if (IsTraining()) {
102  needs_to_backprop_ = needs_backprop;
103  bool retval = needs_backprop;
104  for (int i = 0; i < stack_.size(); ++i) {
105  if (stack_[i]->SetupNeedsBackprop(needs_backprop)) retval = true;
106  }
107  return retval;
108  }
109  // Frozen networks don't do backprop.
110  needs_to_backprop_ = false;
111  return false;
112 }

◆ spec()

STRING tesseract::Plumbing::spec ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

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

Definition at line 38 of file plumbing.h.

38  {
39  return "Sub-classes of Plumbing must implement spec()!";
40  }

◆ stack()

const PointerVector<Network>& tesseract::Plumbing::stack ( ) const
inline

Definition at line 96 of file plumbing.h.

96  {
97  return stack_;
98  }

◆ Update()

void tesseract::Plumbing::Update ( float  learning_rate,
float  momentum,
float  adam_beta,
int  num_samples 
)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 216 of file plumbing.cpp.

217  {
218  for (int i = 0; i < stack_.size(); ++i) {
220  if (i < learning_rates_.size())
221  learning_rate = learning_rates_[i];
222  else
223  learning_rates_.push_back(learning_rate);
224  }
225  if (stack_[i]->IsTraining()) {
226  stack_[i]->Update(learning_rate, momentum, adam_beta, num_samples);
227  }
228  }
229 }

◆ XScaleFactor()

int tesseract::Plumbing::XScaleFactor ( ) const
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 120 of file plumbing.cpp.

120  {
121  return stack_[0]->XScaleFactor();
122 }

Member Data Documentation

◆ learning_rates_

GenericVector<float> tesseract::Plumbing::learning_rates_
protected

Definition at line 139 of file plumbing.h.

◆ stack_

PointerVector<Network> tesseract::Plumbing::stack_
protected

Definition at line 136 of file plumbing.h.


The documentation for this class was generated from the following files:
tesseract::Plumbing::IsPlumbingType
bool IsPlumbingType() const override
Definition: plumbing.h:44
tesseract::NT_PARALLEL
Definition: network.h:49
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
tesseract::Plumbing::learning_rates_
GenericVector< float > learning_rates_
Definition: plumbing.h:139
tesseract::Network::SetEnableTraining
virtual void SetEnableTraining(TrainingState state)
Definition: network.cpp:110
tesseract::Plumbing::AddToStack
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:82
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
STRING
Definition: strngs.h:45
GenericVector::Serialize
bool Serialize(FILE *fp) const
Definition: genericvector.h:929
tesseract::Network::needs_to_backprop_
bool needs_to_backprop_
Definition: network.h:295
tesseract::Plumbing::CountAlternators
void CountAlternators(const Network &other, double *same, double *changed) const override
Definition: plumbing.cpp:234
tesseract::Plumbing::stack_
PointerVector< Network > stack_
Definition: plumbing.h:136
tesseract::Network::IsTraining
bool IsTraining() const
Definition: network.h:115
tesseract::Plumbing::InitWeights
int InitWeights(float range, TRand *randomizer) override
Definition: plumbing.cpp:50
tesseract::Plumbing::SetRandomizer
void SetRandomizer(TRand *randomizer) override
Definition: plumbing.cpp:76
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
tesseract::Plumbing::SetEnableTraining
void SetEnableTraining(TrainingState state) override
Definition: plumbing.cpp:31
tesseract::Network::CreateFromFile
static Network * CreateFromFile(TFile *fp)
Definition: network.cpp:187
tesseract::NT_SERIES
Definition: network.h:54
tesseract::Network::type_
NetworkType type_
Definition: network.h:293
GenericVector::DeSerialize
bool DeSerialize(bool swap, FILE *fp)
Definition: genericvector.h:954
tesseract::Plumbing::ConvertToInt
void ConvertToInt() override
Definition: plumbing.cpp:68
tesseract::Plumbing::LayerLearningRatePtr
float * LayerLearningRatePtr(const char *id) const
Definition: plumbing.cpp:168
tesseract::Network::SetNetworkFlags
virtual void SetNetworkFlags(uint32_t flags)
Definition: network.cpp:124
tesseract::Plumbing::SetupNeedsBackprop
bool SetupNeedsBackprop(bool needs_backprop) override
Definition: plumbing.cpp:100
tesseract::Plumbing::DebugWeights
void DebugWeights() override
Definition: plumbing.cpp:133
tesseract::Network::num_weights_
int32_t num_weights_
Definition: network.h:299
tesseract::Network::name
const STRING & name() const
Definition: network.h:138
tesseract::NF_LAYER_SPECIFIC_LR
Definition: network.h:87
tesseract::Network::Serialize
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151
tesseract::Network::no_
int32_t no_
Definition: network.h:298
tesseract::Network::ni_
int32_t ni_
Definition: network.h:297
GenericVector::size
int size() const
Definition: genericvector.h:71
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
tesseract::Network::network_flags_
int32_t network_flags_
Definition: network.h:296
tesseract::Network::Network
Network()
Definition: network.cpp:76