tesseract  5.0.0
parallel.h
Go to the documentation of this file.
1 // File: parallel.h
3 // Description: Runs networks in parallel on the same input.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2013, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 
18 #ifndef TESSERACT_LSTM_PARALLEL_H_
19 #define TESSERACT_LSTM_PARALLEL_H_
20 
21 #include "plumbing.h"
22 
23 namespace tesseract {
24 
25 // Runs multiple networks in parallel, interlacing their outputs.
26 class Parallel : public Plumbing {
27 public:
28  // ni_ and no_ will be set by AddToStack.
29  TESS_API
30  Parallel(const char *name, NetworkType type);
31  ~Parallel() override = default;
32 
33  // Returns the shape output from the network given an input shape (which may
34  // be partially unknown ie zero).
35  StaticShape OutputShape(const StaticShape &input_shape) const override;
36 
37  std::string spec() const override {
38  std::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 += "L2xy" + std::to_string(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 += "Lbxs" + std::to_string(no_ / 2);
48  } else {
49  spec += "Lbx" + std::to_string(no_ / 2);
50  }
51  } else {
52  if (type_ == NT_REPLICATED) {
53  spec += "R" + std::to_string(stack_.size()) + "(" + stack_[0]->spec();
54  } else {
55  for (auto &it : stack_) {
56  spec += it->spec();
57  }
58  }
59  spec += ")";
60  }
61  return spec;
62  }
63 
64  // Runs forward propagation of activations on the input line.
65  // See Network for a detailed discussion of the arguments.
66  void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
67  NetworkScratch *scratch, NetworkIO *output) override;
68 
69  // Runs backward propagation of errors on the deltas line.
70  // See Network for a detailed discussion of the arguments.
71  bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
72  NetworkIO *back_deltas) override;
73 
74 private:
75  // If *this is a NT_REPLICATED, then it feeds a replicated network with
76  // identical inputs, and it would be extremely wasteful for them to each
77  // calculate and store the same transpose of the inputs, so Parallel does it
78  // and passes a pointer to the replicated network, allowing it to use the
79  // transpose on the next call to Backward.
80  TransposedArray transposed_input_;
81 };
82 
83 } // namespace tesseract.
84 
85 #endif // TESSERACT_LSTM_PARALLEL_H_
NetworkType
Definition: network.h:41
@ NT_PAR_2D_LSTM
Definition: network.h:51
@ NT_LSTM_SUMMARY
Definition: network.h:59
@ NT_PAR_RL_LSTM
Definition: network.h:49
@ NT_REPLICATED
Definition: network.h:48
NetworkType type_
Definition: network.h:300
const std::string & name() const
Definition: network.h:140
NetworkType type() const
Definition: network.h:110
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: parallel.cpp:52
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: parallel.cpp:113
TESS_API Parallel(const char *name, NetworkType type)
Definition: parallel.cpp:34
~Parallel() override=default
std::string spec() const override
Definition: parallel.h:37
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: parallel.cpp:40
std::vector< Network * > stack_
Definition: plumbing.h:150
#define TESS_API
Definition: export.h:34