Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 1 | /* |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 3 | * |
| 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 Spyrou | 33a6990 | 2018-02-23 15:01:52 +0000 | [diff] [blame] | 27 | #include "arm_compute/runtime/CPP/functions/CPPUpsample.h" |
Georgios Pinitas | ced7a8d | 2018-02-01 16:31:33 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h" |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h" |
| 30 | |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/CPP/kernels/CPPFlipWeightsKernel.h" |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/runtime/IFunction.h" |
| 34 | #include "arm_compute/runtime/IMemoryManager.h" |
| 35 | #include "arm_compute/runtime/MemoryGroup.h" |
| 36 | #include "arm_compute/runtime/Tensor.h" |
| 37 | |
| 38 | #include <memory> |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | /** Function to run the deconvolution layer. |
| 43 | * |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 44 | * 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 |
| 45 | * 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 |
| 46 | * specified value where a < stride - 1 that increases the padding top and right of the input image. |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 47 | * |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 48 | * The relation between input to output is as follows: |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 49 | * \f[ |
| 50 | * width\_output = (width\_input - 1) \cdot stride\_x - 2 \cdot padding\_x + kernel\_x |
| 51 | * \f] |
| 52 | * \f[ |
| 53 | * height\_output = (height\_input - 1) \cdot stride\_y - 2 \cdot padding\_y + kernel\_y |
| 54 | * \f] |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 55 | * |
| 56 | * where |
| 57 | * width is the size of the first input dimension. |
| 58 | * height is the size of the second input dimension. |
| 59 | * width_output is the size of the first output dimension. |
| 60 | * height_output is the size of the second output dimension. |
| 61 | * kernel_x and kernel_y are the convolution sizes in x and y. |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 62 | * stride_x and stride_y is the input stride of the first and second dimension. |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 63 | * |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 64 | * The weights used by Deconvolution are supposed to be the same as the ones used for Convolution. Therefore, it will be necessary to use the weights in the |
| 65 | * reverse order to perform an actual convolution. This is achieved by using the @ref CPPFlipWeightsKernel. |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 66 | * |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 67 | * This function calls the following NEON kernels/functions: |
| 68 | * |
| 69 | * -# @ref CPPUpsample |
| 70 | * -# @ref NEConvolutionLayer |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 71 | * |
| 72 | */ |
| 73 | class NEDeconvolutionLayer : public IFunction |
| 74 | { |
| 75 | public: |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 76 | /** Default constructor */ |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 77 | NEDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 78 | |
| 79 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 80 | NEDeconvolutionLayer(const NEDeconvolutionLayer &) = delete; |
| 81 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 82 | NEDeconvolutionLayer &operator=(const NEDeconvolutionLayer &) = delete; |
| 83 | /** Allow instances of this class to be moved */ |
| 84 | NEDeconvolutionLayer(NEDeconvolutionLayer &&) = default; |
| 85 | /** Allow instances of this class to be moved */ |
| 86 | NEDeconvolutionLayer &operator=(NEDeconvolutionLayer &&) = default; |
| 87 | /** Default destructor */ |
| 88 | virtual ~NEDeconvolutionLayer() = default; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 89 | /** Set the input, weights, biases and output tensors. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 90 | * |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 91 | * @note This method will be deprecated in the next release. |
| 92 | * |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 93 | * @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/QASYMM8. |
Michalis Spyrou | a8ca2b0 | 2018-04-04 11:03:51 +0100 | [diff] [blame] | 94 | * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input. |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 95 | * @param[in] bias Optional, ignored if NULL. The biases have one dimension. Data type supported: Data types supported: S32 for QASYMM8 input, F32 for F32 input. |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 96 | * @param[out] output Output tensor. The output has the same number of dimensions as the @p input. |
| 97 | * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo. |
| 98 | * @param[in] inner_border_right The number of zeros added to right edge of the input. |
| 99 | * @param[in] inner_border_top The number of zeros added to top edge of the input. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 100 | * |
| 101 | */ |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 102 | void configure(ITensor *input, const ITensor *weights, const ITensor *bias, ITensor *output, const PadStrideInfo &info, |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 103 | unsigned int inner_border_right, unsigned int inner_border_top); |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 104 | /** Static function to check if given info will lead to a valid configuration of @ref NEDeconvolutionLayer |
| 105 | * |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 106 | * @note This method will be deprecated in the next release. |
| 107 | * |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 108 | * @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/QASYMM8. |
Michalis Spyrou | a8ca2b0 | 2018-04-04 11:03:51 +0100 | [diff] [blame] | 109 | * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input. |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 110 | * @param[in] bias (Optional) The biases have one dimension. Data type supported: Data types supported: S32 for QASYMM8 input, F32 for F32 input. |
Alex Gilday | 27c08ab | 2018-02-22 11:36:16 +0000 | [diff] [blame] | 111 | * @param[in] output Output tensor info. The output has the same number of dimensions as the @p input. |
| 112 | * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo. |
| 113 | * @param[in] inner_border_right The number of zeros added to right edge of the input. |
| 114 | * @param[in] inner_border_top The number of zeros added to top edge of the input. |
| 115 | * |
| 116 | * @return a status |
| 117 | */ |
| 118 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *output, const PadStrideInfo &info, |
| 119 | unsigned int inner_border_right, unsigned int inner_border_top); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 120 | |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 121 | /** Set the input, weights, biases and output tensors. |
| 122 | * |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 123 | * @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/QASYMM8. |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 124 | * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input. |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 125 | * @param[in] bias Optional, ignored if NULL. The biases have one dimension. Data type supported: Data types supported: S32 for QASYMM8 input, F32 for F32 input. |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 126 | * @param[out] output Output tensor. The output has the same number of dimensions as the @p input. |
| 127 | * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo. |
| 128 | * |
| 129 | */ |
| 130 | void configure(ITensor *input, const ITensor *weights, const ITensor *bias, ITensor *output, const PadStrideInfo &info); |
| 131 | /** Static function to check if given info will lead to a valid configuration of @ref NEDeconvolutionLayer |
| 132 | * |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 133 | * @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/QASYMM8. |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 134 | * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM]. Data type supported: Same as @p input. |
Usama Arif | 2899e00 | 2019-04-16 14:32:25 +0100 | [diff] [blame] | 135 | * @param[in] bias (Optional) The biases have one dimension. Data type supported: Data types supported: S32 for QASYMM8 input, F32 for F32 input. |
giuros01 | a69a88b | 2019-01-31 16:29:19 +0000 | [diff] [blame] | 136 | * @param[in] output Output tensor info. The output has the same number of dimensions as the @p input. |
| 137 | * @param[in] info Contains padding and policies to be used in the deconvolution, this is decribed in @ref PadStrideInfo. |
| 138 | * |
| 139 | * @return a status |
| 140 | */ |
| 141 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *output, const PadStrideInfo &info); |
| 142 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 143 | // Inherited methods overridden: |
| 144 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 145 | void prepare() override; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 146 | |
| 147 | private: |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 148 | MemoryGroup _memory_group; |
| 149 | NEConvolutionLayer _conv_f; |
| 150 | CPPUpsample _upsample_f; |
| 151 | CPPFlipWeightsKernel _flip_weights; |
| 152 | Tensor _scaled_output; |
| 153 | Tensor _weights_flipped; |
Michele Di Giorgio | 061dd36 | 2018-10-17 17:10:27 +0100 | [diff] [blame] | 154 | const ITensor *_original_weights; |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 155 | ITensor *_input; |
| 156 | PadStrideInfo _info; |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 157 | std::pair<unsigned int, unsigned int> _inner_border; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 158 | bool _is_prepared; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 159 | }; |
| 160 | } // arm_compute |
| 161 | #endif /* __ARM_COMPUTE_NEDECONVOLUTIONLAYER_H__ */ |