54 std::string modelPath;
55 std::string modelFormat;
56 std::vector<std::string> inputNames;
57 std::vector<std::string> outputNames;
59 std::string modelOutputLabelsPath;
60 std::string validationLabelPath;
61 std::string inputLayout;
62 std::vector<armnn::BackendId> computeDevice;
63 std::string validationRange;
64 std::string blacklistPath;
66 const std::string backendsMessage =
"Which device to run layers on by default. Possible choices: " 71 cxxopts::Options options(
"ModeAccuracyTool-Armnn",
"Options");
74 (
"h,help",
"Display help messages")
76 "Path to armnn format model file",
77 cxxopts::value<std::string>(modelPath))
79 "The model format. Supported values: tflite",
80 cxxopts::value<std::string>(modelFormat))
82 "Identifier of the input tensors in the network separated by comma with no space.",
83 cxxopts::value<std::vector<std::string>>(inputNames))
85 "Identifier of the output tensors in the network separated by comma with no space.",
86 cxxopts::value<std::vector<std::string>>(outputNames))
88 "Path to directory containing the ImageNet test data",
89 cxxopts::value<std::string>(dataDir))
90 (
"p,model-output-labels",
91 "Path to model output labels file.",
92 cxxopts::value<std::string>(modelOutputLabelsPath))
93 (
"v,validation-labels-path",
94 "Path to ImageNet Validation Label file",
95 cxxopts::value<std::string>(validationLabelPath))
97 "Data layout. Supported value: NHWC, NCHW. Default: NHWC",
98 cxxopts::value<std::string>(inputLayout)->default_value(
"NHWC"))
100 backendsMessage.c_str(),
101 cxxopts::value<std::vector<armnn::BackendId>>(computeDevice)->default_value(
"CpuAcc,CpuRef"))
102 (
"r,validation-range",
103 "The range of the images to be evaluated. Specified in the form <begin index>:<end index>." 104 "The index starts at 1 and the range is inclusive." 105 "By default the evaluation will be performed on all images.",
106 cxxopts::value<std::string>(validationRange)->default_value(
"1:0"))
108 "Path to a blacklist file where each line denotes the index of an image to be " 109 "excluded from evaluation.",
110 cxxopts::value<std::string>(blacklistPath)->default_value(
""));
112 auto result = options.parse(argc, argv);
114 if (result.count(
"help") > 0)
116 std::cout << options.help() << std::endl;
121 std::string mandatorySingleParameters[] = {
"model-path",
"model-format",
"input-name",
"output-name",
122 "data-dir",
"model-output-labels",
"validation-labels-path" };
123 for (
auto param : mandatorySingleParameters)
125 if (result.count(param) != 1)
127 std::cerr <<
"Parameter \'--" << param <<
"\' is required but missing." << std::endl;
132 catch (
const cxxopts::OptionException& e)
134 std::cerr << e.what() << std::endl << std::endl;
137 catch (
const std::exception& e)
143 std::cerr <<
"Fatal internal error: " << e.what() << std::endl;
148 std::string invalidBackends;
151 ARMNN_LOG(fatal) <<
"The list of preferred devices contains invalid backend IDs: " 160 std::ifstream file(modelPath);
164 auto armnnparser(IParser::Create());
173 optimizedNet =
armnn::Optimize(*network, computeDevice, runtime->GetDeviceSpec());
177 std::stringstream message;
178 message <<
"armnn::Exception (" << e.
what() <<
") caught from optimize.";
185 status = runtime->LoadNetwork(networkId, std::move(optimizedNet));
188 ARMNN_LOG(fatal) <<
"armnn::IRuntime: Failed to load network";
196 std::vector<BindingPointInfo> inputBindings;
197 for(
auto& input: inputNames)
200 inputBindingInfo = armnnparser->GetNetworkInputBindingInfo(0, input);
202 std::pair<armnn::LayerBindingId, armnn::TensorInfo>
204 inputBindings.push_back(m_InputBindingInfo);
207 std::vector<BindingPointInfo> outputBindings;
208 for(
auto& output: outputNames)
211 outputBindingInfo = armnnparser->GetNetworkOutputBindingInfo(0, output);
213 std::pair<armnn::LayerBindingId, armnn::TensorInfo>
215 outputBindings.push_back(m_OutputBindingInfo);
219 if (modelOutputLabelsPath.empty() || !fs::exists(modelOutputLabelsPath) ||
220 !fs::is_regular_file(modelOutputLabelsPath))
222 ARMNN_LOG(fatal) <<
"Invalid model output labels path at " << modelOutputLabelsPath;
224 const std::vector<armnnUtils::LabelCategoryNames> modelOutputLabels =
229 size_t imageBegIndex;
230 size_t imageEndIndex;
231 if (imageIndexStrs.size() != 2)
233 ARMNN_LOG(fatal) <<
"Invalid validation range specification: Invalid format " << validationRange;
238 imageBegIndex = std::stoul(imageIndexStrs[0]);
239 imageEndIndex = std::stoul(imageIndexStrs[1]);
241 catch (
const std::exception& e)
243 ARMNN_LOG(fatal) <<
"Invalid validation range specification: " << validationRange;
248 if (!blacklistPath.empty() &&
249 !(fs::exists(blacklistPath) && fs::is_regular_file(blacklistPath)))
251 ARMNN_LOG(fatal) <<
"Invalid path to blacklist file at " << blacklistPath;
255 fs::path pathToDataDir(dataDir);
257 validationLabelPath, pathToDataDir.string(), imageBegIndex, imageEndIndex, blacklistPath);
259 using TContainer = mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<uint8_t>>;
270 std::begin(inputNames),
271 std::end(inputNames));
273 std::begin(outputNames),
274 std::end(outputNames));
285 if (inputLayout ==
"NCHW")
289 else if (inputLayout ==
"NHWC")
295 ARMNN_LOG(fatal) <<
"Invalid Data layout: " << inputLayout;
298 const unsigned int inputTensorWidth =
300 const unsigned int inputTensorHeight =
303 const unsigned int outputNumElements = model.GetOutputSize();
305 if (modelOutputLabels.size() != outputNumElements)
307 ARMNN_LOG(fatal) <<
"Number of output elements: " << outputNumElements
308 <<
" , mismatches the number of output labels: " << modelOutputLabels.size();
312 const unsigned int batchSize = 1;
315 if (modelFormat ==
"tflite")
321 ARMNN_LOG(fatal) <<
"Unsupported frontend: " << modelFormat;
325 for (
const auto& imageEntry : imageNameToLabel)
327 const std::string imageName = imageEntry.first;
328 std::cout <<
"Processing image: " << imageName <<
"\n";
330 vector<TContainer> inputDataContainers;
331 vector<TContainer> outputDataContainers;
333 auto imagePath = pathToDataDir / fs::path(imageName);
334 switch (inputTensorDataType)
337 inputDataContainers.push_back(
339 inputTensorWidth, inputTensorHeight,
342 inputTensorDataLayout));
343 outputDataContainers = { vector<int>(outputNumElements) };
346 inputDataContainers.push_back(
348 inputTensorWidth, inputTensorHeight,
351 inputTensorDataLayout));
352 outputDataContainers = { vector<uint8_t>(outputNumElements) };
356 inputDataContainers.push_back(
358 inputTensorWidth, inputTensorHeight,
361 inputTensorDataLayout));
362 outputDataContainers = { vector<float>(outputNumElements) };
366 status = runtime->EnqueueWorkload(networkId,
372 ARMNN_LOG(fatal) <<
"armnn::IRuntime: Failed to enqueue workload for image: " << imageName;
375 checker.AddImageResult<
TContainer>(imageName, outputDataContainers);
383 for(
unsigned int i = 1; i <= 5; ++i)
385 std::cout <<
"Top " << i <<
" Accuracy: " << checker.GetAccuracy(i) <<
"%" <<
"\n";
388 ARMNN_LOG(info) <<
"Accuracy Tool ran successfully!";
396 std::cerr <<
"Armnn Error: " << e.
what() << std::endl;
399 catch (
const std::exception& e)
402 std::cerr <<
"WARNING: ModelAccuracyTool-Armnn: An error has occurred when running the " 403 "Accuracy Tool: " << e.what() << std::endl;
static IRuntimePtr Create(const CreationOptions &options)
const TensorShape & GetShape() const
void ConfigureLogging(bool printToStandardOutput, bool printToDebugOutput, LogSeverity severity)
Configures the logging behaviour of the ARMNN library.
NormalizationParameters GetNormalizationParameters(const SupportedFrontend &modelFormat, const armnn::DataType &outputType)
Get normalization parameters.
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
virtual const char * what() const noexcept override
#define ARMNN_LOG(severity)
BackendRegistry & BackendRegistryInstance()
std::vector< uint8_t > PrepareImageTensor< uint8_t >(const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)
armnn::BindingPointInfo BindingPointInfo
std::string GetBackendIdsAsString() const
std::vector< std::string > m_InputBindings
armnn::InputTensors MakeInputTensors(const std::vector< armnn::BindingPointInfo > &inputBindings, const std::vector< TContainer > &inputDataContainers)
std::vector< std::string > SplitBy(const std::string &originalString, const std::string &delimiter, bool includeEmptyToken)
Split a string into tokens by a delimiter.
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
#define ARMNN_ASSERT_MSG(COND, MSG)
std::vector< std::string > m_OutputBindings
std::vector< armnn::BackendId > m_ComputeDevices
DataType GetDataType() const
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
std::vector< int > PrepareImageTensor< int >(const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)
armnn::OutputTensors MakeOutputTensors(const std::vector< armnn::BindingPointInfo > &outputBindings, std::vector< TContainer > &outputDataContainers)
std::pair< armnn::LayerBindingId, armnn::TensorInfo > BindingPointInfo
mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char >, std::vector< int8_t > > TContainer
armnn::TensorInfo m_TensorInfo
Base class for all ArmNN exceptions so that users can filter to just those.
bool ValidateDirectory(std::string &dir)
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
armnn::LayerBindingId m_BindingId
std::vector< float > PrepareImageTensor< float >(const std::string &imagePath, unsigned int newWidth, unsigned int newHeight, const NormalizationParameters &normParams, unsigned int batchSize, const armnn::DataLayout &outputLayout)