blob: 45defa5c473b7d25fbc0e6e0bed03d26d3915d05 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonc2911442022-04-22 09:08:21 +01002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
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"
22#include "AsrClassifier.hpp"
23#include "AsrResult.hpp"
alexander31ae9f02022-02-10 16:15:54 +000024#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +000025
26namespace arm {
27namespace app {
alexander3c798932021-03-26 21:42:19 +000028
29 /**
30 * @brief Helper class to manage tensor post-processing for "wav2letter"
31 * output.
32 */
Richard Burtonc2911442022-04-22 09:08:21 +010033 class ASRPostProcess : public BasePostProcess {
alexander3c798932021-03-26 21:42:19 +000034 public:
Richard Burtonc2911442022-04-22 09:08:21 +010035 bool m_lastIteration = false; /* Flag to set if processing the last set of data for a clip. */
36
alexander3c798932021-03-26 21:42:19 +000037 /**
Richard Burtonc2911442022-04-22 09:08:21 +010038 * @brief Constructor
39 * @param[in] outputTensor Pointer to the output Tensor.
40 * @param[in] labels Vector of string labels to identify each output of the model.
41 * @param[in/out] result Vector of classification results to store decoded outputs.
42 * @param[in] outputContextLen Left/right context length for output tensor.
43 * @param[in] blankTokenIdx Index in the labels that the "Blank token" takes.
44 * @param[in] reductionAxis The axis that the logits of each time step is on.
alexander3c798932021-03-26 21:42:19 +000045 **/
Richard Burtonc2911442022-04-22 09:08:21 +010046 ASRPostProcess(AsrClassifier& classifier, TfLiteTensor* outputTensor,
47 const std::vector<std::string>& labels, asr::ResultVec& result,
48 uint32_t outputContextLen,
49 uint32_t blankTokenIdx, uint32_t reductionAxis);
alexander3c798932021-03-26 21:42:19 +000050
51 /**
Richard Burtonc2911442022-04-22 09:08:21 +010052 * @brief Should perform post-processing of the result of inference then
53 * populate ASR result data for any later use.
54 * @return true if successful, false otherwise.
55 **/
56 bool DoPostProcess() override;
57
58 /** @brief Gets the output inner length for post-processing. */
59 static uint32_t GetOutputInnerLen(const TfLiteTensor*, uint32_t outputCtxLen);
60
61 /** @brief Gets the output context length (left/right) for post-processing. */
62 static uint32_t GetOutputContextLen(const Model& model, uint32_t inputCtxLen);
63
64 /** @brief Gets the number of feature vectors to be computed. */
65 static uint32_t GetNumFeatureVectors(const Model& model);
alexander3c798932021-03-26 21:42:19 +000066
67 private:
Richard Burtonc2911442022-04-22 09:08:21 +010068 AsrClassifier& m_classifier; /* ASR Classifier object. */
69 TfLiteTensor* m_outputTensor; /* Model output tensor. */
70 const std::vector<std::string>& m_labels; /* ASR Labels. */
71 asr::ResultVec & m_results; /* Results vector for a single inference. */
72 uint32_t m_outputContextLen; /* lengths of left/right contexts for output. */
73 uint32_t m_outputInnerLen; /* Length of output inner context. */
74 uint32_t m_totalLen; /* Total length of the required axis. */
75 uint32_t m_countIterations; /* Current number of iterations. */
76 uint32_t m_blankTokenIdx; /* Index of the labels blank token. */
77 uint32_t m_reductionAxisIdx; /* Axis containing output logits for a single step. */
alexander3c798932021-03-26 21:42:19 +000078
79 /**
Richard Burtonc2911442022-04-22 09:08:21 +010080 * @brief Checks if the tensor and axis index are valid
81 * inputs to the object - based on how it has been initialised.
82 * @return true if valid, false otherwise.
83 */
84 bool IsInputValid(TfLiteTensor* tensor,
85 uint32_t axisIdx) const;
86
87 /**
88 * @brief Gets the tensor data element size in bytes based
89 * on the tensor type.
90 * @return Size in bytes, 0 if not supported.
alexander3c798932021-03-26 21:42:19 +000091 */
alexanderc350cdc2021-04-29 20:36:09 +010092 static uint32_t GetTensorElementSize(TfLiteTensor* tensor);
alexander3c798932021-03-26 21:42:19 +000093
94 /**
Richard Burtonc2911442022-04-22 09:08:21 +010095 * @brief Erases sections from the data assuming row-wise
96 * arrangement along the context axis.
97 * @return true if successful, false otherwise.
alexander3c798932021-03-26 21:42:19 +000098 */
alexanderc350cdc2021-04-29 20:36:09 +010099 bool EraseSectionsRowWise(uint8_t* ptrData,
Richard Burtonc2911442022-04-22 09:08:21 +0100100 uint32_t strideSzBytes,
101 bool lastIteration);
alexander3c798932021-03-26 21:42:19 +0000102 };
103
alexander3c798932021-03-26 21:42:19 +0000104} /* namespace app */
105} /* namespace arm */
106
107#endif /* ASR_WAV2LETTER_POSTPROCESS_HPP */