tesseract  4.00.00dev
tesseract::Parallel Class Reference

#include <parallel.h>

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

Public Member Functions

 Parallel (const STRING &name, NetworkType type)
 
virtual ~Parallel ()
 
virtual StaticShape OutputShape (const StaticShape &input_shape) const
 
virtual STRING spec () const
 
virtual void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)
 
virtual bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)
 
- Public Member Functions inherited from tesseract::Plumbing
 Plumbing (const STRING &name)
 
virtual ~Plumbing ()
 
virtual StaticShape InputShape () const
 
virtual bool IsPlumbingType () const
 
virtual void SetEnableTraining (TrainingState state)
 
virtual void SetNetworkFlags (uinT32 flags)
 
virtual int InitWeights (float range, TRand *randomizer)
 
virtual void ConvertToInt ()
 
virtual void SetRandomizer (TRand *randomizer)
 
virtual void AddToStack (Network *network)
 
virtual bool SetupNeedsBackprop (bool needs_backprop)
 
virtual int XScaleFactor () const
 
virtual void CacheXScaleFactor (int factor)
 
virtual void DebugWeights ()
 
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
 
virtual bool Serialize (TFile *fp) const
 
virtual bool DeSerialize (TFile *fp)
 
virtual void Update (float learning_rate, float momentum, int num_samples)
 
virtual void CountAlternators (const Network &other, double *same, double *changed) const
 
- Public Member Functions inherited from tesseract::Network
 Network ()
 
 Network (NetworkType type, const STRING &name, int ni, int no)
 
virtual ~Network ()
 
NetworkType type () const
 
bool IsTraining () const
 
bool needs_to_backprop () const
 
int num_weights () const
 
int NumInputs () const
 
int NumOutputs () const
 
const STRINGname () const
 
bool TestFlag (NetworkFlags flag) const
 
void DisplayForward (const NetworkIO &matrix)
 
void DisplayBackward (const NetworkIO &matrix)
 

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)
 
- Protected Attributes inherited from tesseract::Plumbing
PointerVector< Networkstack_
 
GenericVector< float > learning_rates_
 
- Protected Attributes inherited from tesseract::Network
NetworkType type_
 
TrainingState training_
 
bool needs_to_backprop_
 
inT32 network_flags_
 
inT32 ni_
 
inT32 no_
 
inT32 num_weights_
 
STRING name_
 
ScrollViewforward_win_
 
ScrollViewbackward_win_
 
TRandrandomizer_
 
- Static Protected Attributes inherited from tesseract::Network
static char const *const kTypeNames [NT_COUNT]
 

Detailed Description

Definition at line 27 of file parallel.h.

Constructor & Destructor Documentation

◆ Parallel()

tesseract::Parallel::Parallel ( const STRING name,
NetworkType  type 
)

Definition at line 31 of file parallel.cpp.

31  : Plumbing(name) {
32  type_ = type;
33 }
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
NetworkType type() const
Definition: network.h:112
NetworkType type_
Definition: network.h:285

◆ ~Parallel()

tesseract::Parallel::~Parallel ( )
virtual

Definition at line 35 of file parallel.cpp.

35  {
36 }

Member Function Documentation

◆ Backward()

bool tesseract::Parallel::Backward ( bool  debug,
const NetworkIO fwd_deltas,
NetworkScratch scratch,
NetworkIO back_deltas 
)
virtual

Reimplemented from tesseract::Network.

Definition at line 113 of file parallel.cpp.

