tesseract  5.0.0
reversed.cpp
Go to the documentation of this file.
1 // File: reversed.cpp
3 // Description: Runs a single network on time-reversed input, reversing output.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2013, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 
18 #include "reversed.h"
19 
20 #include <cstdio>
21 
22 #include "networkscratch.h"
23 
24 namespace tesseract {
25 
26 Reversed::Reversed(const std::string &name, NetworkType type) : Plumbing(name) {
27  type_ = type;
28 }
29 
30 // Returns the shape output from the network given an input shape (which may
31 // be partially unknown ie zero).
32 StaticShape Reversed::OutputShape(const StaticShape &input_shape) const {
33  if (type_ == NT_XYTRANSPOSE) {
34  StaticShape x_shape(input_shape);
35  x_shape.set_width(input_shape.height());
36  x_shape.set_height(input_shape.width());
37  x_shape = stack_[0]->OutputShape(x_shape);
38  x_shape.SetShape(x_shape.batch(), x_shape.width(), x_shape.height(), x_shape.depth());
39  return x_shape;
40  }
41  return stack_[0]->OutputShape(input_shape);
42 }
43 
44 // Takes ownership of the given network to make it the reversed one.
45 void Reversed::SetNetwork(Network *network) {
46  stack_.clear();
47  AddToStack(network);
48 }
49 
50 // Runs forward propagation of activations on the input line.
51 // See NetworkCpp for a detailed discussion of the arguments.
52 void Reversed::Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
53  NetworkScratch *scratch, NetworkIO *output) {
54  NetworkScratch::IO rev_input(input, scratch);
55  ReverseData(input, rev_input);
56  NetworkScratch::IO rev_output(input, scratch);
57  stack_[0]->Forward(debug, *rev_input, nullptr, scratch, rev_output);
58  ReverseData(*rev_output, output);
59 }
60 
61 // Runs backward propagation of errors on the deltas line.
62 // See NetworkCpp for a detailed discussion of the arguments.
63 bool Reversed::Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
64  NetworkIO *back_deltas) {
65  NetworkScratch::IO rev_input(fwd_deltas, scratch);
66  ReverseData(fwd_deltas, rev_input);
67  NetworkScratch::IO rev_output(fwd_deltas, scratch);
68  if (stack_[0]->Backward(debug, *rev_input, scratch, rev_output)) {
69  ReverseData(*rev_output, back_deltas);
70  return true;
71  }
72  return false;
73 }
74 
75 // Copies src to *dest with the reversal according to type_.
76 void Reversed::ReverseData(const NetworkIO &src, NetworkIO *dest) const {
77  if (type_ == NT_XREVERSED) {
78  dest->CopyWithXReversal(src);
79  } else if (type_ == NT_YREVERSED) {
80  dest->CopyWithYReversal(src);
81  } else {
82  dest->CopyWithXYTranspose(src);
83  }
84 }
85 
86 } // namespace tesseract.
NetworkType
Definition: network.h:41
@ NT_XREVERSED
Definition: network.h:54
@ NT_YREVERSED
Definition: network.h:55
@ NT_XYTRANSPOSE
Definition: network.h:56
NetworkType type_
Definition: network.h:300
NetworkType type() const
Definition: network.h:110
void CopyWithXReversal(const NetworkIO &src)
Definition: networkio.cpp:897
void CopyWithXYTranspose(const NetworkIO &src)
Definition: networkio.cpp:915
void CopyWithYReversal(const NetworkIO &src)
Definition: networkio.cpp:877
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:84
std::vector< Network * > stack_
Definition: plumbing.h:150
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reversed.cpp:52
TESS_API Reversed(const std::string &name, NetworkType type)
Definition: reversed.cpp:26
TESS_API void SetNetwork(Network *network)
Definition: reversed.cpp:45
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reversed.cpp:32
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reversed.cpp:63
void SetShape(int batch, int height, int width, int depth)
Definition: static_shape.h:71
void set_width(int value)
Definition: static_shape.h:56
void set_height(int value)
Definition: static_shape.h:50