Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +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/ImageTensorGenerator.hpp" |
| 7 | #include "../InferenceTest.hpp" |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 8 | #include "ModelAccuracyChecker.hpp" |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 9 | #include "armnnDeserializer/IDeserializer.hpp" |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 10 | #include <Filesystem.hpp> |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 11 | |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 12 | #include <boost/program_options/variables_map.hpp> |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 13 | #include <boost/range/iterator_range.hpp> |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 14 | #include <map> |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 15 | |
| 16 | using namespace armnn::test; |
| 17 | |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 18 | /** Load image names and ground-truth labels from the image directory and the ground truth label file |
| 19 | * |
| 20 | * @pre \p validationLabelPath exists and is valid regular file |
| 21 | * @pre \p imageDirectoryPath exists and is valid directory |
| 22 | * @pre labels in validation file correspond to images which are in lexicographical order with the image name |
| 23 | * @pre image index starts at 1 |
| 24 | * @pre \p begIndex and \p endIndex are end-inclusive |
| 25 | * |
| 26 | * @param[in] validationLabelPath Path to validation label file |
| 27 | * @param[in] imageDirectoryPath Path to directory containing validation images |
| 28 | * @param[in] begIndex Begin index of images to be loaded. Inclusive |
| 29 | * @param[in] endIndex End index of images to be loaded. Inclusive |
| 30 | * @param[in] blacklistPath Path to blacklist file |
| 31 | * @return A map mapping image file names to their corresponding ground-truth labels |
| 32 | */ |
| 33 | map<std::string, std::string> LoadValidationImageFilenamesAndLabels(const string& validationLabelPath, |
| 34 | const string& imageDirectoryPath, |
| 35 | size_t begIndex = 0, |
| 36 | size_t endIndex = 0, |
| 37 | const string& blacklistPath = ""); |
| 38 | |
| 39 | /** Load model output labels from file |
| 40 | * |
| 41 | * @pre \p modelOutputLabelsPath exists and is a regular file |
| 42 | * |
| 43 | * @param[in] modelOutputLabelsPath path to model output labels file |
| 44 | * @return A vector of labels, which in turn is described by a list of category names |
| 45 | */ |
| 46 | std::vector<armnnUtils::LabelCategoryNames> LoadModelOutputLabels(const std::string& modelOutputLabelsPath); |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 47 | |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 48 | int main(int argc, char* argv[]) |
| 49 | { |
| 50 | try |
| 51 | { |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 52 | armnn::LogSeverity level = armnn::LogSeverity::Debug; |
| 53 | armnn::ConfigureLogging(true, true, level); |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 54 | |
| 55 | // Set-up program Options |
| 56 | namespace po = boost::program_options; |
| 57 | |
| 58 | std::vector<armnn::BackendId> computeDevice; |
| 59 | std::vector<armnn::BackendId> defaultBackends = {armnn::Compute::CpuAcc, armnn::Compute::CpuRef}; |
| 60 | std::string modelPath; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 61 | std::string modelFormat; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 62 | std::string dataDir; |
| 63 | std::string inputName; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 64 | std::string inputLayout; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 65 | std::string outputName; |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 66 | std::string modelOutputLabelsPath; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 67 | std::string validationLabelPath; |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 68 | std::string validationRange; |
| 69 | std::string blacklistPath; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 70 | |
| 71 | const std::string backendsMessage = "Which device to run layers on by default. Possible choices: " |
| 72 | + armnn::BackendRegistryInstance().GetBackendIdsAsString(); |
| 73 | |
| 74 | po::options_description desc("Options"); |
| 75 | try |
| 76 | { |
| 77 | // Adds generic options needed to run Accuracy Tool. |
| 78 | desc.add_options() |
Conor Kennedy | 3056202 | 2019-05-13 14:48:58 +0100 | [diff] [blame] | 79 | ("help,h", "Display help messages") |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 80 | ("model-path,m", po::value<std::string>(&modelPath)->required(), "Path to armnn format model file") |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 81 | ("model-format,f", po::value<std::string>(&modelFormat)->required(), |
| 82 | "The model format. Supported values: caffe, tensorflow, tflite") |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 83 | ("input-name,i", po::value<std::string>(&inputName)->required(), |
| 84 | "Identifier of the input tensors in the network separated by comma.") |
| 85 | ("output-name,o", po::value<std::string>(&outputName)->required(), |
| 86 | "Identifier of the output tensors in the network separated by comma.") |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 87 | ("data-dir,d", po::value<std::string>(&dataDir)->required(), |
| 88 | "Path to directory containing the ImageNet test data") |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 89 | ("model-output-labels,p", po::value<std::string>(&modelOutputLabelsPath)->required(), |
| 90 | "Path to model output labels file.") |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 91 | ("validation-labels-path,v", po::value<std::string>(&validationLabelPath)->required(), |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 92 | "Path to ImageNet Validation Label file") |
| 93 | ("data-layout,l", po::value<std::string>(&inputLayout)->default_value("NHWC"), |
SiCong Li | 23700bb | 2019-07-25 14:54:39 +0100 | [diff] [blame] | 94 | "Data layout. Supported value: NHWC, NCHW. Default: NHWC") |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 95 | ("compute,c", po::value<std::vector<armnn::BackendId>>(&computeDevice)->default_value(defaultBackends), |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 96 | backendsMessage.c_str()) |
| 97 | ("validation-range,r", po::value<std::string>(&validationRange)->default_value("1:0"), |
| 98 | "The range of the images to be evaluated. Specified in the form <begin index>:<end index>." |
| 99 | "The index starts at 1 and the range is inclusive." |
| 100 | "By default the evaluation will be performed on all images.") |
| 101 | ("blacklist-path,b", po::value<std::string>(&blacklistPath)->default_value(""), |
| 102 | "Path to a blacklist file where each line denotes the index of an image to be " |
| 103 | "excluded from evaluation."); |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 104 | } |
| 105 | catch (const std::exception& e) |
| 106 | { |
| 107 | // Coverity points out that default_value(...) can throw a bad_lexical_cast, |
| 108 | // and that desc.add_options() can throw boost::io::too_few_args. |
| 109 | // They really won't in any of these cases. |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 110 | ARMNN_ASSERT_MSG(false, "Caught unexpected exception"); |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 111 | std::cerr << "Fatal internal error: " << e.what() << std::endl; |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | po::variables_map vm; |
| 116 | try |
| 117 | { |
| 118 | po::store(po::parse_command_line(argc, argv, desc), vm); |
| 119 | |
| 120 | if (vm.count("help")) |
| 121 | { |
| 122 | std::cout << desc << std::endl; |
| 123 | return 1; |
| 124 | } |
| 125 | po::notify(vm); |
| 126 | } |
| 127 | catch (po::error& e) |
| 128 | { |
| 129 | std::cerr << e.what() << std::endl << std::endl; |
| 130 | std::cerr << desc << std::endl; |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | // Check if the requested backend are all valid |
| 135 | std::string invalidBackends; |
| 136 | if (!CheckRequestedBackendsAreValid(computeDevice, armnn::Optional<std::string&>(invalidBackends))) |
| 137 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 138 | ARMNN_LOG(fatal) << "The list of preferred devices contains invalid backend IDs: " |
| 139 | << invalidBackends; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 140 | return EXIT_FAILURE; |
| 141 | } |
| 142 | armnn::Status status; |
| 143 | |
| 144 | // Create runtime |
| 145 | armnn::IRuntime::CreationOptions options; |
| 146 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 147 | std::ifstream file(modelPath); |
| 148 | |
| 149 | // Create Parser |
| 150 | using IParser = armnnDeserializer::IDeserializer; |
| 151 | auto armnnparser(IParser::Create()); |
| 152 | |
| 153 | // Create a network |
| 154 | armnn::INetworkPtr network = armnnparser->CreateNetworkFromBinary(file); |
| 155 | |
| 156 | // Optimizes the network. |
| 157 | armnn::IOptimizedNetworkPtr optimizedNet(nullptr, nullptr); |
| 158 | try |
| 159 | { |
| 160 | optimizedNet = armnn::Optimize(*network, computeDevice, runtime->GetDeviceSpec()); |
| 161 | } |
Pavel Macenauer | 855a47b | 2020-05-26 10:54:22 +0000 | [diff] [blame] | 162 | catch (const armnn::Exception& e) |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 163 | { |
| 164 | std::stringstream message; |
| 165 | message << "armnn::Exception (" << e.what() << ") caught from optimize."; |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 166 | ARMNN_LOG(fatal) << message.str(); |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 167 | return 1; |
| 168 | } |
| 169 | |
| 170 | // Loads the network into the runtime. |
| 171 | armnn::NetworkId networkId; |
| 172 | status = runtime->LoadNetwork(networkId, std::move(optimizedNet)); |
| 173 | if (status == armnn::Status::Failure) |
| 174 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 175 | ARMNN_LOG(fatal) << "armnn::IRuntime: Failed to load network"; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | // Set up Network |
| 180 | using BindingPointInfo = InferenceModelInternal::BindingPointInfo; |
| 181 | |
| 182 | const armnnDeserializer::BindingPointInfo& |
| 183 | inputBindingInfo = armnnparser->GetNetworkInputBindingInfo(0, inputName); |
| 184 | |
| 185 | std::pair<armnn::LayerBindingId, armnn::TensorInfo> |
| 186 | m_InputBindingInfo(inputBindingInfo.m_BindingId, inputBindingInfo.m_TensorInfo); |
| 187 | std::vector<BindingPointInfo> inputBindings = { m_InputBindingInfo }; |
| 188 | |
| 189 | const armnnDeserializer::BindingPointInfo& |
| 190 | outputBindingInfo = armnnparser->GetNetworkOutputBindingInfo(0, outputName); |
| 191 | |
| 192 | std::pair<armnn::LayerBindingId, armnn::TensorInfo> |
| 193 | m_OutputBindingInfo(outputBindingInfo.m_BindingId, outputBindingInfo.m_TensorInfo); |
| 194 | std::vector<BindingPointInfo> outputBindings = { m_OutputBindingInfo }; |
| 195 | |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 196 | // Load model output labels |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 197 | if (modelOutputLabelsPath.empty() || !fs::exists(modelOutputLabelsPath) || |
| 198 | !fs::is_regular_file(modelOutputLabelsPath)) |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 199 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 200 | ARMNN_LOG(fatal) << "Invalid model output labels path at " << modelOutputLabelsPath; |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 201 | } |
| 202 | const std::vector<armnnUtils::LabelCategoryNames> modelOutputLabels = |
| 203 | LoadModelOutputLabels(modelOutputLabelsPath); |
| 204 | |
| 205 | // Parse begin and end image indices |
| 206 | std::vector<std::string> imageIndexStrs = armnnUtils::SplitBy(validationRange, ":"); |
| 207 | size_t imageBegIndex; |
| 208 | size_t imageEndIndex; |
| 209 | if (imageIndexStrs.size() != 2) |
| 210 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 211 | ARMNN_LOG(fatal) << "Invalid validation range specification: Invalid format " << validationRange; |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 212 | return 1; |
| 213 | } |
| 214 | try |
| 215 | { |
| 216 | imageBegIndex = std::stoul(imageIndexStrs[0]); |
| 217 | imageEndIndex = std::stoul(imageIndexStrs[1]); |
| 218 | } |
| 219 | catch (const std::exception& e) |
| 220 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 221 | ARMNN_LOG(fatal) << "Invalid validation range specification: " << validationRange; |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | // Validate blacklist file if it's specified |
| 226 | if (!blacklistPath.empty() && |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 227 | !(fs::exists(blacklistPath) && fs::is_regular_file(blacklistPath))) |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 228 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 229 | ARMNN_LOG(fatal) << "Invalid path to blacklist file at " << blacklistPath; |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 230 | return 1; |
| 231 | } |
| 232 | |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 233 | path pathToDataDir(dataDir); |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 234 | const map<std::string, std::string> imageNameToLabel = LoadValidationImageFilenamesAndLabels( |
| 235 | validationLabelPath, pathToDataDir.string(), imageBegIndex, imageEndIndex, blacklistPath); |
| 236 | armnnUtils::ModelAccuracyChecker checker(imageNameToLabel, modelOutputLabels); |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 237 | using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<uint8_t>>; |
| 238 | |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 239 | if (ValidateDirectory(dataDir)) |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 240 | { |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 241 | InferenceModel<armnnDeserializer::IDeserializer, float>::Params params; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 242 | params.m_ModelPath = modelPath; |
| 243 | params.m_IsModelBinary = true; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 244 | params.m_ComputeDevices = computeDevice; |
| 245 | params.m_InputBindings.push_back(inputName); |
| 246 | params.m_OutputBindings.push_back(outputName); |
| 247 | |
| 248 | using TParser = armnnDeserializer::IDeserializer; |
| 249 | InferenceModel<TParser, float> model(params, false); |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 250 | // Get input tensor information |
| 251 | const armnn::TensorInfo& inputTensorInfo = model.GetInputBindingInfo().second; |
| 252 | const armnn::TensorShape& inputTensorShape = inputTensorInfo.GetShape(); |
| 253 | const armnn::DataType& inputTensorDataType = inputTensorInfo.GetDataType(); |
| 254 | armnn::DataLayout inputTensorDataLayout; |
| 255 | if (inputLayout == "NCHW") |
| 256 | { |
| 257 | inputTensorDataLayout = armnn::DataLayout::NCHW; |
| 258 | } |
| 259 | else if (inputLayout == "NHWC") |
| 260 | { |
| 261 | inputTensorDataLayout = armnn::DataLayout::NHWC; |
| 262 | } |
| 263 | else |
| 264 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 265 | ARMNN_LOG(fatal) << "Invalid Data layout: " << inputLayout; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 266 | return 1; |
| 267 | } |
| 268 | const unsigned int inputTensorWidth = |
| 269 | inputTensorDataLayout == armnn::DataLayout::NCHW ? inputTensorShape[3] : inputTensorShape[2]; |
| 270 | const unsigned int inputTensorHeight = |
| 271 | inputTensorDataLayout == armnn::DataLayout::NCHW ? inputTensorShape[2] : inputTensorShape[1]; |
SiCong Li | c0ed7ba | 2019-06-21 16:02:40 +0100 | [diff] [blame] | 272 | // Get output tensor info |
| 273 | const unsigned int outputNumElements = model.GetOutputSize(); |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 274 | // Check output tensor shape is valid |
| 275 | if (modelOutputLabels.size() != outputNumElements) |
| 276 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 277 | ARMNN_LOG(fatal) << "Number of output elements: " << outputNumElements |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 278 | << " , mismatches the number of output labels: " << modelOutputLabels.size(); |
| 279 | return 1; |
| 280 | } |
SiCong Li | c0ed7ba | 2019-06-21 16:02:40 +0100 | [diff] [blame] | 281 | |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 282 | const unsigned int batchSize = 1; |
| 283 | // Get normalisation parameters |
| 284 | SupportedFrontend modelFrontend; |
| 285 | if (modelFormat == "caffe") |
| 286 | { |
| 287 | modelFrontend = SupportedFrontend::Caffe; |
| 288 | } |
| 289 | else if (modelFormat == "tensorflow") |
| 290 | { |
| 291 | modelFrontend = SupportedFrontend::TensorFlow; |
| 292 | } |
| 293 | else if (modelFormat == "tflite") |
| 294 | { |
| 295 | modelFrontend = SupportedFrontend::TFLite; |
| 296 | } |
| 297 | else |
| 298 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 299 | ARMNN_LOG(fatal) << "Unsupported frontend: " << modelFormat; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 300 | return 1; |
| 301 | } |
| 302 | const NormalizationParameters& normParams = GetNormalizationParameters(modelFrontend, inputTensorDataType); |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 303 | for (const auto& imageEntry : imageNameToLabel) |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 304 | { |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 305 | const std::string imageName = imageEntry.first; |
| 306 | std::cout << "Processing image: " << imageName << "\n"; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 307 | |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 308 | vector<TContainer> inputDataContainers; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 309 | vector<TContainer> outputDataContainers; |
| 310 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 311 | auto imagePath = pathToDataDir / fs::path(imageName); |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 312 | switch (inputTensorDataType) |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 313 | { |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 314 | case armnn::DataType::Signed32: |
| 315 | inputDataContainers.push_back( |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 316 | PrepareImageTensor<int>(imagePath.string(), |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 317 | inputTensorWidth, inputTensorHeight, |
| 318 | normParams, |
| 319 | batchSize, |
| 320 | inputTensorDataLayout)); |
SiCong Li | c0ed7ba | 2019-06-21 16:02:40 +0100 | [diff] [blame] | 321 | outputDataContainers = { vector<int>(outputNumElements) }; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 322 | break; |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 323 | case armnn::DataType::QAsymmU8: |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 324 | inputDataContainers.push_back( |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 325 | PrepareImageTensor<uint8_t>(imagePath.string(), |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 326 | inputTensorWidth, inputTensorHeight, |
| 327 | normParams, |
| 328 | batchSize, |
| 329 | inputTensorDataLayout)); |
SiCong Li | c0ed7ba | 2019-06-21 16:02:40 +0100 | [diff] [blame] | 330 | outputDataContainers = { vector<uint8_t>(outputNumElements) }; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 331 | break; |
| 332 | case armnn::DataType::Float32: |
| 333 | default: |
| 334 | inputDataContainers.push_back( |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 335 | PrepareImageTensor<float>(imagePath.string(), |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 336 | inputTensorWidth, inputTensorHeight, |
| 337 | normParams, |
| 338 | batchSize, |
| 339 | inputTensorDataLayout)); |
SiCong Li | c0ed7ba | 2019-06-21 16:02:40 +0100 | [diff] [blame] | 340 | outputDataContainers = { vector<float>(outputNumElements) }; |
SiCong Li | 39f4639 | 2019-06-21 12:00:04 +0100 | [diff] [blame] | 341 | break; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 342 | } |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 343 | |
| 344 | status = runtime->EnqueueWorkload(networkId, |
| 345 | armnnUtils::MakeInputTensors(inputBindings, inputDataContainers), |
| 346 | armnnUtils::MakeOutputTensors(outputBindings, outputDataContainers)); |
| 347 | |
| 348 | if (status == armnn::Status::Failure) |
| 349 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 350 | ARMNN_LOG(fatal) << "armnn::IRuntime: Failed to enqueue workload for image: " << imageName; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 353 | checker.AddImageResult<TContainer>(imageName, outputDataContainers); |
| 354 | } |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | for(unsigned int i = 1; i <= 5; ++i) |
| 362 | { |
| 363 | std::cout << "Top " << i << " Accuracy: " << checker.GetAccuracy(i) << "%" << "\n"; |
| 364 | } |
| 365 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 366 | ARMNN_LOG(info) << "Accuracy Tool ran successfully!"; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 367 | return 0; |
| 368 | } |
Pavel Macenauer | 855a47b | 2020-05-26 10:54:22 +0000 | [diff] [blame] | 369 | catch (const armnn::Exception& e) |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 370 | { |
| 371 | // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an |
| 372 | // exception of type std::length_error. |
| 373 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 374 | std::cerr << "Armnn Error: " << e.what() << std::endl; |
| 375 | return 1; |
| 376 | } |
Pavel Macenauer | 855a47b | 2020-05-26 10:54:22 +0000 | [diff] [blame] | 377 | catch (const std::exception& e) |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 378 | { |
| 379 | // Coverity fix: various boost exceptions can be thrown by methods called by this test. |
| 380 | std::cerr << "WARNING: ModelAccuracyTool-Armnn: An error has occurred when running the " |
| 381 | "Accuracy Tool: " << e.what() << std::endl; |
| 382 | return 1; |
| 383 | } |
| 384 | } |
| 385 | |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 386 | map<std::string, std::string> LoadValidationImageFilenamesAndLabels(const string& validationLabelPath, |
| 387 | const string& imageDirectoryPath, |
| 388 | size_t begIndex, |
| 389 | size_t endIndex, |
| 390 | const string& blacklistPath) |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 391 | { |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 392 | // Populate imageFilenames with names of all .JPEG, .PNG images |
| 393 | std::vector<std::string> imageFilenames; |
| 394 | for (const auto& imageEntry : |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 395 | boost::make_iterator_range(fs::directory_iterator(fs::path(imageDirectoryPath)))) |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 396 | { |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 397 | fs::path imagePath = imageEntry.path(); |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 398 | std::string imageExtension = boost::to_upper_copy<std::string>(imagePath.extension().string()); |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 399 | if (fs::is_regular_file(imagePath) && (imageExtension == ".JPEG" || imageExtension == ".PNG")) |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 400 | { |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 401 | imageFilenames.push_back(imagePath.filename().string()); |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 402 | } |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 403 | } |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 404 | if (imageFilenames.empty()) |
| 405 | { |
| 406 | throw armnn::Exception("No image file (JPEG, PNG) found at " + imageDirectoryPath); |
| 407 | } |
| 408 | |
| 409 | // Sort the image filenames lexicographically |
| 410 | std::sort(imageFilenames.begin(), imageFilenames.end()); |
| 411 | |
| 412 | std::cout << imageFilenames.size() << " images found at " << imageDirectoryPath << std::endl; |
| 413 | |
| 414 | // Get default end index |
| 415 | if (begIndex < 1 || endIndex > imageFilenames.size()) |
| 416 | { |
| 417 | throw armnn::Exception("Invalid image index range"); |
| 418 | } |
| 419 | endIndex = endIndex == 0 ? imageFilenames.size() : endIndex; |
| 420 | if (begIndex > endIndex) |
| 421 | { |
| 422 | throw armnn::Exception("Invalid image index range"); |
| 423 | } |
| 424 | |
| 425 | // Load blacklist if there is one |
| 426 | std::vector<unsigned int> blacklist; |
| 427 | if (!blacklistPath.empty()) |
| 428 | { |
| 429 | std::ifstream blacklistFile(blacklistPath); |
| 430 | unsigned int index; |
| 431 | while (blacklistFile >> index) |
| 432 | { |
| 433 | blacklist.push_back(index); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | // Load ground truth labels and pair them with corresponding image names |
| 438 | std::string classification; |
| 439 | map<std::string, std::string> imageNameToLabel; |
| 440 | ifstream infile(validationLabelPath); |
| 441 | size_t imageIndex = begIndex; |
| 442 | size_t blacklistIndexCount = 0; |
| 443 | while (std::getline(infile, classification)) |
| 444 | { |
| 445 | if (imageIndex > endIndex) |
| 446 | { |
| 447 | break; |
| 448 | } |
| 449 | // If current imageIndex is included in blacklist, skip the current image |
| 450 | if (blacklistIndexCount < blacklist.size() && imageIndex == blacklist[blacklistIndexCount]) |
| 451 | { |
| 452 | ++imageIndex; |
| 453 | ++blacklistIndexCount; |
| 454 | continue; |
| 455 | } |
| 456 | imageNameToLabel.insert(std::pair<std::string, std::string>(imageFilenames[imageIndex - 1], classification)); |
| 457 | ++imageIndex; |
| 458 | } |
| 459 | std::cout << blacklistIndexCount << " images blacklisted" << std::endl; |
| 460 | std::cout << imageIndex - begIndex - blacklistIndexCount << " images to be loaded" << std::endl; |
| 461 | return imageNameToLabel; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 462 | } |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 463 | |
| 464 | std::vector<armnnUtils::LabelCategoryNames> LoadModelOutputLabels(const std::string& modelOutputLabelsPath) |
| 465 | { |
| 466 | std::vector<armnnUtils::LabelCategoryNames> modelOutputLabels; |
| 467 | ifstream modelOutputLablesFile(modelOutputLabelsPath); |
| 468 | std::string line; |
| 469 | while (std::getline(modelOutputLablesFile, line)) |
| 470 | { |
| 471 | armnnUtils::LabelCategoryNames tokens = armnnUtils::SplitBy(line, ":"); |
| 472 | armnnUtils::LabelCategoryNames predictionCategoryNames = armnnUtils::SplitBy(tokens.back(), ","); |
| 473 | std::transform(predictionCategoryNames.begin(), predictionCategoryNames.end(), predictionCategoryNames.begin(), |
| 474 | [](const std::string& category) { return armnnUtils::Strip(category); }); |
| 475 | modelOutputLabels.push_back(predictionCategoryNames); |
| 476 | } |
| 477 | return modelOutputLabels; |
| 478 | } |