comparison mupdf-source/thirdparty/tesseract/src/lstm/reconfig.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: reconfig.cpp
3 // Description: Network layer that reconfigures the scaling vs feature
4 // depth.
5 // Author: Ray Smith
6 //
7 // (C) Copyright 2014, 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.
17 ///////////////////////////////////////////////////////////////////////
18
19 #include "reconfig.h"
20
21 namespace tesseract {
22
23 Reconfig::Reconfig(const std::string &name, int ni, int x_scale, int y_scale)
24 : Network(NT_RECONFIG, name, ni, ni * x_scale * y_scale)
25 , x_scale_(x_scale)
26 , y_scale_(y_scale) {}
27
28 // Returns the shape output from the network given an input shape (which may
29 // be partially unknown ie zero).
30 StaticShape Reconfig::OutputShape(const StaticShape &input_shape) const {
31 StaticShape result = input_shape;
32 result.set_height(result.height() / y_scale_);
33 result.set_width(result.width() / x_scale_);
34 if (type_ != NT_MAXPOOL) {
35 result.set_depth(result.depth() * y_scale_ * x_scale_);
36 }
37 return result;
38 }
39
40 // Returns an integer reduction factor that the network applies to the
41 // time sequence. Assumes that any 2-d is already eliminated. Used for
42 // scaling bounding boxes of truth data.
43 // WARNING: if GlobalMinimax is used to vary the scale, this will return
44 // the last used scale factor. Call it before any forward, and it will return
45 // the minimum scale factor of the paths through the GlobalMinimax.
46 int Reconfig::XScaleFactor() const {
47 return x_scale_;
48 }
49
50 // Writes to the given file. Returns false in case of error.
51 bool Reconfig::Serialize(TFile *fp) const {
52 return Network::Serialize(fp) && fp->Serialize(&x_scale_) && fp->Serialize(&y_scale_);
53 }
54
55 // Reads from the given file. Returns false in case of error.
56 bool Reconfig::DeSerialize(TFile *fp) {
57 if (!fp->DeSerialize(&x_scale_)) {
58 return false;
59 }
60 if (!fp->DeSerialize(&y_scale_)) {
61 return false;
62 }
63 no_ = ni_ * x_scale_ * y_scale_;
64 return true;
65 }
66
67 // Runs forward propagation of activations on the input line.
68 // See NetworkCpp for a detailed discussion of the arguments.
69 void Reconfig::Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
70 NetworkScratch *scratch, NetworkIO *output) {
71 output->ResizeScaled(input, x_scale_, y_scale_, no_);
72 back_map_ = input.stride_map();
73 StrideMap::Index dest_index(output->stride_map());
74 do {
75 int out_t = dest_index.t();
76 StrideMap::Index src_index(input.stride_map(), dest_index.index(FD_BATCH),
77 dest_index.index(FD_HEIGHT) * y_scale_,
78 dest_index.index(FD_WIDTH) * x_scale_);
79 // Stack x_scale_ groups of y_scale_ inputs together.
80 for (int x = 0; x < x_scale_; ++x) {
81 for (int y = 0; y < y_scale_; ++y) {
82 StrideMap::Index src_xy(src_index);
83 if (src_xy.AddOffset(x, FD_WIDTH) && src_xy.AddOffset(y, FD_HEIGHT)) {
84 output->CopyTimeStepGeneral(out_t, (x * y_scale_ + y) * ni_, ni_, input, src_xy.t(), 0);
85 }
86 }
87 }
88 } while (dest_index.Increment());
89 }
90
91 // Runs backward propagation of errors on the deltas line.
92 // See NetworkCpp for a detailed discussion of the arguments.
93 bool Reconfig::Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
94 NetworkIO *back_deltas) {
95 back_deltas->ResizeToMap(fwd_deltas.int_mode(), back_map_, ni_);
96 StrideMap::Index src_index(fwd_deltas.stride_map());
97 do {
98 int in_t = src_index.t();
99 StrideMap::Index dest_index(back_deltas->stride_map(), src_index.index(FD_BATCH),
100 src_index.index(FD_HEIGHT) * y_scale_,
101 src_index.index(FD_WIDTH) * x_scale_);
102 // Unstack x_scale_ groups of y_scale_ inputs that are together.
103 for (int x = 0; x < x_scale_; ++x) {
104 for (int y = 0; y < y_scale_; ++y) {
105 StrideMap::Index dest_xy(dest_index);
106 if (dest_xy.AddOffset(x, FD_WIDTH) && dest_xy.AddOffset(y, FD_HEIGHT)) {
107 back_deltas->CopyTimeStepGeneral(dest_xy.t(), 0, ni_, fwd_deltas, in_t,
108 (x * y_scale_ + y) * ni_);
109 }
110 }
111 }
112 } while (src_index.Increment());
113 return needs_to_backprop_;
114 }
115
116 } // namespace tesseract.