tesseract  5.0.0-alpha-619-ge9db
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 <tesseract/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.
tesseract::NetworkIO::Randomize
void Randomize(int t, int offset, int num_features, TRand *randomizer)
Definition: networkio.cpp:416
tesseract::Network::DisplayForward
void DisplayForward(const NetworkIO &matrix)
Definition: network.cpp:288
tesseract::StrideMap::Index
Definition: stridemap.h:44
STRING
Definition: strngs.h:45
tesseract::NetworkScratch
Definition: networkscratch.h:34
tesseract::Convolve::Forward
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: convolve.cpp:50
tesseract::NetworkIO::stride_map
const StrideMap & stride_map() const
Definition: networkio.h:133
tesseract::NetworkScratch::IO::ResizeFloat
void ResizeFloat(const NetworkIO &src, int num_features, NetworkScratch *scratch)
Definition: networkscratch.h:99
networkscratch.h
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::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::Convolve::half_y_
int32_t half_y_
Definition: convolve.h:71
tesseract::FD_WIDTH
Definition: stridemap.h:35
tesseract::NetworkIO::f
float * f(int t)
Definition: networkio.h:115
tesseract::TFile
Definition: serialis.h:75
tesseract::NetworkIO
Definition: networkio.h:39
tesseract::Network::randomizer_
TRand * randomizer_
Definition: network.h:305
tesseract::Convolve::DeSerialize
bool DeSerialize(TFile *fp) override
Definition: convolve.cpp:41
tesseract::Convolve::Serialize
bool Serialize(TFile *fp) const override
Definition: convolve.cpp:34
tesseract::NT_CONVOLVE
Definition: network.h:47
tesseract
Definition: baseapi.h:65
tesseract::NetworkIO::AddTimeStepPart
void AddTimeStepPart(int t, int offset, int num_features, float *inout) const
Definition: networkio.cpp:629
tesseract::NetworkScratch::IO
Definition: networkscratch.h:51
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::Convolve::half_x_
int32_t half_x_
Definition: convolve.h:70
tesseract::Network
Definition: network.h:105
tesseract::NetworkIO::Zero
void Zero()
Definition: networkio.cpp:77
tesseract::NetworkIO::CopyAll
void CopyAll(const NetworkIO &src)
Definition: networkio.cpp:811
tesseract::NetworkIO::Resize
void Resize(const NetworkIO &src, int num_features)
Definition: networkio.h:45
tesseract::FD_HEIGHT
Definition: stridemap.h:34
tesseract::Convolve::Backward
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: convolve.cpp:84
tesseract::TransposedArray
Definition: weightmatrix.h:32
tesseract::Convolve::Convolve
Convolve(const STRING &name, int ni, int half_x, int half_y)
Definition: convolve.cpp:28
convolve.h
serialis.h
tesseract::Network::Serialize
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151
tesseract::Network::no_
int32_t no_
Definition: network.h:298
tesseract::Network::ni_
int32_t ni_
Definition: network.h:297