Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Jakub Sujak | 3b504ef | 2022-12-07 23:55:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, 2023 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 "Utils.h" |
| 25 | |
Pablo Tello | cf9c843 | 2019-07-22 17:36:03 +0100 | [diff] [blame] | 26 | #ifdef ARM_COMPUTE_CL |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Pablo Tello | db9116f | 2019-07-11 16:50:37 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Pablo Tello | cf9c843 | 2019-07-22 17:36:03 +0100 | [diff] [blame] | 29 | #endif /* ARM_COMPUTE_CL */ |
Pablo Tello | db9116f | 2019-07-11 16:50:37 +0100 | [diff] [blame] | 30 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include <cctype> |
| 32 | #include <cerrno> |
| 33 | #include <iomanip> |
| 34 | #include <string> |
| 35 | |
Anthony Barbier | 1c28ab4 | 2018-08-29 11:22:33 +0100 | [diff] [blame] | 36 | #pragma GCC diagnostic push |
| 37 | #pragma GCC diagnostic ignored "-Wswitch-default" |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 38 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 39 | #pragma GCC diagnostic ignored "-Wstrict-overflow" |
Manuel Bottini | 827817e | 2020-11-19 12:12:06 +0000 | [diff] [blame] | 40 | #if (defined(__GNUC__) && (__GNUC__ >= 7)) |
| 41 | #pragma GCC diagnostic ignored "-Wimplicit-fallthrough" |
| 42 | #endif // (defined(__GNUC__) && (__GNUC__ >= 7)) |
| 43 | #if defined(__clang__) |
| 44 | #pragma GCC diagnostic ignored "-Wparentheses-equality" |
| 45 | #endif // defined(__clang__) |
Anthony Barbier | 1c28ab4 | 2018-08-29 11:22:33 +0100 | [diff] [blame] | 46 | #define STB_IMAGE_IMPLEMENTATION |
| 47 | #include "stb/stb_image.h" |
| 48 | #pragma GCC diagnostic pop |
| 49 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | namespace arm_compute |
| 51 | { |
| 52 | namespace utils |
| 53 | { |
| 54 | namespace |
| 55 | { |
| 56 | /* Advance the iterator to the first character which is not a comment |
| 57 | * |
| 58 | * @param[in,out] fs Stream to drop comments from |
| 59 | */ |
| 60 | void discard_comments(std::ifstream &fs) |
| 61 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | while (fs.peek() == '#') |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | { |
| 64 | fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /* Advance the string iterator to the next character which is neither a space or a comment |
| 69 | * |
| 70 | * @param[in,out] fs Stream to drop comments from |
| 71 | */ |
| 72 | void discard_comments_and_spaces(std::ifstream &fs) |
| 73 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 74 | while (true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | { |
| 76 | discard_comments(fs); |
| 77 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | if (isspace(fs.peek()) == 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | { |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | fs.ignore(1); |
| 84 | } |
| 85 | } |
| 86 | } // namespace |
| 87 | |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 88 | #ifndef BENCHMARK_EXAMPLES |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 89 | int run_example(int argc, char **argv, std::unique_ptr<Example> example) |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 90 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | std::cout << "\n" << argv[0] << "\n\n"; |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 92 | |
| 93 | try |
| 94 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 95 | bool status = example->do_setup(argc, argv); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | if (!status) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 97 | { |
| 98 | return 1; |
| 99 | } |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 100 | example->do_run(); |
| 101 | example->do_teardown(); |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 102 | |
| 103 | std::cout << "\nTest passed\n"; |
| 104 | return 0; |
| 105 | } |
| 106 | #ifdef ARM_COMPUTE_CL |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | catch (cl::Error &err) |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 108 | { |
| 109 | std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 110 | std::cerr << std::endl << "ERROR " << err.what() << "(" << err.err() << ")" << std::endl; |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 111 | std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; |
| 112 | } |
| 113 | #endif /* ARM_COMPUTE_CL */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | catch (std::runtime_error &err) |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 115 | { |
| 116 | std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 117 | std::cerr << std::endl << "ERROR " << err.what() << " " << (errno ? strerror(errno) : "") << std::endl; |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 118 | std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; |
| 119 | } |
| 120 | |
| 121 | std::cout << "\nTest FAILED\n"; |
| 122 | |
| 123 | return -1; |
| 124 | } |
| 125 | #endif /* BENCHMARK_EXAMPLES */ |
| 126 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | void draw_detection_rectangle(ITensor *tensor, const DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b) |
| 128 | { |
| 129 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(tensor, Format::RGB888); |
| 130 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 131 | uint8_t *top = tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y)) + tensor->buffer(); |
| 132 | uint8_t *bottom = |
| 133 | tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y + rect.height)) + tensor->buffer(); |
| 134 | uint8_t *left = top; |
| 135 | uint8_t *right = |
| 136 | tensor->info()->offset_element_in_bytes(Coordinates(rect.x + rect.width, rect.y)) + tensor->buffer(); |
| 137 | size_t stride = tensor->info()->strides_in_bytes()[Window::DimY]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 139 | for (size_t x = 0; x < rect.width; ++x) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | { |
| 141 | top[0] = r; |
| 142 | top[1] = g; |
| 143 | top[2] = b; |
| 144 | bottom[0] = r; |
| 145 | bottom[1] = g; |
| 146 | bottom[2] = b; |
| 147 | |
| 148 | top += 3; |
| 149 | bottom += 3; |
| 150 | } |
| 151 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 152 | for (size_t y = 0; y < rect.height; ++y) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | { |
| 154 | left[0] = r; |
| 155 | left[1] = g; |
| 156 | left[2] = b; |
| 157 | right[0] = r; |
| 158 | right[1] = g; |
| 159 | right[2] = b; |
| 160 | |
| 161 | left += stride; |
| 162 | right += stride; |
| 163 | } |
| 164 | } |
| 165 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 166 | ImageType get_image_type_from_file(const std::string &filename) |
| 167 | { |
| 168 | ImageType type = ImageType::UNKNOWN; |
| 169 | |
| 170 | try |
| 171 | { |
| 172 | // Open file |
| 173 | std::ifstream fs; |
| 174 | fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 175 | fs.open(filename, std::ios::in | std::ios::binary); |
| 176 | |
| 177 | // Identify type from magic number |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 178 | std::array<unsigned char, 2> magic_number{{0}}; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 179 | fs >> magic_number[0] >> magic_number[1]; |
| 180 | |
| 181 | // PPM check |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 182 | if (static_cast<char>(magic_number[0]) == 'P' && static_cast<char>(magic_number[1]) == '6') |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 183 | { |
| 184 | type = ImageType::PPM; |
| 185 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 186 | else if (magic_number[0] == 0xFF && magic_number[1] == 0xD8) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 187 | { |
| 188 | type = ImageType::JPEG; |
| 189 | } |
| 190 | |
| 191 | fs.close(); |
| 192 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 193 | catch (std::runtime_error &e) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 194 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 195 | ARM_COMPUTE_ERROR_VAR("Accessing %s: %s", filename.c_str(), e.what()); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return type; |
| 199 | } |
| 200 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs) |
| 202 | { |
| 203 | // Check the PPM magic number is valid |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 204 | std::array<char, 2> magic_number{{0}}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | fs >> magic_number[0] >> magic_number[1]; |
| 206 | ARM_COMPUTE_ERROR_ON_MSG(magic_number[0] != 'P' || magic_number[1] != '6', "Invalid file type"); |
| 207 | ARM_COMPUTE_UNUSED(magic_number); |
| 208 | |
| 209 | discard_comments_and_spaces(fs); |
| 210 | |
| 211 | unsigned int width = 0; |
| 212 | fs >> width; |
| 213 | |
| 214 | discard_comments_and_spaces(fs); |
| 215 | |
| 216 | unsigned int height = 0; |
| 217 | fs >> height; |
| 218 | |
| 219 | discard_comments_and_spaces(fs); |
| 220 | |
| 221 | int max_val = 0; |
| 222 | fs >> max_val; |
| 223 | |
| 224 | discard_comments(fs); |
| 225 | |
| 226 | ARM_COMPUTE_ERROR_ON_MSG(isspace(fs.peek()) == 0, "Invalid PPM header"); |
| 227 | fs.ignore(1); |
| 228 | |
| 229 | return std::make_tuple(width, height, max_val); |
| 230 | } |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 231 | |
Jakub Sujak | 3b504ef | 2022-12-07 23:55:22 +0000 | [diff] [blame] | 232 | npy::header_t parse_npy_header(std::ifstream &fs) //NOLINT |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 233 | { |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 234 | // Read header |
Jakub Sujak | 3b504ef | 2022-12-07 23:55:22 +0000 | [diff] [blame] | 235 | std::string header_s = npy::read_header(fs); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 236 | |
| 237 | // Parse header |
Jakub Sujak | 3b504ef | 2022-12-07 23:55:22 +0000 | [diff] [blame] | 238 | npy::header_t header = npy::parse_header(header_s); |
| 239 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 240 | bool fortran_order = false; |
| 241 | std::vector<unsigned long> shape = header.shape; |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 242 | |
Michalis Spyrou | 3941295 | 2018-08-14 17:06:16 +0100 | [diff] [blame] | 243 | std::reverse(shape.begin(), shape.end()); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 244 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 245 | return npy::header_t{header.dtype, fortran_order, shape}; |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 246 | } |
Gian Marco | 5ca7409 | 2018-02-08 16:21:54 +0000 | [diff] [blame] | 247 | |
| 248 | /** This function returns the amount of memory free reading from /proc/meminfo |
| 249 | * |
| 250 | * @return The free memory in kB |
| 251 | */ |
| 252 | uint64_t get_mem_free_from_meminfo() |
| 253 | { |
| 254 | std::string line_attribute; |
| 255 | std::ifstream file_meminfo("/proc/meminfo"); |
| 256 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 257 | if (file_meminfo.is_open()) |
Gian Marco | 5ca7409 | 2018-02-08 16:21:54 +0000 | [diff] [blame] | 258 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 259 | while (!(file_meminfo >> line_attribute).fail()) |
Gian Marco | 5ca7409 | 2018-02-08 16:21:54 +0000 | [diff] [blame] | 260 | { |
| 261 | //Test if is the line containing MemFree |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 262 | if (line_attribute == "MemFree:") |
Gian Marco | 5ca7409 | 2018-02-08 16:21:54 +0000 | [diff] [blame] | 263 | { |
| 264 | uint64_t mem_available; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 265 | if (!(file_meminfo >> mem_available).fail()) |
Gian Marco | 5ca7409 | 2018-02-08 16:21:54 +0000 | [diff] [blame] | 266 | { |
| 267 | return mem_available; |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | return 0; |
| 272 | } |
| 273 | } |
| 274 | // if it's not MemFree ignore rest of the line |
| 275 | file_meminfo.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); |
| 276 | } |
| 277 | } |
| 278 | // Nothing found or an error during opening the file |
| 279 | return 0; |
| 280 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | } // namespace utils |
| 282 | } // namespace arm_compute |