ov2.0 IE samples modification (#8340)

* ov2.0 IE samples modification

apply code style

turn off clang style check for headers order

unify samples a bit

add yuv nv12 reader to format_reader, helloe_nv112 sample

hello_reshape_ssd ov2.0

* sync with PR 8629 preprocessing api changes

* fix for slog << vector<int>

* add operator<< for ov::Version from PR-8687

* Update samples/cpp/hello_nv12_input_classification/main.cpp

Co-authored-by: Mikhail Nosov <mikhail.nosov@intel.com>

* apply code style

* change according to review comments

* add const qualifier

* apply code style

* std::ostream for old inference engine version to make VPU plugin tests happy

* apply code style

* revert changes in print version for old api samples

* keep inference_engine.hpp for not ov2.0 yet samples

* fix merge artifacts

* fix compilation

* apply code style

* Fixed classification sample test

* Revert changes in hello_reshape_ssd sample

* rebase to master, sync with PR-9054

* fix issues found by C++ tests

* rebased and sync with PR-9051

* fix test result parsers for classification tests (except unicode one)

* fix mismatches after merge

* rebase and sync with PR-9144

Co-authored-by: Mikhail Nosov <mikhail.nosov@intel.com>
Co-authored-by: antonrom23 <anton.romanov@intel.com>
This commit is contained in:
Vladimir Dudnik
2021-12-13 11:30:58 +03:00
committed by GitHub
parent 4e8a6d5a4b
commit 5b25dbee22
45 changed files with 1033 additions and 825 deletions

View File

@@ -9,10 +9,14 @@
#pragma once
#include <inference_engine.hpp>
// clang-format off
#include <string>
#include <vector>
#include "inference_engine.hpp"
#include "openvino/openvino.hpp"
// clang-format on
/**
* @brief This function checks input args and existence of specified files in a given folder
* @param arg path to a file to be checked for existence

View File

@@ -15,24 +15,21 @@
#include <utility>
#include <vector>
#include "inference_engine.hpp"
#include "openvino/openvino.hpp"
/**
* @class ClassificationResult
* @brief A ClassificationResult creates an output table with results
*/
template <class strType = std::string>
class ClassificationResultT {
class ClassificationResult {
private:
const std::string _classidStr = "classid";
const std::string _probabilityStr = "probability";
const std::string _labelStr = "label";
size_t _nTop;
ov::runtime::Tensor _outTensor;
InferenceEngine::Blob::Ptr _outBlob;
const std::vector<std::string> _labels;
const std::vector<strType> _imageNames;
const std::vector<std::string> _imageNames;
const size_t _batchSize;
std::vector<unsigned> _results;
@@ -85,43 +82,6 @@ private:
}
}
template <class T>
void topResults(unsigned int n, InferenceEngine::Blob::Ptr& input, std::vector<unsigned>& output) {
InferenceEngine::SizeVector dims = input->getTensorDesc().getDims();
size_t input_rank = dims.size();
if (!input_rank || !dims[0])
IE_THROW() << "Input blob has incorrect dimensions!";
size_t batchSize = dims[0];
std::vector<unsigned> indexes(input->size() / batchSize);
n = static_cast<unsigned>(std::min<size_t>((size_t)n, input->size()));
output.resize(n * batchSize);
InferenceEngine::MemoryBlob::CPtr moutput = InferenceEngine::as<InferenceEngine::MemoryBlob>(input);
if (!moutput) {
IE_THROW() << "Output blob should be inherited from MemoryBlob";
}
// locked memory holder should be alive all time while access to its buffer happens
auto moutputHolder = moutput->rmap();
for (size_t i = 0; i < batchSize; i++) {
size_t offset = i * (input->size() / batchSize);
T* batchData = moutputHolder.as<T*>();
batchData += offset;
std::iota(std::begin(indexes), std::end(indexes), 0);
std::partial_sort(std::begin(indexes),
std::begin(indexes) + n,
std::end(indexes),
[&batchData](unsigned l, unsigned r) {
return batchData[l] > batchData[r];
});
for (unsigned j = 0; j < n; j++) {
output.at(i * n + j) = indexes.at(j);
}
}
}
/**
* @brief Gets the top n results from a blob
*
@@ -156,57 +116,12 @@ private:
#undef TENSOR_TOP_RESULT
}
void topResults(unsigned int n, InferenceEngine::Blob::Ptr& input, std::vector<unsigned>& output) {
#define TBLOB_TOP_RESULT(precision) \
case InferenceEngine::Precision::precision: { \
using myBlobType = InferenceEngine::PrecisionTrait<InferenceEngine::Precision::precision>::value_type; \
topResults<myBlobType>(n, input, output); \
break; \
}
switch (input->getTensorDesc().getPrecision()) {
TBLOB_TOP_RESULT(FP32);
TBLOB_TOP_RESULT(FP64);
TBLOB_TOP_RESULT(FP16);
TBLOB_TOP_RESULT(Q78);
TBLOB_TOP_RESULT(I16);
TBLOB_TOP_RESULT(U8);
TBLOB_TOP_RESULT(I8);
TBLOB_TOP_RESULT(U16);
TBLOB_TOP_RESULT(I32);
TBLOB_TOP_RESULT(U32);
TBLOB_TOP_RESULT(U64);
TBLOB_TOP_RESULT(I64);
default:
IE_THROW() << "cannot locate blob for precision: " << input->getTensorDesc().getPrecision();
}
#undef TBLOB_TOP_RESULT
}
public:
explicit ClassificationResultT(InferenceEngine::Blob::Ptr output_blob,
std::vector<strType> image_names = {},
size_t batch_size = 1,
size_t num_of_top = 10,
std::vector<std::string> labels = {})
: _nTop(num_of_top),
_outBlob(std::move(output_blob)),
_labels(std::move(labels)),
_imageNames(std::move(image_names)),
_batchSize(batch_size),
_results() {
if (_imageNames.size() != _batchSize) {
throw std::logic_error("Batch size should be equal to the number of images.");
}
topResults(_nTop, _outBlob, _results);
}
explicit ClassificationResultT(const ov::runtime::Tensor& output_tensor,
const std::vector<strType>& image_names = {},
size_t batch_size = 1,
size_t num_of_top = 10,
const std::vector<std::string>& labels = {})
explicit ClassificationResult(const ov::runtime::Tensor& output_tensor,
const std::vector<std::string>& image_names = {},
size_t batch_size = 1,
size_t num_of_top = 10,
const std::vector<std::string>& labels = {})
: _nTop(num_of_top),
_outTensor(output_tensor),
_labels(labels),
@@ -225,17 +140,17 @@ public:
/** Print the result iterating over each batch **/
std::ios::fmtflags fmt(std::cout.flags());
std::cout << std::endl << "Top " << _nTop << " results:" << std::endl << std::endl;
for (unsigned int image_id = 0; image_id < _batchSize; ++image_id) {
std::wstring out(_imageNames[image_id].begin(), _imageNames[image_id].end());
std::wcout << L"Image " << out;
std::wcout.flush();
std::wcout.clear();
std::wcout << std::endl << std::endl;
for (size_t image_id = 0; image_id < _batchSize; ++image_id) {
std::string out(_imageNames[image_id].begin(), _imageNames[image_id].end());
std::cout << "Image " << out;
std::cout.flush();
std::cout.clear();
std::cout << std::endl << std::endl;
printHeader();
for (size_t id = image_id * _nTop, cnt = 0; id < (image_id + 1) * _nTop; ++cnt, ++id) {
std::cout.precision(7);
/** Getting probability for resulting class **/
// Getting probability for resulting class
const auto index = _results.at(id) + image_id * (_outTensor.get_size() / _batchSize);
const auto result = _outTensor.data<const float>()[index];
@@ -256,30 +171,18 @@ public:
/** Print the result iterating over each batch **/
std::ios::fmtflags fmt(std::cout.flags());
std::cout << std::endl << "Top " << _nTop << " results:" << std::endl << std::endl;
for (unsigned int image_id = 0; image_id < _batchSize; ++image_id) {
std::wstring out(_imageNames[image_id].begin(), _imageNames[image_id].end());
std::wcout << L"Image " << out;
std::wcout.flush();
std::wcout.clear();
std::wcout << std::endl << std::endl;
for (size_t image_id = 0; image_id < _batchSize; ++image_id) {
std::string out(_imageNames[image_id].begin(), _imageNames[image_id].end());
std::cout << "Image " << out;
std::cout.flush();
std::cout.clear();
std::cout << std::endl << std::endl;
printHeader();
InferenceEngine::MemoryBlob::CPtr moutput = InferenceEngine::as<InferenceEngine::MemoryBlob>(_outBlob);
auto moutputHolder = moutput->rmap();
for (size_t id = image_id * _nTop, cnt = 0; id < (image_id + 1) * _nTop; ++cnt, ++id) {
std::cout.precision(7);
/** Getting probability for resulting class **/
if (!moutput) {
throw std::logic_error("We expect _outBlob to be inherited from MemoryBlob in "
"ClassificationResult::print, "
"but by fact we were not able to cast _outBlob to MemoryBlob");
}
// locked memory holder should be alive all time while access to its buffer happens
const auto result =
moutputHolder
.as<const InferenceEngine::PrecisionTrait<InferenceEngine::Precision::FP32>::value_type*>()
[_results.at(id) + image_id * (_outBlob->size() / _batchSize)];
// Getting probability for resulting class
const auto result = _outTensor.data<float>();
std::cout << std::setw(static_cast<int>(_classidStr.length())) << std::left << _results.at(id) << " ";
std::cout << std::left << std::setw(static_cast<int>(_probabilityStr.length())) << std::fixed << result;
@@ -300,6 +203,3 @@ public:
return _results;
}
};
using ClassificationResult = ClassificationResultT<>;
using ClassificationResultW = ClassificationResultT<std::wstring>;

View File

@@ -12,7 +12,6 @@
#include <algorithm>
#include <fstream>
#include <functional>
#include <inference_engine.hpp>
#include <iomanip>
#include <iostream>
#include <limits>
@@ -23,8 +22,11 @@
#include <utility>
#include <vector>
// clang-format off
#include "inference_engine.hpp"
#include "openvino/openvino.hpp"
#include "slog.hpp"
// clang-format on
#ifndef UNUSED
# if defined(_MSC_VER) && !defined(__clang__)
@@ -411,6 +413,7 @@ static UNUSED void addRectangles(unsigned char* data,
{180, 130, 70}, {60, 20, 220}, {0, 0, 255}, {142, 0, 0}, {70, 0, 0},
{100, 60, 0}, {90, 0, 0}, {230, 0, 0}, {32, 11, 119}, {0, 74, 111},
{81, 0, 81}};
if (rectangles.size() % 4 != 0 || rectangles.size() / 4 != classes.size()) {
return;
}
@@ -433,21 +436,21 @@ static UNUSED void addRectangles(unsigned char* data,
h = 0;
if (static_cast<std::size_t>(x) >= width) {
x = width - 1;
x = static_cast<int>(width - 1);
w = 0;
thickness = 1;
}
if (static_cast<std::size_t>(y) >= height) {
y = height - 1;
y = static_cast<int>(height - 1);
h = 0;
thickness = 1;
}
if (static_cast<std::size_t>(x + w) >= width) {
w = width - x - 1;
w = static_cast<int>(width - x - 1);
}
if (static_cast<std::size_t>(y + h) >= height) {
h = height - y - 1;
h = static_cast<int>(height - y - 1);
}
thickness = std::min(std::min(thickness, w / 2 + 1), h / 2 + 1);
@@ -951,7 +954,7 @@ public:
rec.push_back(recall);
}
int num = rec.size();
int num = static_cast<int>(rec.size());
// 11point from Caffe
double ap = 0;
@@ -1123,8 +1126,8 @@ inline std::size_t getTensorBatch(const InferenceEngine::TensorDesc& desc) {
}
inline void showAvailableDevices() {
InferenceEngine::Core ie;
std::vector<std::string> devices = ie.GetAvailableDevices();
ov::runtime::Core core;
std::vector<std::string> devices = core.get_available_devices();
std::cout << std::endl;
std::cout << "Available target devices:";

View File

@@ -4,14 +4,14 @@
#pragma once
#include <time.h>
#include <ctime>
#include <fstream>
#include <iostream>
#include <samples/slog.hpp>
#include <sstream>
#include <string>
#include "samples/slog.hpp"
/**
* @class CsvDumper
* @brief A CsvDumper class provides functionality for dumping the values in CSV files

View File

@@ -10,9 +10,9 @@
#pragma once
#include <opencv2/opencv.hpp>
#include <samples/common.hpp>
#include "openvino/openvino.hpp"
#include "samples/common.hpp"
/**
* @brief Sets image data stored in cv::Mat object to a given Blob object.

View File

@@ -11,6 +11,7 @@
#include <ostream>
#include <string>
#include <vector>
namespace slog {
/**
@@ -29,6 +30,14 @@ class LogStreamBoolAlpha {};
static constexpr LogStreamBoolAlpha boolalpha;
/**
* @class LogStreamFlush
* @brief The LogStreamFlush class implements flushing for a log stream
*/
class LogStreamFlush {};
static constexpr LogStreamFlush flush;
/**
* @class LogStream
* @brief The LogStream class implements a stream for sample logging
@@ -60,11 +69,30 @@ public:
return *this;
}
/**
* @brief Overload output stream operator to print vectors in pretty form
* [value1, value2, ...]
*/
template <typename T>
LogStream& operator<<(const std::vector<T>& v) {
(*_log_stream) << "[ ";
for (auto&& value : v)
(*_log_stream) << value << " ";
(*_log_stream) << "]";
return *this;
}
// Specializing for LogStreamEndLine to support slog::endl
LogStream& operator<<(const LogStreamEndLine&);
// Specializing for LogStreamBoolAlpha to support slog::boolalpha
LogStream& operator<<(const LogStreamBoolAlpha&);
// Specializing for LogStreamFlush to support slog::flush
LogStream& operator<<(const LogStreamFlush&);
};
extern LogStream info;

View File

@@ -4,6 +4,7 @@
#pragma once
#include <fstream>
#include <map>
#include <string>

View File

@@ -2,20 +2,24 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "samples/args_helper.hpp"
#include <gflags/gflags.h>
// clang-format off
#include <sys/stat.h>
#include <iostream>
#include <samples/slog.hpp>
#ifdef _WIN32
# include <samples/os/windows/w_dirent.h>
# include "samples/os/windows/w_dirent.h"
#else
# include <dirent.h>
#endif
#include "openvino/openvino.hpp"
#include "gflags/gflags.h"
#include "samples/args_helper.hpp"
#include "samples/slog.hpp"
// clang-format on
/**
* @brief Checks input file argument and add it to files vector
* @param files reference to vector to store file names

View File

@@ -2,10 +2,12 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "samples/slog.hpp"
// clang-format off
#include <iostream>
#include "samples/slog.hpp"
// clang-format on
namespace slog {
LogStream info("INFO", std::cout);
@@ -32,4 +34,10 @@ LogStream& LogStream::operator<<(const LogStreamBoolAlpha& /*arg*/) {
return *this;
}
} // namespace slog
// Specializing for LogStreamFlush to support slog::flush
LogStream& LogStream::operator<<(const LogStreamFlush& /*arg*/) {
(*_log_stream) << std::flush;
return *this;
}
} // namespace slog