tesseract  4.0.0-1-g2a2b
convolve.cpp
Go to the documentation of this file.
1 // File: convolve.cpp
3 // Description: Convolutional layer that stacks the inputs over its rectangle
4 // and pulls in random data to fill out-of-input inputs.
5 // Output is therefore same size as its input, but deeper.
6 // Author: Ray Smith
7 // Created: Tue Mar 18 16:56:06 PST 2014
8 //
9 // (C) Copyright 2014, Google Inc.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
20 
21 #include "convolve.h"
22 
23 #include "networkscratch.h"
24 #include "serialis.h"
25 
26 namespace tesseract {
27 
28 Convolve::Convolve(const STRING& name, int ni, int half_x, int half_y)
29  : Network(NT_CONVOLVE, name, ni, ni * (2*half_x + 1) * (2*half_y + 1)),
30  half_x_(half_x), half_y_(half_y) {
31 }
32 
33 // Writes to the given file. Returns false in case of error.
34 bool Convolve::Serialize(TFile* fp) const {
35  return Network::Serialize(fp) &&
36  fp->Serialize(&half_x_) &&
37  fp->Serialize(&half_y_);
38 }
39 
40 // Reads from the given file. Returns false in case of error.
42  if (!fp->DeSerialize(&half_x_)) return false;
43  if (!fp->DeSerialize(&half_y_)) return false;
44  no_ = ni_ * (2*half_x_ + 1) * (2*half_y_ + 1);
45  return true;
46 }
47 
48 // Runs forward propagation of activations on the input line.
49 // See NetworkCpp for a detailed discussion of the arguments.
50 void Convolve::Forward(bool debug, const NetworkIO& input,
51  const TransposedArray* input_transpose,
52  NetworkScratch* scratch, NetworkIO* output) {
53  output->Resize(input, no_);
54  int y_scale = 2 * half_y_ + 1;
55  StrideMap::Index dest_index(output->stride_map());
56  do {
57  // Stack x_scale groups of y_scale * ni_ inputs together.
58  int t = dest_index.t();
59  int out_ix = 0;
60  for (int x = -half_x_; x <= half_x_; ++x, out_ix += y_scale * ni_) {
61  StrideMap::Index x_index(dest_index);
62  if (!x_index.AddOffset(x, FD_WIDTH)) {
63  // This x is outside the image.
64  output->Randomize(t, out_ix, y_scale * ni_, randomizer_);
65  } else {
66  int out_iy = out_ix;
67  for (int y = -half_y_; y <= half_y_; ++y, out_iy += ni_) {
68  StrideMap::Index y_index(x_index);
69  if (!y_index.AddOffset(y, FD_HEIGHT)) {
70  // This y is outside the image.
71  output->Randomize(t, out_iy, ni_, randomizer_);
72  } else {
73  output->CopyTimeStepGeneral(t, out_iy, ni_, input, y_index.t(), 0);
74  }
75  }
76  }
77  }
78  } while (dest_index.Increment());
79  if (debug) DisplayForward(*output);
80 }
81 
82 // Runs backward propagation of errors on the deltas line.
83 // See NetworkCpp for a detailed discussion of the arguments.
84 bool Convolve::Backward(bool debug, const NetworkIO& fwd_deltas,
85  NetworkScratch* scratch,
86  NetworkIO* back_deltas) {
87  back_deltas->Resize(fwd_deltas, ni_);
88  NetworkScratch::IO delta_sum;
89  delta_sum.ResizeFloat(fwd_deltas, ni_, scratch);
90  delta_sum->Zero();
91  int y_scale = 2 * half_y_ + 1;
92  StrideMap::Index src_index(fwd_deltas.stride_map());
93  do {
94  // Stack x_scale groups of y_scale * ni_ inputs together.
95  int t = src_index.t();
96  int out_ix = 0;
97  for (int x = -half_x_; x <= half_x_; ++x, out_ix += y_scale * ni_) {
98  StrideMap::Index x_index(src_index);
99  if (x_index.AddOffset(x, FD_WIDTH)) {
100  int out_iy = out_ix;
101  for (int y = -half_y_; y <= half_y_; ++y, out_iy += ni_) {
102  StrideMap::Index y_index(x_index);
103  if (y_index.AddOffset(y, FD_HEIGHT)) {
104  fwd_deltas.AddTimeStepPart(t, out_iy, ni_,
105  delta_sum->f(y_index.t()));
106  }
107  }
108  }
109  }
110  } while (src_index.Increment());
111  back_deltas->CopyAll(*delta_sum);
112  return true;
113 }
114 
115 } // namespace tesseract.
bool DeSerialize(TFile *fp) override
Definition: convolve.cpp:41
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: convolve.cpp:84
Convolve(const STRING &name, int ni, int half_x, int half_y)
Definition: convolve.cpp:28
void AddTimeStepPart(int t, int offset, int num_features, float *inout) const
Definition: networkio.cpp:634
bool AddOffset(int offset, FlexDimensions dimension)
Definition: stridemap.cpp:63
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:103
bool Serialize(TFile *fp) const override
Definition: convolve.cpp:34
void ResizeFloat(const NetworkIO &src, int num_features, NetworkScratch *scratch)
void Resize(const NetworkIO &src, int num_features)
Definition: networkio.h:45
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151
TRand * randomizer_
Definition: network.h:311
void DisplayForward(const NetworkIO &matrix)
Definition: network.cpp:282
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:147
const StrideMap & stride_map() const
Definition: networkio.h:133
float * f(int t)
Definition: networkio.h:115
void CopyAll(const NetworkIO &src)
Definition: networkio.cpp:816
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
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: convolve.cpp:50
void Randomize(int t, int offset, int num_features, TRand *randomizer)
Definition: networkio.cpp:421