Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +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 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 24 | #include "tests/validation/Helpers.h" |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 25 | |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <cmath> |
| 28 | |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace validation |
| 34 | { |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 35 | void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern) |
| 36 | { |
| 37 | unsigned int v = 0; |
| 38 | std::mt19937 gen(library->seed()); |
| 39 | std::bernoulli_distribution dist(0.5); |
| 40 | |
| 41 | for(int r = 0; r < rows; ++r) |
| 42 | { |
| 43 | for(int c = 0; c < cols; ++c, ++v) |
| 44 | { |
| 45 | uint8_t val = 0; |
| 46 | |
| 47 | switch(pattern) |
| 48 | { |
| 49 | case MatrixPattern::BOX: |
| 50 | val = 255; |
| 51 | break; |
| 52 | case MatrixPattern::CROSS: |
| 53 | val = ((r == (rows / 2)) || (c == (cols / 2))) ? 255 : 0; |
| 54 | break; |
| 55 | case MatrixPattern::DISK: |
| 56 | val = (((r - rows / 2.0f + 0.5f) * (r - rows / 2.0f + 0.5f)) / ((rows / 2.0f) * (rows / 2.0f)) + ((c - cols / 2.0f + 0.5f) * (c - cols / 2.0f + 0.5f)) / ((cols / 2.0f) * |
| 57 | (cols / 2.0f))) <= 1.0f ? 255 : 0; |
| 58 | break; |
| 59 | case MatrixPattern::OTHER: |
| 60 | val = (dist(gen) ? 0 : 255); |
| 61 | break; |
| 62 | default: |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | mask[v] = val; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if(pattern == MatrixPattern::OTHER) |
| 71 | { |
| 72 | std::uniform_int_distribution<uint8_t> distribution_u8(0, ((cols * rows) - 1)); |
| 73 | mask[distribution_u8(gen)] = 255; |
| 74 | } |
| 75 | } |
| 76 | |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 77 | TensorShape calculate_depth_concatenate_shape(const std::vector<TensorShape> &input_shapes) |
| 78 | { |
| 79 | ARM_COMPUTE_ERROR_ON(input_shapes.empty()); |
| 80 | |
| 81 | TensorShape out_shape = input_shapes[0]; |
| 82 | |
| 83 | size_t max_x = 0; |
| 84 | size_t max_y = 0; |
| 85 | size_t depth = 0; |
| 86 | |
| 87 | for(const auto &shape : input_shapes) |
| 88 | { |
| 89 | max_x = std::max(shape.x(), max_x); |
| 90 | max_y = std::max(shape.y(), max_y); |
| 91 | depth += shape.z(); |
| 92 | } |
| 93 | |
| 94 | out_shape.set(0, max_x); |
| 95 | out_shape.set(1, max_y); |
| 96 | out_shape.set(2, depth); |
| 97 | |
| 98 | return out_shape; |
| 99 | } |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 100 | |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 101 | TensorShape calculate_width_concatenate_shape(const std::vector<TensorShape> &input_shapes) |
| 102 | { |
| 103 | ARM_COMPUTE_ERROR_ON(input_shapes.empty()); |
| 104 | |
| 105 | TensorShape out_shape = input_shapes[0]; |
| 106 | |
| 107 | int width = std::accumulate(input_shapes.begin(), input_shapes.end(), 0, [](int sum, const TensorShape & shape) |
| 108 | { |
| 109 | return sum + shape.x(); |
| 110 | }); |
| 111 | out_shape.set(0, width); |
| 112 | |
| 113 | return out_shape; |
| 114 | } |
| 115 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 116 | HarrisCornersParameters harris_corners_parameters() |
| 117 | { |
| 118 | HarrisCornersParameters params; |
| 119 | |
| 120 | std::mt19937 gen(library->seed()); |
Vidhya Sudhan Loganathan | 851a322 | 2018-05-11 14:26:51 +0100 | [diff] [blame] | 121 | std::uniform_real_distribution<float> threshold_dist(0.f, 0.001f); |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 122 | std::uniform_real_distribution<float> sensitivity(0.04f, 0.15f); |
| 123 | std::uniform_real_distribution<float> euclidean_distance(0.f, 30.f); |
| 124 | std::uniform_int_distribution<uint8_t> int_dist(0, 255); |
| 125 | |
| 126 | params.threshold = threshold_dist(gen); |
| 127 | params.sensitivity = sensitivity(gen); |
| 128 | params.min_dist = euclidean_distance(gen); |
| 129 | params.constant_border_value = int_dist(gen); |
| 130 | |
| 131 | return params; |
| 132 | } |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 133 | |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 134 | CannyEdgeParameters canny_edge_parameters() |
| 135 | { |
| 136 | CannyEdgeParameters params; |
| 137 | |
| 138 | std::mt19937 gen(library->seed()); |
| 139 | std::uniform_int_distribution<uint8_t> int_dist(0, 255); |
| 140 | std::uniform_int_distribution<uint8_t> threshold_dist(1, 255); |
| 141 | |
| 142 | params.constant_border_value = int_dist(gen); |
| 143 | params.upper_thresh = threshold_dist(gen); // upper_threshold >= 1 |
Michele Di Giorgio | bb71fe5 | 2018-06-20 11:45:35 +0100 | [diff] [blame] | 144 | threshold_dist = std::uniform_int_distribution<uint8_t>(1, params.upper_thresh - 1); |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 145 | params.lower_thresh = threshold_dist(gen); |
| 146 | |
| 147 | return params; |
| 148 | } |
| 149 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 150 | SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint8_t> &src) |
| 151 | { |
| 152 | const QuantizationInfo &quantization_info = src.quantization_info(); |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 153 | SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() }; |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 154 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 155 | for(int i = 0; i < src.num_elements(); ++i) |
| 156 | { |
| 157 | dst[i] = quantization_info.dequantize(src[i]); |
| 158 | } |
| 159 | return dst; |
| 160 | } |
| 161 | |
| 162 | SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info) |
| 163 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 164 | SimpleTensor<uint8_t> dst{ src.shape(), DataType::QASYMM8, 1, quantization_info }; |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 165 | for(int i = 0; i < src.num_elements(); ++i) |
| 166 | { |
Jaroslaw Rzepecki | 0a878ae | 2017-11-22 17:16:39 +0000 | [diff] [blame] | 167 | dst[i] = quantization_info.quantize(src[i], RoundingPolicy::TO_NEAREST_UP); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 168 | } |
| 169 | return dst; |
| 170 | } |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 171 | |
| 172 | void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out) |
| 173 | { |
| 174 | ARM_COMPUTE_ERROR_ON(a.shape()[0] != b.shape()[1]); |
| 175 | ARM_COMPUTE_ERROR_ON(a.shape()[1] != out.shape()[1]); |
| 176 | ARM_COMPUTE_ERROR_ON(b.shape()[0] != out.shape()[0]); |
| 177 | |
| 178 | const int M = a.shape()[1]; // Rows |
| 179 | const int N = b.shape()[0]; // Cols |
| 180 | const int K = b.shape()[1]; |
| 181 | |
| 182 | for(int y = 0; y < M; ++y) |
| 183 | { |
| 184 | for(int x = 0; x < N; ++x) |
| 185 | { |
| 186 | float acc = 0.0f; |
| 187 | for(int k = 0; k < K; ++k) |
| 188 | { |
| 189 | acc += a[y * K + k] * b[x + k * N]; |
| 190 | } |
| 191 | |
| 192 | out[x + y * N] = acc; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out) |
| 198 | { |
| 199 | ARM_COMPUTE_ERROR_ON((in.shape()[0] != out.shape()[1]) || (in.shape()[1] != out.shape()[0])); |
| 200 | |
| 201 | const int width = in.shape()[0]; |
| 202 | const int height = in.shape()[1]; |
| 203 | |
| 204 | for(int y = 0; y < height; ++y) |
| 205 | { |
| 206 | for(int x = 0; x < width; ++x) |
| 207 | { |
| 208 | const float val = in[x + y * width]; |
| 209 | |
| 210 | out[x * height + y] = val; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | template <typename T> |
| 216 | void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord) |
| 217 | { |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 218 | ARM_COMPUTE_ERROR_ON(tile.shape().num_dimensions() > 2); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 219 | |
| 220 | const int w_tile = tile.shape()[0]; |
| 221 | const int h_tile = tile.shape()[1]; |
| 222 | |
| 223 | // Fill the tile with zeros |
| 224 | std::fill(tile.data() + 0, (tile.data() + (w_tile * h_tile)), static_cast<T>(0)); |
| 225 | |
| 226 | // Check if with the dimensions greater than 2 we could have out-of-bound reads |
| 227 | for(size_t d = 2; d < Coordinates::num_max_dimensions; ++d) |
| 228 | { |
| 229 | if(coord[d] < 0 || coord[d] >= static_cast<int>(in.shape()[d])) |
| 230 | { |
| 231 | ARM_COMPUTE_ERROR("coord[d] < 0 || coord[d] >= in.shape()[d] with d >= 2"); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Since we could have out-of-bound reads along the X and Y dimensions, |
| 236 | // we start calculating the input address with x = 0 and y = 0 |
| 237 | Coordinates start_coord = coord; |
| 238 | start_coord[0] = 0; |
| 239 | start_coord[1] = 0; |
| 240 | |
| 241 | // Get input and roi pointers |
| 242 | auto in_ptr = static_cast<const T *>(in(start_coord)); |
| 243 | auto roi_ptr = static_cast<T *>(tile.data()); |
| 244 | |
| 245 | const int x_in_start = std::max(0, coord[0]); |
| 246 | const int y_in_start = std::max(0, coord[1]); |
| 247 | const int x_in_end = std::min(static_cast<int>(in.shape()[0]), coord[0] + w_tile); |
| 248 | const int y_in_end = std::min(static_cast<int>(in.shape()[1]), coord[1] + h_tile); |
| 249 | |
| 250 | // Number of elements to copy per row |
| 251 | const int n = x_in_end - x_in_start; |
| 252 | |
| 253 | // Starting coordinates for the ROI |
| 254 | const int x_tile_start = coord[0] > 0 ? 0 : std::abs(coord[0]); |
| 255 | const int y_tile_start = coord[1] > 0 ? 0 : std::abs(coord[1]); |
| 256 | |
| 257 | // Update input pointer |
| 258 | in_ptr += x_in_start; |
| 259 | in_ptr += (y_in_start * in.shape()[0]); |
| 260 | |
| 261 | // Update ROI pointer |
| 262 | roi_ptr += x_tile_start; |
| 263 | roi_ptr += (y_tile_start * tile.shape()[0]); |
| 264 | |
| 265 | for(int y = y_in_start; y < y_in_end; ++y) |
| 266 | { |
| 267 | // Copy per row |
| 268 | std::copy(in_ptr, in_ptr + n, roi_ptr); |
| 269 | |
| 270 | in_ptr += in.shape()[0]; |
| 271 | roi_ptr += tile.shape()[0]; |
| 272 | } |
| 273 | } |
| 274 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 275 | template <typename T> |
| 276 | void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape) |
| 277 | { |
| 278 | ARM_COMPUTE_ERROR_ON(anchor.num_dimensions() != shape.num_dimensions()); |
| 279 | ARM_COMPUTE_ERROR_ON(in.shape().num_dimensions() > 2); |
| 280 | ARM_COMPUTE_ERROR_ON(shape.num_dimensions() > 2); |
| 281 | |
| 282 | // Check if with the dimensions greater than 2 we could have out-of-bound reads |
| 283 | for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d) |
| 284 | { |
| 285 | if(anchor[d] < 0 || ((anchor[d] + shape[d]) > in.shape()[d])) |
| 286 | { |
| 287 | ARM_COMPUTE_ERROR("anchor[d] < 0 || (anchor[d] + shape[d]) > in.shape()[d]"); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Get input pointer |
| 292 | auto in_ptr = static_cast<T *>(in(anchor[0] + anchor[1] * in.shape()[0])); |
| 293 | |
| 294 | const unsigned int n = in.shape()[0]; |
| 295 | |
| 296 | for(unsigned int y = 0; y < shape[1]; ++y) |
| 297 | { |
| 298 | std::fill(in_ptr, in_ptr + shape[0], 0); |
| 299 | in_ptr += n; |
| 300 | } |
| 301 | } |
| 302 | |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 303 | template void get_tile(const SimpleTensor<float> &in, SimpleTensor<float> &roi, const Coordinates &coord); |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 304 | template void zeros(SimpleTensor<float> &in, const Coordinates &anchor, const TensorShape &shape); |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 305 | } // namespace validation |
| 306 | } // namespace test |
| 307 | } // namespace arm_compute |