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));
100 #define S_BOOL(name) enum class name {False=0, True=1}; 115 std::ifstream stream(filename, std::ios::in | std::ios::binary);
116 if (!stream.is_open())
119 return OPEN_FILE_ERROR;
122 std::vector<uint8_t> contents((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
125 auto model = parser.CreateNetworkFromBinary(contents);
132 auto optimizedModel =
Optimize(*model, backendPreferences, runtime.GetDeviceSpec(), options);
136 return OPTIMIZE_NETWORK_ERROR;
141 std::stringstream ss;
142 ss << filename <<
".dot";
143 std::ofstream dotStream(ss.str().c_str(), std::ofstream::out);
144 optimizedModel->SerializeToDot(dotStream);
149 std::string errorMessage;
151 Status status = runtime.LoadNetwork(networkId, std::move(optimizedModel), errorMessage, modelProps);
152 if (status != Status::Success)
154 ARMNN_LOG(
fatal) <<
"Could not load " << filename <<
" model into runtime: " << errorMessage;
155 return LOAD_NETWORK_ERROR;
164 if (strlen(filename) == 0)
166 return std::vector<float>(1920*10180*3, 0.0f);
170 ~Memory() {stbi_image_free(m_Data);}
171 bool IsLoaded()
const {
return m_Data !=
nullptr;}
173 unsigned char* m_Data;
176 std::vector<float> image;
182 Memory mem = {stbi_load(filename, &width, &height, &channels, 3)};
185 ARMNN_LOG(
error) <<
"Could not load input image file: " << filename;
189 if (width != 1920 || height != 1080 || channels != 3)
191 ARMNN_LOG(
error) <<
"Input image has wong dimension: " << width <<
"x" << height <<
"x" << channels <<
". " 192 " Expected 1920x1080x3.";
196 image.resize(1920*1080*3);
199 for (
unsigned int idx=0; idx <= 1920*1080*3; idx++)
201 image[idx] =
static_cast<float>(mem.m_Data[idx]) /255.0f;
210 if (!ghc::filesystem::exists(file))
212 std::cerr <<
"Given file path " << file <<
" does not exist" << std::endl;
215 if (!ghc::filesystem::is_regular_file(file) && expectFile ==
ExpectFile::True)
217 std::cerr <<
"Given file path " << file <<
" is not a regular file" << std::endl;
223 void CheckAccuracy(std::vector<float>* toDetector0, std::vector<float>* toDetector1,
224 std::vector<float>* toDetector2, std::vector<float>* detectorOutput,
225 const std::vector<yolov3::Detection>& nmsOut,
const std::vector<std::string>& filePaths)
227 std::ifstream pathStream;
228 std::vector<float> expected;
229 std::vector<std::vector<float>*> outputs;
231 unsigned int count = 0;
234 outputs.push_back(toDetector0);
235 outputs.push_back(toDetector1);
236 outputs.push_back(toDetector2);
237 outputs.push_back(detectorOutput);
239 for (
unsigned int i = 0; i < outputs.size(); ++i)
242 pathStream.open(filePaths[i]);
243 if (!pathStream.is_open())
245 ARMNN_LOG(
error) <<
"Expected output file can not be opened: " << filePaths[i];
249 expected.assign(std::istream_iterator<float>(pathStream), {});
254 if (expected.size() != outputs[i]->size())
256 ARMNN_LOG(
error) <<
"Expected output size does not match actual output size: " << filePaths[i];
263 for (
unsigned int j = 0; j < outputs[i]->size(); ++j)
265 compare =
abs(expected[j] - outputs[i]->at(j));
266 if (compare > 0.001f)
273 ARMNN_LOG(
error) << count <<
" output(s) do not match expected values in: " << filePaths[i];
278 pathStream.open(filePaths[4]);
279 if (!pathStream.is_open())
281 ARMNN_LOG(
error) <<
"Expected output file can not be opened: " << filePaths[4];
285 expected.assign(std::istream_iterator<float>(pathStream), {});
289 unsigned int numOfMember = 6;
290 std::vector<float> intermediate;
292 for (
auto& detection: nmsOut)
294 for (
unsigned int x = y * numOfMember; x < ((y * numOfMember) + numOfMember); ++x)
296 intermediate.push_back(expected[x]);
300 ARMNN_LOG(
error) <<
"Expected NMS output does not match: Detection " << y + 1;
302 intermediate.clear();
310 ParseArgs(
int ac,
char *av[]) : options{
"TfLiteYoloV3Big-Armnn",
311 "Executes YoloV3Big using ArmNN. YoloV3Big consists " 312 "of 3 parts: A backbone TfLite model, a detector TfLite " 313 "model, and None Maximum Suppression. All parts are " 314 "executed successively."}
316 options.add_options()
318 "File path where the TfLite model for the yoloV3big backbone " 319 "can be found e.g. mydir/yoloV3big_backbone.tflite",
320 cxxopts::value<std::string>())
322 (
"c,comparison-files",
323 "Defines the expected outputs for the model " 324 "of yoloV3big e.g. 'mydir/file1.txt,mydir/file2.txt,mydir/file3.txt,mydir/file4.txt'->InputToDetector1" 325 " will be tried first then InputToDetector2 then InputToDetector3 then the Detector Output and finally" 326 " the NMS output. NOTE: Files are passed as comma separated list without whitespaces.",
327 cxxopts::value<std::vector<std::string>>()->default_value({}))
330 "File path where the TfLite model for the yoloV3big " 331 "detector can be found e.g.'mydir/yoloV3big_detector.tflite'",
332 cxxopts::value<std::string>())
334 (
"h,help",
"Produce help message")
337 "File path to a 1080x1920 jpg image that should be " 338 "processed e.g. 'mydir/example_img_180_1920.jpg'",
339 cxxopts::value<std::string>())
341 (
"B,preferred-backends-backbone",
342 "Defines the preferred backends to run the backbone model " 343 "of yoloV3big e.g. 'GpuAcc,CpuRef' -> GpuAcc will be tried " 344 "first before falling back to CpuRef. NOTE: Backends are passed " 345 "as comma separated list without whitespaces.",
346 cxxopts::value<std::vector<std::string>>()->default_value(
"GpuAcc,CpuRef"))
348 (
"D,preferred-backends-detector",
349 "Defines the preferred backends to run the detector model " 350 "of yoloV3big e.g. 'CpuAcc,CpuRef' -> CpuAcc will be tried " 351 "first before falling back to CpuRef. NOTE: Backends are passed " 352 "as comma separated list without whitespaces.",
353 cxxopts::value<std::vector<std::string>>()->default_value(
"CpuAcc,CpuRef"))
356 "Dump the optimized model to a dot file for debugging/analysis",
357 cxxopts::value<bool>()->default_value(
"false"))
359 (
"Y, dynamic-backends-path",
360 "Define a path from which to load any dynamic backends.",
361 cxxopts::value<std::string>());
363 auto result = options.parse(ac, av);
365 if (result.count(
"help"))
367 std::cout << options.help() <<
"\n";
374 comparisonFiles = GetPathArgument(result[
"comparison-files"].as<std::vector<std::string>>(),
OptionalArg::True);
382 prefBackendsBackbone =
GetBackendIDs(result[
"preferred-backends-backbone"].as<std::vector<std::string>>());
383 LogBackendsInfo(prefBackendsBackbone,
"Backbone");
384 prefBackendsDetector =
GetBackendIDs(result[
"preferred-backends-detector"].as<std::vector<std::string>>());
385 LogBackendsInfo(prefBackendsDetector,
"detector");
391 std::vector<BackendId>
GetBackendIDs(
const std::vector<std::string>& backendStrings)
393 std::vector<BackendId> backendIDs;
394 for (
const auto& b : backendStrings)
404 std::string GetPathArgument(cxxopts::ParseResult& result,
405 std::string&& argName,
409 if (result.count(argName))
411 std::string path = result[argName].as<std::string>();
414 std::stringstream ss;
415 ss <<
"Argument given to" << argName <<
"is not a valid file path";
416 throw cxxopts::option_syntax_exception(ss.str().c_str());
427 throw cxxopts::missing_argument_exception(argName);
432 std::vector<std::string> GetPathArgument(
const std::vector<std::string>& pathStrings,
OptionalArg isOptional)
434 if (pathStrings.size() < 5){
437 return std::vector<std::string>();
439 throw cxxopts::option_syntax_exception(
"Comparison files requires 5 file paths.");
442 std::vector<std::string> filePaths;
443 for (
auto& path : pathStrings)
445 filePaths.push_back(path);
448 throw cxxopts::option_syntax_exception(
"Argument given to Comparison Files is not a valid file path");
455 void LogBackendsInfo(std::vector<BackendId>& backends, std::string&& modelName)
458 info =
"Preferred backends for " + modelName +
" set to [ ";
459 for (
auto const &backend : backends)
461 info = info + std::string(backend) +
" ";
467 std::string backboneDir;
468 std::vector<std::string> comparisonFiles;
469 std::string detectorDir;
470 std::string imageDir;
471 std::string dynamicBackendPath;
473 std::vector<BackendId> prefBackendsBackbone;
474 std::vector<BackendId> prefBackendsDetector;
476 cxxopts::Options options;
481 int main(
int argc,
char* argv[])
488 ParseArgs progArgs = ParseArgs(argc, argv);
493 if (!progArgs.dynamicBackendPath.empty())
495 std::cout <<
"Loading backends from" << progArgs.dynamicBackendPath <<
"\n";
499 auto runtime = IRuntime::Create(runtimeOptions);
513 const DumpToDot dumpToDot = progArgs.dumpToDot;
518 progArgs.prefBackendsBackbone,
521 auto inputId = parser->GetNetworkInputBindingInfo(0,
"inputs");
522 auto bbOut0Id = parser->GetNetworkOutputBindingInfo(0,
"input_to_detector_1");
523 auto bbOut1Id = parser->GetNetworkOutputBindingInfo(0,
"input_to_detector_2");
524 auto bbOut2Id = parser->GetNetworkOutputBindingInfo(0,
"input_to_detector_3");
525 auto backboneProfile = runtime->GetProfiler(backboneId);
526 backboneProfile->EnableProfiling(
true);
536 progArgs.prefBackendsDetector,
539 auto detectIn0Id = parser->GetNetworkInputBindingInfo(0,
"input_to_detector_1");
540 auto detectIn1Id = parser->GetNetworkInputBindingInfo(0,
"input_to_detector_2");
541 auto detectIn2Id = parser->GetNetworkInputBindingInfo(0,
"input_to_detector_3");
542 auto outputBoxesId = parser->GetNetworkOutputBindingInfo(0,
"output_boxes");
543 auto detectorProfile = runtime->GetProfiler(detectorId);
547 auto image =
LoadImage(progArgs.imageDir.c_str());
550 return LOAD_IMAGE_ERROR;
554 std::vector<float> intermediateMem0(bbOut0Id.second.GetNumElements());
555 std::vector<float> intermediateMem1(bbOut1Id.second.GetNumElements());
556 std::vector<float> intermediateMem2(bbOut2Id.second.GetNumElements());
557 std::vector<float> intermediateMem3(outputBoxesId.second.GetNumElements());
560 using BindingInfos = std::vector<armnn::BindingPointInfo>;
561 using FloatTensors = std::vector<std::reference_wrapper<std::vector<float>>>;
564 FloatTensors{ image });
566 FloatTensors{ intermediateMem0,
572 FloatTensors{ intermediateMem0,
576 FloatTensors{ intermediateMem3 });
578 static const int numIterations=2;
579 using DurationUS = std::chrono::duration<double, std::micro>;
580 std::vector<DurationUS> nmsDurations(0);
581 std::vector<yolov3::Detection> filtered_boxes;
582 nmsDurations.reserve(numIterations);
583 for (
int i=0; i < numIterations; i++)
587 runtime->EnqueueWorkload(backboneId, bbInputTensors, bbOutputTensors);
591 runtime->EnqueueWorkload(detectorId, detectInputTensors, detectOutputTensors);
595 using clock = std::chrono::steady_clock;
596 auto nmsStartTime = clock::now();
602 filtered_boxes =
yolov3::nms(config, intermediateMem3);
603 auto nmsEndTime = clock::now();
610 const auto nmsDuration = DurationUS(nmsStartTime - nmsEndTime);
611 nmsDurations.push_back(nmsDuration);
613 backboneProfile->EnableProfiling(
true);
614 detectorProfile->EnableProfiling(
true);
617 std::ofstream backboneProfileStream(
"backbone.json");
618 backboneProfile->Print(backboneProfileStream);
619 backboneProfileStream.close();
621 std::ofstream detectorProfileStream(
"detector.json");
622 detectorProfile->Print(detectorProfileStream);
623 detectorProfileStream.close();
626 std::ofstream nmsProfileStream(
"nms.json");
627 nmsProfileStream <<
"{" <<
"\n";
628 nmsProfileStream << R
"( "NmsTimings": {)" << "\n";
629 nmsProfileStream << R
"( "raw": [)" << "\n";
631 for (
auto duration : nmsDurations)
635 nmsProfileStream <<
",\n";
638 nmsProfileStream <<
" " << duration.count();
641 nmsProfileStream <<
"\n";
642 nmsProfileStream << R
"( "units": "us")" << "\n";
643 nmsProfileStream <<
" ]" <<
"\n";
644 nmsProfileStream <<
" }" <<
"\n";
645 nmsProfileStream <<
"}" <<
"\n";
646 nmsProfileStream.close();
648 if (progArgs.comparisonFiles.size() > 0)
655 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, ImportMemory enableImport, DumpToDot dumpToDot)
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) 2021 ARM Limited and Contributors.
unsigned int num_boxes
Number of detected boxes.
static ITfLiteParserPtr Create(const armnn::Optional< TfLiteParserOptions > &options=armnn::EmptyOptional())
mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char > > TContainer
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.
std::vector< armnn::BackendId > GetBackendIDs(const std::vector< std::string > &backendStrings)
Takes a vector of backend strings and returns a vector of backendIDs. Removes duplicate entries...
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.
std::string m_DynamicBackendsPath
Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive Only a...
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.
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.
bool ValidateFilePath(std::string &file, ExpectFile expectFile)
unsigned int num_classes
Number of classes in the detected boxes.