blob: 7b7dcecea0871e6921e48ca8c7058d300dccd76c [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>
Derek Lamberti08446972019-11-26 16:38:31 +00008#include <armnn/Logging.hpp>
David Beckf0b48452018-10-19 15:20:56 +01009#include <armnn/TypesUtils.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010010#include "InferenceModel.hpp"
11
telsoa014fcda012018-03-09 14:13:49 +000012
telsoa014fcda012018-03-09 14:13:49 +000013#include <boost/program_options.hpp>
14
telsoa01c577f2c2018-08-31 09:22:23 +010015
telsoa014fcda012018-03-09 14:13:49 +000016namespace armnn
17{
18
19inline std::istream& operator>>(std::istream& in, armnn::Compute& compute)
20{
21 std::string token;
22 in >> token;
23 compute = armnn::ParseComputeDevice(token.c_str());
24 if (compute == armnn::Compute::Undefined)
25 {
26 in.setstate(std::ios_base::failbit);
27 throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
28 }
29 return in;
30}
31
David Beckf0b48452018-10-19 15:20:56 +010032inline std::istream& operator>>(std::istream& in, armnn::BackendId& backend)
33{
34 std::string token;
35 in >> token;
36 armnn::Compute compute = armnn::ParseComputeDevice(token.c_str());
37 if (compute == armnn::Compute::Undefined)
38 {
39 in.setstate(std::ios_base::failbit);
40 throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
41 }
42 backend = compute;
43 return in;
44}
45
telsoa014fcda012018-03-09 14:13:49 +000046namespace test
47{
48
49class TestFrameworkException : public Exception
50{
51public:
52 using Exception::Exception;
53};
54
55struct InferenceTestOptions
56{
57 unsigned int m_IterationCount;
58 std::string m_InferenceTimesFile;
telsoa01c577f2c2018-08-31 09:22:23 +010059 bool m_EnableProfiling;
Matteo Martincigh00dda4a2019-08-14 11:42:30 +010060 std::string m_DynamicBackendsPath;
telsoa014fcda012018-03-09 14:13:49 +000061
62 InferenceTestOptions()
Matteo Martincigh00dda4a2019-08-14 11:42:30 +010063 : m_IterationCount(0)
64 , m_EnableProfiling(0)
65 , m_DynamicBackendsPath()
telsoa014fcda012018-03-09 14:13:49 +000066 {}
67};
68
69enum class TestCaseResult
70{
71 /// The test completed without any errors.
72 Ok,
73 /// The test failed (e.g. the prediction didn't match the validation file).
74 /// This will eventually fail the whole program but the remaining test cases will still be run.
75 Failed,
76 /// The test failed with a fatal error. The remaining tests will not be run.
77 Abort
78};
79
80class IInferenceTestCase
81{
82public:
83 virtual ~IInferenceTestCase() {}
84
85 virtual void Run() = 0;
86 virtual TestCaseResult ProcessResult(const InferenceTestOptions& options) = 0;
87};
88
89class IInferenceTestCaseProvider
90{
91public:
92 virtual ~IInferenceTestCaseProvider() {}
93
94 virtual void AddCommandLineOptions(boost::program_options::options_description& options) {};
Matthew Bentham3e68b972019-04-09 13:10:46 +010095 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) { return true; };
telsoa014fcda012018-03-09 14:13:49 +000096 virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) = 0;
97 virtual bool OnInferenceTestFinished() { return true; };
98};
99
100template <typename TModel>
101class InferenceModelTestCase : public IInferenceTestCase
102{
103public:
Ferran Balaguerc602f292019-02-08 17:09:55 +0000104 using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>;
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000105
telsoa014fcda012018-03-09 14:13:49 +0000106 InferenceModelTestCase(TModel& model,
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000107 unsigned int testCaseId,
108 const std::vector<TContainer>& inputs,
109 const std::vector<unsigned int>& outputSizes)
telsoa014fcda012018-03-09 14:13:49 +0000110 : m_Model(model)
111 , m_TestCaseId(testCaseId)
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000112 , m_Inputs(std::move(inputs))
telsoa014fcda012018-03-09 14:13:49 +0000113 {
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000114 // Initialize output vector
115 const size_t numOutputs = outputSizes.size();
Ferran Balaguerc602f292019-02-08 17:09:55 +0000116 m_Outputs.reserve(numOutputs);
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000117
118 for (size_t i = 0; i < numOutputs; i++)
119 {
Ferran Balaguerc602f292019-02-08 17:09:55 +0000120 m_Outputs.push_back(std::vector<typename TModel::DataType>(outputSizes[i]));
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000121 }
telsoa014fcda012018-03-09 14:13:49 +0000122 }
123
124 virtual void Run() override
125 {
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000126 m_Model.Run(m_Inputs, m_Outputs);
telsoa014fcda012018-03-09 14:13:49 +0000127 }
128
129protected:
130 unsigned int GetTestCaseId() const { return m_TestCaseId; }
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000131 const std::vector<TContainer>& GetOutputs() const { return m_Outputs; }
telsoa014fcda012018-03-09 14:13:49 +0000132
133private:
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000134 TModel& m_Model;
135 unsigned int m_TestCaseId;
136 std::vector<TContainer> m_Inputs;
137 std::vector<TContainer> m_Outputs;
telsoa014fcda012018-03-09 14:13:49 +0000138};
139
140template <typename TTestCaseDatabase, typename TModel>
141class ClassifierTestCase : public InferenceModelTestCase<TModel>
142{
143public:
144 ClassifierTestCase(int& numInferencesRef,
145 int& numCorrectInferencesRef,
146 const std::vector<unsigned int>& validationPredictions,
147 std::vector<unsigned int>* validationPredictionsOut,
148 TModel& model,
149 unsigned int testCaseId,
150 unsigned int label,
151 std::vector<typename TModel::DataType> modelInput);
152
153 virtual TestCaseResult ProcessResult(const InferenceTestOptions& params) override;
154
155private:
156 unsigned int m_Label;
telsoa01c577f2c2018-08-31 09:22:23 +0100157 InferenceModelInternal::QuantizationParams m_QuantizationParams;
158
telsoa014fcda012018-03-09 14:13:49 +0000159 /// These fields reference the corresponding member in the ClassifierTestCaseProvider.
160 /// @{
161 int& m_NumInferencesRef;
162 int& m_NumCorrectInferencesRef;
163 const std::vector<unsigned int>& m_ValidationPredictions;
164 std::vector<unsigned int>* m_ValidationPredictionsOut;
165 /// @}
166};
167
168template <typename TDatabase, typename InferenceModel>
169class ClassifierTestCaseProvider : public IInferenceTestCaseProvider
170{
171public:
172 template <typename TConstructDatabaseCallable, typename TConstructModelCallable>
173 ClassifierTestCaseProvider(TConstructDatabaseCallable constructDatabase, TConstructModelCallable constructModel);
174
175 virtual void AddCommandLineOptions(boost::program_options::options_description& options) override;
Matthew Bentham3e68b972019-04-09 13:10:46 +0100176 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override;
telsoa014fcda012018-03-09 14:13:49 +0000177 virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override;
178 virtual bool OnInferenceTestFinished() override;
179
180private:
181 void ReadPredictions();
182
183 typename InferenceModel::CommandLineOptions m_ModelCommandLineOptions;
Matthew Bentham3e68b972019-04-09 13:10:46 +0100184 std::function<std::unique_ptr<InferenceModel>(const InferenceTestOptions& commonOptions,
185 typename InferenceModel::CommandLineOptions)> m_ConstructModel;
telsoa014fcda012018-03-09 14:13:49 +0000186 std::unique_ptr<InferenceModel> m_Model;
187
188 std::string m_DataDir;
telsoa01c577f2c2018-08-31 09:22:23 +0100189 std::function<TDatabase(const char*, const InferenceModel&)> m_ConstructDatabase;
telsoa014fcda012018-03-09 14:13:49 +0000190 std::unique_ptr<TDatabase> m_Database;
191
telsoa01c577f2c2018-08-31 09:22:23 +0100192 int m_NumInferences; // Referenced by test cases.
193 int m_NumCorrectInferences; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000194
195 std::string m_ValidationFileIn;
telsoa01c577f2c2018-08-31 09:22:23 +0100196 std::vector<unsigned int> m_ValidationPredictions; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000197
198 std::string m_ValidationFileOut;
telsoa01c577f2c2018-08-31 09:22:23 +0100199 std::vector<unsigned int> m_ValidationPredictionsOut; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000200};
201
202bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider,
203 InferenceTestOptions& outParams);
204
205bool ValidateDirectory(std::string& dir);
206
207bool InferenceTest(const InferenceTestOptions& params,
208 const std::vector<unsigned int>& defaultTestCaseIds,
209 IInferenceTestCaseProvider& testCaseProvider);
210
211template<typename TConstructTestCaseProvider>
212int InferenceTestMain(int argc,
213 char* argv[],
214 const std::vector<unsigned int>& defaultTestCaseIds,
215 TConstructTestCaseProvider constructTestCaseProvider);
216
217template<typename TDatabase,
218 typename TParser,
219 typename TConstructDatabaseCallable>
220int ClassifierInferenceTestMain(int argc, char* argv[], const char* modelFilename, bool isModelBinary,
221 const char* inputBindingName, const char* outputBindingName,
222 const std::vector<unsigned int>& defaultTestCaseIds,
223 TConstructDatabaseCallable constructDatabase,
224 const armnn::TensorShape* inputTensorShape = nullptr);
225
226} // namespace test
227} // namespace armnn
228
229#include "InferenceTest.inl"