tesseract 4.1.1
Loading...
Searching...
No Matches
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 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), y_scale_(y_scale) {
26}
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 return result;
37}
38
39// Returns an integer reduction factor that the network applies to the
40// time sequence. Assumes that any 2-d is already eliminated. Used for
41// scaling bounding boxes of truth data.
42// WARNING: if GlobalMinimax is used to vary the scale, this will return
43// the last used scale factor. Call it before any forward, and it will return
44// the minimum scale factor of the paths through the GlobalMinimax.
46 return x_scale_;
47}
48
49// Writes to the given file. Returns false in case of error.
50bool Reconfig::Serialize(TFile* fp) const {
51 return Network::Serialize(fp) &&
52 fp->Serialize(&x_scale_) &&
53 fp->Serialize(&y_scale_);
54}
55
56// Reads from the given file. Returns false in case of error.
58 if (!fp->DeSerialize(&x_scale_)) return false;
59 if (!fp->DeSerialize(&y_scale_)) return false;
61 return true;
62}
63
64// Runs forward propagation of activations on the input line.
65// See NetworkCpp for a detailed discussion of the arguments.
66void Reconfig::Forward(bool debug, const NetworkIO& input,
67 const TransposedArray* input_transpose,
68 NetworkScratch* scratch, NetworkIO* output) {
69 output->ResizeScaled(input, x_scale_, y_scale_, no_);
70 back_map_ = input.stride_map();
71 StrideMap::Index dest_index(output->stride_map());
72 do {
73 int out_t = dest_index.t();
74 StrideMap::Index src_index(input.stride_map(), dest_index.index(FD_BATCH),
75 dest_index.index(FD_HEIGHT) * y_scale_,
76 dest_index.index(FD_WIDTH) * x_scale_);
77 // Stack x_scale_ groups of y_scale_ inputs together.
78 for (int x = 0; x < x_scale_; ++x) {
79 for (int y = 0; y < y_scale_; ++y) {
80 StrideMap::Index src_xy(src_index);
81 if (src_xy.AddOffset(x, FD_WIDTH) && src_xy.AddOffset(y, FD_HEIGHT)) {
82 output->CopyTimeStepGeneral(out_t, (x * y_scale_ + y) * ni_, ni_,
83 input, src_xy.t(), 0);
84 }
85 }
86 }
87 } while (dest_index.Increment());
88}
89
90// Runs backward propagation of errors on the deltas line.
91// See NetworkCpp for a detailed discussion of the arguments.
92bool Reconfig::Backward(bool debug, const NetworkIO& fwd_deltas,
93 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(),
100 src_index.index(FD_BATCH),
101 src_index.index(FD_HEIGHT) * y_scale_,
102 src_index.index(FD_WIDTH) * x_scale_);
103 // Unstack x_scale_ groups of y_scale_ inputs that are together.
104 for (int x = 0; x < x_scale_; ++x) {
105 for (int y = 0; y < y_scale_; ++y) {
106 StrideMap::Index dest_xy(dest_index);
107 if (dest_xy.AddOffset(x, FD_WIDTH) && dest_xy.AddOffset(y, FD_HEIGHT)) {
108 back_deltas->CopyTimeStepGeneral(dest_xy.t(), 0, ni_, fwd_deltas,
109 in_t, (x * y_scale_ + y) * ni_);
110 }
111 }
112 }
113 } while (src_index.Increment());
114 return needs_to_backprop_;
115}
116
117
118} // namespace tesseract.
@ NT_MAXPOOL
Definition: network.h:48
@ NT_RECONFIG
Definition: network.h:55
@ FD_WIDTH
Definition: stridemap.h:35
@ FD_BATCH
Definition: stridemap.h:33
@ FD_HEIGHT
Definition: stridemap.h:34
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:148
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:104
Definition: strngs.h:45
NetworkType type_
Definition: network.h:293
bool needs_to_backprop_
Definition: network.h:295
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151
bool int_mode() const
Definition: networkio.h:127
void CopyTimeStepGeneral(int dest_t, int dest_offset, int num_features, const NetworkIO &src, int src_t, int src_offset)
Definition: networkio.cpp:393
void ResizeScaled(const NetworkIO &src, int x_scale, int y_scale, int num_features)
Definition: networkio.cpp:62
const StrideMap & stride_map() const
Definition: networkio.h:133
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:46
StrideMap back_map_
Definition: reconfig.h:80
bool DeSerialize(TFile *fp) override
Definition: reconfig.cpp:57
int XScaleFactor() const override
Definition: reconfig.cpp:45
bool Serialize(TFile *fp) const override
Definition: reconfig.cpp:50
int32_t y_scale_
Definition: reconfig.h:83
Reconfig(const STRING &name, int ni, int x_scale, int y_scale)
Definition: reconfig.cpp:23
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reconfig.cpp:30
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reconfig.cpp:92
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reconfig.cpp:66
int32_t x_scale_
Definition: reconfig.h:82
void set_depth(int value)
Definition: static_shape.h:49
void set_width(int value)
Definition: static_shape.h:47
void set_height(int value)
Definition: static_shape.h:45
int index(FlexDimensions dimension) const
Definition: stridemap.h:58
bool AddOffset(int offset, FlexDimensions dimension)
Definition: stridemap.cpp:62