Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | d25af67 | 2019-07-10 17:06:12 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +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 "UpsampleLayer.h" |
| 25 | |
| 26 | #include "tests/validation/Helpers.h" |
| 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
Manuel Bottini | d25af67 | 2019-07-10 17:06:12 +0100 | [diff] [blame] | 36 | namespace |
| 37 | { |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 38 | template <typename T> |
Manuel Bottini | d25af67 | 2019-07-10 17:06:12 +0100 | [diff] [blame] | 39 | SimpleTensor<T> upsample_function(const SimpleTensor<T> &src, const Size2D &info, const InterpolationPolicy policy) |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 40 | { |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 41 | ARM_COMPUTE_ERROR_ON(policy != InterpolationPolicy::NEAREST_NEIGHBOR); |
| 42 | ARM_COMPUTE_UNUSED(policy); |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 43 | |
| 44 | TensorShape output_shape = src.shape(); |
| 45 | output_shape.set(0, src.shape().x() * info.x()); |
| 46 | output_shape.set(1, src.shape().y() * info.y()); |
| 47 | |
| 48 | // Create reference |
| 49 | const int stride_x = info.x(); |
| 50 | const int stride_y = info.y(); |
| 51 | int width_out = output_shape.x(); |
| 52 | int height_out = output_shape.y(); |
| 53 | SimpleTensor<T> out{ output_shape, src.data_type(), 1, src.quantization_info() }; |
| 54 | |
| 55 | const int width_in = src.shape().x(); |
| 56 | const int height_in = src.shape().y(); |
| 57 | const int num_2d_slices = src.shape().total_size() / (width_in * height_in); |
| 58 | |
| 59 | for(int slice = 0; slice < num_2d_slices; ++slice) |
| 60 | { |
| 61 | const int offset_slice_in = slice * width_in * height_in; |
| 62 | const int offset_slice_out = slice * height_out * width_out; |
| 63 | for(int y = 0; y < height_out; ++y) |
| 64 | { |
| 65 | for(int x = 0; x < width_out; ++x) |
| 66 | { |
| 67 | const int out_offset = y * width_out + x; |
| 68 | const int in_offset = (y / stride_y) * width_in + x / stride_x; |
| 69 | |
| 70 | T *_out = out.data() + offset_slice_out + out_offset; |
| 71 | const T *in = src.data() + offset_slice_in + in_offset; |
| 72 | *_out = *in; |
| 73 | } |
| 74 | } |
| 75 | } |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 76 | return out; |
| 77 | } |
| 78 | |
Manuel Bottini | d25af67 | 2019-07-10 17:06:12 +0100 | [diff] [blame] | 79 | } // namespace |
| 80 | |
| 81 | template <typename T> |
| 82 | SimpleTensor<T> upsample_layer(const SimpleTensor<T> &src, const Size2D &info, const InterpolationPolicy policy) |
| 83 | { |
| 84 | return upsample_function<T>(src, info, policy); |
| 85 | } |
| 86 | |
| 87 | template <> |
| 88 | SimpleTensor<uint8_t> upsample_layer(const SimpleTensor<uint8_t> &src, const Size2D &info, const InterpolationPolicy policy) |
| 89 | { |
| 90 | SimpleTensor<uint8_t> dst(src.shape(), src.data_type(), 1, src.quantization_info()); |
| 91 | |
| 92 | if(is_data_type_quantized_asymmetric(src.data_type())) |
| 93 | { |
| 94 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 95 | SimpleTensor<float> dst_tmp = upsample_function<float>(src_tmp, info, policy); |
| 96 | dst = convert_to_asymmetric(dst_tmp, src.quantization_info()); |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | dst = upsample_function<uint8_t>(src, info, policy); |
| 101 | } |
| 102 | return dst; |
| 103 | } |
| 104 | |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 105 | template SimpleTensor<float> upsample_layer(const SimpleTensor<float> &src, |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 106 | const Size2D &info, const InterpolationPolicy policy); |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 107 | template SimpleTensor<half> upsample_layer(const SimpleTensor<half> &src, |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 108 | const Size2D &info, const InterpolationPolicy policy); |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 109 | } // namespace reference |
| 110 | } // namespace validation |
| 111 | } // namespace test |
| 112 | } // namespace arm_compute |