2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Copyright (c) 2017-2020 ARM Limited.
20 * SPDX-License-Identifier: MIT
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to
24 * deal in the Software without restriction, including without limitation the
25 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26 * sell copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 #ifndef __ARM_COMPUTE_NETRANSPOSECONVLAYER_H__
41 #define __ARM_COMPUTE_NETRANSPOSECONVLAYER_H__
43 #include "arm_compute/runtime/CPP/functions/CPPUpsample.h"
44 #include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h"
45 #include "arm_compute/runtime/NEON/functions/NEDirectConvolutionLayer.h"
46 #include "arm_compute/runtime/NEON/functions/NEReverse.h"
48 #include "arm_compute/core/Types.h"
49 #include "arm_compute/runtime/IFunction.h"
50 #include "arm_compute/runtime/IMemoryManager.h"
51 #include "arm_compute/runtime/MemoryGroup.h"
52 #include "arm_compute/runtime/Tensor.h"
58 /** Function to run the deconvolution layer.
60 * Deconvolution Layer is the backward pass of Convolution Layer. First we transform the input
61 * depending on the stride and pad info and then perfrom a 1x1
62 * convolution pass. Input stride defines how many zeroes we should put between each element of the
63 * input, pad is the amount of padding and finaly a is a user
64 * specified value where a < stride - 1 that increases the padding top and right of the input image.
66 * The relation between input to output is as follows:
68 * width\_output = (width\_input - 1) \cdot stride\_x - 2 \cdot padding\_x + kernel\_x
71 * height\_output = (height\_input - 1) \cdot stride\_y - 2 \cdot padding\_y + kernel\_y
75 * width is the size of the first input dimension.
76 * height is the size of the second input dimension.
77 * width_output is the size of the first output dimension.
78 * height_output is the size of the second output dimension.
79 * kernel_x and kernel_y are the convolution sizes in x and y.
80 * stride_x and stride_y is the input stride of the first and second dimension.
82 * The weights used by Deconvolution are supposed to be the same as the ones used for Convolution.
83 * Therefore, it will be necessary to use the weights in the
84 * reverse order to perform an actual convolution. This is achieved by using @ref NEReverse.
86 * This function calls the following NEON kernels/functions:
88 * -# @ref CPPUpsampleEx
89 * -# @ref NEConvolutionLayer
94 class NETransposeConvLayer : public IFunction
98 NETransposeConvLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
100 /** Prevent instances of this class from being copied (As this class contains pointers) */
101 NETransposeConvLayer(const NETransposeConvLayer &) = delete;
102 /** Prevent instances of this class from being copied (As this class contains pointers) */
103 NETransposeConvLayer &operator=(const NETransposeConvLayer &) = delete;
104 /** Allow instances of this class to be moved */
105 NETransposeConvLayer(NETransposeConvLayer &&) = default;
106 /** Allow instances of this class to be moved */
107 NETransposeConvLayer &operator=(NETransposeConvLayer &&) = default;
108 /** Default destructor */
109 virtual ~NETransposeConvLayer() = default;
111 /** Set the input, weights, biases and output tensors.
113 * @param[in,out] input Input tensor. 3 lower dimensions represent a single input,
114 * and an optional 4th dimension for batch of inputs.
115 * Data types supported: F32/F16/QASYMM8/QASYMM8_SIGNED.
116 * @param[in] weights The 4d weights with dimensions [width, height, IFM, OFM].
117 * Data type supported: Same as @p input.
118 * @param[in] bias Optional, ignored if NULL. The biases have one dimension.
119 * Data type supported: Data types supported: S32 for QASYMM8 and
120 * QASYMM8_SIGNED input, F32 for F32 input, F16 for F16 input.
121 * @param[out] output Output tensor. The output has the same number of dimensions as
123 * @param[in] info Contains padding and policies to be used in the deconvolution,
124 * this is decribed in @ref PadStrideInfo.
125 * @param[in] invalid_right The number of zeros added to right edge of the output.
126 * @param[in] invalid_bottom The number of zeros added to bottom edge of the output.
129 void configure(ITensor *input, const ITensor *weights, const ITensor *bias, ITensor *output,
130 const PadStrideInfo &info, unsigned int invalid_right,
131 unsigned int invalid_bottom);
132 /** Static function to check if given info will lead to a valid configuration of @ref
133 * NETransposeConvLayer
135 * @param[in] input Input tensor info. 3 lower dimensions represent a single input,
136 * and an optional 4th dimension for batch of inputs.
137 * Data types supported: F32/F16/QASYMM8/QASYMM8_SIGNED.
138 * @param[in] weights The 4d weights info with dimensions [width, height, IFM, OFM].
139 * Data type supported: Same as @p input.
140 * @param[in] bias (Optional) The biases have one dimension.
141 * Data types supported: S32 for QASYMM8 and QASYMM8_SIGNED input,
142 * F32 for F32 input, F16 for F16 input.
143 * @param[in] output Output tensor info. The output has the same number of dimensions as
145 * @param[in] info Contains padding and policies to be used in the deconvolution,
146 * this is decribed in @ref PadStrideInfo.
147 * @param[in] innvalid_right The number of zeros added to right edge of the output.
148 * @param[in] invalid_bottom The number of zeros added to bottom edge of the output.
152 static Status validate(const ITensorInfo *input, const ITensorInfo *weights,
153 const ITensorInfo *bias, const ITensorInfo *output,
154 const PadStrideInfo &info, unsigned int invalid_right,
155 unsigned int invalid_bottom);
157 // Inherited methods overridden:
159 void prepare() override;
162 MemoryGroup _memory_group;
163 NEConvolutionLayer _conv_f;
164 CPPUpsample _upsample_f;
165 NEReverse _flip_weights;
166 Tensor _scaled_output;
167 Tensor _weights_flipped;
169 const ITensor *_original_weights;
175 #endif /* __ARM_COMPUTE_NETRANSPOSECONVLAYER_H__ */