blob: 547a81fd74d44be8a829f34460e52c1e6c50488f [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00003 * 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 Burtonc2911442022-04-22 09:08:21 +010020#include "TensorFlowLiteMicro.hpp" /* TensorFlow headers. */
21#include "BaseProcessing.hpp"
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010022#include "Model.hpp"
Richard Burtonc2911442022-04-22 09:08:21 +010023#include "AsrClassifier.hpp"
24#include "AsrResult.hpp"
alexander31ae9f02022-02-10 16:15:54 +000025#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +000026
27namespace arm {
28namespace app {
alexander3c798932021-03-26 21:42:19 +000029
30 /**
31 * @brief Helper class to manage tensor post-processing for "wav2letter"
32 * output.
33 */
Richard Burtonb40ecf82022-04-22 16:14:57 +010034 class AsrPostProcess : public BasePostProcess {
alexander3c798932021-03-26 21:42:19 +000035 public:
Richard Burtonc2911442022-04-22 09:08:21 +010036 bool m_lastIteration = false; /* Flag to set if processing the last set of data for a clip. */
37
alexander3c798932021-03-26 21:42:19 +000038 /**
Richard Burtonc2911442022-04-22 09:08:21 +010039 * @brief Constructor
Richard Burtonb40ecf82022-04-22 16:14:57 +010040 * @param[in] outputTensor Pointer to the TFLite Micro output Tensor.
41 * @param[in] classifier Object used to get top N results from classification.
Richard Burtonc2911442022-04-22 09:08:21 +010042 * @param[in] labels Vector of string labels to identify each output of the model.
Richard Burtonb40ecf82022-04-22 16:14:57 +010043 * @param[in/out] result Vector of classification results to store decoded outputs.
Richard Burtonc2911442022-04-22 09:08:21 +010044 * @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.
alexander3c798932021-03-26 21:42:19 +000047 **/
Richard Burtonb40ecf82022-04-22 16:14:57 +010048 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);
alexander3c798932021-03-26 21:42:19 +000052
53 /**
Richard Burtonc2911442022-04-22 09:08:21 +010054 * @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);
alexander3c798932021-03-26 21:42:19 +000068
69 private:
Richard Burtonc2911442022-04-22 09:08:21 +010070 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. */
alexander3c798932021-03-26 21:42:19 +000080
81 /**
Richard Burtonc2911442022-04-22 09:08:21 +010082 * @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.
alexander3c798932021-03-26 21:42:19 +000093 */
alexanderc350cdc2021-04-29 20:36:09 +010094 static uint32_t GetTensorElementSize(TfLiteTensor* tensor);
alexander3c798932021-03-26 21:42:19 +000095
96 /**
Richard Burtonc2911442022-04-22 09:08:21 +010097 * @brief Erases sections from the data assuming row-wise
98 * arrangement along the context axis.
99 * @return true if successful, false otherwise.
alexander3c798932021-03-26 21:42:19 +0000100 */
alexanderc350cdc2021-04-29 20:36:09 +0100101 bool EraseSectionsRowWise(uint8_t* ptrData,
Richard Burtonc2911442022-04-22 09:08:21 +0100102 uint32_t strideSzBytes,
103 bool lastIteration);
alexander3c798932021-03-26 21:42:19 +0000104 };
105
alexander3c798932021-03-26 21:42:19 +0000106} /* namespace app */
107} /* namespace arm */
108
109#endif /* ASR_WAV2LETTER_POSTPROCESS_HPP */