tesseract  5.0.0
convolve.h
Go to the documentation of this file.
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 //
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 
20 #ifndef TESSERACT_LSTM_CONVOLVE_H_
21 #define TESSERACT_LSTM_CONVOLVE_H_
22 
23 #include "matrix.h"
24 #include "network.h"
25 
26 namespace tesseract {
27 
28 // Makes each time-step deeper by stacking inputs over its rectangle. Does not
29 // affect the size of its input. Achieves this by bringing in random values in
30 // out-of-input areas.
31 class Convolve : public Network {
32 public:
33  // The area of convolution is 2*half_x + 1 by 2*half_y + 1, forcing it to
34  // always be odd, so the center is the current pixel.
35  TESS_API
36  Convolve(const std::string &name, int ni, int half_x, int half_y);
37  ~Convolve() override = default;
38 
39  std::string spec() const override {
40  return "C" + std::to_string(half_y_ * 2 + 1) + "," + std::to_string(half_x_ * 2 + 1);
41  }
42 
43  // Writes to the given file. Returns false in case of error.
44  bool Serialize(TFile *fp) const override;
45  // Reads from the given file. Returns false in case of error.
46  bool DeSerialize(TFile *fp) override;
47 
48  // Runs forward propagation of activations on the input line.
49  // See Network for a detailed discussion of the arguments.
50  void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
51  NetworkScratch *scratch, NetworkIO *output) override;
52 
53  // Runs backward propagation of errors on the deltas line.
54  // See Network for a detailed discussion of the arguments.
55  bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
56  NetworkIO *back_deltas) override;
57 
58 private:
59  void DebugWeights() override {
60  tprintf("Must override Network::DebugWeights for type %d\n", type_);
61  }
62 
63 protected:
64  // Serialized data.
65  int32_t half_x_;
66  int32_t half_y_;
67 };
68 
69 } // namespace tesseract.
70 
71 #endif // TESSERACT_LSTM_SUBSAMPLE_H_
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
~Convolve() override=default
std::string spec() const override
Definition: convolve.h:39
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: convolve.cpp:55
bool Serialize(TFile *fp) const override
Definition: convolve.cpp:37
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: convolve.cpp:92
TESS_API Convolve(const std::string &name, int ni, int half_x, int half_y)
Definition: convolve.cpp:31
bool DeSerialize(TFile *fp) override
Definition: convolve.cpp:42
NetworkType type_
Definition: network.h:300
const std::string & name() const
Definition: network.h:140
#define TESS_API
Definition: export.h:34