blob: 34f3d10edb4ed385075e36c2559a57ceae0aa9dc [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 "ConvolutionLayer.h"
25
26#include "tests/validation/FixedPoint.h"
27#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37template <typename T>
38SimpleTensor<T> deconvolution_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<T> &bias, const TensorShape &output_shape,
39 const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &a)
40{
41 // Create reference
42 TensorShape scaled_shape = src.shape();
43 scaled_shape.set(0, output_shape.x());
44 scaled_shape.set(1, output_shape.y());
45 SimpleTensor<T> scaled{ scaled_shape, src.data_type(), 1, src.fixed_point_position() };
46
47 const int width_in = src.shape().x();
48 const int height_in = src.shape().y();
49 const int width_scaled = scaled.shape().x();
50 const int height_scaled = scaled.shape().y();
51 const int num_2d_slices = src.shape().total_size() / (width_in * height_in);
52 const auto width_ratio = static_cast<float>(width_in) / static_cast<float>(width_scaled);
53 const auto height_ratio = static_cast<float>(height_in) / static_cast<float>(height_scaled);
54 const int ax = a.first; // The number of zeros added to right edge of the input.
55 const int ay = a.second; // The number of zeros added to bottom edge of the input.
56 const unsigned int kernel_size = weights.shape().x();
57 ARM_COMPUTE_ERROR_ON(info.pad().first > (kernel_size - 1));
58 const int transposed_convolution_padx = kernel_size - info.pad().first - 1;
59 const int transposed_convolution_pady = kernel_size - info.pad().second - 1;
60 const int stridex = info.stride().first;
61 const int stridey = info.stride().second;
62
63 for(int j = 0; j < scaled.num_elements(); ++j)
64 {
65 scaled[j] = T(0);
66 }
67
68 for(int slice = 0; slice < num_2d_slices; ++slice)
69 {
70 const int offset_slice_in = slice * width_in * height_in;
71 const int offset_slice_out = slice * width_scaled * height_scaled;
72 for(int yi = ay; yi < height_scaled; yi += stridey)
73 {
74 for(int xi = transposed_convolution_padx; xi < width_scaled; xi += stridex)
75 {
76 const float x_src = (xi + 0.5f) * width_ratio - 0.5f;
77 const float y_src = (yi + 0.5f) * height_ratio - 0.5f;
78 T *out = scaled.data() + offset_slice_out + xi + yi * width_scaled;
79 const bool in_bounds = x_src > -1 && y_src > -1 && x_src < width_in && y_src < height_in;
80 const bool in_axy = xi < transposed_convolution_padx || xi >= (width_scaled - ax) // this is checking if the x coordinate is in the padded left/right area
81 || yi < ay || yi >= (height_scaled - transposed_convolution_pady); // like above but top and bottom padding in the upscaled XY plane
82 if(!in_axy)
83 {
84 if(in_bounds)
85 {
86 const int in_scaled_x = support::cpp11::round(x_src);
87 const int in_scaled_y = support::cpp11::round(y_src);
88 const T *in = src.data() + offset_slice_in + in_scaled_x + in_scaled_y * width_in;
89 *out = *in;
90 }
91 else
92 {
93 *out = T(0);
94 }
95 }
96 }
97 }
98 }
99 const PadStrideInfo conv_info(1, 1, 1, 1, DimensionRoundingType::CEIL);
100 return convolution_layer(scaled, weights, bias, output_shape, conv_info);
101}
102
103template SimpleTensor<float> deconvolution_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape,
104 const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &a);
105} // namespace reference
106} // namespace validation
107} // namespace test
108} // namespace arm_compute