blob: 4c4a2bee9462d6861d2057ef3d44397ab2904d5c [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "AsrClassifier.hpp"
18#include "Wav2LetterModel.hpp"
19
20#include <catch.hpp>
21
22TEST_CASE("Test invalid classifier")
23{
24 TfLiteTensor* outputTens = nullptr;
25 std::vector <arm::app::ClassificationResult> resultVec;
26 arm::app::AsrClassifier classifier;
27
28 REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 1));
29}
30
31
32TEST_CASE("Test valid classifier UINT8") {
Richard Burton0d110592021-08-12 17:26:30 +010033 int dimArray[] = {4, 1, 1, 246, 29};
alexander3c798932021-03-26 21:42:19 +000034 std::vector <std::string> labels(29);
35 std::vector <uint8_t> outputVec(7134);
36 TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray);
37 TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor(
alexanderc350cdc2021-04-29 20:36:09 +010038 outputVec.data(), dims, 1, 0);
alexander3c798932021-03-26 21:42:19 +000039 TfLiteTensor* outputTensor = &tfTensor;
40 std::vector <arm::app::ClassificationResult> resultVec;
41 arm::app::AsrClassifier classifier;
42
43 REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1));
44 REQUIRE(246 == resultVec.size());
45}
46
47
48TEST_CASE("Get classification results") {
Richard Burton0d110592021-08-12 17:26:30 +010049 int dimArray[] = {4, 1, 1, 10, 15};
alexander3c798932021-03-26 21:42:19 +000050 std::vector <std::string> labels(15);
51 std::vector<uint8_t> outputVec(150, static_cast<uint8_t>(1));
52 TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray);
53 TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor(
alexanderc350cdc2021-04-29 20:36:09 +010054 outputVec.data(), dims, 1, 0);
alexander3c798932021-03-26 21:42:19 +000055 TfLiteTensor* outputTensor = &tfTensor;
56
57 std::vector <arm::app::ClassificationResult> resultVec(10);
58
59 /* set the top five results: */
60 std::vector<std::pair<uint32_t, std::pair<uint32_t, uint8_t>>> selectedResults {
61 {0, {3, 23}},
62 {0, {9, 15}},
63 {1, {5, 24}},
64 {1, {7, 4}},
65 {2, {9, 5}},
66 {3, {8, 6}},
67 {4, {13, 10}},
68 {4, {6, 18}},
69 {5, {3, 15}},
70 {5, {4, 115}},
71 {6, {6, 25}},
72 {7, {1, 7}},
73 {8, {11, 9}},
74 {9, {1, 10}}
75 };
76
77 const uint32_t nCols = outputTensor->dims->data[arm::app::Wav2LetterModel::ms_outputColsIdx];
78 for (size_t i = 0; i < selectedResults.size(); ++i) {
79 uint32_t rIndex = selectedResults[i].first;
80 uint32_t cIndex = selectedResults[i].second.first;
81 uint8_t value = selectedResults[i].second.second;
82 outputVec[rIndex * nCols + cIndex] = value;
83 }
84
85 arm::app::AsrClassifier classifier;
86
87 REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1));
88 REQUIRE(resultVec[0].m_labelIdx == 3);
89 REQUIRE(resultVec[1].m_labelIdx == 5);
90 REQUIRE(resultVec[2].m_labelIdx == 9);
91 REQUIRE(resultVec[3].m_labelIdx == 8);
92 REQUIRE(resultVec[4].m_labelIdx == 6);
93 REQUIRE(resultVec[5].m_labelIdx == 4);
94 REQUIRE(resultVec[6].m_labelIdx == 6);
95 REQUIRE(resultVec[7].m_labelIdx == 1);
96 REQUIRE(resultVec[8].m_labelIdx == 11);
97 REQUIRE(resultVec[9].m_labelIdx == 1);
98}