comparison mupdf-source/thirdparty/tesseract/src/lstm/reversed.cpp @ 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.cpp
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 #include "reversed.h"
19
20 #include <cstdio>
21
22 #include "networkscratch.h"
23
24 namespace tesseract {
25
26 Reversed::Reversed(const std::string &name, NetworkType type) : Plumbing(name) {
27 type_ = type;
28 }
29
30 // Returns the shape output from the network given an input shape (which may
31 // be partially unknown ie zero).
32 StaticShape Reversed::OutputShape(const StaticShape &input_shape) const {
33 if (type_ == NT_XYTRANSPOSE) {
34 StaticShape x_shape(input_shape);
35 x_shape.set_width(input_shape.height());
36 x_shape.set_height(input_shape.width());
37 x_shape = stack_[0]->OutputShape(x_shape);
38 x_shape.SetShape(x_shape.batch(), x_shape.width(), x_shape.height(), x_shape.depth());
39 return x_shape;
40 }
41 return stack_[0]->OutputShape(input_shape);
42 }
43
44 // Takes ownership of the given network to make it the reversed one.
45 void Reversed::SetNetwork(Network *network) {
46 stack_.clear();
47 AddToStack(network);
48 }
49
50 // Runs forward propagation of activations on the input line.
51 // See NetworkCpp for a detailed discussion of the arguments.
52 void Reversed::Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
53 NetworkScratch *scratch, NetworkIO *output) {
54 NetworkScratch::IO rev_input(input, scratch);
55 ReverseData(input, rev_input);
56 NetworkScratch::IO rev_output(input, scratch);
57 stack_[0]->Forward(debug, *rev_input, nullptr, scratch, rev_output);
58 ReverseData(*rev_output, output);
59 }
60
61 // Runs backward propagation of errors on the deltas line.
62 // See NetworkCpp for a detailed discussion of the arguments.
63 bool Reversed::Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
64 NetworkIO *back_deltas) {
65 NetworkScratch::IO rev_input(fwd_deltas, scratch);
66 ReverseData(fwd_deltas, rev_input);
67 NetworkScratch::IO rev_output(fwd_deltas, scratch);
68 if (stack_[0]->Backward(debug, *rev_input, scratch, rev_output)) {
69 ReverseData(*rev_output, back_deltas);
70 return true;
71 }
72 return false;
73 }
74
75 // Copies src to *dest with the reversal according to type_.
76 void Reversed::ReverseData(const NetworkIO &src, NetworkIO *dest) const {
77 if (type_ == NT_XREVERSED) {
78 dest->CopyWithXReversal(src);
79 } else if (type_ == NT_YREVERSED) {
80 dest->CopyWithYReversal(src);
81 } else {
82 dest->CopyWithXYTranspose(src);
83 }
84 }
85
86 } // namespace tesseract.