tesseract  4.00.00dev
reversed.h
Go to the documentation of this file.
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();
32 
33  // Returns the shape output from the network given an input shape (which may
34  // be partially unknown ie zero).
35  virtual StaticShape OutputShape(const StaticShape& input_shape) const;
36 
37  virtual STRING spec() const {
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  virtual void Forward(bool debug, const NetworkIO& input,
73  const TransposedArray* input_transpose,
74  NetworkScratch* scratch, NetworkIO* output);
75 
76  // Runs backward propagation of errors on the deltas line.
77  // See Network for a detailed discussion of the arguments.
78  virtual bool Backward(bool debug, const NetworkIO& fwd_deltas,
79  NetworkScratch* scratch,
80  NetworkIO* back_deltas);
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_
virtual STRING spec() const
Definition: reversed.h:37
NetworkType type() const
Definition: network.h:112
virtual ~Reversed()
Definition: reversed.cpp:30
Reversed(const STRING &name, NetworkType type)
Definition: reversed.cpp:27
inT32 length() const
Definition: strngs.cpp:193
void SetNetwork(Network *network)
Definition: reversed.cpp:49
PointerVector< Network > stack_
Definition: plumbing.h:133
Definition: strngs.h:45
NetworkType
Definition: network.h:43
NetworkType type_
Definition: network.h:285
const STRING & name() const
Definition: network.h:138
virtual void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)
Definition: reversed.cpp:56
virtual StaticShape OutputShape(const StaticShape &input_shape) const
Definition: reversed.cpp:35
virtual bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)
Definition: reversed.cpp:68