tesseract  5.0.0-alpha-619-ge9db
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 
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.
tesseract::StaticShape
Definition: static_shape.h:38
tesseract::Reversed::OutputShape
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: reversed.cpp:33
tesseract::StaticShape::SetShape
void SetShape(int batch, int height, int width, int depth)
Definition: static_shape.h:52
tesseract::Reversed::Forward
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: reversed.cpp:54
tesseract::NT_XYTRANSPOSE
Definition: network.h:58
tesseract::Plumbing::AddToStack
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:82
tesseract::StaticShape::batch
int batch() const
Definition: static_shape.h:42
STRING
Definition: strngs.h:45
tesseract::NetworkScratch
Definition: networkscratch.h:34
tesseract::Network::type
NetworkType type() const
Definition: network.h:112
tesseract::NetworkType
NetworkType
Definition: network.h:43
tesseract::Plumbing::stack_
PointerVector< Network > stack_
Definition: plumbing.h:136
tesseract::Reversed::Backward
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reversed.cpp:66
networkscratch.h
tesseract::StaticShape::set_width
void set_width(int value)
Definition: static_shape.h:47
tesseract::StaticShape::set_height
void set_height(int value)
Definition: static_shape.h:45
tesseract::Network::type_
NetworkType type_
Definition: network.h:293
tesseract::StaticShape::depth
int depth() const
Definition: static_shape.h:48
tesseract::NT_YREVERSED
Definition: network.h:57
tesseract::Reversed::SetNetwork
void SetNetwork(Network *network)
Definition: reversed.cpp:47
tesseract::NetworkIO
Definition: networkio.h:39
tesseract::Plumbing
Definition: plumbing.h:30
tesseract
Definition: baseapi.h:65
tesseract::StaticShape::width
int width() const
Definition: static_shape.h:46
tesseract::NT_XREVERSED
Definition: network.h:56
tesseract::NetworkScratch::IO
Definition: networkscratch.h:51
tesseract::StaticShape::height
int height() const
Definition: static_shape.h:44
tesseract::Network
Definition: network.h:105
tesseract::Reversed::Reversed
Reversed(const STRING &name, NetworkType type)
Definition: reversed.cpp:27
tesseract::TransposedArray
Definition: weightmatrix.h:32
tesstrain_utils.dest
dest
Definition: tesstrain_utils.py:139
tesstrain_utils.type
type
Definition: tesstrain_utils.py:141
reversed.h