summaryrefslogtreecommitdiff
path: root/python/openvino/runtime/dla_benchmark/top1_top5.hpp
blob: 4f27bb24dd0f883bd48be2c9846cc8942e362280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// Description: This file defines and implements functions to calculate top1 and top5 scores.

#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <utility>

class TopResultsAnalyser {
 public:
  static bool get_top_results(const std::string groundtruth_loc, const std::string results_loc, uint32_t batchSize) {
    // This function loads the output results from a file,
    // The dla benchmark currently uses the get_top_results(string, vector<float>, uint)
    // This function is kept as it can be used to assess accuracy post runtime,
    // although it seems awfully similar to the other version of get_top_results().
    const std::string accuracy_results_loc = "accuracy_report.txt";
    std::ofstream accuracy_file(accuracy_results_loc);

    if (!accuracy_file.is_open()) {
      throw std::invalid_argument("Unable to open accuracy file.");
    }

    std::ifstream groundtruth_file(groundtruth_loc);
    int groundtruth_lineno = 0;

    if (!groundtruth_file.is_open()) {
      throw std::invalid_argument("Unable to open groundtruth file.");
    }

    std::ifstream results_file(results_loc);

    if (!results_file.is_open()) {
      throw std::invalid_argument("Unable to open result file.");
    }

    std::string results_line;
    std::vector<float> results;
    while (std::getline(results_file, results_line)) {
      const float result = std::stof(results_line);
      results.push_back(result);
    }

    if (results.size() % batchSize != 0) {
      std::cout << "Results size = " << results.size() << " Batch size = " << batchSize << std::endl;
      throw std::invalid_argument("Results size is not a multiple of batch size");
    }

    typedef std::pair<uint64_t, float> CatProbPair;
    const uint64_t img_output_size = results.size() / batchSize;
    uint32_t top1_correct_guesses = 0;
    uint32_t top5_correct_guesses = 0;
    const auto top_n = fmin(5, img_output_size);
    for (uint32_t img = 0; img < batchSize; img++) {
      accuracy_file << "image " << img << " top 5:" << std::endl;

      const auto start_addr = img_output_size * img;
      std::vector<CatProbPair> top5;
      for (int i = 0; i < top_n; i++) {
        top5.push_back(std::make_pair(i, results[start_addr + i]));
      }

      for (uint64_t i = 5; i < img_output_size; i++) {
        const auto e = results[start_addr + i];
        auto min_ele = &top5.at(0);
        for (size_t j = 1; j < top5.size(); j++) {
          if (top5.at(j).second < min_ele->second) {
            min_ele = &top5.at(j);
          }
        }
        if (e > min_ele->second) {
          *min_ele = std::make_pair(i, e);
        }
      }

      // sort descending
      std::sort(
          top5.begin(), top5.end(), [](const CatProbPair& a, const CatProbPair& b) { return a.second > b.second; });
      for (const auto& pair : top5) {
        accuracy_file << pair.first << " : " << pair.second << std::endl;
      }
      std::string line;
      std::getline(groundtruth_file, line);
      ++groundtruth_lineno;
      uint64_t truth;
      try {
        truth = std::stoi(line);
      } catch (const std::invalid_argument& ia) {
        THROW_IE_EXCEPTION << "Unable to parse line " << groundtruth_lineno << " "
                           << "of the ground truth file " << groundtruth_loc;
      }
      accuracy_file << truth << " : truth" << std::endl;
      top1_correct_guesses += (top5.at(0).first == truth);

      uint64_t i = 1;
      for (const auto& guess : top5) {
        if (guess.first == truth && i < img_output_size) {
          top5_correct_guesses += 1;
          break;
        }
        i += 1;
      }
    }

    const auto top_n_string = [&](std::ostream& stream, const double correct_guesses, const uint32_t N) {
      stream << "top" << N << " accuracy: " << (correct_guesses * 100.0) / (batchSize) << " %" << std::endl;
    };

    accuracy_file << "====================" << std::endl;

    top_n_string(accuracy_file, top1_correct_guesses, 1);
    top_n_string(std::cout, top1_correct_guesses, 1);
    if (2 < img_output_size && img_output_size < 6) {
      top_n_string(accuracy_file, top5_correct_guesses, img_output_size - 1);
      top_n_string(std::cout, top5_correct_guesses, img_output_size - 1);
    } else if (6 <= img_output_size) {
      top_n_string(accuracy_file, top5_correct_guesses, 5);
      top_n_string(std::cout, top5_correct_guesses, 5);
    }
    return true;
  }

