alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +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 | */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 17 | #ifndef KWS_ASR_WAV2LETTER_POSTPROCESS_HPP |
| 18 | #define KWS_ASR_WAV2LETTER_POSTPROCESS_HPP |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 19 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 20 | #include "TensorFlowLiteMicro.hpp" /* TensorFlow headers. */ |
| 21 | #include "BaseProcessing.hpp" |
| 22 | #include "AsrClassifier.hpp" |
| 23 | #include "AsrResult.hpp" |
| 24 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 25 | |
| 26 | namespace arm { |
| 27 | namespace app { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * @brief Helper class to manage tensor post-processing for "wav2letter" |
| 31 | * output. |
| 32 | */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 33 | class AsrPostProcess : public BasePostProcess { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 34 | public: |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 35 | bool m_lastIteration = false; /* Flag to set if processing the last set of data for a clip. */ |
| 36 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 37 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 38 | * @brief Constructor |
| 39 | * @param[in] outputTensor Pointer to the TFLite Micro output Tensor. |
| 40 | * @param[in] classifier Object used to get top N results from classification. |
| 41 | * @param[in] labels Vector of string labels to identify each output of the model. |
| 42 | * @param[in/out] result Vector of classification results to store decoded outputs. |
| 43 | * @param[in] outputContextLen Left/right context length for output tensor. |
| 44 | * @param[in] blankTokenIdx Index in the labels that the "Blank token" takes. |
| 45 | * @param[in] reductionAxis The axis that the logits of each time step is on. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 46 | **/ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 47 | AsrPostProcess(TfLiteTensor* outputTensor, AsrClassifier& classifier, |
| 48 | const std::vector<std::string>& labels, asr::ResultVec& result, |
| 49 | uint32_t outputContextLen, |
| 50 | uint32_t blankTokenIdx, uint32_t reductionAxis); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | |
| 52 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 53 | * @brief Should perform post-processing of the result of inference then |
| 54 | * populate ASR result data for any later use. |
| 55 | * @return true if successful, false otherwise. |
| 56 | **/ |
| 57 | bool DoPostProcess() override; |
| 58 | |
| 59 | /** @brief Gets the output inner length for post-processing. */ |
| 60 | static uint32_t GetOutputInnerLen(const TfLiteTensor*, uint32_t outputCtxLen); |
| 61 | |
| 62 | /** @brief Gets the output context length (left/right) for post-processing. */ |
| 63 | static uint32_t GetOutputContextLen(const Model& model, uint32_t inputCtxLen); |
| 64 | |
| 65 | /** @brief Gets the number of feature vectors to be computed. */ |
| 66 | static uint32_t GetNumFeatureVectors(const Model& model); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | |
| 68 | private: |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 69 | AsrClassifier& m_classifier; /* ASR Classifier object. */ |
| 70 | TfLiteTensor* m_outputTensor; /* Model output tensor. */ |
| 71 | const std::vector<std::string>& m_labels; /* ASR Labels. */ |
| 72 | asr::ResultVec & m_results; /* Results vector for a single inference. */ |
| 73 | uint32_t m_outputContextLen; /* lengths of left/right contexts for output. */ |
| 74 | uint32_t m_outputInnerLen; /* Length of output inner context. */ |
| 75 | uint32_t m_totalLen; /* Total length of the required axis. */ |
| 76 | uint32_t m_countIterations; /* Current number of iterations. */ |
| 77 | uint32_t m_blankTokenIdx; /* Index of the labels blank token. */ |
| 78 | uint32_t m_reductionAxisIdx; /* Axis containing output logits for a single step. */ |
| 79 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 81 | * @brief Checks if the tensor and axis index are valid |
| 82 | * inputs to the object - based on how it has been initialised. |
| 83 | * @return true if valid, false otherwise. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 84 | */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 85 | bool IsInputValid(TfLiteTensor* tensor, |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 86 | uint32_t axisIdx) const; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 89 | * @brief Gets the tensor data element size in bytes based |
| 90 | * on the tensor type. |
| 91 | * @return Size in bytes, 0 if not supported. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 93 | static uint32_t GetTensorElementSize(TfLiteTensor* tensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 96 | * @brief Erases sections from the data assuming row-wise |
| 97 | * arrangement along the context axis. |
| 98 | * @return true if successful, false otherwise. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 99 | */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 100 | bool EraseSectionsRowWise(uint8_t* ptrData, |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 101 | uint32_t strideSzBytes, |
| 102 | bool lastIteration); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 105 | } /* namespace app */ |
| 106 | } /* namespace arm */ |
| 107 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 108 | #endif /* KWS_ASR_WAV2LETTER_POSTPROCESS_HPP */ |