tesseract v5.3.3.20231005
maxpool.cpp
Go to the documentation of this file.
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.
17
18#include "maxpool.h"
19
20namespace tesseract {
21
22Maxpool::Maxpool(const char *name, int ni, int x_scale, int y_scale)
23 : Reconfig(name, ni, x_scale, y_scale) {
25 no_ = ni;
26}
27
28// Reads from the given file. Returns false in case of error.
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.
37void 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.
70bool 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.
const double y
@ NT_MAXPOOL
Definition: network.h:46
@ FD_WIDTH
Definition: stridemap.h:35
@ FD_BATCH
Definition: stridemap.h:33
@ FD_HEIGHT
Definition: stridemap.h:34
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:94
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: maxpool.cpp:70
bool DeSerialize(TFile *fp) override
Definition: maxpool.cpp:29
TESS_API Maxpool(const char *name, int ni, int x_scale, int y_scale)
Definition: maxpool.cpp:22
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: maxpool.cpp:37
NetworkType type_
Definition: network.h:300
bool int_mode() const
Definition: networkio.h:122
void MaxpoolBackward(const NetworkIO &fwd, const GENERIC_2D_ARRAY< int > &maxes)
Definition: networkio.cpp:703
const StrideMap & stride_map() const
Definition: networkio.h:128
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:46
StrideMap back_map_
Definition: reconfig.h:75
bool DeSerialize(TFile *fp) override
Definition: reconfig.cpp:56
int32_t y_scale_
Definition: reconfig.h:78
int32_t x_scale_
Definition: reconfig.h:77
int index(FlexDimensions dimension) const
Definition: stridemap.h:59
bool AddOffset(int offset, FlexDimensions dimension)
Definition: stridemap.cpp:67