tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
convolve.h
1 // File: convolve.h
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:45:34 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 #ifndef TESSERACT_LSTM_CONVOLVE_H_
22 #define TESSERACT_LSTM_CONVOLVE_H_
23 
24 #include "genericvector.h"
25 #include "matrix.h"
26 #include "network.h"
27 
28 namespace tesseract {
29 
30 // Makes each time-step deeper by stacking inputs over its rectangle. Does not
31 // affect the size of its input. Achieves this by bringing in random values in
32 // out-of-input areas.
33 class Convolve : public Network {
34  public:
35  // The area of convolution is 2*half_x + 1 by 2*half_y + 1, forcing it to
36  // always be odd, so the center is the current pixel.
37  Convolve(const STRING& name, int ni, int half_x, int half_y);
38  virtual ~Convolve() = default;
39 
40  STRING spec() const override {
41  STRING spec;
42  spec.add_str_int("C", half_x_ * 2 + 1);
43  spec.add_str_int(",", half_y_ * 2 + 1);
44  return spec;
45  }
46 
47  // Writes to the given file. Returns false in case of error.
48  bool Serialize(TFile* fp) const override;
49  // Reads from the given file. Returns false in case of error.
50  bool DeSerialize(TFile* fp) override;
51 
52  // Runs forward propagation of activations on the input line.
53  // See Network for a detailed discussion of the arguments.
54  void Forward(bool debug, const NetworkIO& input,
55  const TransposedArray* input_transpose,
56  NetworkScratch* scratch, NetworkIO* output) override;
57 
58  // Runs backward propagation of errors on the deltas line.
59  // See Network for a detailed discussion of the arguments.
60  bool Backward(bool debug, const NetworkIO& fwd_deltas,
61  NetworkScratch* scratch,
62  NetworkIO* back_deltas) override;
63 
64  protected:
65  // Serialized data.
66  int32_t half_x_;
67  int32_t half_y_;
68 };
69 
70 } // namespace tesseract.
71 
72 
73 #endif // TESSERACT_LSTM_SUBSAMPLE_H_
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: convolve.cpp:84
STRING spec() const override
Definition: convolve.h:40
Definition: networkscratch.h:36
Definition: convolve.h:33
Definition: serialis.h:77
Definition: baseapi.cpp:94
int32_t half_y_
Definition: convolve.h:67
const STRING & name() const
Definition: network.h:138
bool Serialize(TFile *fp) const override
Definition: convolve.cpp:34
Definition: weightmatrix.h:33
bool DeSerialize(TFile *fp) override
Definition: convolve.cpp:41
Definition: network.h:105
virtual ~Convolve()=default
Definition: strngs.h:45
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: convolve.cpp:50
Convolve(const STRING &name, int ni, int half_x, int half_y)
Definition: convolve.cpp:28
Definition: networkio.h:39
int32_t half_x_
Definition: convolve.h:66