ArmNN
 22.02
TfLiteMobilenetQuantized-Armnn.cpp File Reference
#include "../InferenceTest.hpp"
#include "../ImagePreprocessor.hpp"
#include "armnnTfLiteParser/ITfLiteParser.hpp"
#include <cxxopts/cxxopts.hpp>
#include <fstream>

Go to the source code of this file.

Functions

std::vector< ImageSetParseDataset (const std::string &filename)
 
std::string GetLabelsFilenameFromOptions (int argc, char *argv[])
 
int main (int argc, char *argv[])
 

Function Documentation

◆ GetLabelsFilenameFromOptions()

std::string GetLabelsFilenameFromOptions ( int  argc,
char *  argv[] 
)

Definition at line 62 of file TfLiteMobilenetQuantized-Armnn.cpp.

Referenced by main().

63 {
64  cxxopts::Options options("TfLiteMobilenetQuantized-Armnn","Validation Options");
65 
66  std::string fileName;
67  try
68  {
69  options
70  .allow_unrecognised_options()
71  .add_options()
72  ("l,labels",
73  "Filename of a text file where in each line contains an image "
74  "filename and the correct label the network should predict when fed that image",
75  cxxopts::value<std::string>(fileName));
76 
77  auto result = options.parse(argc, argv);
78  }
79  catch (const cxxopts::OptionException& e)
80  {
81  std::cerr << e.what() << std::endl;
82  exit(EXIT_FAILURE);
83  }
84  catch (const std::exception& e)
85  {
86  std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
87  exit(EXIT_FAILURE);
88  }
89 
90  return fileName;
91 }

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 94 of file TfLiteMobilenetQuantized-Armnn.cpp.

References armnn::test::ClassifierInferenceTestMain(), GetLabelsFilenameFromOptions(), and ParseDataset().

95 {
96  int retVal = EXIT_FAILURE;
97  try
98  {
99  // Coverity fix: The following code may throw an exception of type std::length_error.
100  const std::string labels_file = GetLabelsFilenameFromOptions(argc,argv);
101  std::vector<ImageSet> imageSet = ParseDataset(labels_file);
102  std::vector<unsigned int> indices(imageSet.size());
103  std::generate(indices.begin(), indices.end(), [n = 0] () mutable { return n++; });
104 
105  armnn::TensorShape inputTensorShape({ 1, 224, 224, 3 });
106 
107  using DataType = uint8_t;
108  using DatabaseType = ImagePreprocessor<DataType>;
109  using ParserType = armnnTfLiteParser::ITfLiteParser;
110  using ModelType = InferenceModel<ParserType, DataType>;
111 
112  // Coverity fix: ClassifierInferenceTestMain() may throw uncaught exceptions.
113  retVal = armnn::test::ClassifierInferenceTestMain<DatabaseType,
114  ParserType>(
115  argc, argv,
116  "mobilenet_v1_1.0_224_quant.tflite", // model name
117  true, // model is binary
118  "input", // input tensor name
119  "MobilenetV1/Predictions/Reshape_1", // output tensor name
120  indices, // vector of indices to select which images to validate
121  [&imageSet](const char* dataDir, const ModelType &) {
122  // we need to get the input quantization parameters from
123  // the parsed model
124  return DatabaseType(
125  dataDir,
126  224,
127  224,
128  imageSet,
129  1);
130  },
131  &inputTensorShape);
132  }
133  catch (const std::exception& e)
134  {
135  // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
136  // exception of type std::length_error.
137  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
138  std::cerr << "WARNING: " << *argv << ": An error has occurred when running "
139  "the classifier inference tests: " << e.what() << std::endl;
140  }
141  return retVal;
142 }
std::vector< ImageSet > ParseDataset(const std::string &filename)
std::string GetLabelsFilenameFromOptions(int argc, char *argv[])
DataType
Definition: Types.hpp:35
int ClassifierInferenceTestMain(int argc, char *argv[], const char *modelFilename, bool isModelBinary, const char *inputBindingName, const char *outputBindingName, const std::vector< unsigned int > &defaultTestCaseIds, TConstructDatabaseCallable constructDatabase, const armnn::TensorShape *inputTensorShape=nullptr)

◆ ParseDataset()

std::vector<ImageSet> ParseDataset ( const std::string &  filename)

Definition at line 14 of file TfLiteMobilenetQuantized-Armnn.cpp.

Referenced by main().

15 {
16  std::ifstream read(filename);
17  std::vector<ImageSet> imageSet;
18  if (read.is_open())
19  {
20  // Get the images and the correct corresponding label from the given file
21  for (std::string line; std::getline(read, line);)
22  {
23  stringstream ss(line);
24  std::string image_name;
25  std::string label;
26  getline(ss, image_name, ' ');
27  getline(ss, label, ' ');
28  imageSet.push_back(ImageSet(image_name, std::stoi(label)));
29  }
30  }
31  else
32  {
33  // Use the default images
34  imageSet.push_back(ImageSet("Dog.jpg", 209));
35  // top five predictions in tensorflow:
36  // -----------------------------------
37  // 209:Labrador retriever 0.949995
38  // 160:Rhodesian ridgeback 0.0270182
39  // 208:golden retriever 0.0192866
40  // 853:tennis ball 0.000470382
41  // 239:Greater Swiss Mountain dog 0.000464451
42  imageSet.push_back(ImageSet("Cat.jpg", 283));
43  // top five predictions in tensorflow:
44  // -----------------------------------
45  // 283:tiger cat 0.579016
46  // 286:Egyptian cat 0.319676
47  // 282:tabby, tabby cat 0.0873346
48  // 288:lynx, catamount 0.011163
49  // 289:leopard, Panthera pardus 0.000856755
50  imageSet.push_back(ImageSet("shark.jpg", 3));
51  // top five predictions in tensorflow:
52  // -----------------------------------
53  // 3:great white shark, white shark, ... 0.996926
54  // 4:tiger shark, Galeocerdo cuvieri 0.00270528
55  // 149:killer whale, killer, orca, ... 0.000121848
56  // 395:sturgeon 7.78977e-05
57  // 5:hammerhead, hammerhead shark 6.44127e-055
58  };
59  return imageSet;
60 }
std::pair< const std::string, unsigned int > ImageSet
Tf requires RGB images, normalized in range [0, 1] and resized using Bilinear algorithm.