1 // Copyright (C) 2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief a header file with ouput classification results
7 * @file classification_results.hpp
17 * @class ClassificationResult
18 * @brief A ClassificationResult creates an output table with results
20 class ClassificationResult {
22 const std::string _classidStr = "classid";
23 const std::string _probabilityStr = "probability";
24 const std::string _labelStr = "label";
26 InferenceEngine::Blob::Ptr _outBlob;
27 const std::vector<std::string> _labels;
28 const std::vector<std::string> _imageNames;
29 const size_t _batchSize;
32 std::cout << _classidStr << " " << _probabilityStr;
34 std::cout << " " << _labelStr;
35 std::string classidColumn(_classidStr.length(), '-');
36 std::string probabilityColumn(_probabilityStr.length(), '-');
37 std::string labelColumn(_labelStr.length(), '-');
38 std::cout << std::endl << classidColumn << " " << probabilityColumn;
40 std::cout << " " << labelColumn;
41 std::cout << std::endl;
45 explicit ClassificationResult(InferenceEngine::Blob::Ptr output_blob,
46 std::vector<std::string> image_names = {},
47 size_t batch_size = 1,
48 size_t num_of_top = 10,
49 std::vector<std::string> labels = {}) :
51 _outBlob(std::move(output_blob)),
52 _labels(std::move(labels)),
53 _imageNames(std::move(image_names)),
54 _batchSize(batch_size) {
55 if (_imageNames.size() != _batchSize) {
56 throw std::logic_error("Batch size should be equal to the number of images.");
61 * @brief prints formatted classification results
64 /** This vector stores id's of top N results **/
65 std::vector<unsigned> results;
66 TopResults(_nTop, *_outBlob, results);
68 /** Print the result iterating over each batch **/
69 std::cout << std::endl << "Top " << _nTop << " results:" << std::endl << std::endl;
70 for (unsigned int image_id = 0; image_id < _batchSize; ++image_id) {
71 std::cout << "Image " << _imageNames[image_id] << std::endl << std::endl;
74 for (size_t id = image_id * _nTop, cnt = 0; id < (image_id + 1) * _nTop; ++cnt, ++id) {
75 std::cout.precision(7);
76 /** Getting probability for resulting class **/
77 const auto result = _outBlob->buffer().
78 as<InferenceEngine::PrecisionTrait<InferenceEngine::Precision::FP32>::value_type*>()
79 [results[id] + image_id * (_outBlob->size() / _batchSize)];
81 std::cout << std::setw(static_cast<int>(_classidStr.length())) << std::left << results[id] << " ";
82 std::cout << std::left << std::setw(static_cast<int>(_probabilityStr.length())) << std::fixed << result;
84 if (!_labels.empty()) {
85 std::cout << " " + _labels[results[id]];
87 std::cout << std::endl;
89 std::cout << std::endl;