Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | ced7a8d | 2018-02-01 16:31:33 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | #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 |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 42 | const int stride_x = info.stride().first; |
| 43 | const int stride_y = info.stride().second; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 44 | TensorShape scaled_shape = src.shape(); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 45 | int out_x = src.shape().x() + (src.shape().x() - 1) * (stride_x - 1) + a.first + 2 * info.pad().first; |
| 46 | int out_y = src.shape().y() + (src.shape().y() - 1) * (stride_y - 1) + a.second + 2 * info.pad().second; |
| 47 | scaled_shape.set(0, out_x); |
| 48 | scaled_shape.set(1, out_y); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 49 | SimpleTensor<T> scaled{ scaled_shape, src.data_type(), 1, src.fixed_point_position() }; |
| 50 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 51 | const int width_in = src.shape().x(); |
| 52 | const int height_in = src.shape().y(); |
| 53 | const int width_scaled = scaled.shape().x(); |
| 54 | const int height_scaled = scaled.shape().y(); |
| 55 | const int num_2d_slices = src.shape().total_size() / (width_in * height_in); |
| 56 | const int ax = a.first; // The number of zeros added to right edge of the input. |
| 57 | const int ay = a.second; // The number of zeros added to top edge of the input. |
| 58 | ARM_COMPUTE_ERROR_ON(info.pad().first > (weights.shape().x() - 1)); |
| 59 | |
| 60 | ARM_COMPUTE_ERROR_ON_MSG(ax > stride_x - 1, "ax must be smaller than stride_x"); |
| 61 | ARM_COMPUTE_ERROR_ON_MSG(ay > stride_y - 1, "ay must be smaller than stride_y"); |
| 62 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 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; |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 72 | const int start_x = info.pad().first; |
| 73 | const int start_y = ay + info.pad().second; |
| 74 | const int end_y = height_scaled - info.pad().second; |
| 75 | const int end_x = width_scaled - ax - info.pad().first; |
| 76 | |
| 77 | for(int yi = start_y, in_y = 0; yi < end_y; yi += stride_y, in_y++) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 78 | { |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 79 | for(int xi = start_x, in_x = 0; xi < end_x; xi += stride_x, in_x++) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 80 | { |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 81 | const T *in = src.data() + offset_slice_in + in_y * width_in + in_x; |
| 82 | T *out = scaled.data() + offset_slice_out + xi + yi * width_scaled; |
| 83 | *out = *in; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 87 | |
| 88 | const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 89 | return convolution_layer(scaled, weights, bias, output_shape, conv_info); |
| 90 | } |
| 91 | |
| 92 | template SimpleTensor<float> deconvolution_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape, |
| 93 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &a); |
| 94 | } // namespace reference |
| 95 | } // namespace validation |
| 96 | } // namespace test |
| 97 | } // namespace arm_compute |