tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
reversed.h
1 // File: reversed.h
3 // Description: Runs a single network on time-reversed input, reversing output.
4 // Author: Ray Smith
5 // Created: Thu May 02 08:38:06 PST 2013
6 //
7 // (C) Copyright 2013, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 
19 #ifndef TESSERACT_LSTM_REVERSED_H_
20 #define TESSERACT_LSTM_REVERSED_H_
21 
22 #include "matrix.h"
23 #include "plumbing.h"
24 
25 namespace tesseract {
26 
27 // C++ Implementation of the Reversed class from lstm.py.
28 class Reversed : public Plumbing {
29  public:
30  explicit Reversed(const STRING& name, NetworkType type);
31  virtual ~Reversed() = 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  STRING spec() const override {
38  STRING spec(type_ == NT_XREVERSED ? "Rx"
39  : (type_ == NT_YREVERSED ? "Ry" : "Txy"));
40  // For most simple cases, we will output Rx<net> or Ry<net> where <net> is
41  // the network in stack_[0], but in the special case that <net> is an
42  // LSTM, we will just output the LSTM's spec modified to take the reversal
43  // into account. This is because when the user specified Lfy64, we actually
44  // generated TxyLfx64, and if the user specified Lrx64 we actually
45  // generated RxLfx64, and we want to display what the user asked for.
46  STRING net_spec = stack_[0]->spec();
47  if (net_spec[0] == 'L') {
48  // Setup a from and to character according to the type of the reversal
49  // such that the LSTM spec gets modified to the spec that the user
50  // asked for
51  char from = 'f';
52  char to = 'r';
53  if (type_ == NT_XYTRANSPOSE) {
54  from = 'x';
55  to = 'y';
56  }
57  // Change the from char to the to char.
58  for (int i = 0; i < net_spec.length(); ++i) {
59  if (net_spec[i] == from) net_spec[i] = to;
60  }
61  return net_spec;
62  }
63  spec += net_spec;
64  return spec;
65  }
66 
67  // Takes ownership of the given network to make it the reversed one.
68  void SetNetwork(Network* network);
69 
70  // Runs forward propagation of activations on the input line.
71  // See Network for a detailed discussion of the arguments.
72  void Forward(bool debug, const NetworkIO& input,
73  const TransposedArray* input_transpose,
74  NetworkScratch* scratch, NetworkIO* output) override;
75 
76  // Runs backward propagation of errors on the deltas line.
77  // See Network for a detailed discussion of the arguments.
78  bool Backward(bool debug, const NetworkIO& fwd_deltas,
79  NetworkScratch* scratch,
80  NetworkIO* back_deltas) override;
81 
82  private:
83  // Copies src to *dest with the reversal according to type_.
84  void ReverseData(const NetworkIO& src, NetworkIO* dest) const;
85 };
86 
87 } // namespace tesseract.
88 
89 #endif // TESSERACT_LSTM_REVERSED_H_
void SetNetwork(Network *network)
Definition: reversed.cpp:47
PointerVector< Network > stack_
Definition: plumbing.h:136
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reversed.cpp:66
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reversed.cpp:54
Definition: network.h:57
Definition: static_shape.h:38
Definition: plumbing.h:30
Definition: networkscratch.h:36
int32_t length() const
Definition: strngs.cpp:191
Definition: baseapi.cpp:94
const STRING & name() const
Definition: network.h:138
NetworkType
Definition: network.h:43
Definition: weightmatrix.h:33
Definition: network.h:105
NetworkType type() const
Definition: network.h:112
Reversed(const STRING &name, NetworkType type)
Definition: reversed.cpp:27
Definition: strngs.h:45
Definition: network.h:56
virtual ~Reversed()=default
Definition: networkio.h:39
NetworkType type_
Definition: network.h:299
void ReverseData(const NetworkIO &src, NetworkIO *dest) const
Definition: reversed.cpp:80
Definition: network.h:58
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reversed.cpp:33
Definition: reversed.h:28
STRING spec() const override
Definition: reversed.h:37