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