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 +++++++++++++++++--------------- tests/use_case/asr/AsrClassifierTests.cc | 4 +- tests/use_case/asr/AsrFeaturesTests.cc | 38 +++++++-------- 3 files changed, 63 insertions(+), 60 deletions(-) (limited to 'tests') 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); } } diff --git a/tests/use_case/asr/AsrClassifierTests.cc b/tests/use_case/asr/AsrClassifierTests.cc index 7c71912..12523aa 100644 --- a/tests/use_case/asr/AsrClassifierTests.cc +++ b/tests/use_case/asr/AsrClassifierTests.cc @@ -35,7 +35,7 @@ TEST_CASE("Test valid classifier UINT8") { std::vector outputVec(7134); TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( - outputVec.data(), dims, 1, 0, "test"); + outputVec.data(), dims, 1, 0); TfLiteTensor* outputTensor = &tfTensor; std::vector resultVec; arm::app::AsrClassifier classifier; @@ -51,7 +51,7 @@ TEST_CASE("Get classification results") { std::vector outputVec(150, static_cast(1)); TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( - outputVec.data(), dims, 1, 0, "test"); + outputVec.data(), dims, 1, 0); TfLiteTensor* outputTensor = &tfTensor; std::vector resultVec(10); diff --git a/tests/use_case/asr/AsrFeaturesTests.cc b/tests/use_case/asr/AsrFeaturesTests.cc index 9401f40..59fe29b 100644 --- a/tests/use_case/asr/AsrFeaturesTests.cc +++ b/tests/use_case/asr/AsrFeaturesTests.cc @@ -25,30 +25,27 @@ class TestPreprocess : public arm::app::audio::asr::Preprocess { public: - TestPreprocess() - : arm::app::audio::asr::Preprocess(0,0,0,0) - {} - bool ComputeDeltas(arm::app::Array2d& mfcc, + static bool ComputeDeltas(arm::app::Array2d& mfcc, arm::app::Array2d& delta1, arm::app::Array2d& delta2) { - return this->_ComputeDeltas(mfcc, delta1, delta2); + return Preprocess::ComputeDeltas(mfcc, delta1, delta2); } - float GetMean(arm::app::Array2d& vec) + static float GetMean(arm::app::Array2d& vec) { - return this->_GetMean(vec); + return Preprocess::GetMean(vec); } - float GetStdDev(arm::app::Array2d& vec, const float mean) + static float GetStdDev(arm::app::Array2d& vec, const float mean) { - return this->_GetStdDev(vec, mean); + return Preprocess::GetStdDev(vec, mean); } - void NormaliseVec(arm::app::Array2d& vec) + static void NormaliseVec(arm::app::Array2d& vec) { - return this->_NormaliseVec(vec); + return Preprocess::NormaliseVec(vec); } }; @@ -86,7 +83,6 @@ void populateArray2dWithVectorOfVector(std::vector> vec, arm: TEST_CASE("Floating point asr features calculation", "[ASR]") { - TestPreprocess tp; SECTION("First and second diff") { @@ -109,7 +105,7 @@ TEST_CASE("Floating point asr features calculation", "[ASR]") std::fill(delta1Buf.begin(), delta1Buf.end(), 0.f); std::fill(delta2Buf.begin(), delta2Buf.end(), 0.f); - tp.ComputeDeltas(mfccBuf, delta1Buf, delta2Buf); + TestPreprocess::ComputeDeltas(mfccBuf, delta1Buf, delta2Buf); /* First 4 and last 4 values are different because we pad AFTER diff calculated. */ for (size_t i = 0; i < numMfccFeats; ++i) { @@ -136,37 +132,37 @@ TEST_CASE("Floating point asr features calculation", "[ASR]") {-1, -2}}; arm::app::Array2d mean1(2,2); /* {{1, 2},{-1, -2}} */ populateArray2dWithVectorOfVector(mean1vec, mean1); - REQUIRE(0 == Approx(tp.GetMean(mean1))); + REQUIRE(0 == Approx(TestPreprocess::GetMean(mean1))); arm::app::Array2d mean2(2, 2); std::fill(mean2.begin(), mean2.end(), 0.f); - REQUIRE(0 == Approx(tp.GetMean(mean2))); + REQUIRE(0 == Approx(TestPreprocess::GetMean(mean2))); arm::app::Array2d mean3(3,3); std::fill(mean3.begin(), mean3.end(), 1.f); - REQUIRE(1 == Approx(tp.GetMean(mean3))); + REQUIRE(1 == Approx(TestPreprocess::GetMean(mean3))); } SECTION("Std") { arm::app::Array2d std1(2, 2); std::fill(std1.begin(), std1.end(), 0.f); /* {{0, 0}, {0, 0}} */ - REQUIRE(0 == Approx(tp.GetStdDev(std1, 0))); + REQUIRE(0 == Approx(TestPreprocess::GetStdDev(std1, 0))); std::vector> std2vec{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}}; arm::app::Array2d std2(2,5); populateArray2dWithVectorOfVector(std2vec, std2); - const float mean = tp.GetMean(std2); - REQUIRE(2.872281323 == Approx(tp.GetStdDev(std2, mean))); + const float mean = TestPreprocess::GetMean(std2); + REQUIRE(2.872281323 == Approx(TestPreprocess::GetStdDev(std2, mean))); arm::app::Array2d std3(2,2); std::fill(std3.begin(), std3.end(), 1.f); /* std3{{1, 1}, {1, 1}}; */ - REQUIRE(0 == Approx(tp.GetStdDev(std3, 1))); + REQUIRE(0 == Approx(TestPreprocess::GetStdDev(std3, 1))); } SECTION("Norm") { auto checker = [&](arm::app::Array2d& d, std::vector& g) { - tp.NormaliseVec(d); + TestPreprocess::NormaliseVec(d); std::vector d_vec(d.begin(), d.end()); REQUIRE_THAT(g, Catch::Approx(d_vec)); }; -- cgit v1.2.1