blob: f2b8c634cc324f34bd695485f6cc6523e8fac2ea [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;
Matteo Martincigh00dda4a2019-08-14 11:42:30 +010061 std::string m_DynamicBackendsPath;
telsoa014fcda012018-03-09 14:13:49 +000062
63 InferenceTestOptions()
Matteo Martincigh00dda4a2019-08-14 11:42:30 +010064 : m_IterationCount(0)
65 , m_EnableProfiling(0)
66 , m_DynamicBackendsPath()
telsoa014fcda012018-03-09 14:13:49 +000067 {}
68};
69
70enum class TestCaseResult
71{
72 /// The test completed without any errors.
73 Ok,
74 /// The test failed (e.g. the prediction didn't match the validation file).
75 /// This will eventually fail the whole program but the remaining test cases will still be run.
76 Failed,
77 /// The test failed with a fatal error. The remaining tests will not be run.
78 Abort
79};
80
81class IInferenceTestCase
82{
83public:
84 virtual ~IInferenceTestCase() {}
85
86 virtual void Run() = 0;
87 virtual TestCaseResult ProcessResult(const InferenceTestOptions& options) = 0;
88};
89
90class IInferenceTestCaseProvider
91{
92public:
93 virtual ~IInferenceTestCaseProvider() {}
94
95 virtual void AddCommandLineOptions(boost::program_options::options_description& options) {};
Matthew Bentham3e68b972019-04-09 13:10:46 +010096 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) { return true; };
telsoa014fcda012018-03-09 14:13:49 +000097 virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) = 0;
98 virtual bool OnInferenceTestFinished() { return true; };
99};
100
101template <typename TModel>
102class InferenceModelTestCase : public IInferenceTestCase
103{
104public:
Ferran Balaguerc602f292019-02-08 17:09:55 +0000105 using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>;
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000106
telsoa014fcda012018-03-09 14:13:49 +0000107 InferenceModelTestCase(TModel& model,
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000108 unsigned int testCaseId,
109 const std::vector<TContainer>& inputs,
110 const std::vector<unsigned int>& outputSizes)
telsoa014fcda012018-03-09 14:13:49 +0000111 : m_Model(model)
112 , m_TestCaseId(testCaseId)
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000113 , m_Inputs(std::move(inputs))
telsoa014fcda012018-03-09 14:13:49 +0000114 {
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000115 // Initialize output vector
116 const size_t numOutputs = outputSizes.size();
Ferran Balaguerc602f292019-02-08 17:09:55 +0000117 m_Outputs.reserve(numOutputs);
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000118
119 for (size_t i = 0; i < numOutputs; i++)
120 {
Ferran Balaguerc602f292019-02-08 17:09:55 +0000121 m_Outputs.push_back(std::vector<typename TModel::DataType>(outputSizes[i]));
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000122 }
telsoa014fcda012018-03-09 14:13:49 +0000123 }
124
125 virtual void Run() override
126 {
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000127 m_Model.Run(m_Inputs, m_Outputs);
telsoa014fcda012018-03-09 14:13:49 +0000128 }
129
130protected:
131 unsigned int GetTestCaseId() const { return m_TestCaseId; }
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000132 const std::vector<TContainer>& GetOutputs() const { return m_Outputs; }
telsoa014fcda012018-03-09 14:13:49 +0000133
134private:
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000135 TModel& m_Model;
136 unsigned int m_TestCaseId;
137 std::vector<TContainer> m_Inputs;
138 std::vector<TContainer> m_Outputs;
telsoa014fcda012018-03-09 14:13:49 +0000139};
140
141template <typename TTestCaseDatabase, typename TModel>
142class ClassifierTestCase : public InferenceModelTestCase<TModel>
143{
144public:
145 ClassifierTestCase(int& numInferencesRef,
146 int& numCorrectInferencesRef,
147 const std::vector<unsigned int>& validationPredictions,
148 std::vector<unsigned int>* validationPredictionsOut,
149 TModel& model,
150 unsigned int testCaseId,
151 unsigned int label,
152 std::vector<typename TModel::DataType> modelInput);
153
154 virtual TestCaseResult ProcessResult(const InferenceTestOptions& params) override;
155
156private:
157 unsigned int m_Label;
telsoa01c577f2c2018-08-31 09:22:23 +0100158 InferenceModelInternal::QuantizationParams m_QuantizationParams;
159
telsoa014fcda012018-03-09 14:13:49 +0000160 /// These fields reference the corresponding member in the ClassifierTestCaseProvider.
161 /// @{
162 int& m_NumInferencesRef;
163 int& m_NumCorrectInferencesRef;
164 const std::vector<unsigned int>& m_ValidationPredictions;
165 std::vector<unsigned int>* m_ValidationPredictionsOut;
166 /// @}
167};
168
169template <typename TDatabase, typename InferenceModel>
170class ClassifierTestCaseProvider : public IInferenceTestCaseProvider
171{
172public:
173 template <typename TConstructDatabaseCallable, typename TConstructModelCallable>
174 ClassifierTestCaseProvider(TConstructDatabaseCallable constructDatabase, TConstructModelCallable constructModel);
175
176 virtual void AddCommandLineOptions(boost::program_options::options_description& options) override;
Matthew Bentham3e68b972019-04-09 13:10:46 +0100177 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override;
telsoa014fcda012018-03-09 14:13:49 +0000178 virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override;
179 virtual bool OnInferenceTestFinished() override;
180
181private:
182 void ReadPredictions();
183
184 typename InferenceModel::CommandLineOptions m_ModelCommandLineOptions;
Matthew Bentham3e68b972019-04-09 13:10:46 +0100185 std::function<std::unique_ptr<InferenceModel>(const InferenceTestOptions& commonOptions,
186 typename InferenceModel::CommandLineOptions)> m_ConstructModel;
telsoa014fcda012018-03-09 14:13:49 +0000187 std::unique_ptr<InferenceModel> m_Model;
188
189 std::string m_DataDir;
telsoa01c577f2c2018-08-31 09:22:23 +0100190 std::function<TDatabase(const char*, const InferenceModel&)> m_ConstructDatabase;
telsoa014fcda012018-03-09 14:13:49 +0000191 std::unique_ptr<TDatabase> m_Database;
192
telsoa01c577f2c2018-08-31 09:22:23 +0100193 int m_NumInferences; // Referenced by test cases.
194 int m_NumCorrectInferences; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000195
196 std::string m_ValidationFileIn;
telsoa01c577f2c2018-08-31 09:22:23 +0100197 std::vector<unsigned int> m_ValidationPredictions; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000198
199 std::string m_ValidationFileOut;
telsoa01c577f2c2018-08-31 09:22:23 +0100200 std::vector<unsigned int> m_ValidationPredictionsOut; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000201};
202
203bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider,
204 InferenceTestOptions& outParams);
205
206bool ValidateDirectory(std::string& dir);
207
208bool InferenceTest(const InferenceTestOptions& params,
209 const std::vector<unsigned int>& defaultTestCaseIds,
210 IInferenceTestCaseProvider& testCaseProvider);
211
212template<typename TConstructTestCaseProvider>
213int InferenceTestMain(int argc,
214 char* argv[],
215 const std::vector<unsigned int>& defaultTestCaseIds,
216 TConstructTestCaseProvider constructTestCaseProvider);
217
218template<typename TDatabase,
219 typename TParser,
220 typename TConstructDatabaseCallable>
221int ClassifierInferenceTestMain(int argc, char* argv[], const char* modelFilename, bool isModelBinary,
222 const char* inputBindingName, const char* outputBindingName,
223 const std::vector<unsigned int>& defaultTestCaseIds,
224 TConstructDatabaseCallable constructDatabase,
225 const armnn::TensorShape* inputTensorShape = nullptr);
226
227} // namespace test
228} // namespace armnn
229
230#include "InferenceTest.inl"