Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / samples / common / samples / classification_results.h
1 // Copyright (C) 2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * @brief a header file with ouput classification results
7  * @file classification_results.hpp
8  */
9 #include <string>
10 #include <vector>
11 #include <iostream>
12 #include <utility>
13
14 #include <ie_blob.h>
15
16 /**
17  * @class ClassificationResult
18  * @brief A ClassificationResult creates an output table with results
19  */
20 class ClassificationResult {
21 private:
22     const std::string _classidStr = "classid";
23     const std::string _probabilityStr = "probability";
24     const std::string _labelStr = "label";
25     size_t _nTop;
26     InferenceEngine::Blob::Ptr _outBlob;
27     const std::vector<std::string> _labels;
28     const std::vector<std::string> _imageNames;
29     const size_t _batchSize;
30
31     void printHeader() {
32         std::cout << _classidStr << " " << _probabilityStr;
33         if (!_labels.empty())
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;
39         if (!_labels.empty())
40             std::cout << " " << labelColumn;
41         std::cout << std::endl;
42     }
43
44 public:
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 = {}) :
50             _nTop(num_of_top),
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.");
57         }
58     }
59
60     /**
61     * @brief prints formatted classification results
62     */
63     void print() {
64         /** This vector stores id's of top N results **/
65         std::vector<unsigned> results;
66         TopResults(_nTop, *_outBlob, results);
67
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;
72             printHeader();
73
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)];
80
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;
83
84                 if (!_labels.empty()) {
85                     std::cout << " " + _labels[results[id]];
86                 }
87                 std::cout << std::endl;
88             }
89             std::cout << std::endl;
90         }
91     }
92 };