blob: 3c22df9a5ea953a887674cb5c26419c0d0fd67b3 [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:
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000103 using TContainer = std::vector<typename TModel::DataType>;
104
telsoa014fcda012018-03-09 14:13:49 +0000105 InferenceModelTestCase(TModel& model,
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000106 unsigned int testCaseId,
107 const std::vector<TContainer>& inputs,
108 const std::vector<unsigned int>& outputSizes)
telsoa014fcda012018-03-09 14:13:49 +0000109 : m_Model(model)
110 , m_TestCaseId(testCaseId)
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000111 , m_Inputs(std::move(inputs))
telsoa014fcda012018-03-09 14:13:49 +0000112 {
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000113 // Initialize output vector
114 const size_t numOutputs = outputSizes.size();
115 m_Outputs.resize(numOutputs);
116
117 for (size_t i = 0; i < numOutputs; i++)
118 {
119 m_Outputs[i].resize(outputSizes[i]);
120 }
telsoa014fcda012018-03-09 14:13:49 +0000121 }
122
123 virtual void Run() override
124 {
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000125 m_Model.Run(m_Inputs, m_Outputs);
telsoa014fcda012018-03-09 14:13:49 +0000126 }
127
128protected:
129 unsigned int GetTestCaseId() const { return m_TestCaseId; }
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000130 const std::vector<TContainer>& GetOutputs() const { return m_Outputs; }
telsoa014fcda012018-03-09 14:13:49 +0000131
132private:
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000133 TModel& m_Model;
134 unsigned int m_TestCaseId;
135 std::vector<TContainer> m_Inputs;
136 std::vector<TContainer> m_Outputs;
telsoa014fcda012018-03-09 14:13:49 +0000137};
138
telsoa01c577f2c2018-08-31 09:22:23 +0100139template <typename TDataType>
140struct ToFloat { }; // nothing defined for the generic case
141
142template <>
143struct ToFloat<float>
144{
145 static inline float Convert(float value, const InferenceModelInternal::QuantizationParams &)
146 {
147 // assuming that float models are not quantized
148 return value;
149 }
150};
151
152template <>
153struct ToFloat<uint8_t>
154{
155 static inline float Convert(uint8_t value,
156 const InferenceModelInternal::QuantizationParams & quantizationParams)
157 {
158 return armnn::Dequantize<uint8_t>(value,
159 quantizationParams.first,
160 quantizationParams.second);
161 }
162};
163
telsoa014fcda012018-03-09 14:13:49 +0000164template <typename TTestCaseDatabase, typename TModel>
165class ClassifierTestCase : public InferenceModelTestCase<TModel>
166{
167public:
168 ClassifierTestCase(int& numInferencesRef,
169 int& numCorrectInferencesRef,
170 const std::vector<unsigned int>& validationPredictions,
171 std::vector<unsigned int>* validationPredictionsOut,
172 TModel& model,
173 unsigned int testCaseId,
174 unsigned int label,
175 std::vector<typename TModel::DataType> modelInput);
176
177 virtual TestCaseResult ProcessResult(const InferenceTestOptions& params) override;
178
179private:
180 unsigned int m_Label;
telsoa01c577f2c2018-08-31 09:22:23 +0100181 InferenceModelInternal::QuantizationParams m_QuantizationParams;
182
telsoa014fcda012018-03-09 14:13:49 +0000183 /// These fields reference the corresponding member in the ClassifierTestCaseProvider.
184 /// @{
185 int& m_NumInferencesRef;
186 int& m_NumCorrectInferencesRef;
187 const std::vector<unsigned int>& m_ValidationPredictions;
188 std::vector<unsigned int>* m_ValidationPredictionsOut;
189 /// @}
190};
191
192template <typename TDatabase, typename InferenceModel>
193class ClassifierTestCaseProvider : public IInferenceTestCaseProvider
194{
195public:
196 template <typename TConstructDatabaseCallable, typename TConstructModelCallable>
197 ClassifierTestCaseProvider(TConstructDatabaseCallable constructDatabase, TConstructModelCallable constructModel);
198
199 virtual void AddCommandLineOptions(boost::program_options::options_description& options) override;
200 virtual bool ProcessCommandLineOptions() override;
201 virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override;
202 virtual bool OnInferenceTestFinished() override;
203
204private:
205 void ReadPredictions();
206
207 typename InferenceModel::CommandLineOptions m_ModelCommandLineOptions;
208 std::function<std::unique_ptr<InferenceModel>(typename InferenceModel::CommandLineOptions)> m_ConstructModel;
209 std::unique_ptr<InferenceModel> m_Model;
210
211 std::string m_DataDir;
telsoa01c577f2c2018-08-31 09:22:23 +0100212 std::function<TDatabase(const char*, const InferenceModel&)> m_ConstructDatabase;
telsoa014fcda012018-03-09 14:13:49 +0000213 std::unique_ptr<TDatabase> m_Database;
214
telsoa01c577f2c2018-08-31 09:22:23 +0100215 int m_NumInferences; // Referenced by test cases.
216 int m_NumCorrectInferences; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000217
218 std::string m_ValidationFileIn;
telsoa01c577f2c2018-08-31 09:22:23 +0100219 std::vector<unsigned int> m_ValidationPredictions; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000220
221 std::string m_ValidationFileOut;
telsoa01c577f2c2018-08-31 09:22:23 +0100222 std::vector<unsigned int> m_ValidationPredictionsOut; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000223};
224
225bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider,
226 InferenceTestOptions& outParams);
227
228bool ValidateDirectory(std::string& dir);
229
230bool InferenceTest(const InferenceTestOptions& params,
231 const std::vector<unsigned int>& defaultTestCaseIds,
232 IInferenceTestCaseProvider& testCaseProvider);
233
234template<typename TConstructTestCaseProvider>
235int InferenceTestMain(int argc,
236 char* argv[],
237 const std::vector<unsigned int>& defaultTestCaseIds,
238 TConstructTestCaseProvider constructTestCaseProvider);
239
240template<typename TDatabase,
241 typename TParser,
242 typename TConstructDatabaseCallable>
243int ClassifierInferenceTestMain(int argc, char* argv[], const char* modelFilename, bool isModelBinary,
244 const char* inputBindingName, const char* outputBindingName,
245 const std::vector<unsigned int>& defaultTestCaseIds,
246 TConstructDatabaseCallable constructDatabase,
247 const armnn::TensorShape* inputTensorShape = nullptr);
248
249} // namespace test
250} // namespace armnn
251
252#include "InferenceTest.inl"