blob: b15ffd4dec99ba4fec7153c4c9ca919de79faf0a [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Liam Barrycca34a92023-11-03 15:49:34 +00002 * SPDX-FileCopyrightText: Copyright 2021,2023 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 "Classifier.hpp"
18
19#include <catch.hpp>
20
alexanderc350cdc2021-04-29 20:36:09 +010021template<typename T>
22void test_classifier_result(std::vector<std::pair<uint32_t, T>>& selectedResults, T defaultTensorValue) {
Richard Burton0d110592021-08-12 17:26:30 +010023 int dimArray[] = {1, 1001};
alexanderc350cdc2021-04-29 20:36:09 +010024 std::vector <std::string> labels(1001);
25 std::vector<T> outputVec(1001, defaultTensorValue);
26 TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray);
27 TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor(outputVec.data(), dims, 1, 0);
28 TfLiteTensor* outputTensor = &tfTensor;
29
30 std::vector <arm::app::ClassificationResult> resultVec;
31
32 for (auto& selectedResult : selectedResults) {
33 outputVec[selectedResult.first] = selectedResult.second;
34 }
35
36 arm::app::Classifier classifier;
Kshitij Sisodia76a15802021-12-24 11:05:11 +000037 REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5, true));
alexanderc350cdc2021-04-29 20:36:09 +010038 REQUIRE(5 == resultVec.size());
39
40 for (size_t i = 0; i < resultVec.size(); ++i) {
41 REQUIRE(resultVec[i].m_labelIdx == selectedResults[i].first);
42 }
43}
44
alexander3c798932021-03-26 21:42:19 +000045TEST_CASE("Common classifier")
46{
47 SECTION("Test invalid classifier")
48 {
49 TfLiteTensor* outputTens = nullptr;
50 std::vector <arm::app::ClassificationResult> resultVec;
51 arm::app::Classifier classifier;
Kshitij Sisodia76a15802021-12-24 11:05:11 +000052 REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 5, true));
alexander3c798932021-03-26 21:42:19 +000053 }
54
alexanderc350cdc2021-04-29 20:36:09 +010055 SECTION("Test classification results")
alexander3c798932021-03-26 21:42:19 +000056 {
alexanderc350cdc2021-04-29 20:36:09 +010057 SECTION("uint8") {
58 /* Set the top five results <position, score>. */
59 std::vector<std::pair<uint32_t, uint8_t>> selectedResults {
60 {1000, 10}, {15, 9}, {0, 8}, {20, 7}, {10, 7} };
alexander3c798932021-03-26 21:42:19 +000061
alexanderc350cdc2021-04-29 20:36:09 +010062 test_classifier_result(selectedResults, static_cast<uint8_t>(5));
alexander3c798932021-03-26 21:42:19 +000063 }
64
alexanderc350cdc2021-04-29 20:36:09 +010065 SECTION("int8") {
66 /* Set the top five results <position, score>. */
67 std::vector<std::pair<uint32_t, int8_t>> selectedResults {
68 {1000, 10}, {15, 9}, {0, 8}, {20, -7}, {10, -7} };
alexander3c798932021-03-26 21:42:19 +000069
alexanderc350cdc2021-04-29 20:36:09 +010070 test_classifier_result(selectedResults, static_cast<int8_t>(-100));
71 }
72
73 SECTION("float") {
74 /* Set the top five results <position, score>. */
75 std::vector<std::pair<uint32_t, float>> selectedResults {
76 {1000, 10.9f}, {15, 9.8f}, {0, 8.7f}, {20, -7.0f}, {10, -7.1f} };
77
78 test_classifier_result(selectedResults, -100.0f);
79 }
80
alexander3c798932021-03-26 21:42:19 +000081 }
82}