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