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) {}; |
| 94 | virtual bool ProcessCommandLineOptions() { return true; }; |
| 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: |
| 103 | InferenceModelTestCase(TModel& model, |
| 104 | unsigned int testCaseId, |
| 105 | std::vector<typename TModel::DataType> modelInput, |
| 106 | unsigned int outputSize) |
| 107 | : m_Model(model) |
| 108 | , m_TestCaseId(testCaseId) |
| 109 | , m_Input(std::move(modelInput)) |
| 110 | { |
| 111 | m_Output.resize(outputSize); |
| 112 | } |
| 113 | |
| 114 | virtual void Run() override |
| 115 | { |
| 116 | m_Model.Run(m_Input, m_Output); |
| 117 | } |
| 118 | |
| 119 | protected: |
| 120 | unsigned int GetTestCaseId() const { return m_TestCaseId; } |
| 121 | const std::vector<typename TModel::DataType>& GetOutput() const { return m_Output; } |
| 122 | |
| 123 | private: |
| 124 | TModel& m_Model; |
| 125 | unsigned int m_TestCaseId; |
| 126 | std::vector<typename TModel::DataType> m_Input; |
| 127 | std::vector<typename TModel::DataType> m_Output; |
| 128 | }; |
| 129 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 130 | template <typename TDataType> |
| 131 | struct ToFloat { }; // nothing defined for the generic case |
| 132 | |
| 133 | template <> |
| 134 | struct ToFloat<float> |
| 135 | { |
| 136 | static inline float Convert(float value, const InferenceModelInternal::QuantizationParams &) |
| 137 | { |
| 138 | // assuming that float models are not quantized |
| 139 | return value; |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | template <> |
| 144 | struct ToFloat<uint8_t> |
| 145 | { |
| 146 | static inline float Convert(uint8_t value, |
| 147 | const InferenceModelInternal::QuantizationParams & quantizationParams) |
| 148 | { |
| 149 | return armnn::Dequantize<uint8_t>(value, |
| 150 | quantizationParams.first, |
| 151 | quantizationParams.second); |
| 152 | } |
| 153 | }; |
| 154 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 155 | template <typename TTestCaseDatabase, typename TModel> |
| 156 | class ClassifierTestCase : public InferenceModelTestCase<TModel> |
| 157 | { |
| 158 | public: |
| 159 | ClassifierTestCase(int& numInferencesRef, |
| 160 | int& numCorrectInferencesRef, |
| 161 | const std::vector<unsigned int>& validationPredictions, |
| 162 | std::vector<unsigned int>* validationPredictionsOut, |
| 163 | TModel& model, |
| 164 | unsigned int testCaseId, |
| 165 | unsigned int label, |
| 166 | std::vector<typename TModel::DataType> modelInput); |
| 167 | |
| 168 | virtual TestCaseResult ProcessResult(const InferenceTestOptions& params) override; |
| 169 | |
| 170 | private: |
| 171 | unsigned int m_Label; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 172 | InferenceModelInternal::QuantizationParams m_QuantizationParams; |
| 173 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 174 | /// These fields reference the corresponding member in the ClassifierTestCaseProvider. |
| 175 | /// @{ |
| 176 | int& m_NumInferencesRef; |
| 177 | int& m_NumCorrectInferencesRef; |
| 178 | const std::vector<unsigned int>& m_ValidationPredictions; |
| 179 | std::vector<unsigned int>* m_ValidationPredictionsOut; |
| 180 | /// @} |
| 181 | }; |
| 182 | |
| 183 | template <typename TDatabase, typename InferenceModel> |
| 184 | class ClassifierTestCaseProvider : public IInferenceTestCaseProvider |
| 185 | { |
| 186 | public: |
| 187 | template <typename TConstructDatabaseCallable, typename TConstructModelCallable> |
| 188 | ClassifierTestCaseProvider(TConstructDatabaseCallable constructDatabase, TConstructModelCallable constructModel); |
| 189 | |
| 190 | virtual void AddCommandLineOptions(boost::program_options::options_description& options) override; |
| 191 | virtual bool ProcessCommandLineOptions() override; |
| 192 | virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override; |
| 193 | virtual bool OnInferenceTestFinished() override; |
| 194 | |
| 195 | private: |
| 196 | void ReadPredictions(); |
| 197 | |
| 198 | typename InferenceModel::CommandLineOptions m_ModelCommandLineOptions; |
| 199 | std::function<std::unique_ptr<InferenceModel>(typename InferenceModel::CommandLineOptions)> m_ConstructModel; |
| 200 | std::unique_ptr<InferenceModel> m_Model; |
| 201 | |
| 202 | std::string m_DataDir; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 203 | std::function<TDatabase(const char*, const InferenceModel&)> m_ConstructDatabase; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 204 | std::unique_ptr<TDatabase> m_Database; |
| 205 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 206 | int m_NumInferences; // Referenced by test cases. |
| 207 | int m_NumCorrectInferences; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 208 | |
| 209 | std::string m_ValidationFileIn; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 210 | std::vector<unsigned int> m_ValidationPredictions; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 211 | |
| 212 | std::string m_ValidationFileOut; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 213 | std::vector<unsigned int> m_ValidationPredictionsOut; // Referenced by test cases. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider, |
| 217 | InferenceTestOptions& outParams); |
| 218 | |
| 219 | bool ValidateDirectory(std::string& dir); |
| 220 | |
| 221 | bool InferenceTest(const InferenceTestOptions& params, |
| 222 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 223 | IInferenceTestCaseProvider& testCaseProvider); |
| 224 | |
| 225 | template<typename TConstructTestCaseProvider> |
| 226 | int InferenceTestMain(int argc, |
| 227 | char* argv[], |
| 228 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 229 | TConstructTestCaseProvider constructTestCaseProvider); |
| 230 | |
| 231 | template<typename TDatabase, |
| 232 | typename TParser, |
| 233 | typename TConstructDatabaseCallable> |
| 234 | int ClassifierInferenceTestMain(int argc, char* argv[], const char* modelFilename, bool isModelBinary, |
| 235 | const char* inputBindingName, const char* outputBindingName, |
| 236 | const std::vector<unsigned int>& defaultTestCaseIds, |
| 237 | TConstructDatabaseCallable constructDatabase, |
| 238 | const armnn::TensorShape* inputTensorShape = nullptr); |
| 239 | |
| 240 | } // namespace test |
| 241 | } // namespace armnn |
| 242 | |
| 243 | #include "InferenceTest.inl" |