Release 18.08
[platform/upstream/armnn.git] / tests / TfLiteMobilenetQuantized-Armnn / TfLiteMobilenetQuantized-Armnn.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5 #include "../InferenceTest.hpp"
6 #include "../ImagePreprocessor.hpp"
7 #include "armnnTfLiteParser/ITfLiteParser.hpp"
8
9 using namespace armnnTfLiteParser;
10
11 int main(int argc, char* argv[])
12 {
13     int retVal = EXIT_FAILURE;
14     try
15     {
16         // Coverity fix: The following code may throw an exception of type std::length_error.
17         std::vector<ImageSet> imageSet =
18         {
19             {"Dog.jpg", 209},
20             // top five predictions in tensorflow:
21             // -----------------------------------
22             // 209:Labrador retriever 0.949995
23             // 160:Rhodesian ridgeback 0.0270182
24             // 208:golden retriever 0.0192866
25             // 853:tennis ball 0.000470382
26             // 239:Greater Swiss Mountain dog 0.000464451
27             {"Cat.jpg", 283},
28             // top five predictions in tensorflow:
29             // -----------------------------------
30             // 283:tiger cat 0.579016
31             // 286:Egyptian cat 0.319676
32             // 282:tabby, tabby cat 0.0873346
33             // 288:lynx, catamount 0.011163
34             // 289:leopard, Panthera pardus 0.000856755
35             {"shark.jpg", 3},
36             // top five predictions in tensorflow:
37             // -----------------------------------
38             // 3:great white shark, white shark, ... 0.996926
39             // 4:tiger shark, Galeocerdo cuvieri 0.00270528
40             // 149:killer whale, killer, orca, ... 0.000121848
41             // 395:sturgeon 7.78977e-05
42             // 5:hammerhead, hammerhead shark 6.44127e-055
43         };
44
45         armnn::TensorShape inputTensorShape({ 1, 224, 224, 3  });
46
47         using DataType = uint8_t;
48         using DatabaseType = ImagePreprocessor<DataType>;
49         using ParserType = armnnTfLiteParser::ITfLiteParser;
50         using ModelType = InferenceModel<ParserType, DataType>;
51
52         // Coverity fix: ClassifierInferenceTestMain() may throw uncaught exceptions.
53         retVal = armnn::test::ClassifierInferenceTestMain<DatabaseType,
54                                                           ParserType>(
55                      argc, argv,
56                      "mobilenet_v1_1.0_224_quant.tflite", // model name
57                      true,                                // model is binary
58                      "input",                             // input tensor name
59                      "MobilenetV1/Predictions/Reshape_1", // output tensor name
60                      { 0, 1, 2 },                         // test images to test with as above
61                      [&imageSet](const char* dataDir, const ModelType & model) {
62                          // we need to get the input quantization parameters from
63                          // the parsed model
64                          auto inputBinding = model.GetInputBindingInfo();
65                          return DatabaseType(
66                              dataDir,
67                              224,
68                              224,
69                              imageSet,
70                              inputBinding.second.GetQuantizationScale(),
71                              inputBinding.second.GetQuantizationOffset());
72                      },
73                      &inputTensorShape);
74     }
75     catch (const std::exception& e)
76     {
77         // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
78         // exception of type std::length_error.
79         // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
80         std::cerr << "WARNING: " << *argv << ": An error has occurred when running "
81                      "the classifier inference tests: " << e.what() << std::endl;
82     }
83     return retVal;
84 }