115  {
116  // If this parallel is a replicator of convolvers, or holds a 1-d LSTM pair,
117  // or a 2-d LSTM quad, do debug locally, and don't pass the flag on.
118  if (debug && type_ != NT_PARALLEL) {
119  DisplayBackward(fwd_deltas);
120  debug = false;
121  }
122  int stack_size = stack_.size();
123  if (type_ == NT_PAR_2D_LSTM) {
124  // Special case, run parallel in parallel.
125  GenericVector<NetworkScratch::IO> in_deltas, out_deltas;
126  in_deltas.init_to_size(stack_size, NetworkScratch::IO());
127  out_deltas.init_to_size(stack_size, NetworkScratch::IO());
128  // Split the forward deltas for each stack element.
129  int feature_offset = 0;
130  for (int i = 0; i < stack_.size(); ++i) {
131  int num_features = stack_[i]->NumOutputs();
132  in_deltas[i].Resize(fwd_deltas, num_features, scratch);
133  out_deltas[i].Resize(fwd_deltas, stack_[i]->NumInputs(), scratch);
134  in_deltas[i]->CopyUnpacking(fwd_deltas, feature_offset, num_features);
135  feature_offset += num_features;
136  }
137 #ifdef _OPENMP
138 #pragma omp parallel for num_threads(stack_size)
139 #endif
140  for (int i = 0; i < stack_size; ++i) {
141  stack_[i]->Backward(debug, *in_deltas[i], scratch,
142  i == 0 ? back_deltas : out_deltas[i]);
143  }
144  if (needs_to_backprop_) {
145  for (int i = 1; i < stack_size; ++i) {
146  back_deltas->AddAllToFloat(*out_deltas[i]);
147  }
148  }
149  } else {
150  // Revolving partial deltas.
151  NetworkScratch::IO in_deltas(fwd_deltas, scratch);
152  // The sum of deltas from different sources, which will eventually go into
153  // back_deltas.
154  NetworkScratch::IO out_deltas;
155  int feature_offset = 0;
156  for (int i = 0; i < stack_.size(); ++i) {
157  int num_features = stack_[i]->NumOutputs();
158  in_deltas->CopyUnpacking(fwd_deltas, feature_offset, num_features);
159  feature_offset += num_features;
160  if (stack_[i]->Backward(debug, *in_deltas, scratch, back_deltas)) {
161  if (i == 0) {
162  out_deltas.ResizeFloat(*back_deltas, back_deltas->NumFeatures(),
163  scratch);
164  out_deltas->CopyAll(*back_deltas);
165  } else if (back_deltas->NumFeatures() == out_deltas->NumFeatures()) {
166  // Widths are allowed to be different going back, as we may have
167  // input nets, so only accumulate the deltas if the widths are the
168  // same.
169  out_deltas->AddAllToFloat(*back_deltas);
170  }
171  }
172  }
173  if (needs_to_backprop_) back_deltas->CopyAll(*out_deltas);
174  }
175  if (needs_to_backprop_) back_deltas->ScaleFloatBy(1.0f / stack_size);
176  return needs_to_backprop_;
177 }
bool needs_to_backprop_
Definition: network.h:287
void init_to_size(int size, T t)
PointerVector< Network > stack_
Definition: plumbing.h:133
NetworkType type_
Definition: network.h:285
int NumInputs() const
Definition: network.h:120
virtual bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)
Definition: parallel.cpp:113
void DisplayBackward(const NetworkIO &matrix)
Definition: network.cpp:296

◆ Forward()

void tesseract::Parallel::Forward ( bool  debug,
const NetworkIO input,
const TransposedArray input_transpose,
NetworkScratch scratch,
NetworkIO output 
)
virtual

Reimplemented from tesseract::Network.

Definition at line 52 of file parallel.cpp.

