From c6ab02a626e15b4a12fc09ecd844eb8b95380c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89anna=20=C3=93=20Cath=C3=A1in?= Date: Wed, 7 Apr 2021 14:35:25 +0100 Subject: MLECO-1252 ASR sample application using the public ArmNN C++ API. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I98cd505b8772a8c8fa88308121bc94135bb45068 Signed-off-by: Éanna Ó Catháin --- .../SpeechRecognition/test/AudioCaptureTest.cpp | 61 +++++++++ samples/SpeechRecognition/test/DecoderTest.cpp | 86 +++++++++++++ samples/SpeechRecognition/test/MFCCTest.cpp | 102 ++++++++++++++++ samples/SpeechRecognition/test/PreprocessTest.cpp | 136 +++++++++++++++++++++ 4 files changed, 385 insertions(+) create mode 100644 samples/SpeechRecognition/test/AudioCaptureTest.cpp create mode 100644 samples/SpeechRecognition/test/DecoderTest.cpp create mode 100644 samples/SpeechRecognition/test/MFCCTest.cpp create mode 100644 samples/SpeechRecognition/test/PreprocessTest.cpp (limited to 'samples/SpeechRecognition/test') diff --git a/samples/SpeechRecognition/test/AudioCaptureTest.cpp b/samples/SpeechRecognition/test/AudioCaptureTest.cpp new file mode 100644 index 0000000000..94b4e7cb7a --- /dev/null +++ b/samples/SpeechRecognition/test/AudioCaptureTest.cpp @@ -0,0 +1,61 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#define CATCH_CONFIG_MAIN +#include +#include + +#include "AudioCapture.hpp" + +TEST_CASE("Test capture of audio file") +{ + std::string testResources = TEST_RESOURCE_DIR; + REQUIRE(testResources != ""); + std::string file = testResources + "/" + "myVoiceIsMyPassportVerifyMe04.wav"; + asr::AudioCapture capture; + std::vector audioData = capture.LoadAudioFile(file); + capture.InitSlidingWindow(audioData.data(), audioData.size(), 47712, 16000); + + std::vector firstAudioBlock = capture.Next(); + float actual1 = firstAudioBlock.at(0); + float actual2 = firstAudioBlock.at(47000); + CHECK(std::to_string(actual1) == "0.000352"); + CHECK(std::to_string(actual2) == "-0.056441"); + CHECK(firstAudioBlock.size() == 47712); + + CHECK(capture.HasNext() == true); + + std::vector secondAudioBlock = capture.Next(); + float actual3 = secondAudioBlock.at(0); + float actual4 = secondAudioBlock.at(47000); + CHECK(std::to_string(actual3) == "0.102077"); + CHECK(std::to_string(actual4) == "0.000194"); + CHECK(capture.HasNext() == true); + + std::vector thirdAudioBlock = capture.Next(); + float actual5 = thirdAudioBlock.at(0); + float actual6 = thirdAudioBlock.at(33500); + float actual7 = thirdAudioBlock.at(33600); + CHECK(std::to_string(actual5) == "-0.076416"); + CHECK(std::to_string(actual6) == "-0.000275"); + CHECK(std::to_string(actual7) == "0.000000"); + CHECK(capture.HasNext() == false); +} + +TEST_CASE("Test sliding window of audio capture") +{ + std::string testResources = TEST_RESOURCE_DIR; + REQUIRE(testResources != ""); + std::string file = testResources + "/" + "myVoiceIsMyPassportVerifyMe04.wav"; + asr::AudioCapture capture; + std::vector audioData = capture.LoadAudioFile(file); + capture.InitSlidingWindow(audioData.data(), audioData.size(), 47712, 16000); + capture.Next(); + capture.Next(); + + CHECK(capture.HasNext() == true); + capture.Next(); + CHECK(capture.HasNext() == false); +} diff --git a/samples/SpeechRecognition/test/DecoderTest.cpp b/samples/SpeechRecognition/test/DecoderTest.cpp new file mode 100644 index 0000000000..13a3905b99 --- /dev/null +++ b/samples/SpeechRecognition/test/DecoderTest.cpp @@ -0,0 +1,86 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include +#include "Decoder.hpp" + +std::map labels = { + {0, "a" }, + {1, "b" }, + {2, "c" }, + {3, "d" }, + {4, "e" }, + {5, "f" }, + {6, "g" }, + {7, "h" }, + {8, "i" }, + {9, "j" }, + {10,"k" }, + {11,"l" }, + {12,"m" }, + {13,"n" }, + {14,"o" }, + {15,"p" }, + {16,"q" }, + {17,"r" }, + {18,"s" }, + {19,"t" }, + {20,"u" }, + {21,"v" }, + {22,"w" }, + {23,"x" }, + {24,"y" }, + {25,"z" }, + {26, "\'" }, + {27, " "}, + {28,"$" } +}; + +TEST_CASE("Test Wav2Letter output decoder") +{ + + std::vector outputValues = + { + 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 + }; + + std::vector convertedValues; + + for(uint16_t outputVal : outputValues) + { + convertedValues.emplace_back(static_cast(outputVal)); + } + + asr::Decoder decoder(labels); + std::string text = decoder.DecodeOutput(convertedValues); + CHECK(text == "hello"); +} + + diff --git a/samples/SpeechRecognition/test/MFCCTest.cpp b/samples/SpeechRecognition/test/MFCCTest.cpp new file mode 100644 index 0000000000..2a552643d5 --- /dev/null +++ b/samples/SpeechRecognition/test/MFCCTest.cpp @@ -0,0 +1,102 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include + +#include "MFCC.hpp" + +const std::vector testWav = std::vector{ + -3.0f, 0.0f, 1.0f, -1.0f, 2.0f, 3.0f, -2.0f, 2.0f, + 1.0f, -2.0f, 0.0f, 3.0f, -1.0f, 8.0f, 3.0f, 2.0f, + -1.0f, -1.0f, 2.0f, 7.0f, 3.0f, 5.0f, 6.0f, 6.0f, + 6.0f, 12.0f, 5.0f, 6.0f, 3.0f, 3.0f, 5.0f, 4.0f, + 4.0f, 6.0f, 7.0f, 7.0f, 7.0f, 3.0f, 7.0f, 2.0f, + 8.0f, 4.0f, 4.0f, 2.0f, -4.0f, -1.0f, -1.0f, -4.0f, + 2.0f, 1.0f, -1.0f, -4.0f, 0.0f, -7.0f, -6.0f, -2.0f, + -5.0f, 1.0f, -5.0f, -1.0f, -7.0f, -3.0f, -3.0f, -7.0f, + 0.0f, -3.0f, 3.0f, -5.0f, 0.0f,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.0f, 3.0f, 0.0f, -2.0f, -1.0f, + -4.0f, -4.0f, -4.0f, -1.0f, -4.0f, -4.0f, -3.0f, -6.0f, + -3.0f, -7.0f, -3.0f, -1.0f, -2.0f, 0.0f, -5.0f, -4.0f, + -7.0f, -3.0f, -2.0f, -2.0f, 1.0f, 2.0f, 2.0f, 8.0f, + 5.0f, 4.0f, 2.0f, 4.0f, 3.0f, 5.0f, 0.0f, 3.0f, + 3.0f, 6.0f, 4.0f, 2.0f, 2.0f, -2.0f, 4.0f, -2.0f, + 3.0f, 3.0f, 2.0f, 1.0f, 1.0f, 4.0f, -5.0f, 2.0f, + -3.0f, 0.0f, -1.0f, 1.0f, -2.0f, 2.0f, 5.0f, 1.0f, + 4.0f, 2.0f, 3.0f, 1.0f, -1.0f, 1.0f, 0.0f, 6.0f, + 0.0f, -2.0f, -1.0f, 1.0f, -1.0f, 2.0f, -5.0f, -1.0f, + -5.0f, -1.0f, -6.0f, -3.0f, -3.0f, 2.0f, 4.0f, 0.0f, + -1.0f, -5.0f, 3.0f, -4.0f, -1.0f, -3.0f, -4.0f, 1.0f, + -4.0f, 1.0f, -1.0f, -1.0f, 0.0f, -5.0f, -4.0f, -2.0f, + -1.0f, -1.0f, -3.0f, -7.0f, -3.0f, -3.0f, 4.0f, 4.0f +}; + +TEST_CASE("Test MFCC") +{ + int sampFreq = 16000; + int frameLenMs = 32; + int frameLenSamples = sampFreq * frameLenMs * 0.001; + int numMfccFeats = 13; + + std::vector fullAudioData; + + for (auto f : testWav) + { + fullAudioData.emplace_back( f / (1<<15)); + } + + + MfccParams mfccParams(sampFreq, 128, 0, 8000, numMfccFeats, frameLenSamples, false, 1); + + MFCC mfccInst = MFCC(mfccParams); + auto mfccOutput = mfccInst.MfccCompute(fullAudioData); + + std::vector expected = { -834.96564f, 21.02699f, 18.62856f, 7.3412f, 18.90791f, -5.36034f, 6.52351f, + -11.27064f, 8.37522f, 12.0672f, 8.30833f, -13.50008f, -18.1761f}; + + REQUIRE_THAT(mfccOutput, Catch::Approx(expected).epsilon(1.e-5) ); +} \ No newline at end of file diff --git a/samples/SpeechRecognition/test/PreprocessTest.cpp b/samples/SpeechRecognition/test/PreprocessTest.cpp new file mode 100644 index 0000000000..2b98831fda --- /dev/null +++ b/samples/SpeechRecognition/test/PreprocessTest.cpp @@ -0,0 +1,136 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include + +#include "Preprocess.hpp" +#include "DataStructures.hpp" + +void PopulateTestWavVector(std::vector& vec) +{ + constexpr int int16max = std::numeric_limits::max(); + int val = 0; + for (size_t i = 0; i < vec.size(); ++i, ++val) + { + + /* We want a differential filter response from both - order 1 + * and 2 => Don't have a linear signal here - we use a signal + * using squares for example. Alternate sign flips might work + * just as well and will be computationally less work! */ + int valsq = val * val; + if (valsq > int16max) + { + val = 0; + valsq = 0; + } + vec[i] = valsq; + } +} + +TEST_CASE("Preprocessing calculation INT8") +{ + /*Test Constants: */ + const uint32_t windowLen = 512; + const uint32_t windowStride = 160; + const float quantScale = 0.1410219967365265; + const int quantOffset = -11; + int numMfccVectors = 10; + const int sampFreq = 16000; + const int frameLenMs = 32; + const int frameLenSamples = sampFreq * frameLenMs * 0.001; + const int numMfccFeats = 13; + const int audioDataToPreProcess = 512 + ((numMfccVectors -1) * windowStride); + int outputBufferSize = numMfccVectors * numMfccFeats * 3; + + /* Test wav memory */ + std::vector testWav1((windowStride * numMfccVectors) + + (windowLen - windowStride)); + /* Populate with dummy input */ + PopulateTestWavVector(testWav1); + + MfccParams mfccParams(sampFreq, 128, 0, 8000, numMfccFeats, frameLenSamples, false, numMfccVectors); + + MFCC mfccInst = MFCC(mfccParams); + + std::vector fullAudioData; + + for(int i = 0; i < 4; ++i) + { + for (auto f : testWav1) + { + fullAudioData.emplace_back(static_cast(f) / (1<<15)); + } + } + + Preprocess prep(frameLenSamples, windowStride, mfccInst); + + std::vector outputBuffer(outputBufferSize); + + prep.Invoke(fullAudioData.data(), audioDataToPreProcess, outputBuffer, quantOffset, quantScale); + + int8_t expectedResult[numMfccVectors][numMfccFeats*3] = + { + /* Feature vec 0 */ + -32, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, /* MFCCs */ + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, /* Delta 1 */ + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, /* Delta 2 */ + + /* Feature vec 1 */ + -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + + /* Feature vec 2 */ + -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -12, -12, -12, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + + /* Feature vec 3 */ + -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + + /* Feature vec 4 : this should have valid delta 1 and delta 2 */ + -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12, + -38, -29, -9, 1, -2, -7, -8, -8, -12, -16, -14, -5, 5, + -68, -50, -13, 5, 0, -9, -9, -8, -13, -20, -19, -3, 15, + + /* Feature vec 5 : this should have valid delta 1 and delta 2 */ + -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -11, -12, -12, + -62, -45, -11, 5, 0, -8, -9, -8, -12, -19, -17, -3, 13, + -27, -22, -13, -9, -11, -12, -12, -11, -11, -13, -13, -10, -6, + + /* Feature vec 6 */ + -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + + /* Feature vec 7 */ + -32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + + /* Feature vec 8 */ + -32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + + /* Feature vec 9 */ + -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10 + }; + + /* Check that the elements have been calculated correctly */ + for (uint32_t j = 0; j < numMfccVectors; ++j) + { + for (uint32_t i = 0; i < numMfccFeats * 3; ++i) + { + size_t tensorIdx = (j * numMfccFeats * 3) + i; + CHECK(static_cast(outputBuffer.at(tensorIdx) == static_cast(expectedResult[j][i]))); + } + } +} -- cgit v1.2.1