summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authoralexander <alexander.efremov@arm.com>2021-04-29 20:36:09 +0100
committerAlexander Efremov <alexander.efremov@arm.com>2021-05-04 19:57:44 +0000
commitc350cdced0a8a2ca17376f58813e6d48d796ac7c (patch)
treef732cde664837a7cb9429b17e1366bb31a635b15 /tests
parent6448932cc1c612d78e62c778ebb228b3cbe96a58 (diff)
downloadml-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')
-rw-r--r--tests/common/ClassifierTests.cc81
-rw-r--r--tests/use_case/asr/AsrClassifierTests.cc4
-rw-r--r--tests/use_case/asr/AsrFeaturesTests.cc38
3 files changed, 63 insertions, 60 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);
}
}
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 <uint8_t> 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 <arm::app::ClassificationResult> resultVec;
arm::app::AsrClassifier classifier;
@@ -51,7 +51,7 @@ TEST_CASE("Get classification results") {
std::vector<uint8_t> outputVec(150, static_cast<uint8_t>(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 <arm::app::ClassificationResult> 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<float>& mfcc,
+ static bool ComputeDeltas(arm::app::Array2d<float>& mfcc,
arm::app::Array2d<float>& delta1,
arm::app::Array2d<float>& delta2)
{
- return this->_ComputeDeltas(mfcc, delta1, delta2);
+ return Preprocess::ComputeDeltas(mfcc, delta1, delta2);
}
- float GetMean(arm::app::Array2d<float>& vec)
+ static float GetMean(arm::app::Array2d<float>& vec)
{
- return this->_GetMean(vec);
+ return Preprocess::GetMean(vec);
}
- float GetStdDev(arm::app::Array2d<float>& vec, const float mean)
+ static float GetStdDev(arm::app::Array2d<float>& vec, const float mean)
{
- return this->_GetStdDev(vec, mean);
+ return Preprocess::GetStdDev(vec, mean);
}
- void NormaliseVec(arm::app::Array2d<float>& vec)
+ static void NormaliseVec(arm::app::Array2d<float>& vec)
{
- return this->_NormaliseVec(vec);
+ return Preprocess::NormaliseVec(vec);
}
};
@@ -86,7 +83,6 @@ void populateArray2dWithVectorOfVector(std::vector<std::vector<float>> 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<float> 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<float> 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<float> 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<float> 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<std::vector<float>> std2vec{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}};
arm::app::Array2d<float> 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<float> 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<float>& d, std::vector<float>& g) {
- tp.NormaliseVec(d);
+ TestPreprocess::NormaliseVec(d);
std::vector<float> d_vec(d.begin(), d.end());
REQUIRE_THAT(g, Catch::Approx(d_vec));
};