Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / samples / calibration_tool / data_stats.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <stdlib.h>
6 #include <cfloat>
7 #include <cmath>
8 #include <stdint.h>
9 #include <iostream>
10 #include <limits>
11 #include <vector>
12 #include <algorithm>
13 #include <string>
14
15 #include "data_stats.h"
16
17
18 TensorStatistic::TensorStatistic(float* data, size_t count, size_t nbuckets) {
19     _min = std::numeric_limits<float>::max();
20     _max = std::numeric_limits<float>::min();
21     for (size_t i = 0; i < count; i++) {
22         float val = static_cast<float>(data[i]);
23         if (_min > val) {
24             _min = val;
25         }
26
27         if (_max < val) {
28             _max = val;
29         }
30     }
31
32     if (_min == _max) {
33         return;
34     }
35 }
36
37 float TensorStatistic::getMaxValue() const {
38     return _max;
39 }
40
41
42 float TensorStatistic::getMinValue() const {
43     return _min;
44 }
45
46 std::vector<std::string> AggregatedDataStats::registeredLayers() {
47     std::vector<std::string> layers;
48     for (auto l : _data) {
49         layers.push_back(l.first);
50     }
51     return layers;
52 }
53
54 void AggregatedDataStats::registerLayer(std::string layer) {
55     _data[layer];
56 }
57
58 void AggregatedDataStats::addTensorStatistics(const std::string& name, size_t channel, float* data, size_t count) {
59     auto&& byChannel = _data[name];
60     byChannel[channel].push_back(TensorStatistic(data, count));
61 }
62
63 void AggregatedDataStats::addTensorStatistics(const std::string &name, size_t channel, uint8_t *data, size_t count) {
64     std::vector<float> intermediate;
65     for (size_t i = 0; i < count; i++) {
66         intermediate.push_back(data[i]);
67     }
68     addTensorStatistics(name, channel, intermediate.data(), count);
69 }
70
71 size_t AggregatedDataStats::getNumberChannels(const std::string& name) const {
72     auto it = _data.find(name);
73     if (it != _data.end()) {
74         return it->second.size();
75     }
76     return 0;
77 }
78
79 void AggregatedDataStats::getDataMinMax(const std::string& name, size_t channel, float& min, float& max, float threshold) {
80     // take data by name
81     auto it = _data.find(name);
82     if (it != _data.end()) {
83         auto stats = it->second[channel];
84         // having absolute min/max values, we can create new statistic
85         std::vector<float> maxValues;
86         std::vector<float> minValues;
87         for (size_t i = 0; i < stats.size(); i++) {
88             const TensorStatistic& tsS = stats[i];
89             maxValues.push_back(tsS.getMaxValue());
90             minValues.push_back(tsS.getMinValue());
91         }
92         // define number of elements to throw out
93         size_t elementToTake = static_cast<size_t>(maxValues.size() * (threshold / 100));
94         int elementsToThrow = maxValues.size() - elementToTake;
95         std::sort(maxValues.begin(), maxValues.end());
96         std::sort(minValues.begin(), minValues.end());
97
98         min = minValues[elementsToThrow];
99         max = maxValues[elementToTake - 1];
100     } else {
101         min = max = 0.f;
102     }
103 }
104