test_ctc_align.py 6.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
import unittest
16

17
import numpy as np
18
from eager_op_test import OpTest
19

H
hong 已提交
20
import paddle
21 22


23 24
def CTCAlign(input, lod, blank, merge_repeated, padding=0, input_length=None):
    if input_length is None:
25 26 27 28 29 30 31
        lod0 = lod[0]
        result = []
        cur_offset = 0
        for i in range(len(lod0)):
            prev_token = -1
            for j in range(cur_offset, cur_offset + lod0[i]):
                token = input[j][0]
32 33 34
                if (token != blank) and not (
                    merge_repeated and token == prev_token
                ):
35 36 37 38 39 40
                    result.append(token)
                prev_token = token
            cur_offset += lod0[i]
        result = np.array(result).reshape([len(result), 1]).astype("int32")
        if len(result) == 0:
            result = np.array([-1])
41
        return result
42 43
    else:
        result = [[] for i in range(len(input))]
44
        output_length = []
45 46
        for i in range(len(input)):
            prev_token = -1
47
            for j in range(input_length[i][0]):
48
                token = input[i][j]
49 50 51
                if (token != blank) and not (
                    merge_repeated and token == prev_token
                ):
52 53 54
                    result[i].append(token)
                prev_token = token
            start = len(result[i])
55
            output_length.append([start])
56 57
            for j in range(start, len(input[i])):
                result[i].append(padding)
58 59 60 61 62 63 64 65
        result = (
            np.array(result)
            .reshape([len(input), len(input[0])])
            .astype("int32")
        )
        output_length = (
            np.array(output_length).reshape([len(input), 1]).astype("int32")
        )
66

67
    return result, output_length
68 69


W
wanghaoshuang 已提交
70
class TestCTCAlignOp(OpTest):
71
    def config(self):
W
wanghaoshuang 已提交
72
        self.op_type = "ctc_align"
73
        self.input_lod = [[11, 7]]
74 75
        self.blank = 0
        self.merge_repeated = False
76 77 78 79 80
        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")
        )
81 82 83

    def setUp(self):
        self.config()
84 85 86
        output = CTCAlign(
            self.input, self.input_lod, self.blank, self.merge_repeated
        )
87

88 89 90
        self.inputs = {
            "Input": (self.input, self.input_lod),
        }
91 92 93
        self.outputs = {"Output": output}
        self.attrs = {
            "blank": self.blank,
94
            "merge_repeated": self.merge_repeated,
95 96 97
        }

    def test_check_output(self):
98 99
        # NODE(yjjiang11): This op will be deprecated.
        self.check_output(check_dygraph=False)
100 101


W
wanghaoshuang 已提交
102
class TestCTCAlignOpCase1(TestCTCAlignOp):
103
    def config(self):
W
wanghaoshuang 已提交
104
        self.op_type = "ctc_align"
105
        self.input_lod = [[11, 8]]
106 107
        self.blank = 0
        self.merge_repeated = True
108 109 110 111 112
        self.input = (
            np.array([0, 1, 2, 2, 0, 4, 0, 4, 5, 0, 6, 6, 0, 0, 7, 7, 7, 0, 0])
            .reshape([19, 1])
            .astype("int32")
        )
113 114


115 116 117
class TestCTCAlignOpCase2(TestCTCAlignOp):
    def config(self):
        self.op_type = "ctc_align"
118
        self.input_lod = [[4]]
119 120 121 122 123
        self.blank = 0
        self.merge_repeated = True
        self.input = np.array([0, 0, 0, 0]).reshape([4, 1]).astype("int32")


124 125 126 127 128
class TestCTCAlignPaddingOp(OpTest):
    def config(self):
        self.op_type = "ctc_align"
        self.input_lod = []
        self.blank = 0
129
        self.padding_value = 0
130
        self.merge_repeated = True
131 132 133 134 135 136 137 138 139 140
        self.input = (
            np.array(
                [
                    [0, 2, 4, 4, 0, 6, 3, 6, 6, 0, 0],
                    [1, 1, 3, 0, 0, 4, 5, 6, 0, 0, 0],
                ]
            )
            .reshape([2, 11])
            .astype("int32")
        )
141
        self.input_length = np.array([[9], [8]]).reshape([2, 1]).astype("int32")
142 143 144

    def setUp(self):
        self.config()
145 146 147 148 149 150 151 152
        output, output_length = CTCAlign(
            self.input,
            self.input_lod,
            self.blank,
            self.merge_repeated,
            self.padding_value,
            self.input_length,
        )
153 154
        self.inputs = {
            "Input": (self.input, self.input_lod),
155
            "InputLength": self.input_length,
156 157
        }
        self.outputs = {"Output": output, "OutputLength": output_length}
158 159 160
        self.attrs = {
            "blank": self.blank,
            "merge_repeated": self.merge_repeated,
161
            "padding_value": self.padding_value,
162 163 164
        }

    def test_check_output(self):
165 166
        # NODE(yjjiang11): This op will be deprecated.
        self.check_output(check_dygraph=False)
167 168 169 170 171 172 173 174


class TestCTCAlignOpCase3(TestCTCAlignPaddingOp):
    def config(self):
        self.op_type = "ctc_align"
        self.blank = 0
        self.input_lod = []
        self.merge_repeated = True
175
        self.padding_value = 0
176 177 178 179 180 181 182 183 184 185
        self.input = (
            np.array(
                [[0, 1, 2, 2, 0, 4], [0, 4, 5, 0, 6, 0], [0, 7, 7, 7, 0, 0]]
            )
            .reshape([3, 6])
            .astype("int32")
        )
        self.input_length = (
            np.array([[6], [5], [4]]).reshape([3, 1]).astype("int32")
        )
186 187 188 189


class TestCTCAlignOpCase4(TestCTCAlignPaddingOp):
    '''
190
    # test tensor input which has attr input padding_value
191 192 193 194 195 196 197
    '''

    def config(self):
        self.op_type = "ctc_align"
        self.blank = 0
        self.input_lod = []
        self.merge_repeated = False
198
        self.padding_value = 0
199 200 201 202 203 204 205 206 207 208
        self.input = (
            np.array(
                [[0, 1, 2, 2, 0, 4], [0, 4, 5, 0, 6, 0], [0, 7, 7, 7, 0, 0]]
            )
            .reshape([3, 6])
            .astype("int32")
        )
        self.input_length = (
            np.array([[6], [5], [4]]).reshape([3, 1]).astype("int32")
        )
209 210 211 212 213 214 215 216


class TestCTCAlignOpCase5(TestCTCAlignPaddingOp):
    def config(self):
        self.op_type = "ctc_align"
        self.blank = 0
        self.input_lod = []
        self.merge_repeated = False
217
        self.padding_value = 1
218 219 220 221 222 223 224 225 226 227
        self.input = (
            np.array(
                [[0, 1, 2, 2, 0, 4], [0, 4, 5, 0, 6, 0], [0, 7, 1, 7, 0, 0]]
            )
            .reshape([3, 6])
            .astype("int32")
        )
        self.input_length = (
            np.array([[6], [5], [4]]).reshape([3, 1]).astype("int32")
        )
228 229


230
if __name__ == "__main__":
H
hong 已提交
231
    paddle.enable_static()
232
    unittest.main()