alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 21 | |
| 22 | template<typename T> |
| 23 | 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] | 24 | int dimArray[] = {1, 1001}; |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 25 | 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; |
| 38 | REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5)); |
| 39 | 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 46 | TEST_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; |
| 53 | REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 5)); |
| 54 | } |
| 55 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 56 | SECTION("Test classification results") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 57 | { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 58 | 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} }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 62 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 63 | test_classifier_result(selectedResults, static_cast<uint8_t>(5)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 64 | } |
| 65 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 66 | 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} }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 70 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 71 | 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 82 | } |
| 83 | } |