alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #include "Wav2LetterPreprocess.hpp" |
| 18 | |
| 19 | #include <limits> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | #include <catch.hpp> |
| 21 | |
| 22 | constexpr uint32_t numMfccFeatures = 13; |
| 23 | constexpr uint32_t numMfccVectors = 10; |
| 24 | |
| 25 | /* Test vector output: generated using test-asr-preprocessing.py. */ |
| 26 | int8_t expectedResult[numMfccVectors][numMfccFeatures * 3] = { |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 27 | /* Feature vec 0. */ |
| 28 | {-32, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, /* MFCCs. */ |
| 29 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, /* Delta 1. */ |
| 30 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10}, /* Delta 2. */ |
| 31 | /* Feature vec 1. */ |
| 32 | {-31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, |
| 33 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 34 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10}, |
| 35 | /* Feature vec 2. */ |
| 36 | {-31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -12, -12, -12, |
| 37 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 38 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10}, |
| 39 | /* Feature vec 3. */ |
| 40 | {-31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12, |
| 41 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 42 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10}, |
| 43 | /* Feature vec 4 : this should have valid delta 1 and delta 2. */ |
| 44 | {-31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12, |
| 45 | -38, -29, -9, 1, -2, -7, -8, -8, -12, -16, -14, -5, 5, |
| 46 | -68, -50, -13, 5, 0, -9, -9, -8, -13, -20, -19, -3, 15}, |
| 47 | /* Feature vec 5 : this should have valid delta 1 and delta 2. */ |
| 48 | {-31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -11, -12, -12, |
| 49 | -62, -45, -11, 5, 0, -8, -9, -8, -12, -19, -17, -3, 13, |
| 50 | -27, -22, -13, -9, -11, -12, -12, -11, -11, -13, -13, -10, -6}, |
| 51 | /* Feature vec 6. */ |
| 52 | {-31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, |
| 53 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 54 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10}, |
| 55 | /* Feature vec 7. */ |
| 56 | {-32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11, |
| 57 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 58 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10}, |
| 59 | /* Feature vec 8. */ |
| 60 | {-32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11, |
| 61 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 62 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10}, |
| 63 | /* Feature vec 9. */ |
| 64 | {-31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, |
| 65 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 66 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10} |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | void PopulateTestWavVector(std::vector<int16_t>& vec) |
| 70 | { |
| 71 | constexpr int int16max = std::numeric_limits<int16_t>::max(); |
| 72 | int val = 0; |
| 73 | for (size_t i = 0; i < vec.size(); ++i, ++val) { |
| 74 | |
| 75 | /* We want a differential filter response from both - order 1 |
| 76 | * and 2 => Don't have a linear signal here - we use a signal |
| 77 | * using squares for example. Alternate sign flips might work |
| 78 | * just as well and will be computationally less work! */ |
| 79 | int valsq = val * val; |
| 80 | if (valsq > int16max) { |
| 81 | val = 0; |
| 82 | valsq = 0; |
| 83 | } |
| 84 | vec[i] = valsq; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | TEST_CASE("Preprocessing calculation INT8") |
| 89 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 90 | /* Constants. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 91 | const uint32_t mfccWindowLen = 512; |
| 92 | const uint32_t mfccWindowStride = 160; |
| 93 | int dimArray[] = {3, 1, numMfccFeatures * 3, numMfccVectors}; |
| 94 | const float quantScale = 0.1410219967365265; |
| 95 | const int quantOffset = -11; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | |
| 97 | /* Test wav memory. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 98 | std::vector<int16_t> testWav((mfccWindowStride * numMfccVectors) + |
| 99 | (mfccWindowLen - mfccWindowStride) |
| 100 | ); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | |
| 102 | /* Populate with dummy input. */ |
| 103 | PopulateTestWavVector(testWav); |
| 104 | |
| 105 | /* Allocate mem for tensor. */ |
| 106 | std::vector<int8_t> tensorVec(dimArray[1]*dimArray[2]*dimArray[3]); |
| 107 | |
| 108 | /* Initialise dimensions and the test tensor. */ |
| 109 | TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 110 | TfLiteTensor inputTensor = tflite::testing::CreateQuantizedTensor( |
| 111 | tensorVec.data(), dims, quantScale, quantOffset, "preprocessedInput"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 112 | |
| 113 | /* Initialise pre-processing module. */ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 114 | arm::app::AsrPreProcess prep{&inputTensor, |
| 115 | numMfccFeatures, numMfccVectors, mfccWindowLen, mfccWindowStride}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 116 | |
| 117 | /* Invoke pre-processing. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 118 | REQUIRE(prep.DoPreProcess(testWav.data(), testWav.size())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 119 | |
| 120 | /* Wrap the tensor with a std::vector for ease. */ |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 121 | auto* tensorData = tflite::GetTensorData<int8_t>(&inputTensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 122 | std::vector <int8_t> vecResults = |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 123 | std::vector<int8_t>(tensorData, tensorData + inputTensor.bytes); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 124 | |
| 125 | /* Check sizes. */ |
| 126 | REQUIRE(vecResults.size() == sizeof(expectedResult)); |
| 127 | |
| 128 | /* Check that the elements have been calculated correctly. */ |
| 129 | for (uint32_t j = 0; j < numMfccVectors; ++j) { |
| 130 | for (uint32_t i = 0; i < numMfccFeatures * 3; ++i) { |
| 131 | size_t tensorIdx = (j * numMfccFeatures * 3) + i; |
| 132 | CHECK(vecResults[tensorIdx] == expectedResult[j][i]); |
| 133 | } |
| 134 | } |
| 135 | } |