diff options
author | alexander <alexander.efremov@arm.com> | 2021-04-29 20:36:09 +0100 |
---|---|---|
committer | Alexander Efremov <alexander.efremov@arm.com> | 2021-05-04 19:57:44 +0000 |
commit | c350cdced0a8a2ca17376f58813e6d48d796ac7c (patch) | |
tree | f732cde664837a7cb9429b17e1366bb31a635b15 /tests/common | |
parent | 6448932cc1c612d78e62c778ebb228b3cbe96a58 (diff) | |
download | ml-embedded-evaluation-kit-c350cdced0a8a2ca17376f58813e6d48d796ac7c.tar.gz |
MLECO-1868: Code static analyzer warnings fixes
Signed-off-by: alexander <alexander.efremov@arm.com>
Change-Id: Ie423e9cad3fabec6ab077ded7236813fe4933dea
Diffstat (limited to 'tests/common')
-rw-r--r-- | tests/common/ClassifierTests.cc | 81 |
1 files changed, 44 insertions, 37 deletions
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 <catch.hpp> + +template<typename T> +void test_classifier_result(std::vector<std::pair<uint32_t, T>>& selectedResults, T defaultTensorValue) { + const int dimArray[] = {1, 1001}; + std::vector <std::string> labels(1001); + std::vector<T> outputVec(1001, defaultTensorValue); + TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); + TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor(outputVec.data(), dims, 1, 0); + TfLiteTensor* outputTensor = &tfTensor; + + std::vector <arm::app::ClassificationResult> 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 <std::string> labels(1001); - std::vector <uint8_t> outputVec(1001); - TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); - TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( - outputVec.data(), dims, 1, 0, "test"); - TfLiteTensor* outputTensor = &tfTensor; - std::vector <arm::app::ClassificationResult> resultVec; - arm::app::Classifier classifier; - REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5)); - REQUIRE(5 == resultVec.size()); - } + SECTION("uint8") { + /* Set the top five results <position, score>. */ + std::vector<std::pair<uint32_t, uint8_t>> selectedResults { + {1000, 10}, {15, 9}, {0, 8}, {20, 7}, {10, 7} }; - SECTION("Get classification results") - { - const int dimArray[] = {1, 1001}; - std::vector <std::string> labels(1001); - std::vector<uint8_t> outputVec(1001, static_cast<uint8_t>(5)); - TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); - TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( - outputVec.data(), dims, 1, 0, "test"); - TfLiteTensor* outputTensor = &tfTensor; - - std::vector <arm::app::ClassificationResult> resultVec; + test_classifier_result(selectedResults, static_cast<uint8_t>(5)); + } - /* Set the top five results. */ - std::vector<std::pair<uint32_t, uint8_t>> selectedResults { - {0, 8}, {20, 7}, {10, 7}, {15, 9}, {1000, 10}}; + SECTION("int8") { + /* Set the top five results <position, score>. */ + std::vector<std::pair<uint32_t, int8_t>> 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<int8_t>(-100)); } - arm::app::Classifier classifier; - REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 5)); - REQUIRE(5 == resultVec.size()); + SECTION("float") { + /* Set the top five results <position, score>. */ + std::vector<std::pair<uint32_t, float>> 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); } } |