tesseract v5.3.3.20231005
reconfig.cpp
Go to the documentation of this file.
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.
18
19#include "reconfig.h"
20
21namespace tesseract {
22
23Reconfig::Reconfig(const char *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).
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.
47 return x_scale_;
48}
49
50// Writes to the given file. Returns false in case of error.
51bool 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.
57 if (!fp->DeSerialize(&x_scale_)) {
58 return false;
59 }
60 if (!fp->DeSerialize(&y_scale_)) {
61 return false;
62 }
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.
69void 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.
93bool 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.
const double y
@ NT_MAXPOOL
Definition: network.h:46
@ NT_RECONFIG
Definition: network.h:53
@ FD_WIDTH
Definition: stridemap.h:35
@ FD_BATCH
Definition: stridemap.h:33
@ FD_HEIGHT
Definition: stridemap.h:34
bool DeSerialize(std::string &data)
Definition: serialis.cpp:94
bool Serialize(const std::string &data)
Definition: serialis.cpp:107
NetworkType type_
Definition: network.h:300
bool needs_to_backprop_
Definition: network.h:302
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:158
bool int_mode() const
Definition: networkio.h:122
void CopyTimeStepGeneral(int dest_t, int dest_offset, int num_features, const NetworkIO &src, int src_t, int src_offset)
Definition: networkio.cpp:405
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
int XScaleFactor() const override
Definition: reconfig.cpp:46
bool Serialize(TFile *fp) const override
Definition: reconfig.cpp:51
int32_t y_scale_
Definition: reconfig.h:78
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reconfig.cpp:30
TESS_API Reconfig(const char *name, int ni, int x_scale, int y_scale)
Definition: reconfig.cpp:23
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reconfig.cpp:93
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reconfig.cpp:69
int32_t x_scale_
Definition: reconfig.h:77
void set_depth(int value)
Definition: static_shape.h:62
void set_width(int value)
Definition: static_shape.h:56
void set_height(int value)
Definition: static_shape.h:50
int index(FlexDimensions dimension) const
Definition: stridemap.h:59
bool AddOffset(int offset, FlexDimensions dimension)
Definition: stridemap.cpp:67