Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
| 24 | #include "Validation.h" |
| 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/TensorShape.h" |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/Tensor.h" |
| 31 | |
| 32 | #include <array> |
| 33 | #include <cmath> |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | namespace |
| 44 | { |
| 45 | /** Get the data from *ptr after casting according to @p data_type and then convert the data to double. |
| 46 | * |
| 47 | * @param[in] ptr Pointer to value. |
| 48 | * @param[in] data_type Data type of both values. |
| 49 | * |
| 50 | * @return The data from the ptr after converted to double. |
| 51 | */ |
| 52 | double get_double_data(const void *ptr, DataType data_type) |
| 53 | { |
steniu01 | 9746fd8 | 2017-06-15 10:49:37 +0100 | [diff] [blame] | 54 | if(ptr == nullptr) |
| 55 | { |
| 56 | ARM_COMPUTE_ERROR("Can't dereference a null pointer!"); |
| 57 | } |
| 58 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | switch(data_type) |
| 60 | { |
| 61 | case DataType::U8: |
| 62 | return *reinterpret_cast<const uint8_t *>(ptr); |
| 63 | case DataType::S8: |
| 64 | return *reinterpret_cast<const int8_t *>(ptr); |
| 65 | case DataType::QS8: |
| 66 | return *reinterpret_cast<const qint8_t *>(ptr); |
| 67 | case DataType::U16: |
| 68 | return *reinterpret_cast<const uint16_t *>(ptr); |
| 69 | case DataType::S16: |
| 70 | return *reinterpret_cast<const int16_t *>(ptr); |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 71 | case DataType::QS16: |
| 72 | return *reinterpret_cast<const qint16_t *>(ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | case DataType::U32: |
| 74 | return *reinterpret_cast<const uint32_t *>(ptr); |
| 75 | case DataType::S32: |
| 76 | return *reinterpret_cast<const int32_t *>(ptr); |
| 77 | case DataType::U64: |
| 78 | return *reinterpret_cast<const uint64_t *>(ptr); |
| 79 | case DataType::S64: |
| 80 | return *reinterpret_cast<const int64_t *>(ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | case DataType::F16: |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 82 | return *reinterpret_cast<const half *>(ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | case DataType::F32: |
| 84 | return *reinterpret_cast<const float *>(ptr); |
| 85 | case DataType::F64: |
| 86 | return *reinterpret_cast<const double *>(ptr); |
| 87 | case DataType::SIZET: |
| 88 | return *reinterpret_cast<const size_t *>(ptr); |
| 89 | default: |
| 90 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void check_border_element(const IAccessor &tensor, const Coordinates &id, |
| 95 | const BorderMode &border_mode, const void *border_value, |
| 96 | int64_t &num_elements, int64_t &num_mismatches) |
| 97 | { |
| 98 | const size_t channel_size = element_size_from_data_type(tensor.data_type()); |
| 99 | const auto ptr = static_cast<const uint8_t *>(tensor(id)); |
| 100 | |
| 101 | if(border_mode == BorderMode::REPLICATE) |
| 102 | { |
| 103 | Coordinates border_id{ id }; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 104 | |
| 105 | if(id.x() < 0) |
| 106 | { |
| 107 | border_id.set(0, 0); |
| 108 | } |
| 109 | else if(static_cast<size_t>(id.x()) >= tensor.shape().x()) |
| 110 | { |
| 111 | border_id.set(0, tensor.shape().x() - 1); |
| 112 | } |
| 113 | |
| 114 | if(id.y() < 0) |
| 115 | { |
| 116 | border_id.set(1, 0); |
| 117 | } |
| 118 | else if(static_cast<size_t>(id.y()) >= tensor.shape().y()) |
| 119 | { |
| 120 | border_id.set(1, tensor.shape().y() - 1); |
| 121 | } |
| 122 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | border_value = tensor(border_id); |
| 124 | } |
| 125 | |
| 126 | // Iterate over all channels within one element |
| 127 | for(int channel = 0; channel < tensor.num_channels(); ++channel) |
| 128 | { |
| 129 | const size_t channel_offset = channel * channel_size; |
| 130 | const double target = get_double_data(ptr + channel_offset, tensor.data_type()); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 131 | const double reference = get_double_data(static_cast<const uint8_t *>(border_value) + channel_offset, tensor.data_type()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 133 | if(!compare<AbsoluteTolerance<double>>(target, reference)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_TEST_INFO("id = " << id); |
| 136 | ARM_COMPUTE_TEST_INFO("channel = " << channel); |
| 137 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << target); |
| 138 | ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << reference); |
| 139 | ARM_COMPUTE_EXPECT_EQUAL(target, reference, framework::LogLevel::DEBUG); |
| 140 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | ++num_mismatches; |
| 142 | } |
| 143 | |
| 144 | ++num_elements; |
| 145 | } |
| 146 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | } // namespace |
| 148 | |
| 149 | void validate(const arm_compute::ValidRegion ®ion, const arm_compute::ValidRegion &reference) |
| 150 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 151 | ARM_COMPUTE_EXPECT_EQUAL(region.anchor.num_dimensions(), reference.anchor.num_dimensions(), framework::LogLevel::ERRORS); |
| 152 | ARM_COMPUTE_EXPECT_EQUAL(region.shape.num_dimensions(), reference.shape.num_dimensions(), framework::LogLevel::ERRORS); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | |
| 154 | for(unsigned int d = 0; d < region.anchor.num_dimensions(); ++d) |
| 155 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 156 | ARM_COMPUTE_EXPECT_EQUAL(region.anchor[d], reference.anchor[d], framework::LogLevel::ERRORS); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | for(unsigned int d = 0; d < region.shape.num_dimensions(); ++d) |
| 160 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 161 | ARM_COMPUTE_EXPECT_EQUAL(region.shape[d], reference.shape[d], framework::LogLevel::ERRORS); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference) |
| 166 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 167 | ARM_COMPUTE_EXPECT_EQUAL(padding.top, reference.top, framework::LogLevel::ERRORS); |
| 168 | ARM_COMPUTE_EXPECT_EQUAL(padding.right, reference.right, framework::LogLevel::ERRORS); |
| 169 | ARM_COMPUTE_EXPECT_EQUAL(padding.bottom, reference.bottom, framework::LogLevel::ERRORS); |
| 170 | ARM_COMPUTE_EXPECT_EQUAL(padding.left, reference.left, framework::LogLevel::ERRORS); |
Isabella Gottardi | 6203153 | 2017-07-04 11:21:28 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 173 | void validate(const IAccessor &tensor, const void *reference_value) |
| 174 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 175 | ARM_COMPUTE_ASSERT(reference_value != nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | |
| 177 | int64_t num_mismatches = 0; |
| 178 | int64_t num_elements = 0; |
| 179 | const size_t channel_size = element_size_from_data_type(tensor.data_type()); |
| 180 | |
| 181 | // Iterate over all elements, e.g. U8, S16, RGB888, ... |
| 182 | for(int element_idx = 0; element_idx < tensor.num_elements(); ++element_idx) |
| 183 | { |
| 184 | const Coordinates id = index2coord(tensor.shape(), element_idx); |
| 185 | |
| 186 | const auto ptr = static_cast<const uint8_t *>(tensor(id)); |
| 187 | |
| 188 | // Iterate over all channels within one element |
| 189 | for(int channel = 0; channel < tensor.num_channels(); ++channel) |
| 190 | { |
| 191 | const size_t channel_offset = channel * channel_size; |
| 192 | const double target = get_double_data(ptr + channel_offset, tensor.data_type()); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 193 | const double reference = get_double_data(reference_value, tensor.data_type()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 194 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 195 | if(!compare<AbsoluteTolerance<double>>(target, reference)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 197 | ARM_COMPUTE_TEST_INFO("id = " << id); |
| 198 | ARM_COMPUTE_TEST_INFO("channel = " << channel); |
| 199 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << target); |
| 200 | ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << reference); |
| 201 | ARM_COMPUTE_EXPECT_EQUAL(target, reference, framework::LogLevel::DEBUG); |
| 202 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | ++num_mismatches; |
| 204 | } |
| 205 | |
| 206 | ++num_elements; |
| 207 | } |
| 208 | } |
| 209 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 210 | if(num_elements > 0) |
| 211 | { |
| 212 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 214 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched"); |
| 215 | ARM_COMPUTE_EXPECT_EQUAL(num_mismatches, 0, framework::LogLevel::ERRORS); |
| 216 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value) |
| 220 | { |
| 221 | if(border_mode == BorderMode::UNDEFINED) |
| 222 | { |
| 223 | return; |
| 224 | } |
| 225 | else if(border_mode == BorderMode::CONSTANT) |
| 226 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 227 | ARM_COMPUTE_ASSERT(border_value != nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int64_t num_mismatches = 0; |
| 231 | int64_t num_elements = 0; |
| 232 | const int slice_size = tensor.shape()[0] * tensor.shape()[1]; |
| 233 | |
| 234 | for(int element_idx = 0; element_idx < tensor.num_elements(); element_idx += slice_size) |
| 235 | { |
| 236 | Coordinates id = index2coord(tensor.shape(), element_idx); |
| 237 | |
| 238 | // Top border |
| 239 | for(int y = -border_size.top; y < 0; ++y) |
| 240 | { |
| 241 | id.set(1, y); |
| 242 | |
| 243 | for(int x = -border_size.left; x < static_cast<int>(tensor.shape()[0]) + static_cast<int>(border_size.right); ++x) |
| 244 | { |
| 245 | id.set(0, x); |
| 246 | |
| 247 | check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Bottom border |
| 252 | for(int y = tensor.shape()[1]; y < static_cast<int>(tensor.shape()[1]) + static_cast<int>(border_size.bottom); ++y) |
| 253 | { |
| 254 | id.set(1, y); |
| 255 | |
| 256 | for(int x = -border_size.left; x < static_cast<int>(tensor.shape()[0]) + static_cast<int>(border_size.right); ++x) |
| 257 | { |
| 258 | id.set(0, x); |
| 259 | |
| 260 | check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // Left/right border |
| 265 | for(int y = 0; y < static_cast<int>(tensor.shape()[1]); ++y) |
| 266 | { |
| 267 | id.set(1, y); |
| 268 | |
| 269 | // Left border |
| 270 | for(int x = -border_size.left; x < 0; ++x) |
| 271 | { |
| 272 | id.set(0, x); |
| 273 | |
| 274 | check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches); |
| 275 | } |
| 276 | |
| 277 | // Right border |
| 278 | for(int x = tensor.shape()[0]; x < static_cast<int>(tensor.shape()[0]) + static_cast<int>(border_size.right); ++x) |
| 279 | { |
| 280 | id.set(0, x); |
| 281 | |
| 282 | check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 287 | if(num_elements > 0) |
| 288 | { |
| 289 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 290 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 291 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched"); |
| 292 | ARM_COMPUTE_EXPECT_EQUAL(num_mismatches, 0, framework::LogLevel::ERRORS); |
| 293 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels) |
| 297 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 298 | ARM_COMPUTE_EXPECT_EQUAL(classified_labels.size(), expected_labels.size(), framework::LogLevel::ERRORS); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 299 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 300 | int64_t num_mismatches = 0; |
| 301 | const int num_elements = std::min(classified_labels.size(), expected_labels.size()); |
Moritz Pflanzer | 6a45048 | 2017-09-11 16:43:32 +0100 | [diff] [blame] | 302 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 303 | for(int i = 0; i < num_elements; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 304 | { |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 305 | if(classified_labels[i] != expected_labels[i]) |
| 306 | { |
| 307 | ++num_mismatches; |
| 308 | ARM_COMPUTE_EXPECT_EQUAL(classified_labels[i], expected_labels[i], framework::LogLevel::DEBUG); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if(num_elements > 0) |
| 313 | { |
| 314 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f; |
| 315 | |
| 316 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched"); |
| 317 | ARM_COMPUTE_EXPECT_EQUAL(num_mismatches, 0, framework::LogLevel::ERRORS); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 318 | } |
| 319 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 320 | } // namespace validation |
| 321 | } // namespace test |
| 322 | } // namespace arm_compute |