blob: 693f744d607005b243af09e944fe91b670fb66eb [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * 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 {
50 TfLiteTensor* outputTens = nullptr;
51 std::vector <arm::app::ClassificationResult> resultVec;
52 arm::app::Classifier classifier;
Kshitij Sisodia76a15802021-12-24 11:05:11 +000053 REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 5, true));
alexander3c798932021-03-26 21:42:19 +000054 }
55
alexanderc350cdc2021-04-29 20:36:09 +010056 SECTION("Test classification results")
alexander3c798932021-03-26 21:42:19 +000057 {
alexanderc350cdc2021-04-29 20:36:09 +010058 SECTION("uint8") {
59 /* Set the top five results <position, score>. */
60 std::vector<std::pair<uint32_t, uint8_t>> selectedResults {
61 {1000, 10}, {15, 9}, {0, 8}, {20, 7}, {10, 7} };
alexander3c798932021-03-26 21:42:19 +000062
alexanderc350cdc2021-04-29 20:36:09 +010063 test_classifier_result(selectedResults, static_cast<uint8_t>(5));
alexander3c798932021-03-26 21:42:19 +000064 }
65
alexanderc350cdc2021-04-29 20:36:09 +010066 SECTION("int8") {
67 /* Set the top five results <position, score>. */
68 std::vector<std::pair<uint32_t, int8_t>> selectedResults {
69 {1000, 10}, {15, 9}, {0, 8}, {20, -7}, {10, -7} };
alexander3c798932021-03-26 21:42:19 +000070
alexanderc350cdc2021-04-29 20:36:09 +010071 test_classifier_result(selectedResults, static_cast<int8_t>(-100));
72 }
73
74 SECTION("float") {
75 /* Set the top five results <position, score>. */
76 std::vector<std::pair<uint32_t, float>> selectedResults {
77 {1000, 10.9f}, {15, 9.8f}, {0, 8.7f}, {20, -7.0f}, {10, -7.1f} };
78
79 test_classifier_result(selectedResults, -100.0f);
80 }
81
alexander3c798932021-03-26 21:42:19 +000082 }
83}