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