comparison mupdf-source/thirdparty/tesseract/src/lstm/maxpool.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: maxpool.cpp
3 // Description: Standard Max-Pooling layer.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2014, 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 "maxpool.h"
19
20 namespace tesseract {
21
22 Maxpool::Maxpool(const std::string &name, int ni, int x_scale, int y_scale)
23 : Reconfig(name, ni, x_scale, y_scale) {
24 type_ = NT_MAXPOOL;
25 no_ = ni;
26 }
27
28 // Reads from the given file. Returns false in case of error.
29 bool Maxpool::DeSerialize(TFile *fp) {
30 bool result = Reconfig::DeSerialize(fp);
31 no_ = ni_;
32 return result;
33 }
34
35 // Runs forward propagation of activations on the input line.
36 // See NetworkCpp for a detailed discussion of the arguments.
37 void Maxpool::Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
38 NetworkScratch *scratch, NetworkIO *output) {
39 output->ResizeScaled(input, x_scale_, y_scale_, no_);
40 maxes_.ResizeNoInit(output->Width(), ni_);
41 back_map_ = input.stride_map();
42
43 StrideMap::Index dest_index(output->stride_map());
44 do {
45 int out_t = dest_index.t();
46 StrideMap::Index src_index(input.stride_map(), dest_index.index(FD_BATCH),
47 dest_index.index(FD_HEIGHT) * y_scale_,
48 dest_index.index(FD_WIDTH) * x_scale_);
49 // Find the max input out of x_scale_ groups of y_scale_ inputs.
50 // Do it independently for each input dimension.
51 int *max_line = maxes_[out_t];
52 int in_t = src_index.t();
53 output->CopyTimeStepFrom(out_t, input, in_t);
54 for (int i = 0; i < ni_; ++i) {
55 max_line[i] = in_t;
56 }
57 for (int x = 0; x < x_scale_; ++x) {
58 for (int y = 0; y < y_scale_; ++y) {
59 StrideMap::Index src_xy(src_index);
60 if (src_xy.AddOffset(x, FD_WIDTH) && src_xy.AddOffset(y, FD_HEIGHT)) {
61 output->MaxpoolTimeStep(out_t, input, src_xy.t(), max_line);
62 }
63 }
64 }
65 } while (dest_index.Increment());
66 }
67
68 // Runs backward propagation of errors on the deltas line.
69 // See NetworkCpp for a detailed discussion of the arguments.
70 bool Maxpool::Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
71 NetworkIO *back_deltas) {
72 back_deltas->ResizeToMap(fwd_deltas.int_mode(), back_map_, ni_);
73 back_deltas->MaxpoolBackward(fwd_deltas, maxes_);
74 return true;
75 }
76
77 } // namespace tesseract.