transposed_convolution.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP
14 #define MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
22 
23 #include "layer_types.hpp"
24 #include "padding.hpp"
25 
26 namespace mlpack {
27 namespace ann {
28 
41 template <
42  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
43  typename BackwardConvolutionRule = NaiveConvolution<ValidConvolution>,
44  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
45  typename InputDataType = arma::mat,
46  typename OutputDataType = arma::mat
47 >
48 class TransposedConvolution
49 {
50  public:
53 
77  TransposedConvolution(const size_t inSize,
78  const size_t outSize,
79  const size_t kW,
80  const size_t kH,
81  const size_t dW = 1,
82  const size_t dH = 1,
83  const size_t padW = 0,
84  const size_t padH = 0,
85  const size_t inputWidth = 0,
86  const size_t inputHeight = 0,
87  const size_t outputWidth = 0,
88  const size_t outputHeight = 0);
89 
90  /*
91  * Set the weight and bias term.
92  */
93  void Reset();
94 
102  template<typename eT>
103  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
104 
114  template<typename eT>
115  void Backward(const arma::Mat<eT>&& /* input */,
116  arma::Mat<eT>&& gy,
117  arma::Mat<eT>&& g);
118 
119  /*
120  * Calculate the gradient using the output delta and the input activation.
121  *
122  * @param input The input parameter used for calculating the gradient.
123  * @param error The calculated error.
124  * @param gradient The calculated gradient.
125  */
126  template<typename eT>
127  void Gradient(const arma::Mat<eT>&& /* input */,
128  arma::Mat<eT>&& error,
129  arma::Mat<eT>&& gradient);
130 
132  OutputDataType const& Parameters() const { return weights; }
134  OutputDataType& Parameters() { return weights; }
135 
137  InputDataType const& InputParameter() const { return inputParameter; }
139  InputDataType& InputParameter() { return inputParameter; }
140 
142  OutputDataType const& OutputParameter() const { return outputParameter; }
144  OutputDataType& OutputParameter() { return outputParameter; }
145 
147  OutputDataType const& Delta() const { return delta; }
149  OutputDataType& Delta() { return delta; }
150 
152  OutputDataType const& Gradient() const { return gradient; }
154  OutputDataType& Gradient() { return gradient; }
155 
157  size_t const& InputWidth() const { return inputWidth; }
159  size_t& InputWidth() { return inputWidth; }
160 
162  size_t const& InputHeight() const { return inputHeight; }
164  size_t& InputHeight() { return inputHeight; }
165 
167  size_t const& OutputWidth() const { return outputWidth; }
169  size_t& OutputWidth() { return outputWidth; }
170 
172  size_t const& OutputHeight() const { return outputHeight; }
174  size_t& OutputHeight() { return outputHeight; }
175 
177  arma::mat& Bias() { return bias; }
178 
182  template<typename Archive>
183  void serialize(Archive& ar, const unsigned int /* version */);
184 
185  private:
186  /*
187  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
188  *
189  * @param input The input data to be rotated.
190  * @param output The rotated output.
191  */
192  template<typename eT>
193  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
194  {
195  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
196 
197  // * left-right flip, up-down flip */
198  for (size_t s = 0; s < output.n_slices; s++)
199  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
200  }
201 
202  /*
203  * Rotates a dense matrix counterclockwise by 180 degrees.
204  *
205  * @param input The input data to be rotated.
206  * @param output The rotated output.
207  */
208  template<typename eT>
209  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
210  {
211  // * left-right flip, up-down flip */
212  output = arma::fliplr(arma::flipud(input));
213  }
214 
215  /*
216  * Pad the given input data.
217  *
218  * @param input The input to be padded.
219  * @param wPad Padding width of the input.
220  * @param hPad Padding height of the input.
221  * @param wExtra The number of extra zeros to the right.
222  * @param hExtra The number of extra zeros to the bottom.
223  * @param output The padded output data.
224  */
225  template<typename eT>
226  void Pad(const arma::Mat<eT>& input,
227  const size_t wPad,
228  const size_t hPad,
229  const size_t wExtra,
230  const size_t hExtra,
231  arma::Mat<eT>& output)
232  {
233  if (output.n_rows != input.n_rows + wPad * 2 + wExtra ||
234  output.n_cols != input.n_cols + hPad * 2 + hExtra)
235  {
236  output = arma::zeros(input.n_rows + wPad * 2 + wExtra,
237  input.n_cols + hPad * 2 + hExtra);
238  }
239 
240  output.submat(wPad, hPad, wPad + input.n_rows - 1,
241  hPad + input.n_cols - 1) = input;
242  }
243 
244  /*
245  * Pad the given input data.
246  *
247  * @param input The input to be padded.
248  * @param wPad Padding width of the input.
249  * @param hPad Padding height of the input.
250  * @param wExtra The number of extra zeros to the right.
251  * @param hExtra The number of extra zeros to the bottom.
252  * @param output The padded output data.
253  */
254  template<typename eT>
255  void Pad(const arma::Cube<eT>& input,
256  const size_t wPad,
257  const size_t hPad,
258  const size_t wExtra,
259  const size_t hExtra,
260  arma::Cube<eT>& output)
261  {
262  output = arma::zeros(input.n_rows + wPad * 2 + wExtra,
263  input.n_cols + hPad * 2 + hExtra, input.n_slices);
264 
265  for (size_t i = 0; i < input.n_slices; ++i)
266  {
267  Pad<eT>(input.slice(i), wPad, hPad, wExtra, hExtra, output.slice(i));
268  }
269  }
270 
271  /*
272  * Insert zeros between the units of the given input data.
273  * Note: This function should be used before the Pad() function.
274  *
275  * @param input The input to be padded.
276  * @param dW Stride of filter application in the x direction.
277  * @param dH Stride of filter application in the y direction.
278  * @param output The padded output data.
279  */
280  template<typename eT>
281  void InsertZeros(const arma::Mat<eT>& input,
282  const size_t dW,
283  const size_t dH,
284  arma::Mat<eT>& output)
285  {
286  if (output.n_rows != input.n_rows * dW - dW + 1 ||
287  output.n_cols != input.n_cols * dH - dH + 1)
288  {
289  output = arma::zeros(input.n_rows * dW - dW + 1,
290  input.n_cols * dH - dH + 1);
291  }
292 
293  for (size_t i = 0; i < output.n_rows; i += dH)
294  {
295  for (size_t j = 0; j < output.n_cols; j += dW)
296  {
297  // TODO: Use [] instead of () for speedup after this is completely
298  // debugged and approved.
299  output(i, j) = input(i / dH, j / dW);
300  }
301  }
302  }
303 
304  /*
305  * Insert zeros between the units of the given input data.
306  * Note: This function should be used before the Pad() function.
307  *
308  * @param input The input to be padded.
309  * @param dW Stride of filter application in the x direction.
310  * @param dH Stride of filter application in the y direction.
311  * @param output The padded output data.
312  */
313  template<typename eT>
314  void InsertZeros(const arma::Cube<eT>& input,
315  const size_t dW,
316  const size_t dH,
317  arma::Cube<eT>& output)
318  {
319  output = arma::zeros(input.n_rows * dW - dW + 1,
320  input.n_cols * dH - dH + 1, input.n_slices);
321 
322  for (size_t i = 0; i < input.n_slices; ++i)
323  {
324  InsertZeros<eT>(input.slice(i), dW, dH, output.slice(i));
325  }
326  }
327 
329  size_t inSize;
330 
332  size_t outSize;
333 
335  size_t batchSize;
336 
338  size_t kW;
339 
341  size_t kH;
342 
344  size_t dW;
345 
347  size_t dH;
348 
350  size_t padW;
351 
353  size_t padH;
354 
356  size_t aW;
357 
359  size_t aH;
360 
362  OutputDataType weights;
363 
365  arma::cube weight;
366 
368  arma::mat bias;
369 
371  size_t inputWidth;
372 
374  size_t inputHeight;
375 
377  size_t outputWidth;
378 
380  size_t outputHeight;
381 
383  arma::cube outputTemp;
384 
386  arma::cube inputTemp;
387 
389  arma::cube inputPaddedTemp;
390 
392  arma::cube inputExpandedTemp;
393 
395  arma::cube gTemp;
396 
398  arma::cube gradientTemp;
399 
401  Padding<>* padding;
402 
404  OutputDataType delta;
405 
407  OutputDataType gradient;
408 
410  InputDataType inputParameter;
411 
413  OutputDataType outputParameter;
414 }; // class TransposedConvolution
415 
416 } // namespace ann
417 } // namespace mlpack
418 
419 // Include implementation.
420 #include "transposed_convolution_impl.hpp"
421 
422 #endif
arma::mat & Bias()
Modify the bias weights of the layer.
void Forward(const arma::Mat< eT > &&input, arma::Mat< eT > &&output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
OutputDataType const & Gradient() const
Get the gradient.
OutputDataType & Parameters()
Modify the parameters.
size_t const & OutputHeight() const
Get the output height.
size_t const & InputHeight() const
Get the input height.
.hpp
Definition: add_to_po.hpp:21
Implementation of the Padding module class.
Definition: layer_types.hpp:68
OutputDataType & Delta()
Modify the delta.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t & InputHeight()
Modify the input height.
OutputDataType const & OutputParameter() const
Get the output parameter.
void Backward(const arma::Mat< eT > &&, arma::Mat< eT > &&gy, arma::Mat< eT > &&g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
OutputDataType const & Parameters() const
Get the parameters.
size_t const & InputWidth() const
Get the input width.
size_t const & OutputWidth() const
Get the output width.
TransposedConvolution()
Create the Transposed Convolution object.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Gradient()
Modify the gradient.
InputDataType const & InputParameter() const
Get the input parameter.
OutputDataType const & Delta() const
Get the delta.
size_t & OutputWidth()
Modify the output width.
size_t & InputWidth()
Modify input the width.
OutputDataType & OutputParameter()
Modify the output parameter.
InputDataType & InputParameter()
Modify the input parameter.
size_t & OutputHeight()
Modify the output height.