56 std::string modelPath;
57 std::string modelFormat;
58 std::vector<std::string> inputNames;
59 std::vector<std::string> outputNames;
61 std::string modelOutputLabelsPath;
62 std::string validationLabelPath;
63 std::string inputLayout;
64 std::vector<armnn::BackendId> computeDevice;
65 std::string validationRange;
66 std::string blacklistPath;
68 const std::string backendsMessage =
"Which device to run layers on by default. Possible choices: " 73 cxxopts::Options options(
"ModeAccuracyTool-Armnn",
"Options");
76 (
"h,help",
"Display help messages")
78 "Path to armnn format model file",
79 cxxopts::value<std::string>(modelPath))
81 "The model format. Supported values: tflite",
82 cxxopts::value<std::string>(modelFormat))
84 "Identifier of the input tensors in the network separated by comma with no space.",
85 cxxopts::value<std::vector<std::string>>(inputNames))
87 "Identifier of the output tensors in the network separated by comma with no space.",
88 cxxopts::value<std::vector<std::string>>(outputNames))
90 "Path to directory containing the ImageNet test data",
91 cxxopts::value<std::string>(dataDir))
92 (
"p,model-output-labels",
93 "Path to model output labels file.",
94 cxxopts::value<std::string>(modelOutputLabelsPath))
95 (
"v,validation-labels-path",
96 "Path to ImageNet Validation Label file",
97 cxxopts::value<std::string>(validationLabelPath))
99 "Data layout. Supported value: NHWC, NCHW. Default: NHWC",
100 cxxopts::value<std::string>(inputLayout)->default_value(
"NHWC"))
102 backendsMessage.c_str(),
103 cxxopts::value<std::vector<armnn::BackendId>>(computeDevice)->default_value(
"CpuAcc,CpuRef"))
104 (
"r,validation-range",
105 "The range of the images to be evaluated. Specified in the form <begin index>:<end index>." 106 "The index starts at 1 and the range is inclusive." 107 "By default the evaluation will be performed on all images.",
108 cxxopts::value<std::string>(validationRange)->default_value(
"1:0"))
110 "Path to a blacklist file where each line denotes the index of an image to be " 111 "excluded from evaluation.",
112 cxxopts::value<std::string>(blacklistPath)->default_value(
""));
114 auto result = options.parse(argc, argv);
116 if (result.count(
"help") > 0)
118 std::cout << options.help() << std::endl;
123 std::string mandatorySingleParameters[] = {
"model-path",
"model-format",
"input-name",
"output-name",
124 "data-dir",
"model-output-labels",
"validation-labels-path" };
125 for (
auto param : mandatorySingleParameters)
127 if (result.count(param) != 1)
129 std::cerr <<
"Parameter \'--" << param <<
"\' is required but missing." << std::endl;
134 catch (
const cxxopts::OptionException& e)
136 std::cerr << e.what() << std::endl << std::endl;
139 catch (
const std::exception& e)
142 std::cerr <<
"Fatal internal error: " << e.what() << std::endl;
147 std::string invalidBackends;
150 ARMNN_LOG(fatal) <<
"The list of preferred devices contains invalid backend IDs: " 159 std::ifstream file(modelPath);
163 auto armnnparser(IParser::Create());
172 optimizedNet =
armnn::Optimize(*network, computeDevice, runtime->GetDeviceSpec());
176 std::stringstream message;
177 message <<
"armnn::Exception (" << e.
what() <<
") caught from optimize.";
184 status = runtime->LoadNetwork(networkId, std::move(optimizedNet));
187 ARMNN_LOG(fatal) <<
"armnn::IRuntime: Failed to load network";
195 std::vector<BindingPointInfo> inputBindings;
196 for(
auto& input: inputNames)
199 inputBindingInfo = armnnparser->GetNetworkInputBindingInfo(0, input);
201 std::pair<armnn::LayerBindingId, armnn::TensorInfo>
203 inputBindings.push_back(m_InputBindingInfo);
206 std::vector<BindingPointInfo> outputBindings;
207 for(
auto& output: outputNames)
210 outputBindingInfo = armnnparser->GetNetworkOutputBindingInfo(0, output);
212 std::pair<armnn::LayerBindingId, armnn::TensorInfo>
214 outputBindings.push_back(m_OutputBindingInfo);
218 if (modelOutputLabelsPath.empty() || !fs::exists(modelOutputLabelsPath) ||
219 !fs::is_regular_file(modelOutputLabelsPath))
221 ARMNN_LOG(fatal) <<
"Invalid model output labels path at " << modelOutputLabelsPath;
223 const std::vector<armnnUtils::LabelCategoryNames> modelOutputLabels =
228 size_t imageBegIndex;
229 size_t imageEndIndex;
230 if (imageIndexStrs.size() != 2)
232 ARMNN_LOG(fatal) <<
"Invalid validation range specification: Invalid format " << validationRange;
237 imageBegIndex = std::stoul(imageIndexStrs[0]);
238 imageEndIndex = std::stoul(imageIndexStrs[1]);
240 catch (
const std::exception& e)
242 ARMNN_LOG(fatal) <<
"Invalid validation range specification: " << validationRange;
247 if (!blacklistPath.empty() &&
248 !(fs::exists(blacklistPath) && fs::is_regular_file(blacklistPath)))
250 ARMNN_LOG(fatal) <<
"Invalid path to blacklist file at " << blacklistPath;
254 fs::path pathToDataDir(dataDir);
256 validationLabelPath, pathToDataDir.string(), imageBegIndex, imageEndIndex, blacklistPath);
268 std::begin(inputNames),
269 std::end(inputNames));
271 std::begin(outputNames),
272 std::end(outputNames));
283 if (inputLayout ==
"NCHW")
287 else if (inputLayout ==
"NHWC")
293 ARMNN_LOG(fatal) <<
"Invalid Data layout: " << inputLayout;
296 const unsigned int inputTensorWidth =
298 const unsigned int inputTensorHeight =
301 const unsigned int outputNumElements = model.GetOutputSize();
303 if (modelOutputLabels.size() != outputNumElements)
305 ARMNN_LOG(fatal) <<
"Number of output elements: " << outputNumElements
306 <<
" , mismatches the number of output labels: " << modelOutputLabels.size();
310 const unsigned int batchSize = 1;
313 if (modelFormat ==
"tflite")
319 ARMNN_LOG(fatal) <<
"Unsupported frontend: " << modelFormat;
323 for (
const auto& imageEntry : imageNameToLabel)
325 const std::string imageName = imageEntry.first;
326 std::cout <<
"Processing image: " << imageName <<
"\n";
328 vector<armnnUtils::TContainer> inputDataContainers;
329 vector<armnnUtils::TContainer> outputDataContainers;
331 auto imagePath = pathToDataDir / fs::path(imageName);
332 switch (inputTensorDataType)
335 inputDataContainers.push_back(
337 inputTensorWidth, inputTensorHeight,
340 inputTensorDataLayout));
341 outputDataContainers = { vector<int>(outputNumElements) };
344 inputDataContainers.push_back(
346 inputTensorWidth, inputTensorHeight,
349 inputTensorDataLayout));
350 outputDataContainers = { vector<uint8_t>(outputNumElements) };
354 inputDataContainers.push_back(
356 inputTensorWidth, inputTensorHeight,
359 inputTensorDataLayout));
360 outputDataContainers = { vector<float>(outputNumElements) };
364 status = runtime->EnqueueWorkload(networkId,
370 ARMNN_LOG(fatal) <<
"armnn::IRuntime: Failed to enqueue workload for image: " << imageName;
381 for(
unsigned int i = 1; i <= 5; ++i)
383 std::cout <<
"Top " << i <<
" Accuracy: " << checker.GetAccuracy(i) <<
"%" <<
"\n";
386 ARMNN_LOG(info) <<
"Accuracy Tool ran successfully!";
394 std::cerr <<
"Armnn Error: " << e.
what() << std::endl;
397 catch (
const std::exception& e)
400 std::cerr <<
"WARNING: ModelAccuracyTool-Armnn: An error has occurred when running the " 401 "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
armnn::TensorInfo m_TensorInfo
Base class for all ArmNN exceptions so that users can filter to just those.
mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char >, std::vector< int8_t > > TContainer
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)