tesseract  4.0.0-1-g2a2b
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 // Created: Thu May 02 08:42:06 PST 2013
6 //
7 // (C) Copyright 2013, 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 #include "reversed.h"
20 
21 #include <cstdio>
22 
23 #include "networkscratch.h"
24 
25 namespace tesseract {
26 
27 Reversed::Reversed(const STRING& name, NetworkType type) : Plumbing(name) {
28  type_ = type;
29 }
30 
31 // Returns the shape output from the network given an input shape (which may
32 // be partially unknown ie zero).
33 StaticShape Reversed::OutputShape(const StaticShape& input_shape) const {
34  if (type_ == NT_XYTRANSPOSE) {
35  StaticShape x_shape(input_shape);
36  x_shape.set_width(input_shape.height());
37  x_shape.set_height(input_shape.width());
38  x_shape = stack_[0]->OutputShape(x_shape);
39  x_shape.SetShape(x_shape.batch(), x_shape.width(), x_shape.height(),
40  x_shape.depth());
41  return x_shape;
42  }
43  return stack_[0]->OutputShape(input_shape);
44 }
45 
46 // Takes ownership of the given network to make it the reversed one.
47 void Reversed::SetNetwork(Network* network) {
48  stack_.clear();
49  AddToStack(network);
50 }
51 
52 // Runs forward propagation of activations on the input line.
53 // See NetworkCpp for a detailed discussion of the arguments.
54 void Reversed::Forward(bool debug, const NetworkIO& input,
55  const TransposedArray* input_transpose,
56  NetworkScratch* scratch, NetworkIO* output) {
57  NetworkScratch::IO rev_input(input, scratch);
58  ReverseData(input, rev_input);
59  NetworkScratch::IO rev_output(input, scratch);
60  stack_[0]->Forward(debug, *rev_input, nullptr, scratch, rev_output);
61  ReverseData(*rev_output, output);
62 }
63 
64 // Runs backward propagation of errors on the deltas line.
65 // See NetworkCpp for a detailed discussion of the arguments.
66 bool Reversed::Backward(bool debug, const NetworkIO& fwd_deltas,
67  NetworkScratch* scratch,
68  NetworkIO* back_deltas) {
69  NetworkScratch::IO rev_input(fwd_deltas, scratch);
70  ReverseData(fwd_deltas, rev_input);
71  NetworkScratch::IO rev_output(fwd_deltas, scratch);
72  if (stack_[0]->Backward(debug, *rev_input, scratch, rev_output)) {
73  ReverseData(*rev_output, back_deltas);
74  return true;
75  }
76  return false;
77 }
78 
79 // Copies src to *dest with the reversal according to type_.
80 void Reversed::ReverseData(const NetworkIO& src, NetworkIO* dest) const {
81  if (type_ == NT_XREVERSED)
82  dest->CopyWithXReversal(src);
83  else if (type_ == NT_YREVERSED)
84  dest->CopyWithYReversal(src);
85  else
86  dest->CopyWithXYTranspose(src);
87 }
88 
89 } // namespace tesseract.
void CopyWithXReversal(const NetworkIO &src)
Definition: networkio.cpp:877
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reversed.cpp:33
Reversed(const STRING &name, NetworkType type)
Definition: reversed.cpp:27
NetworkType
Definition: network.h:43
NetworkType type_
Definition: network.h:299
PointerVector< Network > stack_
Definition: plumbing.h:136
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:82
void set_width(int value)
Definition: static_shape.h:47
void CopyWithXYTranspose(const NetworkIO &src)
Definition: networkio.cpp:896
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reversed.cpp:66
void SetNetwork(Network *network)
Definition: reversed.cpp:47
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reversed.cpp:54
NetworkType type() const
Definition: network.h:112
Definition: strngs.h:45
void set_height(int value)
Definition: static_shape.h:45
void CopyWithYReversal(const NetworkIO &src)
Definition: networkio.cpp:858
void SetShape(int batch, int height, int width, int depth)
Definition: static_shape.h:52