14 #ifndef MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP 15 #define MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP 44 typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
45 typename BackwardConvolutionRule = NaiveConvolution<ValidConvolution>,
46 typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
47 typename InputType = arma::mat,
48 typename OutputType = arma::mat
83 const size_t kernelWidth,
84 const size_t kernelHeight,
85 const size_t strideWidth = 1,
86 const size_t strideHeight = 1,
87 const size_t padW = 0,
88 const size_t padH = 0,
89 const size_t inputWidth = 0,
90 const size_t inputHeight = 0,
91 const size_t outputWidth = 0,
92 const size_t outputHeight = 0,
93 const std::string& paddingType =
"None");
124 const size_t outSize,
125 const size_t kernelWidth,
126 const size_t kernelHeight,
127 const size_t strideWidth,
128 const size_t strideHeight,
129 const std::tuple<size_t, size_t>& padW,
130 const std::tuple<size_t, size_t>& padH,
131 const size_t inputWidth = 0,
132 const size_t inputHeight = 0,
133 const size_t outputWidth = 0,
134 const size_t outputHeight = 0,
135 const std::string& paddingType =
"None");
140 void SetWeights(
const typename OutputType::elem_type* weightsPtr);
149 void Forward(
const InputType& input, OutputType& output);
161 const OutputType& gy,
172 const OutputType& error,
173 OutputType& gradient);
181 arma::Cube<typename OutputType::elem_type>
const&
Weight()
const 186 arma::Cube<typename OutputType::elem_type>&
Weight() {
return weight; }
189 OutputType
const&
Bias()
const {
return bias; }
191 OutputType&
Bias() {
return bias; }
240 size_t const&
PadHTop()
const {
return padHTop; }
250 size_t const&
PadWLeft()
const {
return padWLeft; }
262 return (outSize * inSize * kernelWidth * kernelHeight) + outSize;
268 result[0] = outputWidth;
269 result[1] = outputHeight;
279 template<
typename Archive>
280 void serialize(Archive& ar,
const uint32_t );
289 template<
typename eT>
290 void Rotate180(
const arma::Cube<eT>& input, arma::Cube<eT>& output)
292 output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
295 for (
size_t s = 0; s < output.n_slices; s++)
296 output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
302 void InitializeSamePadding();
310 template<
typename eT>
311 void Rotate180(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
314 output = arma::fliplr(arma::flipud(input));
327 template<
typename eT>
328 void InsertZeros(
const arma::Mat<eT>& input,
329 const size_t strideWidth,
330 const size_t strideHeight,
331 arma::Mat<eT>& output)
333 if (output.n_rows != input.n_rows * strideWidth - strideWidth + 1 ||
334 output.n_cols != input.n_cols * strideHeight - strideHeight + 1)
336 output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
337 input.n_cols * strideHeight - strideHeight + 1);
340 for (
size_t i = 0; i < output.n_rows; i += strideHeight)
342 for (
size_t j = 0; j < output.n_cols; j += strideWidth)
346 output(i, j) = input(i / strideHeight, j / strideWidth);
360 template<
typename eT>
361 void InsertZeros(
const arma::Cube<eT>& input,
362 const size_t strideWidth,
363 const size_t strideHeight,
364 arma::Cube<eT>& output)
366 output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
367 input.n_cols * strideHeight - strideHeight + 1, input.n_slices);
369 for (
size_t i = 0; i < input.n_slices; ++i)
371 InsertZeros<eT>(input.slice(i), strideWidth, strideHeight,
419 arma::Cube<typename OutputType::elem_type> weight;
437 arma::Cube<typename OutputType::elem_type> outputTemp;
440 arma::Cube<typename InputType::elem_type> inputPaddedTemp;
443 arma::Cube<typename InputType::elem_type> inputExpandedTemp;
446 arma::Cube<typename OutputType::elem_type> gTemp;
449 arma::Cube<typename OutputType::elem_type> gradientTemp;
461 NaiveConvolution<ValidConvolution>,
462 NaiveConvolution<ValidConvolution>,
471 #include "transposed_convolution_impl.hpp" size_t & StrideWidth()
Modify the stride width.
size_t const & InputWidth() const
Get the input width.
size_t const & OutputHeight() const
Get the output height.
size_t & OutputWidth()
Modify the output width.
size_t const & InputSize() const
Get the input size.
std::vector< size_t > inputDimensions
Logical input dimensions of each point.
size_t const & PadWRight() const
Get the right padding width.
size_t const & PadHTop() const
Get the top padding height.
size_t & StrideHeight()
Modify the stride height.
size_t & InputWidth()
Modify input the width.
Linear algebra utility functions, generally performed on matrices or vectors.
size_t & InputHeight()
Modify the input height.
arma::Cube< typename OutputType::elem_type > const & Weight() const
Get the weight of the layer.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t const & InputHeight() const
Get the input height.
void SetWeights(const typename OutputType::elem_type *weightsPtr)
const std::vector< size_t > & OutputDimensions() const
void Gradient(const InputType &, const OutputType &error, OutputType &gradient)
Calculate the gradient using the output delta and the input activation.
void Backward(const InputType &, const OutputType &gy, OutputType &g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
size_t & KernelWidth()
Modify the kernel width.
OutputType & Parameters()
Modify the parameters.
size_t const & PadWLeft() const
Get the left padding width.
size_t const & PadHBottom() const
Get the bottom padding height.
OutputType const & Parameters() const
Get the parameters.
size_t & OutputHeight()
Modify the output height.
size_t & PadHBottom()
Modify the bottom padding height.
size_t const & OutputWidth() const
Get the output width.
TransposedConvolutionType()
Create the Transposed Convolution object.
size_t & PadWLeft()
Modify the left padding width.
Computes the two-dimensional convolution.
size_t & KernelHeight()
Modify the kernel height.
size_t const & OutputSize() const
Get the output size.
Implementation of the Transposed Convolution class.
size_t & PadHTop()
Modify the top padding height.
size_t const & KernelHeight() const
Get the kernel height.
A layer is an abstract class implementing common neural networks operations, such as convolution...
size_t const & StrideWidth() const
Get the stride width.
OutputType & Bias()
Modify the bias of the layer.
OutputType const & Bias() const
Get the bias of the layer.
size_t & PadWRight()
Modify the right padding width.
size_t const & KernelWidth() const
Get the kernel width.
arma::Cube< typename OutputType::elem_type > & Weight()
Modify the weight of the layer.
void Forward(const InputType &input, OutputType &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
TransposedConvolutionType< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > TransposedConvolution
size_t WeightSize() const
Get the size of the weight matrix.
size_t const & StrideHeight() const
Get the stride height.