1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 // Collection of scoring classes that can be extended and provided to the 17 // CTCBeamSearchDecoder to incorporate additional scoring logic (such as a 18 // language model). 19 // 20 // To build a custom scorer extend and implement the pure virtual methods from 21 // BeamScorerInterface. The default CTC decoding behavior is implemented 22 // through BaseBeamScorer. 23 24 // Copied from tensorflow/core/util/ctc/ctc_beam_scorer.h 25 // TODO(b/111524997): Remove this file. 26 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_KERNELS_CTC_BEAM_SCORER_H_ 27 #define TENSORFLOW_LITE_EXPERIMENTAL_KERNELS_CTC_BEAM_SCORER_H_ 28 29 #include "tensorflow/lite/experimental/kernels/ctc_beam_entry.h" 30 31 namespace tflite { 32 namespace experimental { 33 namespace ctc { 34 35 // Base implementation of a beam scorer used by default by the decoder that can 36 // be subclassed and provided as an argument to CTCBeamSearchDecoder, if complex 37 // scoring is required. Its main purpose is to provide a thin layer for 38 // integrating language model scoring easily. 39 template <typename CTCBeamState> 40 class BaseBeamScorer { 41 public: ~BaseBeamScorer()42 virtual ~BaseBeamScorer() {} 43 // State initialization. InitializeState(CTCBeamState * root)44 virtual void InitializeState(CTCBeamState* root) const {} 45 // ExpandState is called when expanding a beam to one of its children. 46 // Called at most once per child beam. In the simplest case, no state 47 // expansion is done. ExpandState(const CTCBeamState & from_state,int from_label,CTCBeamState * to_state,int to_label)48 virtual void ExpandState(const CTCBeamState& from_state, int from_label, 49 CTCBeamState* to_state, int to_label) const {} 50 // ExpandStateEnd is called after decoding has finished. Its purpose is to 51 // allow a final scoring of the beam in its current state, before resorting 52 // and retrieving the TopN requested candidates. Called at most once per beam. ExpandStateEnd(CTCBeamState * state)53 virtual void ExpandStateEnd(CTCBeamState* state) const {} 54 // GetStateExpansionScore should be an inexpensive method to retrieve the 55 // (cached) expansion score computed within ExpandState. The score is 56 // multiplied (log-addition) with the input score at the current step from 57 // the network. 58 // 59 // The score returned should be a log-probability. In the simplest case, as 60 // there's no state expansion logic, the expansion score is zero. GetStateExpansionScore(const CTCBeamState & state,float previous_score)61 virtual float GetStateExpansionScore(const CTCBeamState& state, 62 float previous_score) const { 63 return previous_score; 64 } 65 // GetStateEndExpansionScore should be an inexpensive method to retrieve the 66 // (cached) expansion score computed within ExpandStateEnd. The score is 67 // multiplied (log-addition) with the final probability of the beam. 68 // 69 // The score returned should be a log-probability. GetStateEndExpansionScore(const CTCBeamState & state)70 virtual float GetStateEndExpansionScore(const CTCBeamState& state) const { 71 return 0; 72 } 73 }; 74 75 } // namespace ctc 76 } // namespace experimental 77 } // namespace tflite 78 79 #endif // TENSORFLOW_LITE_EXPERIMENTAL_KERNELS_CTC_BEAM_SCORER_H_ 80