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