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 
25 namespace mlpack {
26 namespace ann {
27 
40 template <
41  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
42  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
43  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
44  typename InputDataType = arma::mat,
45  typename OutputDataType = arma::mat
46 >
47 class TransposedConvolution
48 {
49  public:
52 
68  TransposedConvolution(const size_t inSize,
69  const size_t outSize,
70  const size_t kW,
71  const size_t kH,
72  const size_t dW = 1,
73  const size_t dH = 1,
74  const size_t padW = 0,
75  const size_t padH = 0,
76  const size_t inputWidth = 0,
77  const size_t inputHeight = 0);
78 
79  /*
80  * Set the weight and bias term.
81  */
82  void Reset();
83 
91  template<typename eT>
92  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
93 
103  template<typename eT>
104  void Backward(const arma::Mat<eT>&& /* input */,
105  arma::Mat<eT>&& gy,
106  arma::Mat<eT>&& g);
107 
108  /*
109  * Calculate the gradient using the output delta and the input activation.
110  *
111  * @param input The input parameter used for calculating the gradient.
112  * @param error The calculated error.
113  * @param gradient The calculated gradient.
114  */
115  template<typename eT>
116  void Gradient(const arma::Mat<eT>&& /* input */,
117  arma::Mat<eT>&& error,
118  arma::Mat<eT>&& gradient);
119 
121  OutputDataType const& Parameters() const { return weights; }
123  OutputDataType& Parameters() { return weights; }
124 
126  InputDataType const& InputParameter() const { return inputParameter; }
128  InputDataType& InputParameter() { return inputParameter; }
129 
131  OutputDataType const& OutputParameter() const { return outputParameter; }
133  OutputDataType& OutputParameter() { return outputParameter; }
134 
136  OutputDataType const& Delta() const { return delta; }
138  OutputDataType& Delta() { return delta; }
139 
141  OutputDataType const& Gradient() const { return gradient; }
143  OutputDataType& Gradient() { return gradient; }
144 
146  size_t const& InputWidth() const { return inputWidth; }
148  size_t& InputWidth() { return inputWidth; }
149 
151  size_t const& InputHeight() const { return inputHeight; }
153  size_t& InputHeight() { return inputHeight; }
154 
156  size_t const& OutputWidth() const { return outputWidth; }
158  size_t& OutputWidth() { return outputWidth; }
159 
161  size_t const& OutputHeight() const { return outputHeight; }
163  size_t& OutputHeight() { return outputHeight; }
164 
168  template<typename Archive>
169  void serialize(Archive& ar, const unsigned int /* version */);
170 
171  private:
172  /*
173  * Return the transposed convolution output size.
174  *
175  * @param size The size of the input (row or column).
176  * @param k The size of the filter (width or height).
177  * @param s The stride size (x or y direction).
178  * @param p The size of the padding (width or height).
179  * @return The transposed convolution output size.
180  */
181  size_t TransposedConvOutSize(const size_t size,
182  const size_t k,
183  const size_t s,
184  const size_t p)
185  {
186  size_t out = std::floor(size - k + 2 * p) / s;
187  return out * s + 2 * (k - p) - 1 + ((((size + 2 * p - k) % s) + s) % s);
188  }
189 
190  /*
191  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
192  *
193  * @param input The input data to be rotated.
194  * @param output The rotated output.
195  */
196  template<typename eT>
197  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
198  {
199  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
200 
201  // * left-right flip, up-down flip */
202  for (size_t s = 0; s < output.n_slices; s++)
203  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
204  }
205 
206  /*
207  * Rotates a dense matrix counterclockwise by 180 degrees.
208  *
209  * @param input The input data to be rotated.
210  * @param output The rotated output.
211  */
212  template<typename eT>
213  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
214  {
215  // * left-right flip, up-down flip */
216  output = arma::fliplr(arma::flipud(input));
217  }
218 
219  /*
220  * Pad the given input data.
221  *
222  * @param input The input to be padded.
223  * @param wPad Padding width of the input.
224  * @param hPad Padding height of the input.
225  * @param output The padded output data.
226  */
227  template<typename eT>
228  void Pad(const arma::Mat<eT>& input,
229  size_t wPad,
230  size_t hPad,
231  arma::Mat<eT>& output)
232  {
233  if (output.n_rows != input.n_rows + wPad * 2 ||
234  output.n_cols != input.n_cols + hPad * 2)
235  {
236  output = arma::zeros(input.n_rows + wPad * 2, input.n_cols + hPad * 2);
237  }
238 
239  output.submat(wPad, hPad, wPad + input.n_rows - 1,
240  hPad + input.n_cols - 1) = input;
241  }
242 
243  /*
244  * Pad the given input data.
245  *
246  * @param input The input to be padded.
247  * @param wPad Padding width of the input.
248  * @param hPad Padding height of the input.
249  * @param output The padded output data.
250  */
251  template<typename eT>
252  void Pad(const arma::Cube<eT>& input,
253  size_t wPad,
254  size_t hPad,
255  arma::Cube<eT>& output)
256  {
257  output = arma::zeros(input.n_rows + wPad * 2,
258  input.n_cols + hPad * 2, input.n_slices);
259 
260  for (size_t i = 0; i < input.n_slices; ++i)
261  {
262  Pad<eT>(input.slice(i), wPad, hPad, output.slice(i));
263  }
264  }
265 
267  size_t inSize;
268 
270  size_t outSize;
271 
273  size_t kW;
274 
276  size_t kH;
277 
279  size_t dW;
280 
282  size_t dH;
283 
285  size_t padW;
286 
288  size_t padH;
289 
291  OutputDataType weights;
292 
294  arma::cube weight;
295 
297  arma::mat bias;
298 
300  size_t inputWidth;
301 
303  size_t inputHeight;
304 
306  size_t outputWidth;
307 
309  size_t outputHeight;
310 
312  arma::cube outputTemp;
313 
315  arma::cube inputTemp;
316 
318  arma::cube inputPaddedTemp;
319 
321  arma::cube gTemp;
322 
324  arma::cube gradientTemp;
325 
327  OutputDataType delta;
328 
330  OutputDataType gradient;
331 
333  InputDataType inputParameter;
334 
336  OutputDataType outputParameter;
337 }; // class TransposedConvolution
338 
339 } // namespace ann
340 } // namespace mlpack
341 
342 // Include implementation.
343 #include "transposed_convolution_impl.hpp"
344 
345 #endif
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
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.