tesseract  5.0.0
tfnetwork.cpp
Go to the documentation of this file.
1 // File: tfnetwork.cpp
3 // Description: Encapsulation of an entire tensorflow graph as a
4 // Tesseract Network.
5 // Author: Ray Smith
6 //
7 // (C) Copyright 2016, 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 #ifdef INCLUDE_TENSORFLOW
19 
20 # include "tfnetwork.h"
21 
22 # include <allheaders.h>
23 # include "input.h"
24 # include "networkscratch.h"
25 
26 using tensorflow::Status;
27 using tensorflow::Tensor;
28 using tensorflow::TensorShape;
29 
30 namespace tesseract {
31 
32 TFNetwork::TFNetwork(const char *name) : Network(NT_TENSORFLOW, name, 0, 0) {}
33 
34 int TFNetwork::InitFromProtoStr(const std::string &proto_str) {
35  if (!model_proto_.ParseFromString(proto_str))
36  return 0;
37  return InitFromProto();
38 }
39 
40 // Writes to the given file. Returns false in case of error.
41 // Should be overridden by subclasses, but called by their Serialize.
42 bool TFNetwork::Serialize(TFile *fp) const {
43  if (!Network::Serialize(fp))
44  return false;
45  std::string proto_str;
46  model_proto_.SerializeToString(&proto_str);
47  // TODO: optimize and avoid copy from proto_str to data.
48  std::vector<char> data(proto_str.size());
49  memcpy(&data[0], proto_str.data(), proto_str.size());
50  return fp->Serialize(data);
51 }
52 
53 // Reads from the given file. Returns false in case of error.
54 // Should be overridden by subclasses, but NOT called by their DeSerialize.
55 bool TFNetwork::DeSerialize(TFile *fp) {
56  std::vector<char> data;
57  if (!fp->DeSerialize(data))
58  return false;
59  if (!model_proto_.ParseFromArray(&data[0], data.size())) {
60  return false;
61  }
62  return InitFromProto();
63 }
64 
65 // Runs forward propagation of activations on the input line.
66 // See Network for a detailed discussion of the arguments.
67 void TFNetwork::Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
68  NetworkScratch *scratch, NetworkIO *output) {
69  std::vector<std::pair<std::string, Tensor>> tf_inputs;
70  int depth = input_shape_.depth();
71  ASSERT_HOST(depth == input.NumFeatures());
72  // TODO(rays) Allow batching. For now batch_size = 1.
73  const StrideMap &stride_map = input.stride_map();
74  // TF requires a tensor of shape float[batch, height, width, depth].
75  TensorShape shape{1, stride_map.Size(FD_HEIGHT), stride_map.Size(FD_WIDTH), depth};
76  Tensor input_tensor(tensorflow::DT_FLOAT, shape);
77  // The flat() member gives a 1d array, with a data() member to get the data.
78  auto eigen_tensor = input_tensor.flat<float>();
79  memcpy(eigen_tensor.data(), input.f(0), input.Width() * depth * sizeof(input.f(0)[0]));
80  // Add the tensor to the vector of inputs.
81  tf_inputs.emplace_back(model_proto_.image_input(), input_tensor);
82 
83  // Provide tensors giving the width and/or height of the image if they are
84  // required. Some tf ops require a separate tensor with knowledge of the
85  // size of the input as they cannot obtain it from the input tensor. This is
86  // usually true in the case of ops that process a batch of variable-sized
87  // objects.
88  if (!model_proto_.image_widths().empty()) {
89  TensorShape size_shape{1};
90  Tensor width_tensor(tensorflow::DT_INT64, size_shape);
91  auto eigen_wtensor = width_tensor.flat<tensorflow::int64>();
92  *eigen_wtensor.data() = stride_map.Size(FD_WIDTH);
93  tf_inputs.emplace_back(model_proto_.image_widths(), width_tensor);
94  }
95  if (!model_proto_.image_heights().empty()) {
96  TensorShape size_shape{1};
97  Tensor height_tensor(tensorflow::DT_INT64, size_shape);
98  auto eigen_htensor = height_tensor.flat<tensorflow::int64>();
99  *eigen_htensor.data() = stride_map.Size(FD_HEIGHT);
100  tf_inputs.emplace_back(model_proto_.image_heights(), height_tensor);
101  }
102  std::vector<std::string> target_layers = {model_proto_.output_layer()};
103  std::vector<Tensor> outputs;
104  Status s = session_->Run(tf_inputs, target_layers, {}, &outputs);
105  if (!s.ok())
106  tprintf("session->Run failed:%s\n", s.error_message().c_str());
107  ASSERT_HOST(s.ok());
108  ASSERT_HOST(outputs.size() == 1);
109  const Tensor &output_tensor = outputs[0];
110  // Check the dimensions of the output.
111  ASSERT_HOST(output_tensor.shape().dims() == 3);
112  int output_batch = output_tensor.shape().dim_size(0);
113  int output_steps = output_tensor.shape().dim_size(1);
114  int output_depth = output_tensor.shape().dim_size(2);
115  ASSERT_HOST(output_batch == 1);
116  ASSERT_HOST(output_depth == output_shape_.depth());
117  output->Resize2d(false, output_steps, output_depth);
118  auto eigen_output = output_tensor.flat<float>();
119  memcpy(output->f(0), eigen_output.data(), output_steps * output_depth * sizeof(output->f(0)[0]));
120 }
121 
122 int TFNetwork::InitFromProto() {
123  spec_ = model_proto_.spec();
124  input_shape_.SetShape(model_proto_.batch_size(), std::max(0, model_proto_.y_size()),
125  std::max(0, model_proto_.x_size()), model_proto_.depth());
126  output_shape_.SetShape(model_proto_.batch_size(), 1, 0, model_proto_.num_classes());
127  output_shape_.set_loss_type(model_proto_.using_ctc() ? LT_CTC : LT_SOFTMAX);
128  ni_ = input_shape_.height();
129  no_ = output_shape_.depth();
130  // Initialize the session_ with the graph. Since we can't get the graph
131  // back from the session_, we have to keep the proto as well
132  tensorflow::SessionOptions options;
133  session_.reset(NewSession(options));
134  Status s = session_->Create(model_proto_.graph());
135  if (s.ok())
136  return model_proto_.global_step();
137  tprintf("Session_->Create returned '%s'\n", s.error_message().c_str());
138  return 0;
139 }
140 
141 } // namespace tesseract
142 
143 #endif // ifdef INCLUDE_TENSORFLOW
#define ASSERT_HOST(x)
Definition: errcode.h:59
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
bool DeSerialize(bool swap, FILE *fp, std::vector< T > &data)
Definition: helpers.h:220
bool Serialize(FILE *fp, const std::vector< T > &data)
Definition: helpers.h:251
@ NT_TENSORFLOW
Definition: network.h:76
@ FD_WIDTH
Definition: stridemap.h:35
@ FD_HEIGHT
Definition: stridemap.h:34