54  {
55  bool parallel_debug = false;
56  // If this parallel is a replicator of convolvers, or holds a 1-d LSTM pair,
57  // or a 2-d LSTM quad, do debug locally, and don't pass the flag on.
58  if (debug && type_ != NT_PARALLEL) {
59  parallel_debug = true;
60  debug = false;
61  }
62  int stack_size = stack_.size();
63  if (type_ == NT_PAR_2D_LSTM) {
64  // Special case, run parallel in parallel.
66  results.init_to_size(stack_size, NetworkScratch::IO());
67  for (int i = 0; i < stack_size; ++i) {
68  results[i].Resize(input, stack_[i]->NumOutputs(), scratch);
69  }
70 #ifdef _OPENMP
71 #pragma omp parallel for num_threads(stack_size)
72 #endif
73  for (int i = 0; i < stack_size; ++i) {
74  stack_[i]->Forward(debug, input, NULL, scratch, results[i]);
75  }
76  // Now pack all the results (serially) into the output.
77  int out_offset = 0;
78  output->Resize(*results[0], NumOutputs());
79  for (int i = 0; i < stack_size; ++i) {
80  out_offset = output->CopyPacking(*results[i], out_offset);
81  }
82  } else {
83  // Revolving intermediate result.
84  NetworkScratch::IO result(input, scratch);
85  // Source for divided replicated.
86  NetworkScratch::IO source_part;
87  TransposedArray* src_transpose = NULL;
88  if (IsTraining() && type_ == NT_REPLICATED) {
89  // Make a transposed copy of the input.
90  input.Transpose(&transposed_input_);
91  src_transpose = &transposed_input_;
92  }
93  // Run each network, putting the outputs into result.
94  int out_offset = 0;
95  for (int i = 0; i < stack_size; ++i) {
96  stack_[i]->Forward(debug, input, src_transpose, scratch, result);
97  // All networks must have the same output width
98  if (i == 0) {
99  output->Resize(*result, NumOutputs());
100  } else {
101  ASSERT_HOST(result->Width() == output->Width());
102  }
103  out_offset = output->CopyPacking(*result, out_offset);
104  }
105  }
106  if (parallel_debug) {
107  DisplayForward(*output);
108  }
109 }
void init_to_size(int size, T t)
void DisplayForward(const NetworkIO &matrix)
Definition: network.cpp:285
bool IsTraining() const
Definition: network.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84
PointerVector< Network > stack_
Definition: plumbing.h:133
NetworkType type_
Definition: network.h:285
int NumOutputs() const
Definition: network.h:123

◆ OutputShape()

StaticShape tesseract::Parallel::OutputShape ( const StaticShape input_shape) const
virtual

Reimplemented from tesseract::Network.

Definition at line 40 of file parallel.cpp.

40  {
41  StaticShape result = stack_[0]->OutputShape(input_shape);
42  int stack_size = stack_.size();
43  for (int i = 1; i < stack_size; ++i) {
44  StaticShape shape = stack_[i]->OutputShape(input_shape);
45  result.set_depth(result.depth() + shape.depth());
46  }
47  return result;
48 }
PointerVector< Network > stack_
Definition: plumbing.h:133

◆ spec()

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

Reimplemented from tesseract::Plumbing.

Definition at line 37 of file parallel.h.

37  {
38  STRING spec;
39  if (type_ == NT_PAR_2D_LSTM) {
40  // We have 4 LSTMs operating in parallel here, so the size of each is
41  // the number of outputs/4.
42  spec.add_str_int("L2xy", no_ / 4);
43  } else if (type_ == NT_PAR_RL_LSTM) {
44  // We have 2 LSTMs operating in parallel here, so the size of each is
45  // the number of outputs/2.
46  if (stack_[0]->type() == NT_LSTM_SUMMARY)
47  spec.add_str_int("Lbxs", no_ / 2);
48  else
49  spec.add_str_int("Lbx", no_ / 2);
50  } else {
51  if (type_ == NT_REPLICATED) {
52  spec.add_str_int("R", stack_.size());
53  spec += "(";
54  spec += stack_[0]->spec();
55  } else {
56  spec = "(";
57  for (int i = 0; i < stack_.size(); ++i) spec += stack_[i]->spec();
58  }
59  spec += ")";
60  }
61  return spec;
62  }
void add_str_int(const char *str, int number)
Definition: strngs.cpp:381
virtual STRING spec() const
Definition: parallel.h:37
NetworkType type() const
Definition: network.h:112
PointerVector< Network > stack_
Definition: plumbing.h:133
Definition: strngs.h:45
NetworkType type_
Definition: network.h:285

The documentation for this class was generated from the following files: