alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Liam Barry | cca34a9 | 2023-11-03 15:49:34 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021,2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 21 | template<typename T> |
| 22 | void test_classifier_result(std::vector<std::pair<uint32_t, T>>& selectedResults, T defaultTensorValue) { |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 23 | int dimArray[] = {1, 1001}; |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 24 | 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 Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 37 | REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5, true)); |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 38 | 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | TEST_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 Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 52 | REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 5, true)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 53 | } |
| 54 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 55 | SECTION("Test classification results") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 56 | { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 57 | 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} }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 62 | test_classifier_result(selectedResults, static_cast<uint8_t>(5)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | } |
| 64 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 65 | 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} }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 70 | 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | } |
| 82 | } |