“10f7e5192d0d5e58793d1618e52f7de1214131e0”上不存在“ppocr/modeling/transform/__init__.py”
test_seq_pool.py 5.4 KB
Newer Older
1 2 3 4 5
import unittest
import numpy as np
from op_test import OpTest


6 7 8
class TestSeqAvgPool(OpTest):
    def set_data(self):
        self.op_type = 'sequence_pool'
9
        # one level, batch size is 4
10
        x = np.random.uniform(0.1, 1, [11, 23]).astype('float32')
11
        lod = [[0, 4, 5, 8, 11]]
12
        self.inputs = {'X': (x, lod)}
13

14
        out = np.zeros((4, 23)).astype('float32')
15
        self.outputs = {'Out': out}
16
        return x, lod, out
17

18
    def compute(self, x, lod, out):
D
dzhwinter 已提交
19
        self.attrs = {'pooltype': "AVERAGE"}
20 21 22 23
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.mean(axis=0)

24
    def setUp(self):
25 26
        x, lod, out = self.set_data()
        self.compute(x, lod, out)
27 28 29 30 31

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
32 33 34
        # Remove MaxIndex after check_grad is refined.
        self.outputs['MaxIndex'] = \
            np.zeros(self.outputs['Out'].shape).astype('int32')
35 36 37
        self.check_grad(["X"], "Out")


38 39 40
class TestSeqAvgPool2D(TestSeqAvgPool):
    def set_data(self):
        self.op_type = 'sequence_pool'
41 42 43
        # one level, batch size is 4
        x = np.random.uniform(0.1, 1, [13, 3, 17]).astype('float32')
        lod = [[0, 4, 5, 8, 13]]
44
        self.inputs = {'X': (x, lod)}
45 46

        out = np.zeros((4, 3, 17)).astype('float32')
47
        self.outputs = {'Out': out}
48
        return x, lod, out
49

50
    def compute(self, x, lod, out):
D
dzhwinter 已提交
51
        self.attrs = {'pooltype': "AVERAGE"}
52 53 54 55 56
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x.mean(axis=0), (3, 17))


57
class TestSeqSumPool(TestSeqAvgPool):
58
    def compute(self, x, lod, out):
D
dzhwinter 已提交
59
        self.attrs = {'pooltype': "SUM"}
60 61 62
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x.sum(axis=0)
63

64 65

class TestSeqSumPool2D(TestSeqAvgPool2D):
66
    def compute(self, x, lod, out):
D
dzhwinter 已提交
67
        self.attrs = {'pooltype': "SUM"}
68 69 70
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x.sum(axis=0), (3, 17))
71 72


L
Luo Tao 已提交
73
class TestSeqSqrtPool(TestSeqAvgPool):
74
    def compute(self, x, lod, out):
D
dzhwinter 已提交
75
        self.attrs = {'pooltype': "SQRT"}
L
Luo Tao 已提交
76 77 78 79 80 81 82
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            len = lod[0][i + 1] - lod[0][i]
            out[i] = sub_x.sum(axis=0) / np.sqrt(len)


class TestSeqSqrtPool2D(TestSeqAvgPool2D):
83
    def compute(self, x, lod, out):
D
dzhwinter 已提交
84
        self.attrs = {'pooltype': "SQRT"}
L
Luo Tao 已提交
85 86 87 88 89 90
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            len = lod[0][i + 1] - lod[0][i]
            out[i] = np.reshape(sub_x.sum(axis=0) / np.sqrt(len), (3, 17))

    def test_check_grad(self):
91 92 93
        # Remove MaxIndex after check_grad is refined.
        self.outputs['MaxIndex'] = \
            np.zeros(self.outputs['Out'].shape).astype('int32')
L
Luo Tao 已提交
94 95 96
        self.check_grad(["X"], "Out", max_relative_error=0.06)


L
Luo Tao 已提交
97
class TestSeqMaxPool(TestSeqAvgPool):
98 99 100 101 102 103 104 105 106 107 108 109 110 111
    def set_data(self):
        self.op_type = 'sequence_pool'
        x = np.random.uniform(0.1, 1, [13, 23]).astype('float32')
        lod = [[0, 4, 5, 8, 13]]
        for i in range(4):
            l = lod[0][i + 1] - lod[0][i]
            x[lod[0][i] + np.random.randint(l), :] += 2.0

        self.inputs = {'X': (x, lod)}

        out = np.zeros((4, 23)).astype('float32')
        self.outputs = {'Out': out}
        return x, lod, out

112
    def compute(self, x, lod, out):
D
dzhwinter 已提交
113
        self.attrs = {'pooltype': "MAX"}
L
Luo Tao 已提交
114 115 116 117 118 119
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = np.amax(sub_x, axis=0)


class TestSeqMaxPool2D(TestSeqAvgPool2D):
120 121 122 123 124 125 126 127 128 129 130 131 132
    def set_data(self):
        self.op_type = 'sequence_pool'
        x = np.random.uniform(0.1, 1, [13, 3, 11]).astype('float32')
        lod = [[0, 4, 5, 8, 13]]
        self.inputs = {'X': (x, lod)}
        for i in range(4):
            l = lod[0][i + 1] - lod[0][i]
            x[lod[0][i] + np.random.randint(l), :] += 1.0

        out = np.zeros((4, 3, 11)).astype('float32')
        self.outputs = {'Out': out}
        return x, lod, out

133
    def compute(self, x, lod, out):
D
dzhwinter 已提交
134
        self.attrs = {'pooltype': "MAX"}
L
Luo Tao 已提交
135
        for i in range(4):
136 137
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 11))
            out[i] = np.reshape(np.amax(sub_x, axis=0), (3, 11))
L
Luo Tao 已提交
138 139


L
Luo Tao 已提交
140
class TestSeqLastPool(TestSeqAvgPool):
141
    def compute(self, x, lod, out):
D
dzhwinter 已提交
142
        self.attrs = {'pooltype': "LAST"}
L
Luo Tao 已提交
143 144 145 146 147 148
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[-1, :]


class TestSeqLastPool2D(TestSeqAvgPool2D):
149
    def compute(self, x, lod, out):
D
dzhwinter 已提交
150
        self.attrs = {'pooltype': "LAST"}
L
Luo Tao 已提交
151 152 153 154 155 156
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x[-1, :], (3, 17))


class TestSeqFirstPool(TestSeqAvgPool):
157
    def compute(self, x, lod, out):
D
dzhwinter 已提交
158
        self.attrs = {'pooltype': "FIRST"}
L
Luo Tao 已提交
159 160 161 162 163 164
        for i in range(4):
            sub_x = x[lod[0][i]:lod[0][i + 1], :]
            out[i] = sub_x[0, :]


class TestSeqFirstPool2D(TestSeqAvgPool2D):
165
    def compute(self, x, lod, out):
D
dzhwinter 已提交
166
        self.attrs = {'pooltype': "FIRST"}
L
Luo Tao 已提交
167 168 169 170 171
        for i in range(4):
            sub_x = np.reshape(x[lod[0][i]:lod[0][i + 1], :], (-1, 3 * 17))
            out[i] = np.reshape(sub_x[0, :], (3, 17))


172 173
if __name__ == '__main__':
    unittest.main()