From c350cdced0a8a2ca17376f58813e6d48d796ac7c Mon Sep 17 00:00:00 2001 From: alexander Date: Thu, 29 Apr 2021 20:36:09 +0100 Subject: MLECO-1868: Code static analyzer warnings fixes Signed-off-by: alexander Change-Id: Ie423e9cad3fabec6ab077ded7236813fe4933dea --- tests/common/ClassifierTests.cc | 81 ++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 37 deletions(-) (limited to 'tests/common/ClassifierTests.cc') diff --git a/tests/common/ClassifierTests.cc b/tests/common/ClassifierTests.cc index f08a09a..a04e4c2 100644 --- a/tests/common/ClassifierTests.cc +++ b/tests/common/ClassifierTests.cc @@ -18,6 +18,31 @@ #include + +template +void test_classifier_result(std::vector>& selectedResults, T defaultTensorValue) { + const int dimArray[] = {1, 1001}; + std::vector labels(1001); + std::vector outputVec(1001, defaultTensorValue); + TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); + TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor(outputVec.data(), dims, 1, 0); + TfLiteTensor* outputTensor = &tfTensor; + + std::vector resultVec; + + for (auto& selectedResult : selectedResults) { + outputVec[selectedResult.first] = selectedResult.second; + } + + arm::app::Classifier classifier; + REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5)); + REQUIRE(5 == resultVec.size()); + + for (size_t i = 0; i < resultVec.size(); ++i) { + REQUIRE(resultVec[i].m_labelIdx == selectedResults[i].first); + } +} + TEST_CASE("Common classifier") { SECTION("Test invalid classifier") @@ -28,49 +53,31 @@ TEST_CASE("Common classifier") REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 5)); } - SECTION("Test valid classifier UINT8") + SECTION("Test classification results") { - const int dimArray[] = {1, 1001}; - std::vector labels(1001); - std::vector outputVec(1001); - TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); - TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( - outputVec.data(), dims, 1, 0, "test"); - TfLiteTensor* outputTensor = &tfTensor; - std::vector resultVec; - arm::app::Classifier classifier; - REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5)); - REQUIRE(5 == resultVec.size()); - } + SECTION("uint8") { + /* Set the top five results . */ + std::vector> selectedResults { + {1000, 10}, {15, 9}, {0, 8}, {20, 7}, {10, 7} }; - SECTION("Get classification results") - { - const int dimArray[] = {1, 1001}; - std::vector labels(1001); - std::vector outputVec(1001, static_cast(5)); - TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); - TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( - outputVec.data(), dims, 1, 0, "test"); - TfLiteTensor* outputTensor = &tfTensor; - - std::vector resultVec; + test_classifier_result(selectedResults, static_cast(5)); + } - /* Set the top five results. */ - std::vector> selectedResults { - {0, 8}, {20, 7}, {10, 7}, {15, 9}, {1000, 10}}; + SECTION("int8") { + /* Set the top five results . */ + std::vector> selectedResults { + {1000, 10}, {15, 9}, {0, 8}, {20, -7}, {10, -7} }; - for (size_t i = 0; i < selectedResults.size(); ++i) { - outputVec[selectedResults[i].first] = selectedResults[i].second; + test_classifier_result(selectedResults, static_cast(-100)); } - arm::app::Classifier classifier; - REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5)); - REQUIRE(5 == resultVec.size()); + SECTION("float") { + /* Set the top five results . */ + std::vector> selectedResults { + {1000, 10.9f}, {15, 9.8f}, {0, 8.7f}, {20, -7.0f}, {10, -7.1f} }; + + test_classifier_result(selectedResults, -100.0f); + } - REQUIRE(resultVec[0].m_labelIdx == 1000); - REQUIRE(resultVec[1].m_labelIdx == 15); - REQUIRE(resultVec[2].m_labelIdx == 0); - REQUIRE(resultVec[3].m_labelIdx == 20); - REQUIRE(resultVec[4].m_labelIdx == 10); } } -- cgit v1.2.1