2020-02-11 22:48:49 +03:00
|
|
|
// Copyright (C) 2018-2020 Intel Corporation
|
2018-11-23 16:19:43 +03:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-10-16 13:45:03 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief The entry point the Inference Engine sample application
|
2019-08-09 19:02:42 +03:00
|
|
|
* @file classification_sample_async/main.cpp
|
|
|
|
|
* @example classification_sample_async/main.cpp
|
2018-10-16 13:45:03 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <map>
|
2019-08-09 19:02:42 +03:00
|
|
|
#include <condition_variable>
|
|
|
|
|
#include <mutex>
|
2018-10-16 13:45:03 +03:00
|
|
|
|
|
|
|
|
#include <inference_engine.hpp>
|
|
|
|
|
|
2018-11-23 16:19:43 +03:00
|
|
|
#include <format_reader_ptr.h>
|
2018-10-16 13:45:03 +03:00
|
|
|
|
|
|
|
|
#include <samples/common.hpp>
|
|
|
|
|
#include <samples/slog.hpp>
|
|
|
|
|
#include <samples/args_helper.hpp>
|
2019-04-12 18:25:53 +03:00
|
|
|
#include <samples/classification_results.h>
|
2018-10-16 13:45:03 +03:00
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
|
|
#include "classification_sample_async.h"
|
|
|
|
|
|
|
|
|
|
using namespace InferenceEngine;
|
|
|
|
|
|
|
|
|
|
bool ParseAndCheckCommandLine(int argc, char *argv[]) {
|
|
|
|
|
// ---------------------------Parsing and validation of input args--------------------------------------
|
|
|
|
|
slog::info << "Parsing input parameters" << slog::endl;
|
|
|
|
|
|
|
|
|
|
gflags::ParseCommandLineNonHelpFlags(&argc, &argv, true);
|
|
|
|
|
if (FLAGS_h) {
|
|
|
|
|
showUsage();
|
2019-08-09 19:02:42 +03:00
|
|
|
showAvailableDevices();
|
2018-10-16 13:45:03 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
slog::info << "Parsing input parameters" << slog::endl;
|
|
|
|
|
|
2020-11-16 01:26:04 -08:00
|
|
|
if (FLAGS_m.empty()) {
|
|
|
|
|
throw std::logic_error("Model is required but not set. Please set -m option.");
|
2018-10-16 13:45:03 +03:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 01:26:04 -08:00
|
|
|
if (FLAGS_i.empty()) {
|
|
|
|
|
throw std::logic_error("Input is required but not set. Please set -i option.");
|
2018-10-16 13:45:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
try {
|
|
|
|
|
slog::info << "InferenceEngine: " << GetInferenceEngineVersion() << slog::endl;
|
|
|
|
|
|
|
|
|
|
// ------------------------------ Parsing and validation of input args ---------------------------------
|
|
|
|
|
if (!ParseAndCheckCommandLine(argc, argv)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** This vector stores paths to the processed images **/
|
|
|
|
|
std::vector<std::string> imageNames;
|
2018-11-23 16:19:43 +03:00
|
|
|
parseInputFilesArguments(imageNames);
|
2018-10-16 13:45:03 +03:00
|
|
|
if (imageNames.empty()) throw std::logic_error("No suitable images were found");
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
2019-08-09 19:02:42 +03:00
|
|
|
// --------------------------- 1. Load inference engine -------------------------------------
|
|
|
|
|
slog::info << "Creating Inference Engine" << slog::endl;
|
|
|
|
|
|
|
|
|
|
Core ie;
|
|
|
|
|
|
2018-10-16 13:45:03 +03:00
|
|
|
if (!FLAGS_l.empty()) {
|
|
|
|
|
// CPU(MKLDNN) extensions are loaded as a shared library and passed as a pointer to base extension
|
|
|
|
|
IExtensionPtr extension_ptr = make_so_pointer<IExtension>(FLAGS_l);
|
2020-04-27 21:21:29 +03:00
|
|
|
ie.AddExtension(extension_ptr);
|
2018-10-16 13:45:03 +03:00
|
|
|
slog::info << "CPU Extension loaded: " << FLAGS_l << slog::endl;
|
|
|
|
|
}
|
|
|
|
|
if (!FLAGS_c.empty()) {
|
|
|
|
|
// clDNN Extensions are loaded from an .xml description and OpenCL kernel files
|
2019-08-09 19:02:42 +03:00
|
|
|
ie.SetConfig({{PluginConfigParams::KEY_CONFIG_FILE, FLAGS_c}}, "GPU");
|
2018-10-16 13:45:03 +03:00
|
|
|
slog::info << "GPU Extension loaded: " << FLAGS_c << slog::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 19:02:42 +03:00
|
|
|
/** Printing device version **/
|
|
|
|
|
std::cout << ie.GetVersions(FLAGS_d) << std::endl;
|
2018-10-16 13:45:03 +03:00
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
2020-08-26 18:53:24 +03:00
|
|
|
// 2. Read a model in OpenVINO Intermediate Representation (.xml and .bin files) or ONNX (.onnx file) format
|
2018-10-16 13:45:03 +03:00
|
|
|
slog::info << "Loading network files" << slog::endl;
|
|
|
|
|
|
|
|
|
|
/** Read network model **/
|
2020-02-11 22:48:49 +03:00
|
|
|
CNNNetwork network = ie.ReadNetwork(FLAGS_m);
|
2018-10-16 13:45:03 +03:00
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// --------------------------- 3. Configure input & output ---------------------------------------------
|
2020-11-02 19:34:06 -08:00
|
|
|
if (network.getOutputsInfo().size() != 1) throw std::logic_error("Sample supports topologies with 1 output only");
|
2018-10-16 13:45:03 +03:00
|
|
|
|
|
|
|
|
// --------------------------- Prepare input blobs -----------------------------------------------------
|
|
|
|
|
slog::info << "Preparing input blobs" << slog::endl;
|
|
|
|
|
|
|
|
|
|
/** Taking information about all topology inputs **/
|
|
|
|
|
InputsDataMap inputInfo(network.getInputsInfo());
|
2019-08-09 19:02:42 +03:00
|
|
|
if (inputInfo.size() != 1) throw std::logic_error("Sample supports topologies with 1 input only");
|
2018-10-16 13:45:03 +03:00
|
|
|
|
|
|
|
|
auto inputInfoItem = *inputInfo.begin();
|
|
|
|
|
|
|
|
|
|
/** Specifying the precision and layout of input data provided by the user.
|
2019-08-09 19:02:42 +03:00
|
|
|
* This should be called before load of the network to the device **/
|
2018-10-16 13:45:03 +03:00
|
|
|
inputInfoItem.second->setPrecision(Precision::U8);
|
|
|
|
|
inputInfoItem.second->setLayout(Layout::NCHW);
|
|
|
|
|
|
2019-08-09 19:02:42 +03:00
|
|
|
std::vector<std::shared_ptr<unsigned char>> imagesData = {};
|
|
|
|
|
std::vector<std::string> validImageNames = {};
|
|
|
|
|
for (const auto & i : imageNames) {
|
2018-10-16 13:45:03 +03:00
|
|
|
FormatReader::ReaderPtr reader(i.c_str());
|
|
|
|
|
if (reader.get() == nullptr) {
|
|
|
|
|
slog::warn << "Image " + i + " cannot be read!" << slog::endl;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/** Store image data **/
|
|
|
|
|
std::shared_ptr<unsigned char> data(
|
|
|
|
|
reader->getData(inputInfoItem.second->getTensorDesc().getDims()[3],
|
|
|
|
|
inputInfoItem.second->getTensorDesc().getDims()[2]));
|
2019-08-09 19:02:42 +03:00
|
|
|
if (data != nullptr) {
|
2018-10-16 13:45:03 +03:00
|
|
|
imagesData.push_back(data);
|
2019-08-09 19:02:42 +03:00
|
|
|
validImageNames.push_back(i);
|
2018-10-16 13:45:03 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (imagesData.empty()) throw std::logic_error("Valid input images were not found!");
|
|
|
|
|
|
|
|
|
|
/** Setting batch size using image count **/
|
|
|
|
|
network.setBatchSize(imagesData.size());
|
|
|
|
|
size_t batchSize = network.getBatchSize();
|
|
|
|
|
slog::info << "Batch size is " << std::to_string(batchSize) << slog::endl;
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
2019-08-09 19:02:42 +03:00
|
|
|
// --------------------------- 4. Loading model to the device ------------------------------------------
|
|
|
|
|
slog::info << "Loading model to the device" << slog::endl;
|
|
|
|
|
ExecutableNetwork executable_network = ie.LoadNetwork(network, FLAGS_d);
|
2018-10-16 13:45:03 +03:00
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// --------------------------- 5. Create infer request -------------------------------------------------
|
2019-08-09 19:02:42 +03:00
|
|
|
slog::info << "Create infer request" << slog::endl;
|
|
|
|
|
InferRequest inferRequest = executable_network.CreateInferRequest();
|
2018-10-16 13:45:03 +03:00
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// --------------------------- 6. Prepare input --------------------------------------------------------
|
|
|
|
|
for (auto & item : inputInfo) {
|
2019-08-09 19:02:42 +03:00
|
|
|
Blob::Ptr inputBlob = inferRequest.GetBlob(item.first);
|
|
|
|
|
SizeVector dims = inputBlob->getTensorDesc().getDims();
|
2018-10-16 13:45:03 +03:00
|
|
|
/** Fill input tensor with images. First b channel, then g and r channels **/
|
|
|
|
|
size_t num_channels = dims[1];
|
|
|
|
|
size_t image_size = dims[3] * dims[2];
|
|
|
|
|
|
2020-02-11 22:48:49 +03:00
|
|
|
MemoryBlob::Ptr minput = as<MemoryBlob>(inputBlob);
|
|
|
|
|
if (!minput) {
|
|
|
|
|
slog::err << "We expect MemoryBlob from inferRequest, but by fact we were not able to cast inputBlob to MemoryBlob" << slog::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
// locked memory holder should be alive all time while access to its buffer happens
|
|
|
|
|
auto minputHolder = minput->wmap();
|
|
|
|
|
|
|
|
|
|
auto data = minputHolder.as<PrecisionTrait<Precision::U8>::value_type *>();
|
2018-10-16 13:45:03 +03:00
|
|
|
/** Iterate over all input images **/
|
|
|
|
|
for (size_t image_id = 0; image_id < imagesData.size(); ++image_id) {
|
|
|
|
|
/** Iterate over all pixel in image (b,g,r) **/
|
|
|
|
|
for (size_t pid = 0; pid < image_size; pid++) {
|
|
|
|
|
/** Iterate over all channels **/
|
|
|
|
|
for (size_t ch = 0; ch < num_channels; ++ch) {
|
|
|
|
|
/** [images stride + channels stride + pixel id ] all in bytes **/
|
2019-08-09 19:02:42 +03:00
|
|
|
data[image_id * image_size * num_channels + ch * image_size + pid] = imagesData.at(image_id).get()[pid*num_channels + ch];
|
2018-10-16 13:45:03 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// --------------------------- 7. Do inference ---------------------------------------------------------
|
2019-08-09 19:02:42 +03:00
|
|
|
size_t numIterations = 10;
|
|
|
|
|
size_t curIteration = 0;
|
|
|
|
|
std::condition_variable condVar;
|
|
|
|
|
|
|
|
|
|
inferRequest.SetCompletionCallback(
|
|
|
|
|
[&] {
|
|
|
|
|
curIteration++;
|
|
|
|
|
slog::info << "Completed " << curIteration << " async request execution" << slog::endl;
|
|
|
|
|
if (curIteration < numIterations) {
|
|
|
|
|
/* here a user can read output containing inference results and put new input
|
|
|
|
|
to repeat async request again */
|
|
|
|
|
inferRequest.StartAsync();
|
|
|
|
|
} else {
|
|
|
|
|
/* continue sample execution after last Asynchronous inference request execution */
|
|
|
|
|
condVar.notify_one();
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-10-16 13:45:03 +03:00
|
|
|
|
2019-08-09 19:02:42 +03:00
|
|
|
/* Start async request for the first time */
|
|
|
|
|
slog::info << "Start inference (" << numIterations << " asynchronous executions)" << slog::endl;
|
|
|
|
|
inferRequest.StartAsync();
|
2018-10-16 13:45:03 +03:00
|
|
|
|
2019-08-09 19:02:42 +03:00
|
|
|
/* Wait all repetitions of the async request */
|
|
|
|
|
std::mutex mutex;
|
|
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
|
condVar.wait(lock, [&]{ return curIteration == numIterations; });
|
2018-10-16 13:45:03 +03:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// --------------------------- 8. Process output -------------------------------------------------------
|
|
|
|
|
slog::info << "Processing output blobs" << slog::endl;
|
2019-08-09 19:02:42 +03:00
|
|
|
OutputsDataMap outputInfo(network.getOutputsInfo());
|
|
|
|
|
Blob::Ptr outputBlob = inferRequest.GetBlob(outputInfo.begin()->first);
|
|
|
|
|
|
|
|
|
|
/** Validating -nt value **/
|
|
|
|
|
const size_t resultsCnt = outputBlob->size() / batchSize;
|
|
|
|
|
if (FLAGS_nt > resultsCnt || FLAGS_nt < 1) {
|
|
|
|
|
slog::warn << "-nt " << FLAGS_nt << " is not available for this network (-nt should be less than " \
|
|
|
|
|
<< resultsCnt+1 << " and more than 0)\n will be used maximal value : " << resultsCnt << slog::endl;
|
|
|
|
|
FLAGS_nt = resultsCnt;
|
|
|
|
|
}
|
2018-10-16 13:45:03 +03:00
|
|
|
|
2019-08-09 19:02:42 +03:00
|
|
|
/** Read labels from file (e.x. AlexNet.labels) **/
|
|
|
|
|
std::string labelFileName = fileNameNoExt(FLAGS_m) + ".labels";
|
|
|
|
|
std::vector<std::string> labels;
|
|
|
|
|
|
|
|
|
|
std::ifstream inputFile;
|
|
|
|
|
inputFile.open(labelFileName, std::ios::in);
|
|
|
|
|
if (inputFile.is_open()) {
|
|
|
|
|
std::string strLine;
|
|
|
|
|
while (std::getline(inputFile, strLine)) {
|
|
|
|
|
trim(strLine);
|
|
|
|
|
labels.push_back(strLine);
|
2018-10-16 13:45:03 +03:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-09 19:02:42 +03:00
|
|
|
|
|
|
|
|
ClassificationResult classificationResult(outputBlob, validImageNames,
|
|
|
|
|
batchSize, FLAGS_nt,
|
|
|
|
|
labels);
|
|
|
|
|
classificationResult.print();
|
2018-10-16 13:45:03 +03:00
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& error) {
|
|
|
|
|
slog::err << error.what() << slog::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
catch (...) {
|
|
|
|
|
slog::err << "Unknown/internal exception happened." << slog::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog::info << "Execution successful" << slog::endl;
|
2019-08-09 19:02:42 +03:00
|
|
|
slog::info << slog::endl << "This sample is an API example, for any performance measurements "
|
|
|
|
|
"please use the dedicated benchmark_app tool" << slog::endl;
|
2018-10-16 13:45:03 +03:00
|
|
|
return 0;
|
|
|
|
|
}
|