test_ctc_align.py 2.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
D
dzhwinter 已提交
9 10 11 12 13
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
14

15 16
from __future__ import print_function

17 18 19
import sys
import unittest
import numpy as np
20 21
from op_test import OpTest
from test_softmax_op import stable_softmax
22 23


W
wanghaoshuang 已提交
24
def CTCAlign(input, lod, blank, merge_repeated):
25 26
    lod0 = lod[0]
    result = []
27 28
    cur_offset = 0
    for i in range(len(lod0)):
29
        prev_token = -1
30
        for j in range(cur_offset, cur_offset + lod0[i]):
31 32 33 34 35
            token = input[j][0]
            if (token != blank) and not (merge_repeated and
                                         token == prev_token):
                result.append(token)
            prev_token = token
36
        cur_offset += lod0[i]
37
    result = np.array(result).reshape([len(result), 1]).astype("int32")
38 39
    if len(result) == 0:
        result = np.array([-1])
40 41 42
    return result


W
wanghaoshuang 已提交
43
class TestCTCAlignOp(OpTest):
44
    def config(self):
W
wanghaoshuang 已提交
45
        self.op_type = "ctc_align"
46
        self.input_lod = [[11, 7]]
47 48 49 50 51 52 53 54
        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 已提交
55 56
        output = CTCAlign(self.input, self.input_lod, self.blank,
                          self.merge_repeated)
57 58 59 60 61 62 63 64 65 66 67 68 69

        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 已提交
70
class TestCTCAlignOpCase1(TestCTCAlignOp):
71
    def config(self):
W
wanghaoshuang 已提交
72
        self.op_type = "ctc_align"
73
        self.input_lod = [[11, 8]]
74 75 76
        self.blank = 0
        self.merge_repeated = True
        self.input = np.array(
W
wanghaoshuang 已提交
77 78
            [0, 1, 2, 2, 0, 4, 0, 4, 5, 0, 6, 6, 0, 0, 7, 7, 7, 0, 0]).reshape(
                [19, 1]).astype("int32")
79 80


81 82 83
class TestCTCAlignOpCase2(TestCTCAlignOp):
    def config(self):
        self.op_type = "ctc_align"
84
        self.input_lod = [[4]]
85 86 87 88 89
        self.blank = 0
        self.merge_repeated = True
        self.input = np.array([0, 0, 0, 0]).reshape([4, 1]).astype("int32")


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