comparison mupdf-source/thirdparty/tesseract/src/lstm/reversed.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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.
16 ///////////////////////////////////////////////////////////////////////
17
18 #ifndef TESSERACT_LSTM_REVERSED_H_
19 #define TESSERACT_LSTM_REVERSED_H_
20
21 #include "matrix.h"
22 #include "plumbing.h"
23
24 namespace tesseract {
25
26 // C++ Implementation of the Reversed class from lstm.py.
27 class Reversed : public Plumbing {
28 public:
29 TESS_API
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.
70 TESS_API
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
83 private:
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_