52 using namespace boost::filesystem;
57 namespace po = boost::program_options;
59 std::vector<armnn::BackendId> computeDevice;
61 std::string modelPath;
62 std::string modelFormat;
64 std::string inputName;
65 std::string inputLayout;
66 std::string outputName;
67 std::string modelOutputLabelsPath;
68 std::string validationLabelPath;
69 std::string validationRange;
70 std::string blacklistPath;
72 const std::string backendsMessage =
"Which device to run layers on by default. Possible choices: " 75 po::options_description desc(
"Options");
80 (
"help,h",
"Display help messages")
81 (
"model-path,m", po::value<std::string>(&modelPath)->required(),
"Path to armnn format model file")
82 (
"model-format,f", po::value<std::string>(&modelFormat)->required(),
83 "The model format. Supported values: caffe, tensorflow, tflite")
84 (
"input-name,i", po::value<std::string>(&inputName)->required(),
85 "Identifier of the input tensors in the network separated by comma.")
86 (
"output-name,o", po::value<std::string>(&outputName)->required(),
87 "Identifier of the output tensors in the network separated by comma.")
88 (
"data-dir,d", po::value<std::string>(&dataDir)->required(),
89 "Path to directory containing the ImageNet test data")
90 (
"model-output-labels,p", po::value<std::string>(&modelOutputLabelsPath)->required(),
91 "Path to model output labels file.")
92 (
"validation-labels-path,v", po::value<std::string>(&validationLabelPath)->required(),
93 "Path to ImageNet Validation Label file")
94 (
"data-layout,l", po::value<std::string>(&inputLayout)->default_value(
"NHWC"),
95 "Data layout. Supported value: NHWC, NCHW. Default: NHWC")
96 (
"compute,c", po::value<std::vector<armnn::BackendId>>(&computeDevice)->default_value(defaultBackends),
97 backendsMessage.c_str())
98 (
"validation-range,r", po::value<std::string>(&validationRange)->default_value(
"1:0"),
99 "The range of the images to be evaluated. Specified in the form <begin index>:<end index>." 100 "The index starts at 1 and the range is inclusive." 101 "By default the evaluation will be performed on all images.")
102 (
"blacklist-path,b", po::value<std::string>(&blacklistPath)->default_value(
""),
103 "Path to a blacklist file where each line denotes the index of an image to be " 104 "excluded from evaluation.");
106 catch (
const std::exception& e)
112 std::cerr <<
"Fatal internal error: " << e.what() << std::endl;
116 po::variables_map vm;
119 po::store(po::parse_command_line(argc, argv, desc), vm);
121 if (vm.count(
"help"))
123 std::cout << desc << std::endl;
130 std::cerr << e.what() << std::endl << std::endl;
131 std::cerr << desc << std::endl;
136 std::string invalidBackends;
139 ARMNN_LOG(fatal) <<
"The list of preferred devices contains invalid backend IDs: " 148 std::ifstream file(modelPath);
152 auto armnnparser(IParser::Create());
161 optimizedNet =
armnn::Optimize(*network, computeDevice, runtime->GetDeviceSpec());
165 std::stringstream message;
166 message <<
"armnn::Exception (" << e.
what() <<
") caught from optimize.";
173 status = runtime->LoadNetwork(networkId, std::move(optimizedNet));
176 ARMNN_LOG(fatal) <<
"armnn::IRuntime: Failed to load network";
184 inputBindingInfo = armnnparser->GetNetworkInputBindingInfo(0, inputName);
186 std::pair<armnn::LayerBindingId, armnn::TensorInfo>
188 std::vector<BindingPointInfo> inputBindings = { m_InputBindingInfo };
191 outputBindingInfo = armnnparser->GetNetworkOutputBindingInfo(0, outputName);
193 std::pair<armnn::LayerBindingId, armnn::TensorInfo>
195 std::vector<BindingPointInfo> outputBindings = { m_OutputBindingInfo };
198 if (modelOutputLabelsPath.empty() || !boost::filesystem::exists(modelOutputLabelsPath) ||
199 !boost::filesystem::is_regular_file(modelOutputLabelsPath))
201 ARMNN_LOG(fatal) <<
"Invalid model output labels path at " << modelOutputLabelsPath;
203 const std::vector<armnnUtils::LabelCategoryNames> modelOutputLabels =
208 size_t imageBegIndex;
209 size_t imageEndIndex;
210 if (imageIndexStrs.size() != 2)
212 ARMNN_LOG(fatal) <<
"Invalid validation range specification: Invalid format " << validationRange;
217 imageBegIndex = std::stoul(imageIndexStrs[0]);
218 imageEndIndex = std::stoul(imageIndexStrs[1]);
220 catch (
const std::exception& e)
222 ARMNN_LOG(fatal) <<
"Invalid validation range specification: " << validationRange;
227 if (!blacklistPath.empty() &&
228 !(boost::filesystem::exists(blacklistPath) && boost::filesystem::is_regular_file(blacklistPath)))
230 ARMNN_LOG(fatal) <<
"Invalid path to blacklist file at " << blacklistPath;
234 path pathToDataDir(dataDir);
236 validationLabelPath, pathToDataDir.string(), imageBegIndex, imageEndIndex, blacklistPath);
238 using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<uint8_t>>;
256 if (inputLayout ==
"NCHW")
260 else if (inputLayout ==
"NHWC")
266 ARMNN_LOG(fatal) <<
"Invalid Data layout: " << inputLayout;
269 const unsigned int inputTensorWidth =
271 const unsigned int inputTensorHeight =
274 const unsigned int outputNumElements = model.GetOutputSize();
276 if (modelOutputLabels.size() != outputNumElements)
278 ARMNN_LOG(fatal) <<
"Number of output elements: " << outputNumElements
279 <<
" , mismatches the number of output labels: " << modelOutputLabels.size();
283 const unsigned int batchSize = 1;
286 if (modelFormat ==
"caffe")
290 else if (modelFormat ==
"tensorflow")
294 else if (modelFormat ==
"tflite")
300 ARMNN_LOG(fatal) <<
"Unsupported frontend: " << modelFormat;
304 for (
const auto& imageEntry : imageNameToLabel)
306 const std::string imageName = imageEntry.first;
307 std::cout <<
"Processing image: " << imageName <<
"\n";
309 vector<TContainer> inputDataContainers;
310 vector<TContainer> outputDataContainers;
312 auto imagePath = pathToDataDir / boost::filesystem::path(imageName);
313 switch (inputTensorDataType)
316 inputDataContainers.push_back(
318 inputTensorWidth, inputTensorHeight,
321 inputTensorDataLayout));
322 outputDataContainers = { vector<int>(outputNumElements) };
325 inputDataContainers.push_back(
327 inputTensorWidth, inputTensorHeight,
330 inputTensorDataLayout));
331 outputDataContainers = { vector<uint8_t>(outputNumElements) };
335 inputDataContainers.push_back(
337 inputTensorWidth, inputTensorHeight,
340 inputTensorDataLayout));
341 outputDataContainers = { vector<float>(outputNumElements) };
345 status = runtime->EnqueueWorkload(networkId,
351 ARMNN_LOG(fatal) <<
"armnn::IRuntime: Failed to enqueue workload for image: " << imageName;
354 checker.AddImageResult<
TContainer>(imageName, outputDataContainers);
362 for(
unsigned int i = 1; i <= 5; ++i)
364 std::cout <<
"Top " << i <<
" Accuracy: " << checker.GetAccuracy(i) <<
"%" <<
"\n";
367 ARMNN_LOG(info) <<
"Accuracy Tool ran successfully!";
375 std::cerr <<
"Armnn Error: " << e.
what() << std::endl;
378 catch (
const std::exception & e)
381 std::cerr <<
"WARNING: ModelAccuracyTool-Armnn: An error has occurred when running the " 382 "Accuracy Tool: " << e.what() << std::endl;
static IRuntimePtr Create(const CreationOptions &options)
const TensorShape & GetShape() const
CPU Execution: Reference C++ kernels.
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
boost::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char > > TContainer
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
std::vector< armnn::BackendId > defaultBackends
armnn::TensorInfo m_TensorInfo
Base class for all ArmNN exceptions so that users can filter to just those.
CPU Execution: NEON: ArmCompute.
bool ValidateDirectory(std::string &dir)
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
armnn::LayerBindingId m_BindingId
armnn::Runtime::CreationOptions::ExternalProfilingOptions options
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)