tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
input.h
1 // File: input.h
3 // Description: Input layer class for neural network implementations.
4 // Author: Ray Smith
5 // Created: Thu Mar 13 08:56:26 PDT 2014
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_INPUT_H_
20 #define TESSERACT_LSTM_INPUT_H_
21 
22 #include "network.h"
23 
24 class ScrollView;
25 
26 namespace tesseract {
27 
28 class Input : public Network {
29  public:
30  Input(const STRING& name, int ni, int no);
31  Input(const STRING& name, const StaticShape& shape);
32  virtual ~Input() = default;
33 
34  STRING spec() const override {
35  STRING spec;
36  spec.add_str_int("", shape_.batch());
37  spec.add_str_int(",", shape_.height());
38  spec.add_str_int(",", shape_.width());
39  spec.add_str_int(",", shape_.depth());
40  return spec;
41  }
42 
43  // Returns the required shape input to the network.
44  StaticShape InputShape() const override { return shape_; }
45  // Returns the shape output from the network given an input shape (which may
46  // be partially unknown ie zero).
47  StaticShape OutputShape(const StaticShape& input_shape) const override {
48  return shape_;
49  }
50  // Writes to the given file. Returns false in case of error.
51  // Should be overridden by subclasses, but called by their Serialize.
52  bool Serialize(TFile* fp) const override;
53  // Reads from the given file. Returns false in case of error.
54  bool DeSerialize(TFile* fp) override;
55 
56  // Returns an integer reduction factor that the network applies to the
57  // time sequence. Assumes that any 2-d is already eliminated. Used for
58  // scaling bounding boxes of truth data.
59  // WARNING: if GlobalMinimax is used to vary the scale, this will return
60  // the last used scale factor. Call it before any forward, and it will return
61  // the minimum scale factor of the paths through the GlobalMinimax.
62  int XScaleFactor() const override;
63 
64  // Provides the (minimum) x scale factor to the network (of interest only to
65  // input units) so they can determine how to scale bounding boxes.
66  void CacheXScaleFactor(int factor) override;
67 
68  // Runs forward propagation of activations on the input line.
69  // See Network for a detailed discussion of the arguments.
70  void Forward(bool debug, const NetworkIO& input,
71  const TransposedArray* input_transpose,
72  NetworkScratch* scratch, NetworkIO* output) override;
73 
74  // Runs backward propagation of errors on the deltas line.
75  // See Network for a detailed discussion of the arguments.
76  bool Backward(bool debug, const NetworkIO& fwd_deltas,
77  NetworkScratch* scratch,
78  NetworkIO* back_deltas) override;
79  // Creates and returns a Pix of appropriate size for the network from the
80  // image_data. If non-null, *image_scale returns the image scale factor used.
81  // Returns nullptr on error.
82  /* static */
83  static Pix* PrepareLSTMInputs(const ImageData& image_data,
84  const Network* network, int min_width,
85  TRand* randomizer, float* image_scale);
86  // Converts the given pix to a NetworkIO of height and depth appropriate to
87  // the given StaticShape:
88  // If depth == 3, convert to 24 bit color, otherwise normalized grey.
89  // Scale to target height, if the shape's height is > 1, or its depth if the
90  // height == 1. If height == 0 then no scaling.
91  // NOTE: It isn't safe for multiple threads to call this on the same pix.
92  static void PreparePixInput(const StaticShape& shape, const Pix* pix,
93  TRand* randomizer, NetworkIO* input);
94 
95  private:
96  // Input shape determines how images are dealt with.
98  // Cached total network x scale factor for scaling bounding boxes.
100 };
101 
102 } // namespace tesseract.
103 
104 #endif // TESSERACT_LSTM_INPUT_H_
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
static Pix * PrepareLSTMInputs(const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
Definition: input.cpp:84
Definition: helpers.h:42
int XScaleFactor() const override
Definition: input.cpp:53
Definition: static_shape.h:38
int width() const
Definition: static_shape.h:46
virtual ~Input()=default
Definition: imagedata.h:105
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: input.cpp:65
Definition: networkscratch.h:36
Definition: serialis.h:77
bool Serialize(TFile *fp) const override
Definition: input.cpp:41
Definition: baseapi.cpp:94
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: input.h:47
const STRING & name() const
Definition: network.h:138
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: input.cpp:73
int height() const
Definition: static_shape.h:44
void CacheXScaleFactor(int factor) override
Definition: input.cpp:59
Definition: weightmatrix.h:33
Definition: scrollview.h:102
Definition: network.h:105
StaticShape shape_
Definition: input.h:97
Input(const STRING &name, int ni, int no)
Definition: input.cpp:31
static void PreparePixInput(const StaticShape &shape, const Pix *pix, TRand *randomizer, NetworkIO *input)
Definition: input.cpp:112
Definition: strngs.h:45
int depth() const
Definition: static_shape.h:48
STRING spec() const override
Definition: input.h:34
int cached_x_scale_
Definition: input.h:99
bool DeSerialize(TFile *fp) override
Definition: input.cpp:46
Definition: networkio.h:39
Definition: input.h:28
StaticShape InputShape() const override
Definition: input.h:44
int batch() const
Definition: static_shape.h:42