tesseract  4.0.0-1-g2a2b
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.proto.h"
31 #include "third_party/tensorflow/core/framework/graph.pb.h"
32 #include "third_party/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 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  int InitFromProto();
73 
74  // The original network definition for reference.
75  string spec_;
76  // Input tensor parameters.
77  StaticShape input_shape_;
78  // Output tensor parameters.
79  StaticShape output_shape_;
80  // The tensor flow graph is contained in here.
81  std::unique_ptr<tensorflow::Session> session_;
82  // The serialized graph is also contained in here.
83  TFNetworkModel model_proto_;
84 };
85 
86 } // namespace tesseract.
87 
88 #endif // ifdef INCLUDE_TENSORFLOW
89 
90 #endif // TESSERACT_TENSORFLOW_TFNETWORK_H_
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
const char * c_str() const
Definition: strngs.cpp:207
Definition: strngs.h:45
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27