tesseract  5.0.0-alpha-619-ge9db
reconfig.h
Go to the documentation of this file.
1 // File: reconfig.h
3 // Description: Network layer that reconfigures the scaling vs feature
4 // depth.
5 // Author: Ray Smith
6 //
7 // (C) Copyright 2014, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 
19 #ifndef TESSERACT_LSTM_RECONFIG_H_
20 #define TESSERACT_LSTM_RECONFIG_H_
21 
23 #include "matrix.h"
24 #include "network.h"
25 
26 namespace tesseract {
27 
28 // Reconfigures (Shrinks) the inputs by concatenating an x_scale by y_scale tile
29 // of inputs together, producing a single, deeper output per tile.
30 // Note that fractional parts are truncated for efficiency, so make sure the
31 // input stride is a multiple of the y_scale factor!
32 class Reconfig : public Network {
33  public:
34  Reconfig(const STRING& name, int ni, int x_scale, int y_scale);
35  ~Reconfig() override = default;
36 
37  // Returns the shape output from the network given an input shape (which may
38  // be partially unknown ie zero).
39  StaticShape OutputShape(const StaticShape& input_shape) const override;
40 
41  STRING spec() const override {
42  STRING spec;
45  return spec;
46  }
47 
48  // Returns an integer reduction factor that the network applies to the
49  // time sequence. Assumes that any 2-d is already eliminated. Used for
50  // scaling bounding boxes of truth data.
51  // WARNING: if GlobalMinimax is used to vary the scale, this will return
52  // the last used scale factor. Call it before any forward, and it will return
53  // the minimum scale factor of the paths through the GlobalMinimax.
54  int XScaleFactor() const override;
55 
56  // Writes to the given file. Returns false in case of error.
57  bool Serialize(TFile* fp) const override;
58  // Reads from the given file. Returns false in case of error.
59  bool DeSerialize(TFile* fp) override;
60 
61  // Runs forward propagation of activations on the input line.
62  // See Network for a detailed discussion of the arguments.
63  void Forward(bool debug, const NetworkIO& input,
64  const TransposedArray* input_transpose,
65  NetworkScratch* scratch, NetworkIO* output) override;
66 
67  // Runs backward propagation of errors on the deltas line.
68  // See Network for a detailed discussion of the arguments.
69  bool Backward(bool debug, const NetworkIO& fwd_deltas,
70  NetworkScratch* scratch,
71  NetworkIO* back_deltas) override;
72 
73  private:
74  void DebugWeights() override {
75  tprintf("Must override Network::DebugWeights for type %d\n", type_);
76  }
77 
78  protected:
79  // Non-serialized data used to store parameters between forward and back.
81  // Serialized data.
82  int32_t x_scale_;
83  int32_t y_scale_;
84 };
85 
86 } // namespace tesseract.
87 
88 #endif // TESSERACT_LSTM_SUBSAMPLE_H_
tesseract::StaticShape
Definition: static_shape.h:38
tesseract::Reconfig::spec
STRING spec() const override
Definition: reconfig.h:41
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
tesseract::Reconfig::DeSerialize
bool DeSerialize(TFile *fp) override
Definition: reconfig.cpp:57
STRING
Definition: strngs.h:45
tesseract::Reconfig
Definition: reconfig.h:32
tesseract::NetworkScratch
Definition: networkscratch.h:34
network.h
tesseract::Reconfig::Reconfig
Reconfig(const STRING &name, int ni, int x_scale, int y_scale)
Definition: reconfig.cpp:23
genericvector.h
tesseract::Reconfig::y_scale_
int32_t y_scale_
Definition: reconfig.h:83
tesseract::Network::type_
NetworkType type_
Definition: network.h:293
matrix.h
tesseract::TFile
Definition: serialis.h:75
tesseract::NetworkIO
Definition: networkio.h:39
tesseract::Reconfig::Forward
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reconfig.cpp:66
tesseract
Definition: baseapi.h:65
tesseract::Reconfig::OutputShape
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reconfig.cpp:30
tesseract::Reconfig::Backward
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reconfig.cpp:92
tesseract::Reconfig::Serialize
bool Serialize(TFile *fp) const override
Definition: reconfig.cpp:50
tesseract::Reconfig::x_scale_
int32_t x_scale_
Definition: reconfig.h:82
tesseract::Network
Definition: network.h:105
tesseract::StrideMap
Definition: stridemap.h:41
tesseract::Network::name
const STRING & name() const
Definition: network.h:138
tesseract::Reconfig::~Reconfig
~Reconfig() override=default
tesseract::TransposedArray
Definition: weightmatrix.h:32
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::Reconfig::back_map_
StrideMap back_map_
Definition: reconfig.h:80
tesseract::Reconfig::XScaleFactor
int XScaleFactor() const override
Definition: reconfig.cpp:45