tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
reconfig.h
1 // File: reconfig.h
3 // Description: Network layer that reconfigures the scaling vs feature
4 // depth.
5 // Author: Ray Smith
6 // Created: Wed Feb 26 15:37:42 PST 2014
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 #ifndef TESSERACT_LSTM_RECONFIG_H_
20 #define TESSERACT_LSTM_RECONFIG_H_
21 
22 
23 #include "genericvector.h"
24 #include "matrix.h"
25 #include "network.h"
26 
27 namespace tesseract {
28 
29 // Reconfigures (Shrinks) the inputs by concatenating an x_scale by y_scale tile
30 // of inputs together, producing a single, deeper output per tile.
31 // Note that fractional parts are truncated for efficiency, so make sure the
32 // input stride is a multiple of the y_scale factor!
33 class Reconfig : public Network {
34  public:
35  Reconfig(const STRING& name, int ni, int x_scale, int y_scale);
36  virtual ~Reconfig() = default;
37 
38  // Returns the shape output from the network given an input shape (which may
39  // be partially unknown ie zero).
40  StaticShape OutputShape(const StaticShape& input_shape) const override;
41 
42  STRING spec() const override {
43  STRING spec;
44  spec.add_str_int("S", y_scale_);
45  spec.add_str_int(",", x_scale_);
46  return spec;
47  }
48 
49  // Returns an integer reduction factor that the network applies to the
50  // time sequence. Assumes that any 2-d is already eliminated. Used for
51  // scaling bounding boxes of truth data.
52  // WARNING: if GlobalMinimax is used to vary the scale, this will return
53  // the last used scale factor. Call it before any forward, and it will return
54  // the minimum scale factor of the paths through the GlobalMinimax.
55  int XScaleFactor() const override;
56 
57  // Writes to the given file. Returns false in case of error.
58  bool Serialize(TFile* fp) const override;
59  // Reads from the given file. Returns false in case of error.
60  bool DeSerialize(TFile* fp) override;
61 
62  // Runs forward propagation of activations on the input line.
63  // See Network for a detailed discussion of the arguments.
64  void Forward(bool debug, const NetworkIO& input,
65  const TransposedArray* input_transpose,
66  NetworkScratch* scratch, NetworkIO* output) override;
67 
68  // Runs backward propagation of errors on the deltas line.
69  // See Network for a detailed discussion of the arguments.
70  bool Backward(bool debug, const NetworkIO& fwd_deltas,
71  NetworkScratch* scratch,
72  NetworkIO* back_deltas) override;
73 
74  protected:
75  // Non-serialized data used to store parameters between forward and back.
77  // Serialized data.
78  int32_t x_scale_;
79  int32_t y_scale_;
80 };
81 
82 } // namespace tesseract.
83 
84 
85 #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: reconfig.cpp:93
virtual ~Reconfig()=default
Definition: static_shape.h:38
int XScaleFactor() const override
Definition: reconfig.cpp:46
Definition: networkscratch.h:36
int32_t y_scale_
Definition: reconfig.h:79
Definition: serialis.h:77
STRING spec() const override
Definition: reconfig.h:42
Definition: baseapi.cpp:94
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reconfig.cpp:31
const STRING & name() const
Definition: network.h:138
StrideMap back_map_
Definition: reconfig.h:76
Definition: weightmatrix.h:33
bool DeSerialize(TFile *fp) override
Definition: reconfig.cpp:58
Definition: network.h:105
Reconfig(const STRING &name, int ni, int x_scale, int y_scale)
Definition: reconfig.cpp:24
Definition: strngs.h:45
int32_t x_scale_
Definition: reconfig.h:78
Definition: stridemap.h:43
Definition: networkio.h:39
Definition: reconfig.h:33
bool Serialize(TFile *fp) const override
Definition: reconfig.cpp:51
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reconfig.cpp:67