Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 6 | #include "ImageTensorGenerator.hpp" |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 7 | #include "../InferenceTestImage.hpp" |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 8 | #include <armnn/Logging.hpp> |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 9 | #include <armnn/TypesUtils.hpp> |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 10 | #include <Filesystem.hpp> |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 11 | |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 12 | #include <mapbox/variant.hpp> |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 13 | #include <cxxopts/cxxopts.hpp> |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 14 | |
| 15 | #include <algorithm> |
| 16 | #include <fstream> |
| 17 | #include <iostream> |
| 18 | #include <string> |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | |
| 23 | // parses the command line to extract |
| 24 | // * the input image file -i the input image file path (must exist) |
| 25 | // * the layout -l the data layout output generated with (optional - default value is NHWC) |
| 26 | // * the output file -o the output raw tensor file path (must not already exist) |
| 27 | class CommandLineProcessor |
| 28 | { |
| 29 | public: |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 30 | bool ParseOptions(cxxopts::ParseResult& result) |
| 31 | { |
| 32 | // infile is mandatory |
| 33 | if (result.count("infile")) |
| 34 | { |
| 35 | if (!ValidateInputFile(result["infile"].as<std::string>())) |
| 36 | { |
| 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | std::cerr << "-i/--infile parameter is mandatory." << std::endl; |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // model-format is mandatory |
| 47 | if (!result.count("model-format")) |
| 48 | { |
| 49 | std::cerr << "-f/--model-format parameter is mandatory." << std::endl; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | // outfile is mandatory |
| 54 | if (result.count("outfile")) |
| 55 | { |
| 56 | if (!ValidateOutputFile(result["outfile"].as<std::string>())) |
| 57 | { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | std::cerr << "-o/--outfile parameter is mandatory." << std::endl; |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (result.count("layout")) |
| 68 | { |
| 69 | if(!ValidateLayout(result["layout"].as<std::string>())) |
| 70 | { |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 78 | bool ValidateInputFile(const std::string& inputFileName) |
| 79 | { |
| 80 | if (inputFileName.empty()) |
| 81 | { |
| 82 | std::cerr << "No input file name specified" << std::endl; |
| 83 | return false; |
| 84 | } |
| 85 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 86 | if (!fs::exists(inputFileName)) |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 87 | { |
| 88 | std::cerr << "Input file [" << inputFileName << "] does not exist" << std::endl; |
| 89 | return false; |
| 90 | } |
| 91 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 92 | if (fs::is_directory(inputFileName)) |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 93 | { |
| 94 | std::cerr << "Input file [" << inputFileName << "] is a directory" << std::endl; |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool ValidateLayout(const std::string& layout) |
| 102 | { |
| 103 | if (layout.empty()) |
| 104 | { |
| 105 | std::cerr << "No layout specified" << std::endl; |
| 106 | return false; |
| 107 | } |
| 108 | |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 109 | std::vector<std::string> supportedLayouts = { "NHWC", "NCHW" }; |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 110 | |
| 111 | auto iterator = std::find(supportedLayouts.begin(), supportedLayouts.end(), layout); |
| 112 | if (iterator == supportedLayouts.end()) |
| 113 | { |
| 114 | std::cerr << "Layout [" << layout << "] is not supported" << std::endl; |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 121 | bool ValidateOutputFile(const std::string& outputFileName) |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 122 | { |
| 123 | if (outputFileName.empty()) |
| 124 | { |
| 125 | std::cerr << "No output file name specified" << std::endl; |
| 126 | return false; |
| 127 | } |
| 128 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 129 | if (fs::exists(outputFileName)) |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 130 | { |
| 131 | std::cerr << "Output file [" << outputFileName << "] already exists" << std::endl; |
| 132 | return false; |
| 133 | } |
| 134 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 135 | if (fs::is_directory(outputFileName)) |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 136 | { |
| 137 | std::cerr << "Output file [" << outputFileName << "] is a directory" << std::endl; |
| 138 | return false; |
| 139 | } |
| 140 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 141 | fs::path outputPath(outputFileName); |
| 142 | if (!fs::exists(outputPath.parent_path())) |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 143 | { |
| 144 | std::cerr << "Output directory [" << outputPath.parent_path().c_str() << "] does not exist" << std::endl; |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool ProcessCommandLine(int argc, char* argv[]) |
| 152 | { |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 153 | cxxopts::Options options("ImageTensorGenerator", |
| 154 | "Program for pre-processing a .jpg image " |
| 155 | "before generating a .raw tensor file from it."); |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 156 | |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 157 | try |
| 158 | { |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 159 | options.add_options() |
| 160 | ("h,help", "Display help messages") |
| 161 | ("i,infile", |
| 162 | "Input image file to generate tensor from", |
| 163 | cxxopts::value<std::string>(m_InputFileName)) |
| 164 | ("f,model-format", |
| 165 | "Format of the intended model file that uses the images." |
| 166 | "Different formats have different image normalization styles." |
Kevin May | eb03e0f | 2021-04-28 16:16:22 +0100 | [diff] [blame] | 167 | "Accepted value (tflite)", |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 168 | cxxopts::value<std::string>(m_ModelFormat)) |
| 169 | ("o,outfile", |
| 170 | "Output raw tensor file path", |
| 171 | cxxopts::value<std::string>(m_OutputFileName)) |
| 172 | ("z,output-type", |
| 173 | "The data type of the output tensors." |
| 174 | "If unset, defaults to \"float\" for all defined inputs. " |
| 175 | "Accepted values (float, int or qasymm8)", |
| 176 | cxxopts::value<std::string>(m_OutputType)->default_value("float")) |
| 177 | ("new-width", |
| 178 | "Resize image to new width. Keep original width if unspecified", |
| 179 | cxxopts::value<std::string>(m_NewWidth)->default_value("0")) |
| 180 | ("new-height", |
| 181 | "Resize image to new height. Keep original height if unspecified", |
| 182 | cxxopts::value<std::string>(m_NewHeight)->default_value("0")) |
| 183 | ("l,layout", |
| 184 | "Output data layout, \"NHWC\" or \"NCHW\", default value NHWC", |
| 185 | cxxopts::value<std::string>(m_Layout)->default_value("NHWC")); |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 186 | } |
| 187 | catch (const std::exception& e) |
| 188 | { |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 189 | std::cerr << options.help() << std::endl; |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 193 | try |
| 194 | { |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 195 | auto result = options.parse(argc, argv); |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 196 | |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 197 | if (result.count("help")) |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 198 | { |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 199 | std::cout << options.help() << std::endl; |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 203 | // Check for mandatory parameters and validate inputs |
| 204 | if(!ParseOptions(result)){ |
| 205 | return false; |
| 206 | } |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 207 | } |
Matthew Sloyan | bf88b6f | 2020-09-25 17:22:00 +0100 | [diff] [blame] | 208 | catch (const cxxopts::OptionException& e) |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 209 | { |
| 210 | std::cerr << e.what() << std::endl << std::endl; |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | std::string GetInputFileName() {return m_InputFileName;} |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 218 | armnn::DataLayout GetLayout() |
| 219 | { |
| 220 | if (m_Layout == "NHWC") |
| 221 | { |
| 222 | return armnn::DataLayout::NHWC; |
| 223 | } |
| 224 | else if (m_Layout == "NCHW") |
| 225 | { |
| 226 | return armnn::DataLayout::NCHW; |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | throw armnn::Exception("Unsupported data layout: " + m_Layout); |
| 231 | } |
| 232 | } |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 233 | std::string GetOutputFileName() {return m_OutputFileName;} |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 234 | unsigned int GetNewWidth() {return static_cast<unsigned int>(std::stoi(m_NewWidth));} |
| 235 | unsigned int GetNewHeight() {return static_cast<unsigned int>(std::stoi(m_NewHeight));} |
| 236 | SupportedFrontend GetModelFormat() |
| 237 | { |
Kevin May | eb03e0f | 2021-04-28 16:16:22 +0100 | [diff] [blame] | 238 | if (m_ModelFormat == "tflite") |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 239 | { |
| 240 | return SupportedFrontend::TFLite; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | throw armnn::Exception("Unsupported model format" + m_ModelFormat); |
| 245 | } |
| 246 | } |
| 247 | armnn::DataType GetOutputType() |
| 248 | { |
| 249 | if (m_OutputType == "float") |
| 250 | { |
| 251 | return armnn::DataType::Float32; |
| 252 | } |
| 253 | else if (m_OutputType == "int") |
| 254 | { |
| 255 | return armnn::DataType::Signed32; |
| 256 | } |
| 257 | else if (m_OutputType == "qasymm8") |
| 258 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 259 | return armnn::DataType::QAsymmU8; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 260 | } |
| 261 | else |
| 262 | { |
| 263 | throw armnn::Exception("Unsupported input type" + m_OutputType); |
| 264 | } |
| 265 | } |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 266 | |
| 267 | private: |
| 268 | std::string m_InputFileName; |
| 269 | std::string m_Layout; |
| 270 | std::string m_OutputFileName; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 271 | std::string m_NewWidth; |
| 272 | std::string m_NewHeight; |
| 273 | std::string m_ModelFormat; |
| 274 | std::string m_OutputType; |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | } // namespace anonymous |
| 278 | |
| 279 | int main(int argc, char* argv[]) |
| 280 | { |
| 281 | CommandLineProcessor cmdline; |
| 282 | if (!cmdline.ProcessCommandLine(argc, argv)) |
| 283 | { |
| 284 | return -1; |
| 285 | } |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 286 | const std::string imagePath(cmdline.GetInputFileName()); |
| 287 | const std::string outputPath(cmdline.GetOutputFileName()); |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 288 | const SupportedFrontend& modelFormat(cmdline.GetModelFormat()); |
| 289 | const armnn::DataType outputType(cmdline.GetOutputType()); |
| 290 | const unsigned int newWidth = cmdline.GetNewWidth(); |
| 291 | const unsigned int newHeight = cmdline.GetNewHeight(); |
| 292 | const unsigned int batchSize = 1; |
| 293 | const armnn::DataLayout outputLayout(cmdline.GetLayout()); |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 294 | |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 295 | using TContainer = mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<uint8_t>>; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 296 | std::vector<TContainer> imageDataContainers; |
| 297 | const NormalizationParameters& normParams = GetNormalizationParameters(modelFormat, outputType); |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 298 | try |
| 299 | { |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 300 | switch (outputType) |
| 301 | { |
| 302 | case armnn::DataType::Signed32: |
| 303 | imageDataContainers.push_back(PrepareImageTensor<int>( |
| 304 | imagePath, newWidth, newHeight, normParams, batchSize, outputLayout)); |
| 305 | break; |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 306 | case armnn::DataType::QAsymmU8: |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 307 | imageDataContainers.push_back(PrepareImageTensor<uint8_t>( |
| 308 | imagePath, newWidth, newHeight, normParams, batchSize, outputLayout)); |
| 309 | break; |
| 310 | case armnn::DataType::Float32: |
| 311 | default: |
| 312 | imageDataContainers.push_back(PrepareImageTensor<float>( |
| 313 | imagePath, newWidth, newHeight, normParams, batchSize, outputLayout)); |
| 314 | break; |
| 315 | } |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 316 | } |
| 317 | catch (const InferenceTestImageException& e) |
| 318 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 319 | ARMNN_LOG(fatal) << "Failed to load image file " << imagePath << " with error: " << e.what(); |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | std::ofstream imageTensorFile; |
| 324 | imageTensorFile.open(outputPath, std::ofstream::out); |
| 325 | if (imageTensorFile.is_open()) |
| 326 | { |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 327 | mapbox::util::apply_visitor( |
| 328 | [&imageTensorFile](auto&& imageData){ WriteImageTensorImpl(imageData,imageTensorFile); }, |
| 329 | imageDataContainers[0] |
| 330 | ); |
| 331 | |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 332 | if (!imageTensorFile) |
| 333 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 334 | ARMNN_LOG(fatal) << "Failed to write to output file" << outputPath; |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 335 | imageTensorFile.close(); |
| 336 | return -1; |
| 337 | } |
| 338 | imageTensorFile.close(); |
| 339 | } |
| 340 | else |
| 341 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 342 | ARMNN_LOG(fatal) << "Failed to open output file" << outputPath; |
Sadik Armagan | 8271f81 | 2019-04-19 09:55:06 +0100 | [diff] [blame] | 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | return 0; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 347 | } |