tesseract  4.1.0
tfnetwork.h
Go to the documentation of this file.
1 // File: tfnetwork.h
3 // Description: Encapsulation of an entire tensorflow graph as a
4 // Tesseract Network.
5 // Author: Ray Smith
6 // Created: Fri Feb 26 09:35:29 PST 2016
7 //
8 // (C) Copyright 2016, 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_TFNETWORK_H_
21 #define TESSERACT_LSTM_TFNETWORK_H_
22 
23 #ifdef INCLUDE_TENSORFLOW
24 
25 #include <memory>
26 #include <string>
27 
28 #include "network.h"
29 #include "static_shape.h"
30 #include "tfnetwork.pb.h"
31 #include "tensorflow/core/framework/graph.pb.h"
32 #include "tensorflow/core/public/session.h"
33 
34 namespace tesseract {
35 
36 class TFNetwork : public Network {
37  public:
38  explicit TFNetwork(const STRING& name);
39  virtual ~TFNetwork() = default;
40 
41  // Returns the required shape input to the network.
42  StaticShape InputShape() const override { return input_shape_; }
43  // Returns the shape output from the network given an input shape (which may
44  // be partially unknown ie zero).
45  StaticShape OutputShape(const StaticShape& input_shape) const override {
46  return output_shape_;
47  }
48 
49  STRING spec() const override { return spec_.c_str(); }
50 
51  // Deserializes *this from a serialized TFNetwork proto. Returns 0 if failed,
52  // otherwise the global step of the serialized graph.
53  int InitFromProtoStr(const std::string& proto_str);
54  // The number of classes in this network should be equal to those in the
55  // recoder_ in LSTMRecognizer.
56  int num_classes() const { return output_shape_.depth(); }
57 
58  // Writes to the given file. Returns false in case of error.
59  // Should be overridden by subclasses, but called by their Serialize.
60  bool Serialize(TFile* fp) const override;
61  // Reads from the given file. Returns false in case of error.
62  // Should be overridden by subclasses, but NOT called by their DeSerialize.
63  bool DeSerialize(TFile* fp) override;
64 
65  // Runs forward propagation of activations on the input line.
66  // See Network for a detailed discussion of the arguments.
67  void Forward(bool debug, const NetworkIO& input,
68  const TransposedArray* input_transpose,
69  NetworkScratch* scratch, NetworkIO* output) override;
70 
71  private:
72  // Runs backward propagation of errors on the deltas line.
73  // See Network for a detailed discussion of the arguments.
74  bool Backward(bool debug, const NetworkIO& fwd_deltas,
75  NetworkScratch* scratch,
76  NetworkIO* back_deltas) override {
77  tprintf("Must override Network::DebugWeights for type %d\n", type_);
78  }
79 
80  void DebugWeights() override {
81  tprintf("Must override Network::DebugWeights for type %d\n", type_);
82  }
83 
84  int InitFromProto();
85 
86  // The original network definition for reference.
87  std::string spec_;
88  // Input tensor parameters.
89  StaticShape input_shape_;
90  // Output tensor parameters.
91  StaticShape output_shape_;
92  // The tensor flow graph is contained in here.
93  std::unique_ptr<tensorflow::Session> session_;
94  // The serialized graph is also contained in here.
95  TFNetworkModel model_proto_;
96 };
97 
98 } // namespace tesseract.
99 
100 #endif // ifdef INCLUDE_TENSORFLOW
101 
102 #endif // TESSERACT_TENSORFLOW_TFNETWORK_H_
Definition: strngs.h:45
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27
const char * c_str() const
Definition: strngs.cpp:205