tesseract  5.0.0-alpha-619-ge9db
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 //
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 
21 namespace tesseract {
22 
23 Reconfig::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).
30 StaticShape Reconfig::OutputShape(const StaticShape& input_shape) const {
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.
50 bool 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;
60  no_ = ni_ * x_scale_ * y_scale_;
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.
66 void 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.
92 bool 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.
tesseract::StaticShape
Definition: static_shape.h:38
tesseract::Reconfig::DeSerialize
bool DeSerialize(TFile *fp) override
Definition: reconfig.cpp:57
tesseract::NetworkIO::int_mode
bool int_mode() const
Definition: networkio.h:127
tesseract::NetworkIO::ResizeScaled
void ResizeScaled(const NetworkIO &src, int x_scale, int y_scale, int num_features)
Definition: networkio.cpp:62
tesseract::StrideMap::Index
Definition: stridemap.h:44
STRING
Definition: strngs.h:45
tesseract::NetworkScratch
Definition: networkscratch.h:34
tesseract::NetworkIO::stride_map
const StrideMap & stride_map() const
Definition: networkio.h:133
tesseract::Network::needs_to_backprop_
bool needs_to_backprop_
Definition: network.h:295
tesseract::Reconfig::Reconfig
Reconfig(const STRING &name, int ni, int x_scale, int y_scale)
Definition: reconfig.cpp:23
tesseract::Reconfig::y_scale_
int32_t y_scale_
Definition: reconfig.h:83
tesseract::StaticShape::set_width
void set_width(int value)
Definition: static_shape.h:47
tesseract::StaticShape::set_height
void set_height(int value)
Definition: static_shape.h:45
tesseract::Network::type_
NetworkType type_
Definition: network.h:293
tesseract::StrideMap::Index::AddOffset
bool AddOffset(int offset, FlexDimensions dimension)
Definition: stridemap.cpp:62
tesseract::TFile::DeSerialize
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:117
tesseract::StaticShape::depth
int depth() const
Definition: static_shape.h:48
tesseract::TFile::Serialize
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:161
tesseract::StrideMap::Index::t
int t() const
Definition: stridemap.h:57
tesseract::FD_WIDTH
Definition: stridemap.h:35
tesseract::TFile
Definition: serialis.h:75
tesseract::NetworkIO
Definition: networkio.h:39
tesseract::Reconfig::Forward
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reconfig.cpp:66
tesseract
Definition: baseapi.h:65
tesseract::Reconfig::OutputShape
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reconfig.cpp:30
tesseract::StaticShape::width
int width() const
Definition: static_shape.h:46
tesseract::Reconfig::Backward
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reconfig.cpp:92
tesseract::Reconfig::Serialize
bool Serialize(TFile *fp) const override
Definition: reconfig.cpp:50
tesseract::Reconfig::x_scale_
int32_t x_scale_
Definition: reconfig.h:82
tesseract::NetworkIO::CopyTimeStepGeneral
void CopyTimeStepGeneral(int dest_t, int dest_offset, int num_features, const NetworkIO &src, int src_t, int src_offset)
Definition: networkio.cpp:393
tesseract::NetworkIO::ResizeToMap
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:46
tesseract::StaticShape::height
int height() const
Definition: static_shape.h:44
tesseract::Network
Definition: network.h:105
reconfig.h
tesseract::FD_HEIGHT
Definition: stridemap.h:34
tesseract::TransposedArray
Definition: weightmatrix.h:32
tesseract::Network::Serialize
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151
tesseract::Reconfig::back_map_
StrideMap back_map_
Definition: reconfig.h:80
tesseract::FD_BATCH
Definition: stridemap.h:33
tesseract::Network::no_
int32_t no_
Definition: network.h:298
tesseract::Network::ni_
int32_t ni_
Definition: network.h:297
tesseract::NT_MAXPOOL
Definition: network.h:48
tesseract::Reconfig::XScaleFactor
int XScaleFactor() const override
Definition: reconfig.cpp:45
tesseract::StaticShape::set_depth
void set_depth(int value)
Definition: static_shape.h:49
tesseract::NT_RECONFIG
Definition: network.h:55