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