blob: 3ea70962d262b42b715ceed2a9effb86aaa2ef64 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#pragma once
6
David Beckf0b48452018-10-19 15:20:56 +01007#include <armnn/ArmNN.hpp>
8#include <armnn/TypesUtils.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +01009#include "InferenceModel.hpp"
10
telsoa014fcda012018-03-09 14:13:49 +000011#include <Logging.hpp>
12
13#include <boost/log/core/core.hpp>
14#include <boost/program_options.hpp>
15
telsoa01c577f2c2018-08-31 09:22:23 +010016
telsoa014fcda012018-03-09 14:13:49 +000017namespace armnn
18{
19
20inline 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 Beckf0b48452018-10-19 15:20:56 +010033inline 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
telsoa014fcda012018-03-09 14:13:49 +000047namespace test
48{
49
50class TestFrameworkException : public Exception
51{
52public:
53 using Exception::Exception;
54};
55
56struct InferenceTestOptions
57{
58 unsigned int m_IterationCount;
59 std::string m_InferenceTimesFile;
telsoa01c577f2c2018-08-31 09:22:23 +010060 bool m_EnableProfiling;
telsoa014fcda012018-03-09 14:13:49 +000061
62 InferenceTestOptions()
telsoa01c577f2c2018-08-31 09:22:23 +010063 : m_IterationCount(0),
64 m_EnableProfiling(0)
telsoa014fcda012018-03-09 14:13:49 +000065 {}
66};
67
68enum 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
79class IInferenceTestCase
80{
81public:
82 virtual ~IInferenceTestCase() {}
83
84 virtual void Run() = 0;
85 virtual TestCaseResult ProcessResult(const InferenceTestOptions& options) = 0;
86};
87
88class IInferenceTestCaseProvider
89{
90public:
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
99template <typename TModel>
100class InferenceModelTestCase : public IInferenceTestCase
101{
102public:
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
119protected:
120 unsigned int GetTestCaseId() const { return m_TestCaseId; }
121 const std::vector<typename TModel::DataType>& GetOutput() const { return m_Output; }
122
123private:
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
telsoa01c577f2c2018-08-31 09:22:23 +0100130template <typename TDataType>
131struct ToFloat { }; // nothing defined for the generic case
132
133template <>
134struct 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
143template <>
144struct 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
telsoa014fcda012018-03-09 14:13:49 +0000155template <typename TTestCaseDatabase, typename TModel>
156class ClassifierTestCase : public InferenceModelTestCase<TModel>
157{
158public:
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
170private:
171 unsigned int m_Label;
telsoa01c577f2c2018-08-31 09:22:23 +0100172 InferenceModelInternal::QuantizationParams m_QuantizationParams;
173
telsoa014fcda012018-03-09 14:13:49 +0000174 /// 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
183template <typename TDatabase, typename InferenceModel>
184class ClassifierTestCaseProvider : public IInferenceTestCaseProvider
185{
186public:
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
195private:
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;
telsoa01c577f2c2018-08-31 09:22:23 +0100203 std::function<TDatabase(const char*, const InferenceModel&)> m_ConstructDatabase;
telsoa014fcda012018-03-09 14:13:49 +0000204 std::unique_ptr<TDatabase> m_Database;
205
telsoa01c577f2c2018-08-31 09:22:23 +0100206 int m_NumInferences; // Referenced by test cases.
207 int m_NumCorrectInferences; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000208
209 std::string m_ValidationFileIn;
telsoa01c577f2c2018-08-31 09:22:23 +0100210 std::vector<unsigned int> m_ValidationPredictions; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000211
212 std::string m_ValidationFileOut;
telsoa01c577f2c2018-08-31 09:22:23 +0100213 std::vector<unsigned int> m_ValidationPredictionsOut; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000214};
215
216bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider,
217 InferenceTestOptions& outParams);
218
219bool ValidateDirectory(std::string& dir);
220
221bool InferenceTest(const InferenceTestOptions& params,
222 const std::vector<unsigned int>& defaultTestCaseIds,
223 IInferenceTestCaseProvider& testCaseProvider);
224
225template<typename TConstructTestCaseProvider>
226int InferenceTestMain(int argc,
227 char* argv[],
228 const std::vector<unsigned int>& defaultTestCaseIds,
229 TConstructTestCaseProvider constructTestCaseProvider);
230
231template<typename TDatabase,
232 typename TParser,
233 typename TConstructDatabaseCallable>
234int 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"