tesseract  4.0.0-1-g2a2b
reconfig.cpp
Go to the documentation of this file.
1 // File: reconfig.cpp
3 // Description: Network layer that reconfigures the scaling vs feature
4 // depth.
5 // Author: Ray Smith
6 // Created: Wed Feb 26 15:42:25 PST 2014
7 //
8 // (C) Copyright 2014, Google Inc.
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 #include "reconfig.h"
20 #include "tprintf.h"
21 
22 namespace tesseract {
23 
24 Reconfig::Reconfig(const STRING& name, int ni, int x_scale, int y_scale)
25  : Network(NT_RECONFIG, name, ni, ni * x_scale * y_scale),
26  x_scale_(x_scale), y_scale_(y_scale) {
27 }
28 
29 // Returns the shape output from the network given an input shape (which may
30 // be partially unknown ie zero).
31 StaticShape Reconfig::OutputShape(const StaticShape& input_shape) const {
32  StaticShape result = input_shape;
33  result.set_height(result.height() / y_scale_);
34  result.set_width(result.width() / x_scale_);
35  if (type_ != NT_MAXPOOL)
36  result.set_depth(result.depth() * y_scale_ * x_scale_);
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.
51 bool Reconfig::Serialize(TFile* fp) const {
52  return Network::Serialize(fp) &&
53  fp->Serialize(&x_scale_) &&
54  fp->Serialize(&y_scale_);
55 }
56 
57 // Reads from the given file. Returns false in case of error.
59  if (!fp->DeSerialize(&x_scale_)) return false;
60  if (!fp->DeSerialize(&y_scale_)) return false;
61  no_ = ni_ * x_scale_ * y_scale_;
62  return true;
63 }
64 
65 // Runs forward propagation of activations on the input line.
66 // See NetworkCpp for a detailed discussion of the arguments.
67 void Reconfig::Forward(bool debug, const NetworkIO& input,
68  const TransposedArray* input_transpose,
69  NetworkScratch* scratch, NetworkIO* output) {
70  output->ResizeScaled(input, x_scale_, y_scale_, no_);
71  back_map_ = input.stride_map();
72  StrideMap::Index dest_index(output->stride_map());
73  do {
74  int out_t = dest_index.t();
75  StrideMap::Index src_index(input.stride_map(), dest_index.index(FD_BATCH),
76  dest_index.index(FD_HEIGHT) * y_scale_,
77  dest_index.index(FD_WIDTH) * x_scale_);
78  // Stack x_scale_ groups of y_scale_ inputs together.
79  for (int x = 0; x < x_scale_; ++x) {
80  for (int y = 0; y < y_scale_; ++y) {
81  StrideMap::Index src_xy(src_index);
82  if (src_xy.AddOffset(x, FD_WIDTH) && src_xy.AddOffset(y, FD_HEIGHT)) {
83  output->CopyTimeStepGeneral(out_t, (x * y_scale_ + y) * ni_, ni_,
84  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,
94  NetworkScratch* scratch,
95  NetworkIO* back_deltas) {
96  back_deltas->ResizeToMap(fwd_deltas.int_mode(), back_map_, ni_);
97  StrideMap::Index src_index(fwd_deltas.stride_map());
98  do {
99  int in_t = src_index.t();
100  StrideMap::Index dest_index(back_deltas->stride_map(),
101  src_index.index(FD_BATCH),
102  src_index.index(FD_HEIGHT) * y_scale_,
103  src_index.index(FD_WIDTH) * x_scale_);
104  // Unstack x_scale_ groups of y_scale_ inputs that are together.
105  for (int x = 0; x < x_scale_; ++x) {
106  for (int y = 0; y < y_scale_; ++y) {
107  StrideMap::Index dest_xy(dest_index);
108  if (dest_xy.AddOffset(x, FD_WIDTH) && dest_xy.AddOffset(y, FD_HEIGHT)) {
109  back_deltas->CopyTimeStepGeneral(dest_xy.t(), 0, ni_, fwd_deltas,
110  in_t, (x * y_scale_ + y) * ni_);
111  }
112  }
113  }
114  } while (src_index.Increment());
115  return needs_to_backprop_;
116 }
117 
118 
119 } // namespace tesseract.
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reconfig.cpp:93
int32_t y_scale_
Definition: reconfig.h:79
int32_t x_scale_
Definition: reconfig.h:78
bool AddOffset(int offset, FlexDimensions dimension)
Definition: stridemap.cpp:63
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:103
int XScaleFactor() const override
Definition: reconfig.cpp:46
NetworkType type_
Definition: network.h:299
void ResizeScaled(const NetworkIO &src, int x_scale, int y_scale, int num_features)
Definition: networkio.cpp:67
void set_width(int value)
Definition: static_shape.h:47
bool DeSerialize(TFile *fp) override
Definition: reconfig.cpp:58
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151
bool needs_to_backprop_
Definition: network.h:301
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:147
const StrideMap & stride_map() const
Definition: networkio.h:133
bool Serialize(TFile *fp) const override
Definition: reconfig.cpp:51
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reconfig.cpp:31
Definition: strngs.h:45
void CopyTimeStepGeneral(int dest_t, int dest_offset, int num_features, const NetworkIO &src, int src_t, int src_offset)
Definition: networkio.cpp:398
bool int_mode() const
Definition: networkio.h:127
void set_depth(int value)
Definition: static_shape.h:49
void set_height(int value)
Definition: static_shape.h:45
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:51
Reconfig(const STRING &name, int ni, int x_scale, int y_scale)
Definition: reconfig.cpp:24
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reconfig.cpp:67
StrideMap back_map_
Definition: reconfig.h:76