Пример #1
0
 def _prediction_from_string(self, probabilities, sentence):
     pred = Prediction()
     pred.labels[:] = self.codec.encode(sentence)
     pred.is_voted_result = False
     pred.logits = probabilities
     for c, l in zip(sentence, pred.labels):
         pred.positions.append(
             PredictionPosition(chars=[
                 PredictionCharacter(label=l, char=c, probability=1.0)
             ]))
     return pred
Пример #2
0
    def find_alternatives(self, probabilities, sentence, threshold) -> Prediction:
        """
        Find alternatives to the decoded sentence in the logits.
        E.g. if a 'c' is decoded in the range 2 to 4, this algorithm will add all characters in the interval [2, 4] to
        the output if the confidence of the character is higher than the threshold, respectively.


        Parameters
        ----------
        probabilities : array_like
            Prediction of the neural net to decode or shape (length x character probability).
            The blank index must be 0.
        sentence : list of tuple (character index, start pos, end pos)
            The decoded sentence (depends on the CTCDecoder).
            The position refer to the character position in the logits.
        threshold : float
            Minimum confidence for alternative characters to be listed.
        Returns
        -------
            a Prediction object

        """
        # find alternatives
        pred = Prediction()
        pred.labels[:] = [c for c, _, _ in sentence]
        pred.is_voted_result = False
        pred.logits = probabilities
        pred.avg_char_probability = 0
        for c, start, end in sentence:
            p = probabilities[start:end]
            p = np.max(p, axis=0)

            pos = PredictionPosition(
                local_start=start,
                local_end=end
            )
            pred.positions.append(pos)

            for label in reversed(sorted(range(len(p)), key=lambda v: p[v])):
                if p[label] < threshold and len(pos.chars) > 0:
                    break
                else:
                    pos.chars.append(PredictionCharacter(
                        label=label,
                        probability=p[label],
                    ))

            if len(pos.chars) > 0:
                pred.avg_char_probability += pos.chars[0].probability

        pred.avg_char_probability /= len(pred.positions) if len(pred.positions) > 0 else 1
        return pred
Пример #3
0
    def decode(self, probabilities):
        """
        Decoding algorithm of the individual CTCDecoder. This abstract function is reimplemented
        by the DefaultCTCDecoder and the FuzzyCTCDecoder.

        Parameters
        ----------
        probabilities : array_like
            Prediction probabilities of the neural net to decode or shape (length x character probability).
            The blank index must be 0.

        Returns
        -------
            a Prediction object
        """
        return Prediction()