summaryrefslogtreecommitdiff
path: root/tests/use_case/asr/AsrClassifierTests.cc
blob: e2bfb18325bcf40db151e99e52f2115718aad887 (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
/*
 * 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 "AsrClassifier.hpp"
#include "Wav2LetterModel.hpp"

#include <catch.hpp>

TEST_CASE("Test invalid classifier")
{
    TfLiteTensor* outputTens = nullptr;
    std::vector <arm::app::ClassificationResult> resultVec;
    arm::app::AsrClassifier classifier;

    REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 1));
}


TEST_CASE("Test valid classifier UINT8") {
    int dimArray[] = {4, 1, 1, 246, 29};
    std::vector <std::string> labels(29);
    std::vector <uint8_t> outputVec(7134);
    TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray);
    TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor(
                                outputVec.data(), dims, 1, 0);
    TfLiteTensor* outputTensor = &tfTensor;
    std::vector <arm::app::ClassificationResult> resultVec;
    arm::app::AsrClassifier classifier;

    REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1));
    REQUIRE(246 == resultVec.size());
}


TEST_CASE("Get classification results") {
    int dimArray[] = {4, 1, 1, 10, 15};
    std::vector <std::string> labels(15);
    std::vector<uint8_t> outputVec(150, static_cast<uint8_t>(1));
    TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray);
    TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor(
                                outputVec.data(), dims, 1, 0);
    TfLiteTensor* outputTensor = &tfTensor;

    std::vector <arm::app::ClassificationResult> resultVec(10);

    /* set the top five results: */
    std::vector<std::pair<uint32_t, std::pair<uint32_t, uint8_t>>> selectedResults {
        {0, {3, 23}},
        {0, {9, 15}},
        {1, {5, 24}},
        {1, {7, 4}},
        {2, {9, 5}},
        {3, {8, 6}},
        {4, {13, 10}},
        {4, {6, 18}},
        {5, {3, 15}},
        {5, {4, 115}},
        {6, {6, 25}},
        {7, {1, 7}},
        {8, {11, 9}},
        {9, {1, 10}}
    };

    const uint32_t nCols = outputTensor->dims->data[arm::app::Wav2LetterModel::ms_outputColsIdx];
    for (size_t i = 0; i < selectedResults.size(); ++i) {
        uint32_t rIndex = selectedResults[i].first;
        uint32_t cIndex = selectedResults[i].second.first;
        uint8_t   value = selectedResults[i].second.second;
        outputVec[rIndex * nCols + cIndex] = value;
    }

    arm::app::AsrClassifier classifier;

    REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1));
    REQUIRE(resultVec[0].m_labelIdx == 3);
    REQUIRE(resultVec[1].m_labelIdx == 5);
    REQUIRE(resultVec[2].m_labelIdx == 9);
    REQUIRE(resultVec[3].m_labelIdx == 8);
    REQUIRE(resultVec[4].m_labelIdx == 6);
    REQUIRE(resultVec[5].m_labelIdx == 4);
    REQUIRE(resultVec[6].m_labelIdx == 6);
    REQUIRE(resultVec[7].m_labelIdx == 1);
    REQUIRE(resultVec[8].m_labelIdx == 11);
    REQUIRE(resultVec[9].m_labelIdx == 1);
}