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