NNXSW-1853 Change SubgraphViewSelector algorithm
[platform/upstream/armnn.git] / tests / InferenceTest.inl
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "InferenceTest.hpp"
6
7 #include <boost/algorithm/string.hpp>
8 #include <boost/numeric/conversion/cast.hpp>
9 #include <boost/log/trivial.hpp>
10 #include <boost/filesystem/path.hpp>
11 #include <boost/assert.hpp>
12 #include <boost/format.hpp>
13 #include <boost/program_options.hpp>
14 #include <boost/filesystem/operations.hpp>
15
16 #include <fstream>
17 #include <iostream>
18 #include <iomanip>
19 #include <array>
20 #include <chrono>
21
22 using namespace std;
23 using namespace std::chrono;
24 using namespace armnn::test;
25
26 namespace armnn
27 {
28 namespace test
29 {
30
31 using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>;
32
33 template <typename TTestCaseDatabase, typename TModel>
34 ClassifierTestCase<TTestCaseDatabase, TModel>::ClassifierTestCase(
35     int& numInferencesRef,
36     int& numCorrectInferencesRef,
37     const std::vector<unsigned int>& validationPredictions,
38     std::vector<unsigned int>* validationPredictionsOut,
39     TModel& model,
40     unsigned int testCaseId,
41     unsigned int label,
42     std::vector<typename TModel::DataType> modelInput)
43     : InferenceModelTestCase<TModel>(
44             model, testCaseId, std::vector<TContainer>{ modelInput }, { model.GetOutputSize() })
45     , m_Label(label)
46     , m_QuantizationParams(model.GetQuantizationParams())
47     , m_NumInferencesRef(numInferencesRef)
48     , m_NumCorrectInferencesRef(numCorrectInferencesRef)
49     , m_ValidationPredictions(validationPredictions)
50     , m_ValidationPredictionsOut(validationPredictionsOut)
51 {
52 }
53
54 struct ClassifierResultProcessor : public boost::static_visitor<>
55 {
56     using ResultMap = std::map<float,int>;
57
58     ClassifierResultProcessor(float scale, int offset)
59         : m_Scale(scale)
60         , m_Offset(offset)
61     {}
62
63     void operator()(const std::vector<float>& values)
64     {
65         SortPredictions(values, [](float value)
66                                 {
67                                     return value;
68                                 });
69     }
70
71     void operator()(const std::vector<uint8_t>& values)
72     {
73         auto& scale = m_Scale;
74         auto& offset = m_Offset;
75         SortPredictions(values, [&scale, &offset](uint8_t value)
76                                 {
77                                     return armnn::Dequantize(value, scale, offset);
78                                 });
79     }
80
81     void operator()(const std::vector<int>& values)
82     {
83         BOOST_ASSERT_MSG(false, "Non-float predictions output not supported.");
84     }
85
86     ResultMap& GetResultMap() { return m_ResultMap; }
87
88 private:
89     template<typename Container, typename Delegate>
90     void SortPredictions(const Container& c, Delegate delegate)
91     {
92         int index = 0;
93         for (const auto& value : c)
94         {
95             int classification = index++;
96             // Take the first class with each probability
97             // This avoids strange results when looping over batched results produced
98             // with identical test data.
99             ResultMap::iterator lb = m_ResultMap.lower_bound(value);
100
101             if (lb == m_ResultMap.end() || !m_ResultMap.key_comp()(value, lb->first))
102             {
103                 // If the key is not already in the map, insert it.
104                 m_ResultMap.insert(lb, ResultMap::value_type(delegate(value), classification));
105             }
106         }
107     }
108
109     ResultMap m_ResultMap;
110
111     float m_Scale=0.0f;
112     int m_Offset=0;
113 };
114
115 template <typename TTestCaseDatabase, typename TModel>
116 TestCaseResult ClassifierTestCase<TTestCaseDatabase, TModel>::ProcessResult(const InferenceTestOptions& params)
117 {
118     auto& output = this->GetOutputs()[0];
119     const auto testCaseId = this->GetTestCaseId();
120
121     ClassifierResultProcessor resultProcessor(m_QuantizationParams.first, m_QuantizationParams.second);
122     boost::apply_visitor(resultProcessor, output);
123
124     BOOST_LOG_TRIVIAL(info) << "= Prediction values for test #" << testCaseId;
125     auto it = resultProcessor.GetResultMap().rbegin();
126     for (int i=0; i<5 && it != resultProcessor.GetResultMap().rend(); ++i)
127     {
128         BOOST_LOG_TRIVIAL(info) << "Top(" << (i+1) << ") prediction is " << it->second <<
129           " with value: " << (it->first);
130         ++it;
131     }
132
133     unsigned int prediction = 0;
134     boost::apply_visitor([&](auto&& value)
135                          {
136                              prediction = boost::numeric_cast<unsigned int>(
137                                      std::distance(value.begin(), std::max_element(value.begin(), value.end())));
138                          },
139                          output);
140
141     // If we're just running the defaultTestCaseIds, each one must be classified correctly.
142     if (params.m_IterationCount == 0 && prediction != m_Label)
143     {
144         BOOST_LOG_TRIVIAL(error) << "Prediction for test case " << testCaseId << " (" << prediction << ")" <<
145             " is incorrect (should be " << m_Label << ")";
146         return TestCaseResult::Failed;
147     }
148
149     // If a validation file was provided as input, it checks that the prediction matches.
150     if (!m_ValidationPredictions.empty() && prediction != m_ValidationPredictions[testCaseId])
151     {
152         BOOST_LOG_TRIVIAL(error) << "Prediction for test case " << testCaseId << " (" << prediction << ")" <<
153             " doesn't match the prediction in the validation file (" << m_ValidationPredictions[testCaseId] << ")";
154         return TestCaseResult::Failed;
155     }
156
157     // If a validation file was requested as output, it stores the predictions.
158     if (m_ValidationPredictionsOut)
159     {
160         m_ValidationPredictionsOut->push_back(prediction);
161     }
162
163     // Updates accuracy stats.
164     m_NumInferencesRef++;
165     if (prediction == m_Label)
166     {
167         m_NumCorrectInferencesRef++;
168     }
169
170     return TestCaseResult::Ok;
171 }
172
173 template <typename TDatabase, typename InferenceModel>
174 template <typename TConstructDatabaseCallable, typename TConstructModelCallable>
175 ClassifierTestCaseProvider<TDatabase, InferenceModel>::ClassifierTestCaseProvider(
176     TConstructDatabaseCallable constructDatabase, TConstructModelCallable constructModel)
177     : m_ConstructModel(constructModel)
178     , m_ConstructDatabase(constructDatabase)
179     , m_NumInferences(0)
180     , m_NumCorrectInferences(0)
181 {
182 }
183
184 template <typename TDatabase, typename InferenceModel>
185 void ClassifierTestCaseProvider<TDatabase, InferenceModel>::AddCommandLineOptions(
186     boost::program_options::options_description& options)
187 {
188     namespace po = boost::program_options;
189
190     options.add_options()
191         ("validation-file-in", po::value<std::string>(&m_ValidationFileIn)->default_value(""),
192             "Reads expected predictions from the given file and confirms they match the actual predictions.")
193         ("validation-file-out", po::value<std::string>(&m_ValidationFileOut)->default_value(""),
194             "Predictions are saved to the given file for later use via --validation-file-in.")
195         ("data-dir,d", po::value<std::string>(&m_DataDir)->required(),
196             "Path to directory containing test data");
197
198     InferenceModel::AddCommandLineOptions(options, m_ModelCommandLineOptions);
199 }
200
201 template <typename TDatabase, typename InferenceModel>
202 bool ClassifierTestCaseProvider<TDatabase, InferenceModel>::ProcessCommandLineOptions(
203         const InferenceTestOptions& commonOptions)
204 {
205     if (!ValidateDirectory(m_DataDir))
206     {
207         return false;
208     }
209
210     ReadPredictions();
211
212     m_Model = m_ConstructModel(commonOptions, m_ModelCommandLineOptions);
213     if (!m_Model)
214     {
215         return false;
216     }
217
218     m_Database = std::make_unique<TDatabase>(m_ConstructDatabase(m_DataDir.c_str(), *m_Model));
219     if (!m_Database)
220     {
221         return false;
222     }
223
224     return true;
225 }
226
227 template <typename TDatabase, typename InferenceModel>
228 std::unique_ptr<IInferenceTestCase>
229 ClassifierTestCaseProvider<TDatabase, InferenceModel>::GetTestCase(unsigned int testCaseId)
230 {
231     std::unique_ptr<typename TDatabase::TTestCaseData> testCaseData = m_Database->GetTestCaseData(testCaseId);
232     if (testCaseData == nullptr)
233     {
234         return nullptr;
235     }
236
237     return std::make_unique<ClassifierTestCase<TDatabase, InferenceModel>>(
238         m_NumInferences,
239         m_NumCorrectInferences,
240         m_ValidationPredictions,
241         m_ValidationFileOut.empty() ? nullptr : &m_ValidationPredictionsOut,
242         *m_Model,
243         testCaseId,
244         testCaseData->m_Label,
245         std::move(testCaseData->m_InputImage));
246 }
247
248 template <typename TDatabase, typename InferenceModel>
249 bool ClassifierTestCaseProvider<TDatabase, InferenceModel>::OnInferenceTestFinished()
250 {
251     const double accuracy = boost::numeric_cast<double>(m_NumCorrectInferences) /
252         boost::numeric_cast<double>(m_NumInferences);
253     BOOST_LOG_TRIVIAL(info) << std::fixed << std::setprecision(3) << "Overall accuracy: " << accuracy;
254
255     // If a validation file was requested as output, the predictions are saved to it.
256     if (!m_ValidationFileOut.empty())
257     {
258         std::ofstream validationFileOut(m_ValidationFileOut.c_str(), std::ios_base::trunc | std::ios_base::out);
259         if (validationFileOut.good())
260         {
261             for (const unsigned int prediction : m_ValidationPredictionsOut)
262             {
263                 validationFileOut << prediction << std::endl;
264             }
265         }
266         else
267         {
268             BOOST_LOG_TRIVIAL(error) << "Failed to open output validation file: " << m_ValidationFileOut;
269             return false;
270         }
271     }
272
273     return true;
274 }
275
276 template <typename TDatabase, typename InferenceModel>
277 void ClassifierTestCaseProvider<TDatabase, InferenceModel>::ReadPredictions()
278 {
279     // Reads the expected predictions from the input validation file (if provided).
280     if (!m_ValidationFileIn.empty())
281     {
282         std::ifstream validationFileIn(m_ValidationFileIn.c_str(), std::ios_base::in);
283         if (validationFileIn.good())
284         {
285             while (!validationFileIn.eof())
286             {
287                 unsigned int i;
288                 validationFileIn >> i;
289                 m_ValidationPredictions.emplace_back(i);
290             }
291         }
292         else
293         {
294             throw armnn::Exception(boost::str(boost::format("Failed to open input validation file: %1%")
295                 % m_ValidationFileIn));
296         }
297     }
298 }
299
300 template<typename TConstructTestCaseProvider>
301 int InferenceTestMain(int argc,
302     char* argv[],
303     const std::vector<unsigned int>& defaultTestCaseIds,
304     TConstructTestCaseProvider constructTestCaseProvider)
305 {
306     // Configures logging for both the ARMNN library and this test program.
307 #ifdef NDEBUG
308     armnn::LogSeverity level = armnn::LogSeverity::Info;
309 #else
310     armnn::LogSeverity level = armnn::LogSeverity::Debug;
311 #endif
312     armnn::ConfigureLogging(true, true, level);
313     armnnUtils::ConfigureLogging(boost::log::core::get().get(), true, true, level);
314
315     try
316     {
317         std::unique_ptr<IInferenceTestCaseProvider> testCaseProvider = constructTestCaseProvider();
318         if (!testCaseProvider)
319         {
320             return 1;
321         }
322
323         InferenceTestOptions inferenceTestOptions;
324         if (!ParseCommandLine(argc, argv, *testCaseProvider, inferenceTestOptions))
325         {
326             return 1;
327         }
328
329         const bool success = InferenceTest(inferenceTestOptions, defaultTestCaseIds, *testCaseProvider);
330         return success ? 0 : 1;
331     }
332     catch (armnn::Exception const& e)
333     {
334         BOOST_LOG_TRIVIAL(fatal) << "Armnn Error: " << e.what();
335         return 1;
336     }
337 }
338
339 //
340 // This function allows us to create a classifier inference test based on:
341 //  - a model file name
342 //  - which can be a binary or a text file for protobuf formats
343 //  - an input tensor name
344 //  - an output tensor name
345 //  - a set of test case ids
346 //  - a callback method which creates an object that can return images
347 //    called 'Database' in these tests
348 //  - and an input tensor shape
349 //
350 template<typename TDatabase,
351          typename TParser,
352          typename TConstructDatabaseCallable>
353 int ClassifierInferenceTestMain(int argc,
354                                 char* argv[],
355                                 const char* modelFilename,
356                                 bool isModelBinary,
357                                 const char* inputBindingName,
358                                 const char* outputBindingName,
359                                 const std::vector<unsigned int>& defaultTestCaseIds,
360                                 TConstructDatabaseCallable constructDatabase,
361                                 const armnn::TensorShape* inputTensorShape)
362
363 {
364     BOOST_ASSERT(modelFilename);
365     BOOST_ASSERT(inputBindingName);
366     BOOST_ASSERT(outputBindingName);
367
368     return InferenceTestMain(argc, argv, defaultTestCaseIds,
369         [=]
370         ()
371         {
372             using InferenceModel = InferenceModel<TParser, typename TDatabase::DataType>;
373             using TestCaseProvider = ClassifierTestCaseProvider<TDatabase, InferenceModel>;
374
375             return make_unique<TestCaseProvider>(constructDatabase,
376                 [&]
377                 (const InferenceTestOptions &commonOptions,
378                  typename InferenceModel::CommandLineOptions modelOptions)
379                 {
380                     if (!ValidateDirectory(modelOptions.m_ModelDir))
381                     {
382                         return std::unique_ptr<InferenceModel>();
383                     }
384
385                     typename InferenceModel::Params modelParams;
386                     modelParams.m_ModelPath = modelOptions.m_ModelDir + modelFilename;
387                     modelParams.m_InputBindings  = { inputBindingName };
388                     modelParams.m_OutputBindings = { outputBindingName };
389
390                     if (inputTensorShape)
391                     {
392                         modelParams.m_InputShapes.push_back(*inputTensorShape);
393                     }
394
395                     modelParams.m_IsModelBinary = isModelBinary;
396                     modelParams.m_ComputeDevices = modelOptions.GetComputeDevicesAsBackendIds();
397                     modelParams.m_VisualizePostOptimizationModel = modelOptions.m_VisualizePostOptimizationModel;
398                     modelParams.m_EnableFp16TurboMode = modelOptions.m_EnableFp16TurboMode;
399
400                     return std::make_unique<InferenceModel>(modelParams,
401                                                             commonOptions.m_EnableProfiling,
402                                                             commonOptions.m_DynamicBackendsPath);
403             });
404         });
405 }
406
407 } // namespace test
408 } // namespace armnn