コード例 #1
0
 def _prediction_from_string(self, probabilities, sentence):
     pred = Prediction()
     pred.labels[:] = self.codec.encode(sentence)
     pred.is_voted_result = False
     pred.logits.rows, pred.logits.cols = probabilities.shape
     pred.logits.data[:] = probabilities.reshape([-1])
     return pred
コード例 #2
0
 def _prediction_from_string(self, probabilities, sentence):
     pred = Prediction()
     pred.labels[:] = self.codec.encode(sentence)
     pred.is_voted_result = False
     pred.logits.rows, pred.logits.cols = probabilities.shape
     pred.logits.data[:] = probabilities.reshape([-1])
     for c, l in zip(sentence, pred.labels):
         pos = pred.positions.add()
         char = pos.chars.add()
         char.label = l
         char.char = c
         char.probability = 1.0
     return pred
コード例 #3
0
    def find_alternatives(self, probabilities, sentence, threshold):
        """
        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.rows, pred.logits.cols = probabilities.shape
        pred.logits.data[:] = probabilities.reshape([-1])
        for c, start, end in sentence:
            p = probabilities[start:end]
            p = np.max(p, axis=0)

            pos = pred.positions.add()
            pos.local_start = start
            pos.local_end = end

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

        return pred
コード例 #4
0
    def vote_prediction_result_tuple(self, predictions):
        p = Prediction()
        p.is_voted_result = True
        self._apply_vote(predictions, p)

        # postprocessing after voting
        # option 1: Use custom text postprocessor
        # option 2: (Not implemented) Use only the first text postprocessor
        # option 3: Apply all known postprocessors and apply a sequence voting if different results are received
        if self.text_postproc:
            p.sentence = self.text_postproc.apply(p.sentence)
        else:
            sentences = [
                pred.text_postproc.apply(p.sentence) for pred in predictions
            ]

            if all([s == sentences[0] for s in sentences[1:]]):
                # usually all postproc should yield the same results
                p.sentence = sentences[0]
            else:
                # we need to vote again
                from calamari_ocr.ocr.voting import SequenceVoter
                sv = SequenceVoter()
                p.sentence = "".join(
                    [c for c, _ in sv.process_text(sentences)])

        p.avg_char_probability = 0
        for pos in p.positions:
            if len(pos.chars) > 0:
                p.avg_char_probability += pos.chars[0].probability
        p.avg_char_probability /= len(
            p.positions) if len(p.positions) > 0 else 1

        return p
コード例 #5
0
ファイル: ctc_decoder.py プロジェクト: AIRob/calamari
    def find_alternatives(self, probabilities, sentence, threshold):
        """
        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.rows, pred.logits.cols = probabilities.shape
        pred.logits.data[:] = probabilities.reshape([-1])
        for c, start, end in sentence:
            p = probabilities[start:end]
            p = np.max(p, axis=0)

            pos = pred.positions.add()
            pos.local_start = start
            pos.local_end = end

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

        return pred
コード例 #6
0
ファイル: ctc_decoder.py プロジェクト: templeblock/calamari
    def find_alternatives(self, logits, sentence, threshold):
        # find alternatives
        pred = Prediction()
        pred.labels[:] = [c for c, _, _ in sentence]
        pred.is_voted_result = False
        pred.logits.rows, pred.logits.cols = logits.shape
        pred.logits.data[:] = logits.reshape([-1])
        for c, start, end in sentence:
            p = logits[start:end]
            p = np.max(p, axis=0)

            pos = pred.positions.add()
            pos.start = start
            pos.end = end

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

        return pred
コード例 #7
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()
コード例 #8
0
ファイル: voter.py プロジェクト: CurtLH/calamari
 def vote_prediction_result_tuple(self, predictions):
     p = Prediction()
     p.is_voted_result = True
     self._apply_vote(predictions, p)
     return p