Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +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 | */ |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 24 | #include "Utils.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 25 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 26 | #include "tests/validation/Helpers.h" |
| 27 | #include "tests/validation/half.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace validation |
| 34 | { |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 35 | // Return a tensor element at a specified coordinate with different border modes |
| 36 | template <typename T> |
| 37 | T tensor_elem_at(const SimpleTensor<T> &in, Coordinates coord, BorderMode border_mode, T constant_border_value) |
| 38 | { |
| 39 | const int x = coord.x(); |
| 40 | const int y = coord.y(); |
| 41 | const auto width = static_cast<int>(in.shape().x()); |
| 42 | const auto height = static_cast<int>(in.shape().y()); |
| 43 | |
| 44 | // If coordinates beyond range of tensor's width or height |
| 45 | if(x < 0 || y < 0 || x >= width || y >= height) |
| 46 | { |
| 47 | if(border_mode == BorderMode::REPLICATE) |
| 48 | { |
| 49 | coord.set(0, std::max(0, std::min(x, width - 1))); |
| 50 | coord.set(1, std::max(0, std::min(y, height - 1))); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 51 | } |
| 52 | else |
| 53 | { |
| 54 | return constant_border_value; |
| 55 | } |
| 56 | } |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 57 | return in[coord2index(in.shape(), coord)]; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 58 | } |
| 59 | template float tensor_elem_at(const SimpleTensor<float> &in, Coordinates coord, BorderMode border_mode, float constant_border_value); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 60 | template uint8_t tensor_elem_at(const SimpleTensor<uint8_t> &in, Coordinates coord, BorderMode border_mode, uint8_t constant_border_value); |
| 61 | |
| 62 | // Return the bilinear value at a specified coordinate with different border modes |
| 63 | template <typename T> |
| 64 | T bilinear_policy(const SimpleTensor<T> &in, Coordinates id, float xn, float yn, BorderMode border_mode, uint8_t constant_border_value) |
| 65 | { |
| 66 | int idx = std::floor(xn); |
| 67 | int idy = std::floor(yn); |
| 68 | |
| 69 | const float dx = xn - idx; |
| 70 | const float dy = yn - idy; |
| 71 | const float dx_1 = 1.0f - dx; |
| 72 | const float dy_1 = 1.0f - dy; |
| 73 | |
| 74 | id.set(0, idx); |
| 75 | id.set(1, idy); |
| 76 | const T tl = tensor_elem_at(in, id, border_mode, constant_border_value); |
| 77 | id.set(0, idx + 1); |
| 78 | id.set(1, idy); |
| 79 | const T tr = tensor_elem_at(in, id, border_mode, constant_border_value); |
| 80 | id.set(0, idx); |
| 81 | id.set(1, idy + 1); |
| 82 | const T bl = tensor_elem_at(in, id, border_mode, constant_border_value); |
| 83 | id.set(0, idx + 1); |
| 84 | id.set(1, idy + 1); |
| 85 | const T br = tensor_elem_at(in, id, border_mode, constant_border_value); |
| 86 | |
| 87 | return tl * (dx_1 * dy_1) + tr * (dx * dy_1) + bl * (dx_1 * dy) + br * (dx * dy); |
| 88 | } |
| 89 | template uint8_t bilinear_policy(const SimpleTensor<uint8_t> &in, Coordinates id, float xn, float yn, BorderMode border_mode, uint8_t constant_border_value); |
| 90 | |
Abe Mbise | 3f0ab6e | 2017-09-01 16:43:47 +0100 | [diff] [blame] | 91 | /* Apply 2D spatial filter on a single element of @p in at coordinates @p coord |
| 92 | * |
| 93 | * - filter sizes have to be odd number |
| 94 | * - Row major order of filter assumed |
| 95 | * - TO_ZERO rounding policy assumed |
| 96 | * - SATURATE convert policy assumed |
| 97 | * |
| 98 | */ |
| 99 | template <typename T1, typename T2, typename T3> |
| 100 | void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor<T1> &in, SimpleTensor<T3> &out, const TensorShape &filter_shape, const T2 *filter_itr, float scale, BorderMode border_mode, |
| 101 | T1 constant_border_value) |
| 102 | { |
| 103 | double val = 0; |
| 104 | const int x = coord.x(); |
| 105 | const int y = coord.y(); |
| 106 | for(int j = y - static_cast<int>(filter_shape[1] / 2); j <= y + static_cast<int>(filter_shape[1] / 2); ++j) |
| 107 | { |
| 108 | for(int i = x - static_cast<int>(filter_shape[0] / 2); i <= x + static_cast<int>(filter_shape[0] / 2); ++i) |
| 109 | { |
| 110 | coord.set(0, i); |
| 111 | coord.set(1, j); |
| 112 | val += static_cast<double>(*filter_itr) * tensor_elem_at(in, coord, border_mode, constant_border_value); |
| 113 | ++filter_itr; |
| 114 | } |
| 115 | } |
| 116 | coord.set(0, x); |
| 117 | coord.set(1, y); |
| 118 | const double rounded_val = support::cpp11::trunc(val * static_cast<double>(scale)); |
| 119 | out[coord2index(in.shape(), coord)] = saturate_cast<T3>(rounded_val); |
| 120 | } |
| 121 | template void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor<float> &in, SimpleTensor<float> &out, const TensorShape &filter_shape, const float *filter_itr, float scale, |
| 122 | BorderMode border_mode, |
| 123 | float constant_border_value); |
| 124 | template void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor<uint8_t> &in, SimpleTensor<uint8_t> &out, const TensorShape &filter_shape, const uint8_t *filter_itr, float scale, |
| 125 | BorderMode border_mode, |
| 126 | uint8_t constant_border_value); |
| 127 | |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 128 | RawTensor transpose(const RawTensor &src, int chunk_width) |
| 129 | { |
| 130 | // Create reference |
| 131 | TensorShape dst_shape(src.shape()); |
| 132 | dst_shape.set(0, src.shape().y() * chunk_width); |
| 133 | dst_shape.set(1, std::ceil(src.shape().x() / static_cast<float>(chunk_width))); |
| 134 | |
| 135 | RawTensor dst{ dst_shape, src.data_type() }; |
| 136 | |
| 137 | // Compute reference |
| 138 | uint8_t *out_ptr = dst.data(); |
| 139 | |
| 140 | for(int i = 0; i < dst.num_elements(); i += chunk_width) |
| 141 | { |
| 142 | Coordinates coord = index2coord(dst.shape(), i); |
| 143 | size_t coord_x = coord.x(); |
| 144 | coord.set(0, coord.y() * chunk_width); |
| 145 | coord.set(1, coord_x / chunk_width); |
| 146 | |
| 147 | const int num_elements = std::min<int>(chunk_width, src.shape().x() - coord.x()); |
| 148 | |
| 149 | std::copy_n(static_cast<const uint8_t *>(src(coord)), num_elements * src.element_size(), out_ptr); |
| 150 | |
| 151 | out_ptr += chunk_width * dst.element_size(); |
| 152 | } |
| 153 | |
| 154 | return dst; |
| 155 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 156 | } // namespace validation |
| 157 | } // namespace test |
| 158 | } // namespace arm_compute |