blob: 7df424297ab072390a3ade6b9ab63888840946e8 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Liam Barryf30d7412023-09-27 12:48:35 +01002 * 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 +010021
22template<typename T>
23void test_classifier_result(std::vector<std::pair<uint32_t, T>>& selectedResults, T defaultTensorValue) {
Richard Burton0d110592021-08-12 17:26:30 +010024 int dimArray[] = {1, 1001};
alexanderc350cdc2021-04-29 20:36:09 +010025 std::vector <std::string> labels(1001);
26 std::vector<T> outputVec(1001, defaultTensorValue);
27 TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray);
28 TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor(outputVec.data(), dims, 1, 0);
29 TfLiteTensor* outputTensor = &tfTensor;
30
31 std::vector <arm::app::ClassificationResult> resultVec;
32
33 for (auto& selectedResult : selectedResults) {
34 outputVec[selectedResult.first] = selectedResult.second;
35 }
36
37 arm::app::Classifier classifier;
Kshitij Sisodia76a15802021-12-24 11:05:11 +000038 REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5, true));
alexanderc350cdc2021-04-29 20:36:09 +010039 REQUIRE(5 == resultVec.size());
40
41 for (size_t i = 0; i < resultVec.size(); ++i) {
42 REQUIRE(resultVec[i].m_labelIdx == selectedResults[i].first);
43 }
44}
45
alexander3c798932021-03-26 21:42:19 +000046TEST_CASE("Common classifier")
47{
48 SECTION("Test invalid classifier")
49 {
Liam Barryf30d7412023-09-27 12:48:35 +010050 /* Note: Errors or warnings generated by this test will appear in output of any subsequent
51 * failing tests causing misleading output. Give warning until solution is found */
52 printf("Invalid classifier common test output:\n");
alexander3c798932021-03-26 21:42:19 +000053 TfLiteTensor* outputTens = nullptr;
54 std::vector <arm::app::ClassificationResult> resultVec;
55 arm::app::Classifier classifier;
Kshitij Sisodia76a15802021-12-24 11:05:11 +000056 REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 5, true));
Liam Barryf30d7412023-09-27 12:48:35 +010057 printf("End of invalid classifier common test output. \nERROR messages above this line are "
58 "expected and can be ignored.\n\n");
alexander3c798932021-03-26 21:42:19 +000059 }
60
alexanderc350cdc2021-04-29 20:36:09 +010061 SECTION("Test classification results")
alexander3c798932021-03-26 21:42:19 +000062 {
alexanderc350cdc2021-04-29 20:36:09 +010063 SECTION("uint8") {
64 /* Set the top five results <position, score>. */
65 std::vector<std::pair<uint32_t, uint8_t>> selectedResults {
66 {1000, 10}, {15, 9}, {0, 8}, {20, 7}, {10, 7} };
alexander3c798932021-03-26 21:42:19 +000067
alexanderc350cdc2021-04-29 20:36:09 +010068 test_classifier_result(selectedResults, static_cast<uint8_t>(5));
alexander3c798932021-03-26 21:42:19 +000069 }
70
alexanderc350cdc2021-04-29 20:36:09 +010071 SECTION("int8") {
72 /* Set the top five results <position, score>. */
73 std::vector<std::pair<uint32_t, int8_t>> selectedResults {
74 {1000, 10}, {15, 9}, {0, 8}, {20, -7}, {10, -7} };
alexander3c798932021-03-26 21:42:19 +000075
alexanderc350cdc2021-04-29 20:36:09 +010076 test_classifier_result(selectedResults, static_cast<int8_t>(-100));
77 }
78
79 SECTION("float") {
80 /* Set the top five results <position, score>. */
81 std::vector<std::pair<uint32_t, float>> selectedResults {
82 {1000, 10.9f}, {15, 9.8f}, {0, 8.7f}, {20, -7.0f}, {10, -7.1f} };
83
84 test_classifier_result(selectedResults, -100.0f);
85 }
86
alexander3c798932021-03-26 21:42:19 +000087 }
88}