telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [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 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 7 | #include <armnn/ArmNN.hpp> |
| 8 | #include <armnn/TypesUtils.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 9 | #include "InferenceModel.hpp" |
| 10 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 11 | #include <Logging.hpp> |
| 12 | |
| 13 | #include <boost/log/core/core.hpp> |
| 14 | #include <boost/program_options.hpp> |
| 15 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 16 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | inline std::istream& operator>>(std::istream& in, armnn::Compute& compute) |
| 21 | { |
| 22 | std::string token; |
| 23 | in >> token; |
| 24 | compute = armnn::ParseComputeDevice(token.c_str()); |
| 25 | if (compute == armnn::Compute::Undefined) |
| 26 | { |
| 27 | in.setstate(std::ios_base::failbit); |
| 28 | throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value); |
| 29 | } |
| 30 | return in; |
| 31 | } |
| 32 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 33 | inline std::istream& operator>>(std::istream& in, armnn::BackendId& backend) |
| 34 | { |
| 35 | std::string token; |
| 36 | in >> token; |
| 37 | armnn::Compute compute = armnn::ParseComputeDevice(token.c_str()); |
| 38 | if (compute == armnn::Compute::Undefined) |
| 39 | { |
| 40 | in.setstate(std::ios_base::failbit); |
| 41 | throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value); |
| 42 | } |
| 43 | backend = compute; |
| 44 | return in; |
| 45 | } |
| 46 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | namespace test |
| 48 | { |
| 49 | |
| 50 | class TestFrameworkException : public Exception |
| 51 | { |
| 52 | public: |
| 53 | using Exception::Exception; |
| 54 | }; |
| 55 | |
| 56 | struct InferenceTestOptions |
| 57 | { |
| 58 | unsigned int m_IterationCount; |
| 59 | std::string m_InferenceTimesFile; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | bool m_EnableProfiling; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | |
| 62 | InferenceTestOptions() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | : m_IterationCount(0), |
| 64 | m_EnableProfiling(0) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | {} |
| 66 | }; |
| 67 | |
| 68 | enum class TestCaseResult |
| 69 | { |
| 70 | /// The test completed without any errors. |
| 71 | Ok, |
| 72 | /// The test failed (e.g. the prediction didn't match the validation file). |
| 73 | /// This will eventually fail the whole program but the remaining test cases will still be run. |
| 74 | Failed, |
| 75 | /// The test failed with a fatal error. The remaining tests will not be run. |
| 76 | Abort |
| 77 | }; |
| 78 | |
| 79 | class IInferenceTestCase |
| 80 | { |
| 81 | public: |
| 82 | virtual ~IInferenceTestCase() {} |
| 83 | |
| 84 | virtual void Run() = 0; |
| 85 | virtual TestCaseResult ProcessResult(const InferenceTestOptions& options) = 0; |
| 86 | }; |
| 87 | |
| 88 | class IInferenceTestCaseProvider |
| 89 | { |
| 90 | public: |
| 91 | virtual ~IInferenceTestCaseProvider() {} |
| 92 | |
| 93 | virtual void AddCommandLineOptions(boost::program_options::options_description& options) {}; |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 94 | virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) { return true; }; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 95 | virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) = 0; |
| 96 | virtual bool OnInferenceTestFinished() { return true; }; |
| 97 | }; |
| 98 | |
| 99 | template <typename TModel> |
| 100 | class InferenceModelTestCase : public IInferenceTestCase |
| 101 | { |
| 102 | public: |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 103 | using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 104 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 105 | InferenceModelTestCase(TModel& model, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 106 | unsigned int testCaseId, |
| 107 | const std::vector<TContainer>& inputs, |
| 108 | const std::vector<unsigned int>& outputSizes) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 109 | : m_Model(model) |
| 110 | , m_TestCaseId(testCaseId) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 111 | , m_Inputs(std::move(inputs)) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 112 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 113 | // Initialize output vector |
| 114 | const size_t numOutputs = outputSizes.size(); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 115 | m_Outputs.reserve(numOutputs); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 116 | |
| 117 | for (size_t i = 0; i < numOutputs; i++) |
| 118 | { |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 119 | m_Outputs.push_back(std::vector<typename TModel::DataType>(outputSizes[i])); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 120 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | virtual void Run() override |
| 124 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 125 | m_Model.Run(m_Inputs, m_Outputs); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | protected: |
| 129 | unsigned int GetTestCaseId() const { return m_TestCaseId; } |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 130 | const std::vector<TContainer>& GetOutputs() const { return m_Outputs; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 131 | |
| 132 | private: |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 133 | TModel& m_Model; |
| 134 | unsigned int m_TestCaseId; |
| 135 | std::vector<TContainer> m_Inputs; |
| 136 | std::vector<TContainer> m_Outputs; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | template <typename TTestCaseDatabase, typename TModel> |
| 140 | class ClassifierTestCase : public InferenceModelTestCase<TModel> |
| 141 | { |
| 142 | public: |
| 143 | ClassifierTestCase(int& numInferencesRef, |
| 144 | int& numCorrectInferencesRef, |
| 145 | const std::vector<unsigned int>& validationPredictions, |
| 146 | std::vector<unsigned int>* validationPredictionsOut, |
| 147 | TModel& model, |
| 148 | unsigned int testCaseId, |
| 149 | unsigned int label, |
| 150 | std::vector<typename TModel::DataType> modelInput); |
| 151 | |
| 152 | virtual TestCaseResult ProcessResult(const InferenceTestOptions& params) override; |
| 153 | |
| 154 | private: |
| 155 | unsigned int m_Label; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 156 | InferenceModelInternal::QuantizationParams m_QuantizationParams; |
| 157 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 158 | /// These fields reference the corresponding member in the ClassifierTestCaseProvider. |
| 159 | /// @{ |
| 160 | int& m_NumInferencesRef; |
| 161 | int& m_NumCorrectInferencesRef; |
| 162 | const std::vector<unsigned int>& m_ValidationPredictions; |
| 163 | std::vector<unsigned int>* m_ValidationPredictionsOut; |
| 164 | /// @} |
| 165 | }; |
| 166 | |
| 167 | template <typename TDatabase, typename InferenceModel> |
| 168 | class ClassifierTestCaseProvider : public IInferenceTestCaseProvider |
| 169 | { |
| 170 | public: |
| 171 | template <typename TConstructDatabaseCallable, typename TConstructModelCallable> |
| 172 | ClassifierTestCaseProvider(TConstructDatabaseCallable constructDatabase, TConstructModelCallable constructModel); |
| 173 | |
| 174 | virtual void AddCommandLineOptions(boost::program_options::options_description& options) override; |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 175 | virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 176 | virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override; |
| 177 | virtual bool OnInferenceTestFinished() override; |
| 178 | |
| 179 | private: |
| 180 | void ReadPredictions(); |
| 181 | |
| 182 | typename InferenceModel::CommandLineOptions m_ModelCommandLineOptions; |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 183 | std::function<std::unique_ptr<InferenceModel>(const InferenceTestOptions& commonOptions, |
| 184 | typename InferenceModel::CommandLineOptions)> m_ConstructModel; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 185 | std::unique_ptr<InferenceModel> m_Model; |
| 186 | |
| 187 | std::string m_DataDir; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 188 | std::function<TDatabase(const char*, const InferenceModel&)> m_ConstructDatabase; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 189 | std::unique_ptr<TDatabase> m_Database; |
| 190 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 191 | int m_NumInferences; // Referenced by test cases. |
| 192 | int m_NumCorrectInferences; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 193 | |
| 194 | std::string m_ValidationFileIn; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 195 | std::vector<unsigned int> m_ValidationPredictions; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 196 | |
| 197 | std::string m_ValidationFileOut; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 198 | std::vector<unsigned int> m_ValidationPredictionsOut; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider, |
| 202 | InferenceTestOptions& outParams); |
| 203 | |
| 204 | bool ValidateDirectory(std::string& dir); |
| 205 | |
| 206 | bool InferenceTest(const InferenceTestOptions& params, |
| 207 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 208 | IInferenceTestCaseProvider& testCaseProvider); |
| 209 | |
| 210 | template<typename TConstructTestCaseProvider> |
| 211 | int InferenceTestMain(int argc, |
| 212 | char* argv[], |
| 213 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 214 | TConstructTestCaseProvider constructTestCaseProvider); |
| 215 | |
| 216 | template<typename TDatabase, |
| 217 | typename TParser, |
| 218 | typename TConstructDatabaseCallable> |
| 219 | int ClassifierInferenceTestMain(int argc, char* argv[], const char* modelFilename, bool isModelBinary, |
| 220 | const char* inputBindingName, const char* outputBindingName, |
| 221 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 222 | TConstructDatabaseCallable constructDatabase, |
| 223 | const armnn::TensorShape* inputTensorShape = nullptr); |
| 224 | |
| 225 | } // namespace test |
| 226 | } // namespace armnn |
| 227 | |
| 228 | #include "InferenceTest.inl" |