Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace validation |
| 34 | { |
| 35 | namespace reference |
| 36 | { |
| 37 | template <typename T> |
| 38 | SimpleTensor<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); |
Pablo Tello | daaa1fa | 2017-10-25 11:40:50 +0100 | [diff] [blame^] | 52 | const float width_ratio = static_cast<float>(width_in) / static_cast<float>(width_scaled); |
| 53 | const float height_ratio = static_cast<float>(height_in) / static_cast<float>(height_scaled); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 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; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 62 | for(int j = 0; j < scaled.num_elements(); ++j) |
| 63 | { |
| 64 | scaled[j] = T(0); |
| 65 | } |
| 66 | |
| 67 | for(int slice = 0; slice < num_2d_slices; ++slice) |
| 68 | { |
| 69 | const int offset_slice_in = slice * width_in * height_in; |
| 70 | const int offset_slice_out = slice * width_scaled * height_scaled; |
| 71 | for(int yi = ay; yi < height_scaled; yi += stridey) |
| 72 | { |
| 73 | for(int xi = transposed_convolution_padx; xi < width_scaled; xi += stridex) |
| 74 | { |
| 75 | const float x_src = (xi + 0.5f) * width_ratio - 0.5f; |
| 76 | const float y_src = (yi + 0.5f) * height_ratio - 0.5f; |
| 77 | T *out = scaled.data() + offset_slice_out + xi + yi * width_scaled; |
| 78 | const bool in_bounds = x_src > -1 && y_src > -1 && x_src < width_in && y_src < height_in; |
| 79 | 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 |
| 80 | || yi < ay || yi >= (height_scaled - transposed_convolution_pady); // like above but top and bottom padding in the upscaled XY plane |
| 81 | if(!in_axy) |
| 82 | { |
| 83 | if(in_bounds) |
| 84 | { |
Pablo Tello | daaa1fa | 2017-10-25 11:40:50 +0100 | [diff] [blame^] | 85 | const int in_scaled_x = (x_src < 0.f) ? static_cast<int>(x_src - 0.5f) : static_cast<int>(x_src + 0.5f); |
| 86 | const int in_scaled_y = (y_src < 0.f) ? static_cast<int>(y_src - 0.5f) : static_cast<int>(y_src + 0.5f); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 87 | const T *in = src.data() + offset_slice_in + in_scaled_x + in_scaled_y * width_in; |
| 88 | *out = *in; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | *out = T(0); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | const PadStrideInfo conv_info(1, 1, 1, 1, DimensionRoundingType::CEIL); |
| 99 | return convolution_layer(scaled, weights, bias, output_shape, conv_info); |
| 100 | } |
| 101 | |
| 102 | template SimpleTensor<float> deconvolution_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape, |
| 103 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &a); |
| 104 | } // namespace reference |
| 105 | } // namespace validation |
| 106 | } // namespace test |
| 107 | } // namespace arm_compute |