aboutsummaryrefslogtreecommitdiff
path: root/samples/KeywordSpotting/src/Decoder.cpp
blob: 107e25caa96f424b0a0258e3c9d70a365ccc2a6a (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
//
// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "Decoder.hpp"

std::pair<int, float> kws::Decoder::decodeOutput(std::vector<int8_t>& modelOutput) 
{

    std::vector<float> dequantisedOutput;
    //Normalise vector values into new vector
    for (auto& value : modelOutput) 
    {
        float normalisedModelOutput = this->quantisationScale * (static_cast<float >(value) -
                                                                 static_cast<float >(this->quantisationOffset));
        dequantisedOutput.push_back(normalisedModelOutput);
    }

    //Get largest value in modelOutput
    const std::vector<float>::iterator& maxElementIterator = std::max_element(dequantisedOutput.begin(),
                                                                              dequantisedOutput.end());
    //Find the labelMapIndex of the largest value which corresponds to a key in a label map
    int labelMapIndex = static_cast<int>(std::distance(dequantisedOutput.begin(), maxElementIterator));

    //Round to two DP
    float maxModelOutputProbability = std::roundf((*maxElementIterator) * 100) / 100;

    return std::make_pair(labelMapIndex, maxModelOutputProbability);

}