alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 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 <algorithm> |
| 20 | #include <catch.hpp> |
| 21 | #include <limits> |
| 22 | |
| 23 | constexpr uint32_t numMfccFeatures = 13; |
| 24 | constexpr uint32_t numMfccVectors = 10; |
| 25 | |
| 26 | /* Test vector output: generated using test-asr-preprocessing.py. */ |
| 27 | int8_t expectedResult[numMfccVectors][numMfccFeatures*3] = { |
| 28 | /* Feature vec 0. */ |
| 29 | -32, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, /* MFCCs. */ |
| 30 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, /* Delta 1. */ |
| 31 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, /* Delta 2. */ |
| 32 | |
| 33 | /* Feature vec 1. */ |
| 34 | -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, |
| 35 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 36 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, |
| 37 | |
| 38 | /* Feature vec 2. */ |
| 39 | -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -12, -12, -12, |
| 40 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 41 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, |
| 42 | |
| 43 | /* Feature vec 3. */ |
| 44 | -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12, |
| 45 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 46 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, |
| 47 | |
| 48 | /* Feature vec 4 : this should have valid delta 1 and delta 2. */ |
| 49 | -31, 4, -9, -9, -10, -10, -11, -11, -11, -11, -11, -12, -12, |
| 50 | -38, -29, -9, 1, -2, -7, -8, -8, -12, -16, -14, -5, 5, |
| 51 | -68, -50, -13, 5, 0, -9, -9, -8, -13, -20, -19, -3, 15, |
| 52 | |
| 53 | /* Feature vec 5 : this should have valid delta 1 and delta 2. */ |
| 54 | -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -11, -12, -12, |
| 55 | -62, -45, -11, 5, 0, -8, -9, -8, -12, -19, -17, -3, 13, |
| 56 | -27, -22, -13, -9, -11, -12, -12, -11, -11, -13, -13, -10, -6, |
| 57 | |
| 58 | /* Feature vec 6. */ |
| 59 | -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, |
| 60 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 61 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, |
| 62 | |
| 63 | /* Feature vec 7. */ |
| 64 | -32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -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, |
| 67 | |
| 68 | /* Feature vec 8. */ |
| 69 | -32, 4, -9, -8, -10, -10, -11, -11, -11, -12, -12, -11, -11, |
| 70 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 71 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, |
| 72 | |
| 73 | /* Feature vec 9. */ |
| 74 | -31, 4, -9, -8, -10, -10, -11, -11, -11, -11, -12, -11, -11, |
| 75 | -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, |
| 76 | -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10 |
| 77 | }; |
| 78 | |
| 79 | void PopulateTestWavVector(std::vector<int16_t>& vec) |
| 80 | { |
| 81 | constexpr int int16max = std::numeric_limits<int16_t>::max(); |
| 82 | int val = 0; |
| 83 | for (size_t i = 0; i < vec.size(); ++i, ++val) { |
| 84 | |
| 85 | /* We want a differential filter response from both - order 1 |
| 86 | * and 2 => Don't have a linear signal here - we use a signal |
| 87 | * using squares for example. Alternate sign flips might work |
| 88 | * just as well and will be computationally less work! */ |
| 89 | int valsq = val * val; |
| 90 | if (valsq > int16max) { |
| 91 | val = 0; |
| 92 | valsq = 0; |
| 93 | } |
| 94 | vec[i] = valsq; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | TEST_CASE("Preprocessing calculation INT8") |
| 99 | { |
| 100 | /* Initialise the HAL and platform. */ |
| 101 | hal_platform platform; |
| 102 | data_acq_module data_acq; |
| 103 | data_psn_module data_psn; |
| 104 | platform_timer timer; |
| 105 | hal_init(&platform, &data_acq, &data_psn, &timer); |
| 106 | hal_platform_init(&platform); |
| 107 | |
| 108 | /* Constants. */ |
| 109 | const uint32_t windowLen = 512; |
| 110 | const uint32_t windowStride = 160; |
| 111 | const int dimArray[] = {3, 1, numMfccFeatures * 3, numMfccVectors}; |
| 112 | const float quantScale = 0.1410219967365265; |
| 113 | const int quantOffset = -11; |
| 114 | |
| 115 | /* Test wav memory. */ |
| 116 | std::vector <int16_t> testWav((windowStride * numMfccVectors) + |
| 117 | (windowLen - windowStride)); |
| 118 | |
| 119 | /* Populate with dummy input. */ |
| 120 | PopulateTestWavVector(testWav); |
| 121 | |
| 122 | /* Allocate mem for tensor. */ |
| 123 | std::vector<int8_t> tensorVec(dimArray[1]*dimArray[2]*dimArray[3]); |
| 124 | |
| 125 | /* Initialise dimensions and the test tensor. */ |
| 126 | TfLiteIntArray* dims= tflite::testing::IntArrayFromInts(dimArray); |
| 127 | TfLiteTensor tensor = tflite::testing::CreateQuantizedTensor( |
| 128 | tensorVec.data(), dims, quantScale, quantOffset, "preprocessedInput"); |
| 129 | |
| 130 | /* Initialise pre-processing module. */ |
| 131 | arm::app::audio::asr::Preprocess prep{ |
| 132 | numMfccFeatures, windowLen, windowStride, numMfccVectors}; |
| 133 | |
| 134 | /* Invoke pre-processing. */ |
| 135 | REQUIRE(prep.Invoke(testWav.data(), testWav.size(), &tensor)); |
| 136 | |
| 137 | /* Wrap the tensor with a std::vector for ease. */ |
| 138 | int8_t * tensorData = tflite::GetTensorData<int8_t>(&tensor); |
| 139 | std::vector <int8_t> vecResults = |
| 140 | std::vector<int8_t>(tensorData, tensorData + tensor.bytes); |
| 141 | |
| 142 | /* Check sizes. */ |
| 143 | REQUIRE(vecResults.size() == sizeof(expectedResult)); |
| 144 | |
| 145 | /* Check that the elements have been calculated correctly. */ |
| 146 | for (uint32_t j = 0; j < numMfccVectors; ++j) { |
| 147 | for (uint32_t i = 0; i < numMfccFeatures * 3; ++i) { |
| 148 | size_t tensorIdx = (j * numMfccFeatures * 3) + i; |
| 149 | CHECK(vecResults[tensorIdx] == expectedResult[j][i]); |
| 150 | } |
| 151 | } |
| 152 | } |