MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Cropping.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_MLNN_CROPPING_HPP_
24 #define SRC_MLNN_CROPPING_HPP_
25 
26 #include <mlnn/layer/Layer.hpp>
27 
28 namespace mic {
29 namespace mlnn {
30 namespace convolution {
31 
37 template <typename eT=float>
38 class Cropping : public mic::mlnn::Layer<eT> {
39 public:
40 
49  Cropping(size_t input_height_, size_t input_width_, size_t depth_,
50  size_t cropping_,
51  std::string name_ = "Cropping") :
52  Layer<eT>::Layer(input_height_, input_width_, depth_,
53  (input_height_ - 2*cropping_), (input_width_ - 2*cropping_), depth_,
54  LayerTypes::Cropping, name_),
55  cropping(cropping_)
56  {
57 
58  }
59 
63  virtual ~Cropping() { }
64 
68  void forward(bool test = false) {
69 
70  // Get pointer to input batch.
71  mic::types::MatrixPtr<eT> batch_x = s['x'];
72  LOG(LTRACE) << "Cropping::forward input x activation: min:" << (*batch_x).minCoeff() <<" max: " << (*batch_x).maxCoeff() << std::endl;
73 
74  // Get pointer to output batch - so the results will be stored!
75  mic::types::MatrixPtr<eT> batch_y = s['y'];
76  batch_y->setZero();
77 
78  // TODO: should work for more channels - but requires testing!
79  assert(input_depth == 1);
80 
81  // Iterate through batch.
82  #pragma omp parallel for
83  for (size_t ib = 0; ib < batch_size; ib++) {
84 
85  // Iterate through input/output channels.
86  for (size_t ic=0; ic< input_depth; ic++) {
87 
88  // Iterate through "blocks" o in channels.
89  for (size_t iw=0; iw< output_width; iw++) {
90  // Calculate addresses.
91  size_t ia = ic * (input_width) * (input_height) + (iw+cropping)*(input_height) + cropping;
92  size_t oa = ic * (output_width) * (output_height) + iw*(output_height);
93  //std::cout << " ib = " << ib << " ic = " << ic <<" iw = " << iw << " ia = " << ia << " oa = " << oa << std::endl;
94 
95  #pragma omp critical
96  {
97  // Copy "height" block from input to output.
98  batch_y->block(oa, ib, output_height, 1) =
99  batch_x->block(ia, ib, output_height, 1);
100  }//: omp critical
101 
102  }//: for width
103  }//: for channels
104  }//: for batch
105  LOG(LTRACE) << "Cropping::forward end\n";
106  }
107 
111  void backward() {
112  LOG(LTRACE) << "Cropping::backward\n";
113  // Get pointer to dy batch.
114  mic::types::MatrixPtr<eT> batch_dy = g['y'];
115 
116  //std::cout << "batch_dy [batch x height x width] = " << batch_size << " x " << output_height << " x " << output_width << std::endl;
117  //std::cout << "batch_dx [batch x height x width] = " << batch_size << " x " << input_height << " x " << input_width << std::endl;
118 
119  // Get pointer to dx batch.
120  mic::types::MatrixPtr<eT> batch_dx = g['x'];
121  //batch_dx->setZero();
122 
123  // Iterate through batch.
124  //#pragma omp parallel for
125  for (size_t ib = 0; ib < batch_size; ib++) {
126 
127  // Iterate through input/output channels.
128  for (size_t ic=0; ic< input_depth; ic++) {
129 
130  // Iterate through "blocks" o in channels.
131  for (size_t iw=0; iw< output_width; iw++) {
132  // Calculate addresses.
133  size_t ia = ic * (input_width) * (input_height) + (iw+cropping)*(input_height) + cropping;
134  size_t oa = ic * (output_width) * (output_height) + iw*(output_height);
135  //std::cout << " ib = " << ib << " ic = " << ic <<" iw = " << iw << " ia = " << ia << " oa = " << oa << std::endl;
136 
137  #pragma omp critical
138  {
139  //std::cout << "batch_dy->block(oa, ib, output_height, 1) = " << batch_dy->block(oa, ib, output_height, 1) << std::endl;
140  // Copy "height" block from input to output.
141  batch_dx->block(ia, ib, output_height, 1) = batch_dy->block(oa, ib, output_height, 1);
142  }//: omp critical
143 
144  }//: for width
145  }//: for channels
146  }//: for batch
147 
148  LOG(LTRACE) << "Cropping::backward end\n";
149  }
150 
156  void update(eT alpha_, eT decay_ = 0.0f) { }
157 
158  // Unhide the overloaded methods inherited from the template class Layer fields via "using" statement.
159  using Layer<eT>::forward;
160  using Layer<eT>::backward;
161 
162 protected:
163  // Unhide the fields inherited from the template class Layer via "using" statement.
164  using Layer<eT>::g;
165  using Layer<eT>::s;
166  using Layer<eT>::p;
167  using Layer<eT>::m;
168 
169  // Uncover "sizes" for visualization.
176  using Layer<eT>::batch_size;
177 
179  size_t cropping;
180 
181 private:
182  // Friend class - required for using boost serialization.
183  template<typename tmp> friend class mic::mlnn::MultiLayerNeuralNetwork;
184 
189 
190 
191 
192 };
193 
194 
195 } /* namespace convolution */
196 } /* namespace mlnn */
197 } /* namespace mic */
198 
199 #endif /* SRC_MLNN_CROPPING_HPP_ */
size_t cropping
Cropping size - number of pixels removed in each channel (width and height)
Definition: Cropping.hpp:179
size_t input_depth
Number of channels of the input (e.g. 3 for RGB images).
Definition: Layer.hpp:732
Class implementing cropping operation - crops the size of image (matrix) by a margin of n pixels on e...
Definition: Cropping.hpp:38
size_t batch_size
Size (length) of (mini)batch.
Definition: Layer.hpp:744
void forward(bool test=false)
Definition: Cropping.hpp:68
size_t input_height
Height of the input (e.g. 28 for MNIST).
Definition: Layer.hpp:726
Class representing a multi-layer neural network.
Definition: Layer.hpp:86
LayerTypes
Enumeration of possible layer types.
Definition: Layer.hpp:58
mic::types::MatrixArray< eT > s
States - contains input [x] and output [y] matrices.
Definition: Layer.hpp:753
mic::types::MatrixArray< eT > g
Gradients - contains input [x] and output [y] matrices.
Definition: Layer.hpp:756
size_t input_width
Width of the input (e.g. 28 for MNIST).
Definition: Layer.hpp:729
Cropping(size_t input_height_, size_t input_width_, size_t depth_, size_t cropping_, std::string name_="Cropping")
Definition: Cropping.hpp:49
size_t output_height
Number of receptive fields in a single channel - vertical direction.
Definition: Layer.hpp:735
void update(eT alpha_, eT decay_=0.0f)
Definition: Cropping.hpp:156
size_t output_width
Number of receptive fields in a single channel - horizontal direction.
Definition: Layer.hpp:738
Contains a template class representing a layer.