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