blob: 40c9e5e597cd4cf5f684cbb0bdddaab50464f82b [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) {};
Matthew Bentham3e68b972019-04-09 13:10:46 +010094 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) { return true; };
telsoa014fcda012018-03-09 14:13:49 +000095 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:
Ferran Balaguerc602f292019-02-08 17:09:55 +0000103 using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>;
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000104
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();
Ferran Balaguerc602f292019-02-08 17:09:55 +0000115 m_Outputs.reserve(numOutputs);
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000116
117 for (size_t i = 0; i < numOutputs; i++)
118 {
Ferran Balaguerc602f292019-02-08 17:09:55 +0000119 m_Outputs.push_back(std::vector<typename TModel::DataType>(outputSizes[i]));
Aron Virginas-Tar7cf0eaa2019-01-24 17:05:36 +0000120 }
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
139template <typename TTestCaseDatabase, typename TModel>
140class ClassifierTestCase : public InferenceModelTestCase<TModel>
141{
142public:
143 ClassifierTestCase(int& numInferencesRef,
144 int& numCorrectInferencesRef,
145 const std::vector<unsigned int>& validationPredictions,
146 std::vector<unsigned int>* validationPredictionsOut,
147 TModel& model,
148 unsigned int testCaseId,
149 unsigned int label,
150 std::vector<typename TModel::DataType> modelInput);
151
152 virtual TestCaseResult ProcessResult(const InferenceTestOptions& params) override;
153
154private:
155 unsigned int m_Label;
telsoa01c577f2c2018-08-31 09:22:23 +0100156 InferenceModelInternal::QuantizationParams m_QuantizationParams;
157
telsoa014fcda012018-03-09 14:13:49 +0000158 /// These fields reference the corresponding member in the ClassifierTestCaseProvider.
159 /// @{
160 int& m_NumInferencesRef;
161 int& m_NumCorrectInferencesRef;
162 const std::vector<unsigned int>& m_ValidationPredictions;
163 std::vector<unsigned int>* m_ValidationPredictionsOut;
164 /// @}
165};
166
167template <typename TDatabase, typename InferenceModel>
168class ClassifierTestCaseProvider : public IInferenceTestCaseProvider
169{
170public:
171 template <typename TConstructDatabaseCallable, typename TConstructModelCallable>
172 ClassifierTestCaseProvider(TConstructDatabaseCallable constructDatabase, TConstructModelCallable constructModel);
173
174 virtual void AddCommandLineOptions(boost::program_options::options_description& options) override;
Matthew Bentham3e68b972019-04-09 13:10:46 +0100175 virtual bool ProcessCommandLineOptions(const InferenceTestOptions &commonOptions) override;
telsoa014fcda012018-03-09 14:13:49 +0000176 virtual std::unique_ptr<IInferenceTestCase> GetTestCase(unsigned int testCaseId) override;
177 virtual bool OnInferenceTestFinished() override;
178
179private:
180 void ReadPredictions();
181
182 typename InferenceModel::CommandLineOptions m_ModelCommandLineOptions;
Matthew Bentham3e68b972019-04-09 13:10:46 +0100183 std::function<std::unique_ptr<InferenceModel>(const InferenceTestOptions& commonOptions,
184 typename InferenceModel::CommandLineOptions)> m_ConstructModel;
telsoa014fcda012018-03-09 14:13:49 +0000185 std::unique_ptr<InferenceModel> m_Model;
186
187 std::string m_DataDir;
telsoa01c577f2c2018-08-31 09:22:23 +0100188 std::function<TDatabase(const char*, const InferenceModel&)> m_ConstructDatabase;
telsoa014fcda012018-03-09 14:13:49 +0000189 std::unique_ptr<TDatabase> m_Database;
190
telsoa01c577f2c2018-08-31 09:22:23 +0100191 int m_NumInferences; // Referenced by test cases.
192 int m_NumCorrectInferences; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000193
194 std::string m_ValidationFileIn;
telsoa01c577f2c2018-08-31 09:22:23 +0100195 std::vector<unsigned int> m_ValidationPredictions; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000196
197 std::string m_ValidationFileOut;
telsoa01c577f2c2018-08-31 09:22:23 +0100198 std::vector<unsigned int> m_ValidationPredictionsOut; // Referenced by test cases.
telsoa014fcda012018-03-09 14:13:49 +0000199};
200
201bool ParseCommandLine(int argc, char** argv, IInferenceTestCaseProvider& testCaseProvider,
202 InferenceTestOptions& outParams);
203
204bool ValidateDirectory(std::string& dir);
205
206bool InferenceTest(const InferenceTestOptions& params,
207 const std::vector<unsigned int>& defaultTestCaseIds,
208 IInferenceTestCaseProvider& testCaseProvider);
209
210template<typename TConstructTestCaseProvider>
211int InferenceTestMain(int argc,
212 char* argv[],
213 const std::vector<unsigned int>& defaultTestCaseIds,
214 TConstructTestCaseProvider constructTestCaseProvider);
215
216template<typename TDatabase,
217 typename TParser,
218 typename TConstructDatabaseCallable>
219int ClassifierInferenceTestMain(int argc, char* argv[], const char* modelFilename, bool isModelBinary,
220 const char* inputBindingName, const char* outputBindingName,
221 const std::vector<unsigned int>& defaultTestCaseIds,
222 TConstructDatabaseCallable constructDatabase,
223 const armnn::TensorShape* inputTensorShape = nullptr);
224
225} // namespace test
226} // namespace armnn
227
228#include "InferenceTest.inl"