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 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 26 | #include "tests/validation/Helpers.h" |
| 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 36 | template <typename T, typename TB> |
| 37 | SimpleTensor<T> deconvolution_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const TensorShape &output_shape, |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 38 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &a) |
| 39 | { |
| 40 | // Create reference |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 41 | const int stride_x = info.stride().first; |
| 42 | const int stride_y = info.stride().second; |
| 43 | const int weights_width = weights.shape().x(); |
| 44 | const int weights_height = weights.shape().y(); |
| 45 | const int weights_upper_dims = weights.shape().total_size() / (weights_width * weights_height); |
| 46 | |
| 47 | // Find the upsampled dimensions |
| 48 | unsigned int out_x = (src.shape().x() - 1) * stride_x + a.first + 1; |
| 49 | unsigned int out_y = (src.shape().y() - 1) * stride_y + a.second + 1; |
| 50 | |
| 51 | // Find the padding needed for the convolution with stride 1 in order to match output shape |
| 52 | unsigned int padx = output_shape.x() - (out_x - weights_width + 1); |
| 53 | unsigned int pady = output_shape.y() - (out_y - weights_height + 1); |
| 54 | out_x += padx; |
| 55 | out_y += pady; |
| 56 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 57 | TensorShape scaled_shape = src.shape(); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 58 | scaled_shape.set(0, out_x); |
| 59 | scaled_shape.set(1, out_y); |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 60 | SimpleTensor<T> scaled{ scaled_shape, src.data_type(), 1, src.quantization_info() }; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 61 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 62 | const int width_in = src.shape().x(); |
| 63 | const int height_in = src.shape().y(); |
| 64 | const int width_scaled = scaled.shape().x(); |
| 65 | const int height_scaled = scaled.shape().y(); |
| 66 | const int num_2d_slices = src.shape().total_size() / (width_in * height_in); |
| 67 | const int ax = a.first; // The number of zeros added to right edge of the input. |
| 68 | const int ay = a.second; // The number of zeros added to top edge of the input. |
| 69 | ARM_COMPUTE_ERROR_ON(info.pad().first > (weights.shape().x() - 1)); |
| 70 | |
| 71 | ARM_COMPUTE_ERROR_ON_MSG(ax > stride_x - 1, "ax must be smaller than stride_x"); |
| 72 | ARM_COMPUTE_ERROR_ON_MSG(ay > stride_y - 1, "ay must be smaller than stride_y"); |
| 73 | |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 74 | if(src.data_type() == DataType::QASYMM8) |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 75 | { |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 76 | const uint8_t quantized_zero = src.quantization_info().offset; |
| 77 | std::fill_n(scaled.data(), scaled.num_elements(), quantized_zero); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | std::fill_n(scaled.data(), scaled.num_elements(), T(0)); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 84 | // Flip weights by 180 degrees |
| 85 | SimpleTensor<T> weights_flipped{ weights.shape(), weights.data_type(), 1, weights.quantization_info() }; |
| 86 | for(int ud = 0; ud < weights_upper_dims; ++ud) |
| 87 | { |
| 88 | const int offset = ud * weights_width * weights_height; |
| 89 | for(int y = 0; y < weights_height; ++y) |
| 90 | { |
| 91 | for(int x = 0; x < weights_width; ++x) |
| 92 | { |
| 93 | weights_flipped[offset + (weights_height - 1 - y) * weights_width + (weights_width - 1 - x)] = weights[offset + y * weights_width + x]; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 98 | for(int slice = 0; slice < num_2d_slices; ++slice) |
| 99 | { |
| 100 | const int offset_slice_in = slice * width_in * height_in; |
| 101 | const int offset_slice_out = slice * width_scaled * height_scaled; |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 102 | const int start_x = padx / 2; |
| 103 | const int start_y = ay + pady / 2; |
| 104 | const int end_y = height_scaled - pady / 2; |
| 105 | const int end_x = width_scaled - ax - padx / 2; |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 106 | |
| 107 | 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] | 108 | { |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 109 | 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] | 110 | { |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 111 | const T *in = src.data() + offset_slice_in + in_y * width_in + in_x; |
| 112 | T *out = scaled.data() + offset_slice_out + xi + yi * width_scaled; |
| 113 | *out = *in; |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 117 | |
| 118 | const PadStrideInfo conv_info(1, 1, 0, 0, 0, 0, DimensionRoundingType::CEIL); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 119 | return convolution_layer(scaled, weights_flipped, bias, output_shape, conv_info); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 122 | template SimpleTensor<uint8_t> deconvolution_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &output_shape, |
| 123 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &a); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 124 | template SimpleTensor<float> deconvolution_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape, |
| 125 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &a); |
Georgios Pinitas | 793f87d | 2018-05-18 20:08:58 +0100 | [diff] [blame] | 126 | template SimpleTensor<half> deconvolution_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &output_shape, |
| 127 | const PadStrideInfo &info, const std::pair<unsigned int, unsigned int> &a); |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 128 | } // namespace reference |
| 129 | } // namespace validation |
| 130 | } // namespace test |
| 131 | } // namespace arm_compute |