test_ctc_align.py 1.8 KB
Newer Older
1 2 3 4 5 6 7
import sys
import unittest
import numpy as np
from op_test import OpTest
from test_softmax_op import stable_softmax


W
wanghaoshuang 已提交
8
def CTCAlign(input, lod, blank, merge_repeated):
9 10 11 12 13 14 15 16 17 18 19 20 21 22
    lod0 = lod[0]
    result = []
    for i in range(len(lod0) - 1):
        prev_token = -1
        for j in range(lod0[i], lod0[i + 1]):
            token = input[j][0]
            if (token != blank) and not (merge_repeated and
                                         token == prev_token):
                result.append(token)
            prev_token = token
    result = np.array(result).reshape([len(result), 1]).astype("int32")
    return result


W
wanghaoshuang 已提交
23
class TestCTCAlignOp(OpTest):
24
    def config(self):
W
wanghaoshuang 已提交
25
        self.op_type = "ctc_align"
26 27 28 29 30 31 32 33 34
        self.input_lod = [[0, 11, 18]]
        self.blank = 0
        self.merge_repeated = False
        self.input = np.array(
            [0, 1, 2, 2, 0, 4, 0, 4, 5, 0, 6, 6, 0, 0, 7, 7, 7, 0]).reshape(
                [18, 1]).astype("int32")

    def setUp(self):
        self.config()
W
wanghaoshuang 已提交
35 36
        output = CTCAlign(self.input, self.input_lod, self.blank,
                          self.merge_repeated)
37 38 39 40 41 42 43 44 45 46 47 48 49

        self.inputs = {"Input": (self.input, self.input_lod), }
        self.outputs = {"Output": output}
        self.attrs = {
            "blank": self.blank,
            "merge_repeated": self.merge_repeated
        }

    def test_check_output(self):
        self.check_output()
        pass


W
wanghaoshuang 已提交
50
class TestCTCAlignOpCase1(TestCTCAlignOp):
51
    def config(self):
W
wanghaoshuang 已提交
52
        self.op_type = "ctc_align"
W
wanghaoshuang 已提交
53
        self.input_lod = [[0, 11, 19]]
54 55 56
        self.blank = 0
        self.merge_repeated = True
        self.input = np.array(
W
wanghaoshuang 已提交
57 58
            [0, 1, 2, 2, 0, 4, 0, 4, 5, 0, 6, 6, 0, 0, 7, 7, 7, 0, 0]).reshape(
                [19, 1]).astype("int32")
59 60 61 62


if __name__ == "__main__":
    unittest.main()