summaryrefslogtreecommitdiff
path: root/source/application/main/Classifier.cc
blob: bc2c378779048ced0c6120ef70571cb645bf620f (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
/*
 * Copyright (c) 2021 Arm Limited. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "Classifier.hpp"

#include "hal.h"
#include "TensorFlowLiteMicro.hpp"

#include <vector>
#include <string>
#include <set>
#include <cstdint>

namespace arm {
namespace app {

    template<typename T>
    bool Classifier::_GetTopNResults(TfLiteTensor* tensor,
                         std::vector<ClassificationResult>& vecResults,
                         uint32_t topNCount,
                         const std::vector <std::string>& labels)
    {
        std::set<std::pair<T, uint32_t>> sortedSet;

        /* NOTE: inputVec's size verification against labels should be
         *       checked by the calling/public function. */
        T* tensorData = tflite::GetTensorData<T>(tensor);

        /* Set initial elements. */
        for (uint32_t i = 0; i < topNCount; ++i) {
            sortedSet.insert({tensorData[i], i});
        }

        /* Initialise iterator. */
        auto setFwdIter = sortedSet.begin();

        /* Scan through the rest of elements with compare operations. */
        for (uint32_t i = topNCount; i < labels.size(); ++i) {
            if (setFwdIter->first < tensorData[i]) {
                sortedSet.erase(*setFwdIter);
                sortedSet.insert({tensorData[i], i});
                setFwdIter = sortedSet.begin();
            }
        }

        /* Final results' container. */
        vecResults = std::vector<ClassificationResult>(topNCount);

        /* For getting the floating point values, we need quantization parameters. */
        QuantParams quantParams = GetTensorQuantParams(tensor);

        /* Reset the iterator to the largest element - use reverse iterator. */
        auto setRevIter = sortedSet.rbegin();

        /* Populate results
         * Note: we could combine this loop with the loop above, but that
         *       would, involve more multiplications and other operations.
         **/
        for (size_t i = 0; i < vecResults.size(); ++i, ++setRevIter) {
            double score = static_cast<int> (setRevIter->first);
            vecResults[i].m_normalisedVal = quantParams.scale *
                                         (score - quantParams.offset);
            vecResults[i].m_label = labels[setRevIter->second];
            vecResults[i].m_labelIdx = setRevIter->second;
        }

        return true;
    }

    template<>
    bool Classifier::_GetTopNResults<float>(TfLiteTensor* tensor,
                                     std::vector<ClassificationResult>& vecResults,
                                     uint32_t topNCount,
                                     const std::vector <std::string>& labels)
    {
        std::set<std::pair<float, uint32_t>> sortedSet;

        /* NOTE: inputVec's size verification against labels should be
         *       checked by the calling/public function. */
        float* tensorData = tflite::GetTensorData<float>(tensor);

        /* Set initial elements. */
        for (uint32_t i = 0; i < topNCount; ++i) {
            sortedSet.insert({tensorData[i], i});
        }

        /* Initialise iterator. */
        auto setFwdIter = sortedSet.begin();

        /* Scan through the rest of elements with compare operations. */
        for (uint32_t i = topNCount; i < labels.size(); ++i) {
            if (setFwdIter->first < tensorData[i]) {
                sortedSet.erase(*setFwdIter);
                sortedSet.insert({tensorData[i], i});
                setFwdIter = sortedSet.begin();
            }
        }

        /* Final results' container. */
        vecResults = std::vector<ClassificationResult>(topNCount);

        /* Reset the iterator to the largest element - use reverse iterator. */
        auto setRevIter = sortedSet.rbegin();

        /* Populate results
         * Note: we could combine this loop with the loop above, but that
         *       would, involve more multiplications and other operations.
         **/
        for (size_t i = 0; i < vecResults.size(); ++i, ++setRevIter) {
            vecResults[i].m_normalisedVal = setRevIter->first;
            vecResults[i].m_label = labels[setRevIter->second];
            vecResults[i].m_labelIdx = setRevIter->second;
        }

        return true;
    }

    template bool  Classifier::_GetTopNResults<uint8_t>(TfLiteTensor* tensor,
                                           std::vector<ClassificationResult>& vecResults,
                                           uint32_t topNCount, const std::vector <std::string>& labels);

    template bool  Classifier::_GetTopNResults<int8_t>(TfLiteTensor* tensor,
                                          std::vector<ClassificationResult>& vecResults,
                                          uint32_t topNCount, const std::vector <std::string>& labels);

    bool  Classifier::GetClassificationResults(
        TfLiteTensor* outputTensor,
        std::vector<ClassificationResult>& vecResults,
        const std::vector <std::string>& labels, uint32_t topNCount)
    {
        if (outputTensor == nullptr) {
            printf_err("Output vector is null pointer.\n");
            return false;
        }

        uint32_t totalOutputSize = 1;
        for (int inputDim = 0; inputDim < outputTensor->dims->size; inputDim++){
            totalOutputSize *= outputTensor->dims->data[inputDim];
        }

        /* Sanity checks. */
        if (totalOutputSize < topNCount) {
            printf_err("Output vector is smaller than %u\n", topNCount);
            return false;
        } else if (totalOutputSize != labels.size()) {
            printf_err("Output size doesn't match the labels' size\n");
            return false;
        }

        bool resultState;
        vecResults.clear();

        /* Get the top N results. */
        switch (outputTensor->type) {
            case kTfLiteUInt8:
                resultState = _GetTopNResults<uint8_t>(outputTensor, vecResults, topNCount, labels);
                break;
            case kTfLiteInt8:
                resultState = _GetTopNResults<int8_t>(outputTensor, vecResults, topNCount, labels);
                break;
            case kTfLiteFloat32:
                resultState = _GetTopNResults<float>(outputTensor, vecResults, topNCount, labels);
                break;
            default:
                printf_err("Tensor type %s not supported by classifier\n", TfLiteTypeGetName(outputTensor->type));
                return false;
        }

        if (!resultState) {
            printf_err("Failed to get sorted set\n");
            return false;
        }

        return true;
    }

} /* namespace app */
} /* namespace arm */