blob: 7b4e77b29632b62d16a6c4b1f762b03246905568 [file] [log] [blame]
Pablo Tellof5f34bb2017-08-22 13:34:13 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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#include "arm_compute/runtime/NEON/functions/NEDeconvolutionLayer.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/PixelValue.h"
28#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
30
31using namespace arm_compute;
32
33NEDeconvolutionLayer::NEDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
34 : _memory_group(std::move(memory_manager)),
35 _scale_f(),
36 _conv_f(),
37 _scaled_output()
38{
39}
40
41void NEDeconvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *bias, ITensor *output, const PadStrideInfo &info,
42 unsigned int ax, unsigned int ay, float upscalex, float upscaley)
43{
44 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
46 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != weights->info()->dimension(1));
47 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) < 1);
48
49 auto out_dims = deconvolution_output_dimensions(input->info()->dimension(0), input->info()->dimension(1), weights->info()->dimension(0), weights->info()->dimension(1),
50 info.pad().first, info.pad().second, ax, ay, upscalex, upscaley, info.round());
51
52 const TensorShape output_shape = deconvolution_output_shape(out_dims, input->info()->tensor_shape(), weights->info()->tensor_shape());
53
54 // Output auto initialization if not yet initialized
55 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
56
57 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights, bias);
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output, weights, bias);
59
60 ARM_COMPUTE_ERROR_ON_MSG(output->info()->dimension(Window::DimX) != output_shape.x(), "Output's width is invalid.");
61 ARM_COMPUTE_ERROR_ON_MSG(output->info()->dimension(Window::DimY) != output_shape.y(), "Output's height is invalid.");
62 ARM_COMPUTE_ERROR_ON_MSG(output->info()->dimension(Window::DimZ) != output_shape.z(), "Output's depth is invalid.");
63
64 _memory_group.manage(&_scaled_output);
65
66 // configure scale function
67 //Init and allocate intermmidiate tensor for output, same size as input but the first two axis are the same as the output tensor
68 TensorShape scale_out_shape(input->info()->tensor_shape());
69 scale_out_shape.set(0, output->info()->dimension(0));
70 scale_out_shape.set(1, output->info()->dimension(1));
71 TensorInfo scale_out_info(scale_out_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
72 _scaled_output.allocator()->init(scale_out_info);
73 const unsigned int kernel_size = weights->info()->dimension(0);
74 // Padding for the upsampled image is calculated with the equiation: p' = k - p - 1, where k is kernel size and p is the input padding
75 ARM_COMPUTE_ERROR_ON(info.pad().first > (kernel_size - 1));
76 const unsigned int tr_px = kernel_size - info.pad().first - 1;
77 const unsigned int tr_py = kernel_size - info.pad().second - 1;
78 const unsigned int tr_stride = 1;
79 const PadStrideInfo transposed_info(tr_stride, tr_stride, tr_px, tr_py);
80 _scale_f.configure(input, &_scaled_output, std::make_pair(ax, ay), std::make_pair(info.stride().first - 1u, info.stride().second - 1u), transposed_info);
81 // setup the function to convolve the upscaled output
82 switch(kernel_size)
83 {
84 case 1:
85 {
86 _conv_f.configure(&_scaled_output, weights, bias, output, PadStrideInfo(1, 1, 0, 0, DimensionRoundingType::CEIL));
87 break;
88 }
89 case 3:
90 {
91 _conv_f.configure(&_scaled_output, weights, bias, output, PadStrideInfo(1, 1, 1, 1, DimensionRoundingType::CEIL));
92 break;
93 }
94 case 5:
95 {
96 _conv_f.configure(&_scaled_output, weights, bias, output, PadStrideInfo(1, 1, 2, 2, DimensionRoundingType::CEIL));
97 break;
98 }
99 default:
100 {
101 ARM_COMPUTE_ERROR("Not supported");
102 break;
103 }
104 }
105 _scaled_output.allocator()->allocate();
106}
107
108void NEDeconvolutionLayer::run()
109{
110 _memory_group.acquire();
111 _scale_f.run();
112 _conv_f.run();
113 _memory_group.release();
114}