telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | #include "../InferenceTest.hpp" |
| 6 | #include "../ImagePreprocessor.hpp" |
| 7 | #include "armnnTfLiteParser/ITfLiteParser.hpp" |
| 8 | |
Matthew Sloyan | 0029cd6 | 2020-09-28 12:58:14 +0100 | [diff] [blame] | 9 | #include <cxxopts/cxxopts.hpp> |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 10 | #include <fstream> |
| 11 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | using namespace armnnTfLiteParser; |
| 13 | |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 14 | std::vector<ImageSet> ParseDataset(const std::string& filename) |
| 15 | { |
| 16 | std::ifstream read(filename); |
| 17 | std::vector<ImageSet> imageSet; |
| 18 | if (read.is_open()) |
| 19 | { |
| 20 | // Get the images and the correct corresponding label from the given file |
| 21 | for (std::string line; std::getline(read, line);) |
| 22 | { |
| 23 | stringstream ss(line); |
| 24 | std::string image_name; |
| 25 | std::string label; |
| 26 | getline(ss, image_name, ' '); |
| 27 | getline(ss, label, ' '); |
| 28 | imageSet.push_back(ImageSet(image_name, std::stoi(label))); |
| 29 | } |
| 30 | } |
| 31 | else |
| 32 | { |
| 33 | // Use the default images |
| 34 | imageSet.push_back(ImageSet("Dog.jpg", 209)); |
| 35 | // top five predictions in tensorflow: |
| 36 | // ----------------------------------- |
| 37 | // 209:Labrador retriever 0.949995 |
| 38 | // 160:Rhodesian ridgeback 0.0270182 |
| 39 | // 208:golden retriever 0.0192866 |
| 40 | // 853:tennis ball 0.000470382 |
| 41 | // 239:Greater Swiss Mountain dog 0.000464451 |
| 42 | imageSet.push_back(ImageSet("Cat.jpg", 283)); |
| 43 | // top five predictions in tensorflow: |
| 44 | // ----------------------------------- |
| 45 | // 283:tiger cat 0.579016 |
| 46 | // 286:Egyptian cat 0.319676 |
| 47 | // 282:tabby, tabby cat 0.0873346 |
| 48 | // 288:lynx, catamount 0.011163 |
| 49 | // 289:leopard, Panthera pardus 0.000856755 |
| 50 | imageSet.push_back(ImageSet("shark.jpg", 3)); |
| 51 | // top five predictions in tensorflow: |
| 52 | // ----------------------------------- |
| 53 | // 3:great white shark, white shark, ... 0.996926 |
| 54 | // 4:tiger shark, Galeocerdo cuvieri 0.00270528 |
| 55 | // 149:killer whale, killer, orca, ... 0.000121848 |
| 56 | // 395:sturgeon 7.78977e-05 |
| 57 | // 5:hammerhead, hammerhead shark 6.44127e-055 |
| 58 | }; |
| 59 | return imageSet; |
| 60 | } |
| 61 | |
| 62 | std::string GetLabelsFilenameFromOptions(int argc, char* argv[]) |
| 63 | { |
Matthew Sloyan | 0029cd6 | 2020-09-28 12:58:14 +0100 | [diff] [blame] | 64 | cxxopts::Options options("TfLiteMobilenetQuantized-Armnn","Validation Options"); |
| 65 | |
| 66 | std::string fileName; |
| 67 | try |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 68 | { |
Matthew Sloyan | 0029cd6 | 2020-09-28 12:58:14 +0100 | [diff] [blame] | 69 | options |
| 70 | .allow_unrecognised_options() |
| 71 | .add_options() |
| 72 | ("l,labels", |
| 73 | "Filename of a text file where in each line contains an image " |
| 74 | "filename and the correct label the network should predict when fed that image", |
| 75 | cxxopts::value<std::string>(fileName)); |
| 76 | |
| 77 | auto result = options.parse(argc, argv); |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 78 | } |
Matthew Sloyan | 0029cd6 | 2020-09-28 12:58:14 +0100 | [diff] [blame] | 79 | catch (const cxxopts::OptionException& e) |
| 80 | { |
| 81 | std::cerr << e.what() << std::endl; |
| 82 | exit(EXIT_FAILURE); |
| 83 | } |
| 84 | catch (const std::exception& e) |
| 85 | { |
| 86 | std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl; |
| 87 | exit(EXIT_FAILURE); |
| 88 | } |
| 89 | |
| 90 | return fileName; |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 94 | int main(int argc, char* argv[]) |
| 95 | { |
| 96 | int retVal = EXIT_FAILURE; |
| 97 | try |
| 98 | { |
| 99 | // Coverity fix: The following code may throw an exception of type std::length_error. |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 100 | const std::string labels_file = GetLabelsFilenameFromOptions(argc,argv); |
| 101 | std::vector<ImageSet> imageSet = ParseDataset(labels_file); |
| 102 | std::vector<unsigned int> indices(imageSet.size()); |
| 103 | std::generate(indices.begin(), indices.end(), [n = 0] () mutable { return n++; }); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 104 | |
| 105 | armnn::TensorShape inputTensorShape({ 1, 224, 224, 3 }); |
| 106 | |
| 107 | using DataType = uint8_t; |
| 108 | using DatabaseType = ImagePreprocessor<DataType>; |
| 109 | using ParserType = armnnTfLiteParser::ITfLiteParser; |
| 110 | using ModelType = InferenceModel<ParserType, DataType>; |
| 111 | |
| 112 | // Coverity fix: ClassifierInferenceTestMain() may throw uncaught exceptions. |
| 113 | retVal = armnn::test::ClassifierInferenceTestMain<DatabaseType, |
| 114 | ParserType>( |
| 115 | argc, argv, |
| 116 | "mobilenet_v1_1.0_224_quant.tflite", // model name |
| 117 | true, // model is binary |
| 118 | "input", // input tensor name |
| 119 | "MobilenetV1/Predictions/Reshape_1", // output tensor name |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 120 | indices, // vector of indices to select which images to validate |
Derek Lamberti | eb1fce0 | 2019-12-10 21:20:10 +0000 | [diff] [blame] | 121 | [&imageSet](const char* dataDir, const ModelType &) { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 122 | // we need to get the input quantization parameters from |
| 123 | // the parsed model |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 124 | return DatabaseType( |
| 125 | dataDir, |
| 126 | 224, |
| 127 | 224, |
| 128 | imageSet, |
FinnWilliamsArm | af8b72d | 2019-05-22 14:50:55 +0100 | [diff] [blame] | 129 | 1); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 130 | }, |
| 131 | &inputTensorShape); |
| 132 | } |
| 133 | catch (const std::exception& e) |
| 134 | { |
| 135 | // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an |
| 136 | // exception of type std::length_error. |
| 137 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 138 | std::cerr << "WARNING: " << *argv << ": An error has occurred when running " |
| 139 | "the classifier inference tests: " << e.what() << std::endl; |
| 140 | } |
| 141 | return retVal; |
| 142 | } |