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 | |
| 21 | TEST_CASE("Common classifier") |
| 22 | { |
| 23 | SECTION("Test invalid classifier") |
| 24 | { |
| 25 | TfLiteTensor* outputTens = nullptr; |
| 26 | std::vector <arm::app::ClassificationResult> resultVec; |
| 27 | arm::app::Classifier classifier; |
| 28 | REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 5)); |
| 29 | } |
| 30 | |
| 31 | SECTION("Test valid classifier UINT8") |
| 32 | { |
| 33 | const int dimArray[] = {1, 1001}; |
| 34 | std::vector <std::string> labels(1001); |
| 35 | std::vector <uint8_t> outputVec(1001); |
| 36 | TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); |
| 37 | TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( |
| 38 | outputVec.data(), dims, 1, 0, "test"); |
| 39 | TfLiteTensor* outputTensor = &tfTensor; |
| 40 | std::vector <arm::app::ClassificationResult> resultVec; |
| 41 | arm::app::Classifier classifier; |
| 42 | REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5)); |
| 43 | REQUIRE(5 == resultVec.size()); |
| 44 | } |
| 45 | |
| 46 | SECTION("Get classification results") |
| 47 | { |
| 48 | const int dimArray[] = {1, 1001}; |
| 49 | std::vector <std::string> labels(1001); |
| 50 | std::vector<uint8_t> outputVec(1001, static_cast<uint8_t>(5)); |
| 51 | TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); |
| 52 | TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( |
| 53 | outputVec.data(), dims, 1, 0, "test"); |
| 54 | TfLiteTensor* outputTensor = &tfTensor; |
| 55 | |
| 56 | std::vector <arm::app::ClassificationResult> resultVec; |
| 57 | |
| 58 | /* Set the top five results. */ |
| 59 | std::vector<std::pair<uint32_t, uint8_t>> selectedResults { |
| 60 | {0, 8}, {20, 7}, {10, 7}, {15, 9}, {1000, 10}}; |
| 61 | |
| 62 | for (size_t i = 0; i < selectedResults.size(); ++i) { |
| 63 | outputVec[selectedResults[i].first] = selectedResults[i].second; |
| 64 | } |
| 65 | |
| 66 | arm::app::Classifier classifier; |
| 67 | REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5)); |
| 68 | REQUIRE(5 == resultVec.size()); |
| 69 | |
| 70 | REQUIRE(resultVec[0].m_labelIdx == 1000); |
| 71 | REQUIRE(resultVec[1].m_labelIdx == 15); |
| 72 | REQUIRE(resultVec[2].m_labelIdx == 0); |
| 73 | REQUIRE(resultVec[3].m_labelIdx == 20); |
| 74 | REQUIRE(resultVec[4].m_labelIdx == 10); |
| 75 | } |
| 76 | } |