atrous_convolution.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_ATROUS_CONVOLUTION_HPP
14 #define MLPACK_METHODS_ANN_LAYER_ATROUS_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 
44 template <
45  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
46  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
47  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
48  typename InputDataType = arma::mat,
49  typename OutputDataType = arma::mat
50 >
52 {
53  public:
56 
75  AtrousConvolution(const size_t inSize,
76  const size_t outSize,
77  const size_t kW,
78  const size_t kH,
79  const size_t dW = 1,
80  const size_t dH = 1,
81  const size_t padW = 0,
82  const size_t padH = 0,
83  const size_t inputWidth = 0,
84  const size_t inputHeight = 0,
85  const size_t dilationW = 1,
86  const size_t dilationH = 1);
87 
88  /*
89  * Set the weight and bias term.
90  */
91  void Reset();
92 
100  template<typename eT>
101  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
102 
112  template<typename eT>
113  void Backward(const arma::Mat<eT>&& /* input */,
114  arma::Mat<eT>&& gy,
115  arma::Mat<eT>&& g);
116 
117  /*
118  * Calculate the gradient using the output delta and the input activation.
119  *
120  * @param input The input parameter used for calculating the gradient.
121  * @param error The calculated error.
122  * @param gradient The calculated gradient.
123  */
124  template<typename eT>
125  void Gradient(const arma::Mat<eT>&& /* input */,
126  arma::Mat<eT>&& error,
127  arma::Mat<eT>&& gradient);
128 
130  OutputDataType const& Parameters() const { return weights; }
132  OutputDataType& Parameters() { return weights; }
133 
135  OutputDataType const& OutputParameter() const { return outputParameter; }
137  OutputDataType& OutputParameter() { return outputParameter; }
138 
140  OutputDataType const& Delta() const { return delta; }
142  OutputDataType& Delta() { return delta; }
143 
145  OutputDataType const& Gradient() const { return gradient; }
147  OutputDataType& Gradient() { return gradient; }
148 
150  size_t const& InputWidth() const { return inputWidth; }
152  size_t& InputWidth() { return inputWidth; }
153 
155  size_t const& InputHeight() const { return inputHeight; }
157  size_t& InputHeight() { return inputHeight; }
158 
160  size_t const& OutputWidth() const { return outputWidth; }
162  size_t& OutputWidth() { return outputWidth; }
163 
165  size_t const& OutputHeight() const { return outputHeight; }
167  size_t& OutputHeight() { return outputHeight; }
168 
170  arma::mat& Bias() { return bias; }
171 
175  template<typename Archive>
176  void serialize(Archive& ar, const unsigned int /* version */);
177 
178  private:
179  /*
180  * Return the convolution output size.
181  *
182  * @param size The size of the input (row or column).
183  * @param k The size of the filter (width or height).
184  * @param s The stride size (x or y direction).
185  * @param p The size of the padding (width or height).
186  * @param d The dilation size.
187  * @return The convolution output size.
188  */
189  size_t ConvOutSize(const size_t size,
190  const size_t k,
191  const size_t s,
192  const size_t p,
193  const size_t d)
194  {
195  return std::floor(size + p * 2 - d * (k - 1) - 1) / s + 1;
196  }
197 
198  /*
199  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
200  *
201  * @param input The input data to be rotated.
202  * @param output The rotated output.
203  */
204  template<typename eT>
205  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
206  {
207  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
208 
209  // * left-right flip, up-down flip */
210  for (size_t s = 0; s < output.n_slices; s++)
211  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
212  }
213 
214  /*
215  * Rotates a dense matrix counterclockwise by 180 degrees.
216  *
217  * @param input The input data to be rotated.
218  * @param output The rotated output.
219  */
220  template<typename eT>
221  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
222  {
223  // * left-right flip, up-down flip */
224  output = arma::fliplr(arma::flipud(input));
225  }
226 
228  size_t inSize;
229 
231  size_t outSize;
232 
234  size_t batchSize;
235 
237  size_t kW;
238 
240  size_t kH;
241 
243  size_t dW;
244 
246  size_t dH;
247 
249  size_t padW;
250 
252  size_t padH;
253 
255  OutputDataType weights;
256 
258  arma::cube weight;
259 
261  arma::mat bias;
262 
264  size_t inputWidth;
265 
267  size_t inputHeight;
268 
270  size_t outputWidth;
271 
273  size_t outputHeight;
274 
276  size_t dilationW;
277 
279  size_t dilationH;
280 
282  arma::cube outputTemp;
283 
285  arma::cube inputTemp;
286 
288  arma::cube inputPaddedTemp;
289 
291  arma::cube gTemp;
292 
294  arma::cube gradientTemp;
295 
297  Padding<>* padding;
298 
300  OutputDataType delta;
301 
303  OutputDataType gradient;
304 
306  OutputDataType outputParameter;
307 }; // class AtrousConvolution
308 
309 } // namespace ann
310 } // namespace mlpack
311 
312 // Include implementation
313 #include "atrous_convolution_impl.hpp"
314 
315 #endif
OutputDataType const & Parameters() const
Get the parameters.
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...
size_t const & OutputHeight() const
Get the output height.
arma::mat & Bias()
Modify the bias weights of the layer.
.hpp
Definition: add_to_po.hpp:21
Implementation of the Padding module class.
Definition: layer_types.hpp:68
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Delta()
Modify the delta.
size_t & InputHeight()
Modify the input height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t const & InputHeight() const
Get the input height.
OutputDataType & OutputParameter()
Modify the output parameter.
OutputDataType & Parameters()
Modify the parameters.
AtrousConvolution()
Create the AtrousConvolution object.
size_t const & OutputWidth() const
Get the output width.
OutputDataType const & Gradient() const
Get the gradient.
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...
size_t & InputWidth()
Modify input the width.
size_t const & InputWidth() const
Get the input width.
OutputDataType & Gradient()
Modify the gradient.
size_t & OutputWidth()
Modify the output width.
size_t & OutputHeight()
Modify the output height.
OutputDataType const & Delta() const
Get the delta.
Implementation of the Atrous Convolution class.