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<FullConvolution>,
44  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
45  typename InputDataType = arma::mat,
46  typename OutputDataType = arma::mat
47 >
48 class TransposedConvolution
49 {
50  public:
53 
69  TransposedConvolution(const size_t inSize,
70  const size_t outSize,
71  const size_t kW,
72  const size_t kH,
73  const size_t dW = 1,
74  const size_t dH = 1,
75  const size_t padW = 0,
76  const size_t padH = 0,
77  const size_t inputWidth = 0,
78  const size_t inputHeight = 0);
79 
80  /*
81  * Set the weight and bias term.
82  */
83  void Reset();
84 
92  template<typename eT>
93  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
94 
104  template<typename eT>
105  void Backward(const arma::Mat<eT>&& /* input */,
106  arma::Mat<eT>&& gy,
107  arma::Mat<eT>&& g);
108 
109  /*
110  * Calculate the gradient using the output delta and the input activation.
111  *
112  * @param input The input parameter used for calculating the gradient.
113  * @param error The calculated error.
114  * @param gradient The calculated gradient.
115  */
116  template<typename eT>
117  void Gradient(const arma::Mat<eT>&& /* input */,
118  arma::Mat<eT>&& error,
119  arma::Mat<eT>&& gradient);
120 
122  OutputDataType const& Parameters() const { return weights; }
124  OutputDataType& Parameters() { return weights; }
125 
127  InputDataType const& InputParameter() const { return inputParameter; }
129  InputDataType& InputParameter() { return inputParameter; }
130 
132  OutputDataType const& OutputParameter() const { return outputParameter; }
134  OutputDataType& OutputParameter() { return outputParameter; }
135 
137  OutputDataType const& Delta() const { return delta; }
139  OutputDataType& Delta() { return delta; }
140 
142  OutputDataType const& Gradient() const { return gradient; }
144  OutputDataType& Gradient() { return gradient; }
145 
147  size_t const& InputWidth() const { return inputWidth; }
149  size_t& InputWidth() { return inputWidth; }
150 
152  size_t const& InputHeight() const { return inputHeight; }
154  size_t& InputHeight() { return inputHeight; }
155 
157  size_t const& OutputWidth() const { return outputWidth; }
159  size_t& OutputWidth() { return outputWidth; }
160 
162  size_t const& OutputHeight() const { return outputHeight; }
164  size_t& OutputHeight() { return outputHeight; }
165 
167  arma::mat& Bias() { return bias; }
168 
172  template<typename Archive>
173  void serialize(Archive& ar, const unsigned int /* version */);
174 
175  private:
176  /*
177  * Return the transposed convolution output size.
178  *
179  * @param size The size of the input (row or column).
180  * @param k The size of the filter (width or height).
181  * @param s The stride size (x or y direction).
182  * @param p The size of the padding (width or height).
183  * @return The transposed convolution output size.
184  */
185  size_t TransposedConvOutSize(const size_t size,
186  const size_t k,
187  const size_t s,
188  const size_t p)
189  {
190  size_t out = std::floor(size - k + 2 * p) / s;
191  return out * s + 2 * (k - p) - 1 + ((((size + 2 * p - k) % s) + s) % s);
192  }
193 
194  /*
195  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
196  *
197  * @param input The input data to be rotated.
198  * @param output The rotated output.
199  */
200  template<typename eT>
201  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
202  {
203  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
204 
205  // * left-right flip, up-down flip */
206  for (size_t s = 0; s < output.n_slices; s++)
207  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
208  }
209 
210  /*
211  * Rotates a dense matrix counterclockwise by 180 degrees.
212  *
213  * @param input The input data to be rotated.
214  * @param output The rotated output.
215  */
216  template<typename eT>
217  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
218  {
219  // * left-right flip, up-down flip */
220  output = arma::fliplr(arma::flipud(input));
221  }
222 
224  size_t inSize;
225 
227  size_t outSize;
228 
230  size_t batchSize;
231 
233  size_t kW;
234 
236  size_t kH;
237 
239  size_t dW;
240 
242  size_t dH;
243 
245  size_t padW;
246 
248  size_t padH;
249 
251  OutputDataType weights;
252 
254  arma::cube weight;
255 
257  arma::mat bias;
258 
260  size_t inputWidth;
261 
263  size_t inputHeight;
264 
266  size_t outputWidth;
267 
269  size_t outputHeight;
270 
272  arma::cube outputTemp;
273 
275  arma::cube inputTemp;
276 
278  arma::cube inputPaddedTemp;
279 
281  arma::cube gTemp;
282 
284  arma::cube gradientTemp;
285 
287  Padding<>* padding;
288 
290  OutputDataType delta;
291 
293  OutputDataType gradient;
294 
296  InputDataType inputParameter;
297 
299  OutputDataType outputParameter;
300 }; // class TransposedConvolution
301 
302 } // namespace ann
303 } // namespace mlpack
304 
305 // Include implementation.
306 #include "transposed_convolution_impl.hpp"
307 
308 #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.