tesseract v5.3.3.20231005
reversed.h
Go to the documentation of this file.
1
2// File: reversed.h
3// Description: Runs a single network on time-reversed input, reversing output.
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_REVERSED_H_
19#define TESSERACT_LSTM_REVERSED_H_
20
21#include "matrix.h"
22#include "plumbing.h"
23
24namespace tesseract {
25
26// C++ Implementation of the Reversed class from lstm.py.
27class Reversed : public Plumbing {
28public:
30 explicit Reversed(const std::string &name, NetworkType type);
31 ~Reversed() 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(type_ == NT_XREVERSED ? "Rx" : (type_ == NT_YREVERSED ? "Ry" : "Txy"));
39 // For most simple cases, we will output Rx<net> or Ry<net> where <net> is
40 // the network in stack_[0], but in the special case that <net> is an
41 // LSTM, we will just output the LSTM's spec modified to take the reversal
42 // into account. This is because when the user specified Lfy64, we actually
43 // generated TxyLfx64, and if the user specified Lrx64 we actually
44 // generated RxLfx64, and we want to display what the user asked for.
45 std::string net_spec(stack_[0]->spec());
46 if (net_spec[0] == 'L') {
47 // Setup a from and to character according to the type of the reversal
48 // such that the LSTM spec gets modified to the spec that the user
49 // asked for
50 char from = 'f';
51 char to = 'r';
52 if (type_ == NT_XYTRANSPOSE) {
53 from = 'x';
54 to = 'y';
55 }
56 // Change the from char to the to char.
57 for (auto &it : net_spec) {
58 if (it == from) {
59 it = to;
60 }
61 }
62 spec += net_spec;
63 return spec;
64 }
65 spec += net_spec;
66 return spec;
67 }
68
69 // Takes ownership of the given network to make it the reversed one.
71 void SetNetwork(Network *network);
72
73 // Runs forward propagation of activations on the input line.
74 // See Network for a detailed discussion of the arguments.
75 void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
76 NetworkScratch *scratch, NetworkIO *output) override;
77
78 // Runs backward propagation of errors on the deltas line.
79 // See Network for a detailed discussion of the arguments.
80 bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
81 NetworkIO *back_deltas) override;
82
83private:
84 // Copies src to *dest with the reversal according to type_.
85 void ReverseData(const NetworkIO &src, NetworkIO *dest) const;
86};
87
88} // namespace tesseract.
89
90#endif // TESSERACT_LSTM_REVERSED_H_
NetworkType
Definition: network.h:41
@ NT_XREVERSED
Definition: network.h:54
@ NT_YREVERSED
Definition: network.h:55
@ NT_XYTRANSPOSE
Definition: network.h:56
dest
Definition: upload.py:409
NetworkType type_
Definition: network.h:300
const std::string & name() const
Definition: network.h:140
NetworkType type() const
Definition: network.h:110
std::vector< Network * > stack_
Definition: plumbing.h:147
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reversed.cpp:52
TESS_API Reversed(const std::string &name, NetworkType type)
Definition: reversed.cpp:26
~Reversed() override=default
TESS_API void SetNetwork(Network *network)
Definition: reversed.cpp:45
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reversed.cpp:32
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reversed.cpp:63
std::string spec() const override
Definition: reversed.h:37
#define TESS_API
Definition: export.h:32