10 #include <stb/stb_image.h> 17 #include <cxxopts/cxxopts.hpp> 18 #include <ghc/filesystem.hpp> 26 using namespace armnn;
28 static const int OPEN_FILE_ERROR = -2;
29 static const int OPTIMIZE_NETWORK_ERROR = -3;
30 static const int LOAD_NETWORK_ERROR = -4;
31 static const int LOAD_IMAGE_ERROR = -5;
32 static const int GENERAL_ERROR = -100;
38 if (r_local != 0) { return r_local;} \ 40 catch (const armnn::Exception& e) \ 42 ARMNN_LOG(error) << "Oops: " << e.what(); \ 43 return GENERAL_ERROR; \ 49 template<
typename TContainer>
51 const std::vector<std::reference_wrapper<TContainer>>& inputDataContainers)
55 const size_t numInputs = inputBindings.size();
56 if (numInputs != inputDataContainers.size())
61 for (
size_t i = 0; i < numInputs; i++)
64 const TContainer& inputData = inputDataContainers[i].get();
67 inputTensors.push_back(std::make_pair(inputBinding.first, inputTensor));
73 template<
typename TContainer>
75 const std::vector<armnn::BindingPointInfo>& outputBindings,
76 const std::vector<std::reference_wrapper<TContainer>>& outputDataContainers)
80 const size_t numOutputs = outputBindings.size();
81 if (numOutputs != outputDataContainers.size())
86 outputTensors.reserve(numOutputs);
88 for (
size_t i = 0; i < numOutputs; i++)
91 const TContainer& outputData = outputDataContainers[i].get();
93 armnn::Tensor outputTensor(outputBinding.second, const_cast<float*>(outputData.data()));
94 outputTensors.push_back(std::make_pair(outputBinding.first, outputTensor));
104 const std::vector<BackendId>& backendPreferences,
105 bool enableImport =
false)
107 std::ifstream stream(filename, std::ios::in | std::ios::binary);
108 if (!stream.is_open())
111 return OPEN_FILE_ERROR;
114 std::vector<uint8_t> contents((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
128 return OPTIMIZE_NETWORK_ERROR;
133 std::string errorMessage;
135 Status status = runtime.
LoadNetwork(networkId, std::move(optimizedModel), errorMessage, modelProps);
136 if (status != Status::Success)
138 ARMNN_LOG(
fatal) <<
"Could not load " << filename <<
" model into runtime: " << errorMessage;
139 return LOAD_NETWORK_ERROR;
150 ~Memory() {stbi_image_free(m_Data);}
151 bool IsLoaded()
const {
return m_Data !=
nullptr;}
153 unsigned char* m_Data;
156 std::vector<float> image;
162 Memory mem = {stbi_load(filename, &width, &height, &channels, 3)};
165 ARMNN_LOG(
error) <<
"Could not load input image file: " << filename;
169 if (width != 1920 || height != 1080 || channels != 3)
171 ARMNN_LOG(
error) <<
"Input image has wong dimension: " << width <<
"x" << height <<
"x" << channels <<
". " 172 " Expected 1920x1080x3.";
176 image.resize(1920*1080*3);
179 for (
unsigned int idx=0; idx <= 1920*1080*3; idx++)
181 image[idx] =
static_cast<float>(mem.m_Data[idx]) /255.0f;
190 if (!ghc::filesystem::exists(file))
192 std::cerr <<
"Given file path " << file <<
" does not exist" << std::endl;
195 if (!ghc::filesystem::is_regular_file(file))
197 std::cerr <<
"Given file path " << file <<
" is not a regular file" << std::endl;
203 void CheckAccuracy(std::vector<float>* toDetector0, std::vector<float>* toDetector1,
204 std::vector<float>* toDetector2, std::vector<float>* detectorOutput,
205 const std::vector<yolov3::Detection>& nmsOut,
const std::vector<std::string>& filePaths)
207 std::ifstream pathStream;
208 std::vector<float> expected;
209 std::vector<std::vector<float>*> outputs;
211 unsigned int count = 0;
214 outputs.push_back(toDetector0);
215 outputs.push_back(toDetector1);
216 outputs.push_back(toDetector2);
217 outputs.push_back(detectorOutput);
219 for (
unsigned int i = 0; i < outputs.size(); ++i)
222 pathStream.open(filePaths[i]);
223 if (!pathStream.is_open())
225 ARMNN_LOG(
error) <<
"Expected output file can not be opened: " << filePaths[i];
229 expected.assign(std::istream_iterator<float>(pathStream), {});
234 if (expected.size() != outputs[i]->size())
236 ARMNN_LOG(
error) <<
"Expected output size does not match actual output size: " << filePaths[i];
243 for (
unsigned int j = 0; j < outputs[i]->size(); ++j)
245 compare =
abs(expected[j] - outputs[i]->at(j));
246 if (compare > 0.001f)
253 ARMNN_LOG(
error) << count <<
" output(s) do not match expected values in: " << filePaths[i];
258 pathStream.open(filePaths[4]);
259 if (!pathStream.is_open())
261 ARMNN_LOG(
error) <<
"Expected output file can not be opened: " << filePaths[4];
265 expected.assign(std::istream_iterator<float>(pathStream), {});
269 unsigned int numOfMember = 6;
270 std::vector<float> intermediate;
272 for (
auto& detection: nmsOut)
274 for (
unsigned int x = y * numOfMember; x < ((y * numOfMember) + numOfMember); ++x)
276 intermediate.push_back(expected[x]);
280 ARMNN_LOG(
error) <<
"Expected NMS output does not match: Detection " << y + 1;
282 intermediate.clear();
290 ParseArgs(
int ac,
char *av[]) : options{
"TfLiteYoloV3Big-Armnn",
291 "Executes YoloV3Big using ArmNN. YoloV3Big consists " 292 "of 3 parts: A backbone TfLite model, a detector TfLite " 293 "model, and None Maximum Suppression. All parts are " 294 "executed successively."}
296 options.add_options()
298 "File path where the TfLite model for the yoloV3big backbone " 299 "can be found e.g. mydir/yoloV3big_backbone.tflite",
300 cxxopts::value<std::string>())
302 (
"c,comparison-files",
303 "Defines the expected outputs for the model " 304 "of yoloV3big e.g. 'mydir/file1.txt,mydir/file2.txt,mydir/file3.txt,mydir/file4.txt'->InputToDetector1" 305 " will be tried first then InputToDetector2 then InputToDetector3 then the Detector Output and finally" 306 " the NMS output. NOTE: Files are passed as comma separated list without whitespaces.",
307 cxxopts::value<std::vector<std::string>>())
310 "File path where the TfLite model for the yoloV3big " 311 "detector can be found e.g.'mydir/yoloV3big_detector.tflite'",
312 cxxopts::value<std::string>())
314 (
"h,help",
"Produce help message")
317 "File path to a 1080x1920 jpg image that should be " 318 "processed e.g. 'mydir/example_img_180_1920.jpg'",
319 cxxopts::value<std::string>())
321 (
"B,preferred-backends-backbone",
322 "Defines the preferred backends to run the backbone model " 323 "of yoloV3big e.g. 'GpuAcc,CpuRef' -> GpuAcc will be tried " 324 "first before falling back to CpuRef. NOTE: Backends are passed " 325 "as comma separated list without whitespaces.",
326 cxxopts::value<std::vector<std::string>>()->default_value(
"GpuAcc,CpuRef"))
328 (
"D,preferred-backends-detector",
329 "Defines the preferred backends to run the detector model " 330 "of yoloV3big e.g. 'CpuAcc,CpuRef' -> CpuAcc will be tried " 331 "first before falling back to CpuRef. NOTE: Backends are passed " 332 "as comma separated list without whitespaces.",
333 cxxopts::value<std::vector<std::string>>()->default_value(
"CpuAcc,CpuRef"));
335 auto result = options.parse(ac, av);
337 if (result.count(
"help"))
339 std::cout << options.help() <<
"\n";
343 backboneDir = GetPathArgument(result,
"backbone-path");
344 comparisonFiles = GetPathArgument(result[
"comparison-files"].as<std::vector<std::string>>());
345 detectorDir = GetPathArgument(result,
"detector-path");
346 imageDir = GetPathArgument(result,
"image-path");
350 prefBackendsBackbone = GetBackendIDs(result[
"preferred-backends-backbone"].as<std::vector<std::string>>());
351 LogBackendsInfo(prefBackendsBackbone,
"Backbone");
352 prefBackendsDetector = GetBackendIDs(result[
"preferred-backends-detector"].as<std::vector<std::string>>());
353 LogBackendsInfo(prefBackendsDetector,
"detector");
357 std::vector<BackendId> GetBackendIDs(
const std::vector<std::string>& backendStrings)
359 std::vector<BackendId> backendIDs;
360 for (
const auto& b : backendStrings)
370 std::string GetPathArgument(cxxopts::ParseResult& result, std::string&& argName)
372 if (result.count(argName))
374 std::string fileDir = result[argName].as<std::string>();
377 throw cxxopts::option_syntax_exception(
"Argument given to backbone-path is not a valid file path");
383 throw cxxopts::missing_argument_exception(argName);
388 std::vector<std::string> GetPathArgument(
const std::vector<std::string>& pathStrings)
390 if (pathStrings.size() < 5){
391 throw cxxopts::option_syntax_exception(
"Comparison files requires 5 file paths.");
394 std::vector<std::string> filePaths;
395 for (
auto& path : pathStrings)
397 filePaths.push_back(path);
400 throw cxxopts::option_syntax_exception(
"Argument given to Comparison Files is not a valid file path");
407 void LogBackendsInfo(std::vector<BackendId>& backends, std::string&& modelName)
410 info =
"Preferred backends for " + modelName +
" set to [ ";
411 for (
auto const &backend : backends)
413 info = info + std::string(backend) +
" ";
419 std::string backboneDir;
420 std::vector<std::string> comparisonFiles;
421 std::string detectorDir;
422 std::string imageDir;
424 std::vector<BackendId> prefBackendsBackbone;
425 std::vector<BackendId> prefBackendsDetector;
427 cxxopts::Options options;
430 int main(
int argc,
char* argv[])
437 ParseArgs progArgs = ParseArgs(argc, argv);
441 auto runtime = IRuntime::Create(runtimeOptions);
455 CHECK_OK(
LoadModel(progArgs.backboneDir.c_str(), *parser, *runtime, backboneId, progArgs.prefBackendsBackbone));
456 auto inputId = parser->GetNetworkInputBindingInfo(0,
"inputs");
457 auto bbOut0Id = parser->GetNetworkOutputBindingInfo(0,
"input_to_detector_1");
458 auto bbOut1Id = parser->GetNetworkOutputBindingInfo(0,
"input_to_detector_2");
459 auto bbOut2Id = parser->GetNetworkOutputBindingInfo(0,
"input_to_detector_3");
460 auto backboneProfile = runtime->GetProfiler(backboneId);
461 backboneProfile->EnableProfiling(
true);
467 progArgs.detectorDir.c_str(), *parser, *runtime, detectorId, progArgs.prefBackendsDetector,
true));
468 auto detectIn0Id = parser->GetNetworkInputBindingInfo(0,
"input_to_detector_1");
469 auto detectIn1Id = parser->GetNetworkInputBindingInfo(0,
"input_to_detector_2");
470 auto detectIn2Id = parser->GetNetworkInputBindingInfo(0,
"input_to_detector_3");
471 auto outputBoxesId = parser->GetNetworkOutputBindingInfo(0,
"output_boxes");
472 auto detectorProfile = runtime->GetProfiler(detectorId);
476 auto image =
LoadImage(progArgs.imageDir.c_str());
479 return LOAD_IMAGE_ERROR;
483 std::vector<float> intermediateMem0(bbOut0Id.second.GetNumElements());
484 std::vector<float> intermediateMem1(bbOut1Id.second.GetNumElements());
485 std::vector<float> intermediateMem2(bbOut2Id.second.GetNumElements());
486 std::vector<float> intermediateMem3(outputBoxesId.second.GetNumElements());
489 using BindingInfos = std::vector<armnn::BindingPointInfo>;
490 using FloatTensors = std::vector<std::reference_wrapper<std::vector<float>>>;
493 FloatTensors{ image });
495 FloatTensors{ intermediateMem0,
501 FloatTensors{ intermediateMem0,
505 FloatTensors{ intermediateMem3 });
507 static const int numIterations=2;
508 using DurationUS = std::chrono::duration<double, std::micro>;
509 std::vector<DurationUS> nmsDurations(0);
510 std::vector<yolov3::Detection> filtered_boxes;
511 nmsDurations.reserve(numIterations);
512 for (
int i=0; i < numIterations; i++)
516 runtime->EnqueueWorkload(backboneId, bbInputTensors, bbOutputTensors);
520 runtime->EnqueueWorkload(detectorId, detectInputTensors, detectOutputTensors);
524 using clock = std::chrono::steady_clock;
525 auto nmsStartTime = clock::now();
531 filtered_boxes =
yolov3::nms(config, intermediateMem3);
532 auto nmsEndTime = clock::now();
539 const auto nmsDuration = DurationUS(nmsStartTime - nmsEndTime);
540 nmsDurations.push_back(nmsDuration);
542 backboneProfile->EnableProfiling(
true);
543 detectorProfile->EnableProfiling(
true);
546 std::ofstream backboneProfileStream(
"backbone.json");
547 backboneProfile->Print(backboneProfileStream);
548 backboneProfileStream.close();
550 std::ofstream detectorProfileStream(
"detector.json");
551 detectorProfile->Print(detectorProfileStream);
552 detectorProfileStream.close();
555 std::ofstream nmsProfileStream(
"nms.json");
556 nmsProfileStream <<
"{" <<
"\n";
557 nmsProfileStream << R
"( "NmsTimings": {)" << "\n";
558 nmsProfileStream << R
"( "raw": [)" << "\n";
560 for (
auto duration : nmsDurations)
564 nmsProfileStream <<
",\n";
567 nmsProfileStream <<
" " << duration.count();
570 nmsProfileStream <<
"\n";
571 nmsProfileStream << R
"( "units": "us")" << "\n";
572 nmsProfileStream <<
" ]" <<
"\n";
573 nmsProfileStream <<
" }" <<
"\n";
574 nmsProfileStream <<
"}" <<
"\n";
575 nmsProfileStream.close();
578 &intermediateMem2, &intermediateMem3,
579 filtered_boxes, progArgs.comparisonFiles);
void CheckAccuracy(std::vector< float > *toDetector0, std::vector< float > *toDetector1, std::vector< float > *toDetector2, std::vector< float > *detectorOutput, const std::vector< yolov3::Detection > &nmsOut, const std::vector< std::string > &filePaths)
void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured)
int LoadModel(const char *filename, ITfLiteParser &parser, IRuntime &runtime, NetworkId &networkId, const std::vector< BackendId > &backendPreferences, bool enableImport=false)
armnn::InputTensors MakeInputTensors(const std::vector< armnn::BindingPointInfo > &inputBindings, const std::vector< std::reference_wrapper< TContainer >> &inputDataContainers)
int main(int argc, char *argv[])
#define ARMNN_LOG(severity)
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Copyright (c) 2020 ARM Limited.
unsigned int num_boxes
Number of detected boxes.
static ITfLiteParserPtr Create(const armnn::Optional< TfLiteParserOptions > &options=armnn::EmptyOptional())
virtual const IDeviceSpec & GetDeviceSpec() const =0
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
std::vector< float > LoadImage(const char *filename)
void SetLogFilter(LogSeverity level)
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
void print_detection(std::ostream &os, const std::vector< Detection > &detections)
Print identified yolo detections.
virtual armnn::INetworkPtr CreateNetworkFromBinary(const std::vector< uint8_t > &binaryContent)=0
Create the network from a flatbuffers binary.
virtual Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network)=0
Loads a complete network into the IRuntime.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
float iou_threshold
Inclusion threshold for Intersection-Over-Union.
boost::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char > > TContainer
std::pair< armnn::LayerBindingId, armnn::TensorInfo > BindingPointInfo
Base class for all ArmNN exceptions so that users can filter to just those.
std::vector< Detection > nms(const NMSConfig &config, const std::vector< float > &detected_boxes)
Perform Non-Maxima Supression on a list of given detections.
armnn::OutputTensors MakeOutputTensors(const std::vector< armnn::BindingPointInfo > &outputBindings, const std::vector< std::reference_wrapper< TContainer >> &outputDataContainers)
Non Maxima Suprresion configuration meta-data.
bool ValidateFilePath(std::string &file)
float confidence_threshold
Inclusion confidence threshold for a box.
bool compare_detection(const yolov3::Detection &detection, const std::vector< float > &expected)
Compare a detection object with a vector of float values.
unsigned int num_classes
Number of classes in the detected boxes.