  static bool get_top_results(const std::string groundtruth_loc, std::vector<float> results, uint32_t batchSize) {
    // This function takes the output results directly from runtime in a vector
    // The dla benchmark currently uses this version of get_top_results
    const std::string accuracy_results_loc = "accuracy_report.txt";
    std::ofstream accuracy_file(accuracy_results_loc);

    if (!accuracy_file.is_open()) {
      throw std::invalid_argument("Unable to open accuracy file.");
    }

    std::ifstream groundtruth_file(groundtruth_loc);
    int groundtruth_lineno = 0;

    if (!groundtruth_file.is_open()) {
      throw std::invalid_argument("Unable to open groundtruth file.");
    }

    if (results.size() % batchSize != 0) {
      std::cout << "Results size = " << results.size() << " Batch size = " << batchSize << std::endl;
      throw std::invalid_argument("Results size is not a multiple of batch size");
    }

    typedef std::pair<int, float> CatProbPair;
    const int img_output_size = results.size() / batchSize;
    uint32_t top1_correct_guesses = 0;
    uint32_t top5_correct_guesses = 0;
    const auto top_n = fmin(5, img_output_size);
    for (uint32_t img = 0; img < batchSize; img++) {
      accuracy_file << "image " << img << " top 5:" << std::endl;

      const auto start_addr = img_output_size * img;
      std::vector<CatProbPair> top5;
      for (int i = 0; i < top_n; i++) {
        top5.push_back(std::make_pair(i, results[start_addr + i]));
      }

      for (int i = 5; i < img_output_size; i++) {
        const auto e = results[start_addr + i];
        auto min_ele = &top5.at(0);
        for (size_t j = 1; j < top5.size(); j++) {
          if (top5.at(j).second < min_ele->second) {
            min_ele = &top5.at(j);
          }
        }
        if (e > min_ele->second) {
          *min_ele = std::make_pair(i, e);
        }
      }

      // sort descending
      std::sort(
          top5.begin(), top5.end(), [](const CatProbPair& a, const CatProbPair& b) { return a.second > b.second; });
      for (const auto& pair : top5) {
        accuracy_file << pair.first << " : " << pair.second << std::endl;
      }
      std::string line;
      std::getline(groundtruth_file, line);
      ++groundtruth_lineno;
      int truth;
      try {
        truth = std::stoi(line);
      } catch (const std::invalid_argument& ia) {
        THROW_IE_EXCEPTION << "Unable to parse line " << groundtruth_lineno << " "
                           << "of the ground truth file " << groundtruth_loc;
      }
      accuracy_file << truth << " : truth" << std::endl;
      top1_correct_guesses += top5.at(0).first == truth;

      int i = 1;
      for (const auto& guess : top5) {
        if (guess.first == truth && i < img_output_size) {
          top5_correct_guesses += 1;
          break;
        }
        i += 1;
      }
    }

    const auto top_n_string = [&](std::ostream& stream, const double correct_guesses, const uint32_t N) {
      stream << "top" << N << " accuracy: " << (correct_guesses * 100.0) / (batchSize) << " %" << std::endl;
    };

    accuracy_file << "====================" << std::endl;

    top_n_string(accuracy_file, top1_correct_guesses, 1);
    top_n_string(std::cout, top1_correct_guesses, 1);
    if (2 < img_output_size && img_output_size < 6) {
      top_n_string(accuracy_file, top5_correct_guesses, img_output_size - 1);
      top_n_string(std::cout, top5_correct_guesses, img_output_size - 1);
    } else if (6 <= img_output_size) {
      top_n_string(accuracy_file, top5_correct_guesses, 5);
      top_n_string(std::cout, top5_correct_guesses, 5);
    }

    return true;
  }
};