blob: 3e527168c1dcf32ed47e6472f40248c18b37e821 [file] [log] [blame]
Pablo Tellof5f34bb2017-08-22 13:34:13 +01001/*
Georgios Pinitasced7a8d2018-02-01 16:31:33 +00002 * Copyright (c) 2017-2018 ARM Limited.
Pablo Tellof5f34bb2017-08-22 13:34:13 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_NEDECONVOLUTIONLAYER_H__
25#define __ARM_COMPUTE_NEDECONVOLUTIONLAYER_H__
26
Michalis Spyrou33a69902018-02-23 15:01:52 +000027#include "arm_compute/runtime/CPP/functions/CPPUpsample.h"
Georgios Pinitasced7a8d2018-02-01 16:31:33 +000028#include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h"
Pablo Tellof5f34bb2017-08-22 13:34:13 +010029#include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
30
31#include "arm_compute/core/Types.h"
32#include "arm_compute/runtime/IFunction.h"
33#include "arm_compute/runtime/IMemoryManager.h"
34#include "arm_compute/runtime/MemoryGroup.h"
35#include "arm_compute/runtime/Tensor.h"
36
37#include <memory>
38
39namespace arm_compute
40{
41/** Function to run the deconvolution layer.
42 *
Michalis Spyrou780db4e2017-11-23 09:49:51 +000043 * Deconvolution Layer is the backward pass of Convolution Layer. First we transform the input depending on the stride and pad info and then perfrom a 1x1
44 * convolution pass. Input stride defines how many zeroes we should put between each element of the input, pad is the amount of padding and finaly a is a user
45 * specified value where a < stride - 1 that increases the padding top and right of the input image.
Pablo Tellof5f34bb2017-08-22 13:34:13 +010046 *
Michalis Spyrou780db4e2017-11-23 09:49:51 +000047 * The relation between input to output is as follows:
48 * width_output = round((width_input − 1) ∗ (stride_x - 1) − 2 ∗ padding_x + kernel_x + inner_border_right )
49 * height_output = round((height_input − 1) ∗ (stride_y - 1) − 2 ∗ padding_y + kernel_y + inner_border_top )
Pablo Tellof5f34bb2017-08-22 13:34:13 +010050 *
51 * where
52 * width is the size of the first input dimension.
53 * height is the size of the second input dimension.
54 * width_output is the size of the first output dimension.
55 * height_output is the size of the second output dimension.
56 * kernel_x and kernel_y are the convolution sizes in x and y.
Michalis Spyrou780db4e2017-11-23 09:49:51 +000057 * inner_border_right and inner_border_top the number of zeros added to the top and right edges of the input.
58 * stride_x and stride_y is the input stride of the first and second dimension.
Pablo Tellof5f34bb2017-08-22 13:34:13 +010059 *
60 * This function calls the following NEON kernels:
61 *
Pablo Tellof5f34bb2017-08-22 13:34:13 +010062 * -# @ref NEDirectConvolutionLayer
63 *
64 */
65class NEDeconvolutionLayer : public IFunction
66{
67public:
Michalis Spyrou780db4e2017-11-23 09:49:51 +000068 /** Default constructor */
Pablo Tellof5f34bb2017-08-22 13:34:13 +010069 NEDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000070
71 /** Prevent instances of this class from being copied (As this class contains pointers) */
72 NEDeconvolutionLayer(const NEDeconvolutionLayer &) = delete;
73 /** Prevent instances of this class from being copied (As this class contains pointers) */
74 NEDeconvolutionLayer &operator=(const NEDeconvolutionLayer &) = delete;
75 /** Allow instances of this class to be moved */
76 NEDeconvolutionLayer(NEDeconvolutionLayer &&) = default;
77 /** Allow instances of this class to be moved */
78 NEDeconvolutionLayer &operator=(NEDeconvolutionLayer &&) = default;
79 /** Default destructor */
80 virtual ~NEDeconvolutionLayer() = default;
Pablo Tellof5f34bb2017-08-22 13:34:13 +010081 /** Set the input, weights, biases and output tensors.
Anthony Barbierf202e502017-11-23 18:02:04 +000082 *
Michalis Spyrou780db4e2017-11-23 09:49:51 +000083 * @param[in,out] input Input tensor. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs. Data types supported: F32.
Michalis Spyroua8ca2b02018-04-04 11:03:51 +010084 * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input.
Michalis Spyrou780db4e2017-11-23 09:49:51 +000085 * @param[in] bias Optional, ignored if NULL. The biases have one dimension. Data type supported: Same as @p input.
86 * @param[out] output Output tensor. The output has the same number of dimensions as the @p input.
87 * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo.
88 * @param[in] inner_border_right The number of zeros added to right edge of the input.
89 * @param[in] inner_border_top The number of zeros added to top edge of the input.
Anthony Barbierf202e502017-11-23 18:02:04 +000090 *
91 */
Pablo Tellof5f34bb2017-08-22 13:34:13 +010092 void configure(ITensor *input, const ITensor *weights, const ITensor *bias, ITensor *output, const PadStrideInfo &info,
Michalis Spyrou780db4e2017-11-23 09:49:51 +000093 unsigned int inner_border_right, unsigned int inner_border_top);
Alex Gilday27c08ab2018-02-22 11:36:16 +000094 /** Static function to check if given info will lead to a valid configuration of @ref NEDeconvolutionLayer
95 *
96 * @param[in] input Input tensor info. 3 lower dimensions represent a single input, and an optional 4th dimension for batch of inputs. Data types supported: F32.
Michalis Spyroua8ca2b02018-04-04 11:03:51 +010097 * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input.
Alex Gilday27c08ab2018-02-22 11:36:16 +000098 * @param[in] bias (Optional) The biases have one dimension. Data type supported: Same as @p input.
99 * @param[in] output Output tensor info. The output has the same number of dimensions as the @p input.
100 * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo.
101 * @param[in] inner_border_right The number of zeros added to right edge of the input.
102 * @param[in] inner_border_top The number of zeros added to top edge of the input.
103 *
104 * @return a status
105 */
106 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *output, const PadStrideInfo &info,
107 unsigned int inner_border_right, unsigned int inner_border_top);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100108
109 // Inherited methods overridden:
110 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100111 void prepare() override;
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100112
113private:
Georgios Pinitas056b5d92018-02-13 18:50:55 +0000114 MemoryGroup _memory_group;
115 NEConvolutionLayer _conv_f;
Michalis Spyrou33a69902018-02-23 15:01:52 +0000116 CPPUpsample _upsample_f;
Georgios Pinitas056b5d92018-02-13 18:50:55 +0000117 Tensor _scaled_output;
118 ITensor *_input;
119 PadStrideInfo _info;
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000120 std::pair<unsigned int, unsigned int> _inner_border;
Georgios Pinitas72219332018-06-05 14:56:06 +0100121 bool _is_prepared;
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100122};
123} // arm_compute
124#endif /* __ARM_COMPUTE_NEDECONVOLUTIONLAYER_H__ */