From ec5e99be3ae6dd0d3811950f155b01e144431452 Mon Sep 17 00:00:00 2001 From: Richard Burton Date: Wed, 5 Oct 2022 11:00:37 +0100 Subject: MLECO-3164: Additional refactoring of KWS API Part 1 * Add KwsClassifier * KwsPostProcess can now be told to average results * Averaging is handlded by KwsClassifier * Current sliding window index is now an argument of DoPreProcess Change-Id: I07626da595ad1cbd982e8366f0d1bb56d1040459 --- .../use_case/img_class/ImgClassificationUCTest.cc | 3 +- tests/use_case/kws/KWSHandlerTest.cc | 3 +- tests/use_case/kws/KwsClassifierTests.cc | 125 +++++++++++++++++++++ tests/use_case/noise_reduction/RNNNoiseUCTests.cc | 8 +- .../object_detection/InferenceTestYoloFastest.cc | 2 +- tests/use_case/vww/VisualWakeWordUCTests.cc | 3 +- 6 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 tests/use_case/kws/KwsClassifierTests.cc (limited to 'tests') diff --git a/tests/use_case/img_class/ImgClassificationUCTest.cc b/tests/use_case/img_class/ImgClassificationUCTest.cc index 1685e5f..78700dc 100644 --- a/tests/use_case/img_class/ImgClassificationUCTest.cc +++ b/tests/use_case/img_class/ImgClassificationUCTest.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. All rights reserved. + * Copyright (c) 2021-2022 Arm Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,6 +21,7 @@ #include "MobileNetModel.hpp" #include "UseCaseHandler.hpp" #include "UseCaseCommonUtils.hpp" +#include "BufAttributes.hpp" #include diff --git a/tests/use_case/kws/KWSHandlerTest.cc b/tests/use_case/kws/KWSHandlerTest.cc index db67e54..413e089 100644 --- a/tests/use_case/kws/KWSHandlerTest.cc +++ b/tests/use_case/kws/KWSHandlerTest.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. All rights reserved. + * Copyright (c) 2021-2022 Arm Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,6 +23,7 @@ #include "UseCaseHandler.hpp" #include "Classifier.hpp" #include "UseCaseCommonUtils.hpp" +#include "BufAttributes.hpp" namespace arm { namespace app { diff --git a/tests/use_case/kws/KwsClassifierTests.cc b/tests/use_case/kws/KwsClassifierTests.cc new file mode 100644 index 0000000..92fb15e --- /dev/null +++ b/tests/use_case/kws/KwsClassifierTests.cc @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2022 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "KwsClassifier.hpp" + +#include + +TEST_CASE("Test invalid classifier") +{ + TfLiteTensor* outputTens = nullptr; + std::vector resultVec; + arm::app::KwsClassifier classifier; + std::vector> resultHistory; + REQUIRE(!classifier.GetClassificationResults(outputTens, resultVec, {}, 5, true, resultHistory)); +} + +TEST_CASE("Test valid classifier, average=0 should be same as 1)") +{ + int dimArray[] = {1, 5}; + std::vector labels(5); + std::vector outputVec = {0, 1, 2, 3, 4}; + std::vector> resultHistory = {}; + TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); + TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( + outputVec.data(), dims, 1, 0); + TfLiteTensor* outputTensor = &tfTensor; + std::vector resultVec; + arm::app::KwsClassifier classifier; + + REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1, false, resultHistory)); + REQUIRE(resultVec[0].m_labelIdx == 4); + REQUIRE(resultVec[0].m_normalisedVal == 4); + + REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1, false, resultHistory)); + REQUIRE(resultVec[0].m_labelIdx == 4); + REQUIRE(resultVec[0].m_normalisedVal == 4); + + std::vector> expectedHistory = {}; + REQUIRE(resultHistory == expectedHistory); +} + +TEST_CASE("Test valid classifier UINT8, average=1, softmax=false") +{ + int dimArray[] = {1, 5}; + std::vector labels(5); + std::vector outputVec = {0, 1, 2, 3, 4}; + std::vector> resultHistory = {{0, 0, 0, 0, 0}}; + TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); + TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( + outputVec.data(), dims, 1, 0); + TfLiteTensor* outputTensor = &tfTensor; + std::vector resultVec; + arm::app::KwsClassifier classifier; + + REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1, false, resultHistory)); + REQUIRE(resultVec[0].m_labelIdx == 4); + REQUIRE(resultVec[0].m_normalisedVal == 4); + + /* We do not update history if not >1 in size. */ + std::vector> expectedHistory = {{0, 0, 0, 0, 0}}; + REQUIRE(resultHistory == expectedHistory); +} + +TEST_CASE("Test valid classifier UINT8, average=2") +{ + int dimArray[] = {1, 5}; + std::vector labels(5); + std::vector outputVec = {0, 1, 2, 3, 4}; + std::vector> resultHistory = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}}; + TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); + TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( + outputVec.data(), dims, 1, 0); + TfLiteTensor* outputTensor = &tfTensor; + std::vector resultVec; + arm::app::KwsClassifier classifier; + + REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1, false, resultHistory)); + REQUIRE(resultVec[0].m_labelIdx == 4); + REQUIRE(resultVec[0].m_normalisedVal == 2); + + REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1, false, resultHistory)); + REQUIRE(resultVec[0].m_labelIdx == 4); + REQUIRE(resultVec[0].m_normalisedVal == 4); + + std::vector> expectedHistory = {{0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}}; + REQUIRE(resultHistory == expectedHistory); +} + +TEST_CASE("Test valid classifier int8, average=0") +{ + int dimArray[] = {1, 5}; + std::vector labels(5); + std::vector outputVec = {-2, -1, 0, 2, 1}; + std::vector> resultHistory = {}; + TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); + TfLiteTensor tfTensor = tflite::testing::CreateQuantizedTensor( + outputVec.data(), dims, 1, 0); + TfLiteTensor* outputTensor = &tfTensor; + std::vector resultVec; + arm::app::KwsClassifier classifier; + + REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1, false, resultHistory)); + REQUIRE(resultVec[0].m_labelIdx == 3); + REQUIRE(resultVec[0].m_normalisedVal == 2); + + REQUIRE(classifier.GetClassificationResults(outputTensor, resultVec, labels, 1, false, resultHistory)); + REQUIRE(resultVec[0].m_labelIdx == 3); + REQUIRE(resultVec[0].m_normalisedVal == 2); + + std::vector> expectedHistory = {}; + REQUIRE(resultHistory == expectedHistory); +} \ No newline at end of file diff --git a/tests/use_case/noise_reduction/RNNNoiseUCTests.cc b/tests/use_case/noise_reduction/RNNNoiseUCTests.cc index 310814f..144fc9f 100644 --- a/tests/use_case/noise_reduction/RNNNoiseUCTests.cc +++ b/tests/use_case/noise_reduction/RNNNoiseUCTests.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. All rights reserved. + * Copyright (c) 2021-2022 Arm Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,11 +18,11 @@ #include "UseCaseHandler.hpp" #include "InputFiles.hpp" #include "RNNUCTestCaseData.hpp" -#include "UseCaseCommonUtils.hpp" +#include "BufAttributes.hpp" +#include "hal.h" +#include "Profiler.hpp" #include -#include -#include namespace arm { namespace app { diff --git a/tests/use_case/object_detection/InferenceTestYoloFastest.cc b/tests/use_case/object_detection/InferenceTestYoloFastest.cc index d328684..ff70bf9 100644 --- a/tests/use_case/object_detection/InferenceTestYoloFastest.cc +++ b/tests/use_case/object_detection/InferenceTestYoloFastest.cc @@ -20,7 +20,7 @@ #include "TensorFlowLiteMicro.hpp" #include "DetectorPostProcessing.hpp" #include "InputFiles.hpp" -#include "UseCaseCommonUtils.hpp" +#include "BufAttributes.hpp" namespace arm { namespace app { diff --git a/tests/use_case/vww/VisualWakeWordUCTests.cc b/tests/use_case/vww/VisualWakeWordUCTests.cc index fe3782b..96c1346 100644 --- a/tests/use_case/vww/VisualWakeWordUCTests.cc +++ b/tests/use_case/vww/VisualWakeWordUCTests.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. All rights reserved. + * Copyright (c) 2021-2022 Arm Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,6 +23,7 @@ #include "UseCaseHandler.hpp" #include "Classifier.hpp" #include "UseCaseCommonUtils.hpp" +#include "BufAttributes.hpp" namespace arm { namespace app { -- cgit v1.2.1