test_var_conv_2d.py 9.6 KB
Newer Older
K
Kevin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

import unittest
16

K
Kevin 已提交
17
import numpy as np
18
from op_test import OpTest, skip_check_grad_ci
K
Kevin 已提交
19 20


C
cnn 已提交
21
class TestVarConv2DOp(OpTest):
K
Kevin 已提交
22 23 24 25 26 27 28 29 30
    def setUp(self):
        self.init_op_type()
        self.set_data()
        self.compute()

    def init_op_type(self):
        self.op_type = "var_conv_2d"

    def set_data(self):
31
        input_channel = 8
K
Kevin 已提交
32 33 34 35 36
        output_channel = 2
        filter_size = [2, 3]
        stride = [1, 1]
        row = [2, 4]
        col = [3, 2]
37 38 39
        self.init_data(
            input_channel, output_channel, filter_size, stride, row, col
        )
K
Kevin 已提交
40

41 42 43
    def init_data(
        self, input_channel, output_channel, filter_size, stride, row, col
    ):
K
Kevin 已提交
44 45 46 47 48 49 50

        feature = [row[i] * col[i] for i in range(len(row))]
        numel = sum(feature) * input_channel
        x_data = np.random.random((numel, 1)).astype('float32')
        x_lod = [[x * input_channel for x in feature]]
        row_data = np.random.random((sum(row), 10)).astype('float32')
        col_data = np.random.random((sum(col), 10)).astype('float32')
51 52 53 54
        w_shape = (
            output_channel,
            input_channel * filter_size[0] * filter_size[1],
        )
K
Kevin 已提交
55 56 57 58 59
        w_data = np.random.random(w_shape).astype('float32')
        self.inputs = {
            'X': (x_data, x_lod),
            'ROW': (row_data, [row]),
            'COLUMN': (col_data, [col]),
60
            'W': w_data,
K
Kevin 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        }
        self.attrs = {
            'InputChannel': input_channel,
            'OutputChannel': output_channel,
            'StrideH': stride[0],
            'StrideW': stride[1],
            'KernelH': filter_size[0],
            'KernelW': filter_size[1],
        }

    def compute(self):
        in_ch = self.attrs['InputChannel']
        out_ch = self.attrs['OutputChannel']
        kernel_h = self.attrs['KernelH']
        kernel_w = self.attrs['KernelW']
        stride_h = self.attrs['StrideH']
        stride_w = self.attrs['StrideW']
        row_data, row_lod = self.inputs['ROW']
        col_data, col_lod = self.inputs['COLUMN']
        x_data, x_lod = self.inputs['X']
        w_data = self.inputs['W']
        out_data = np.zeros((0, 1)).astype('float32')

        col_res_data, col_res_lod = self.Im2Col()
        out_lod = [[]]
        col_data_offset = 0
        batch_size = len(x_lod[0])
        for idx in range(batch_size):
            width = col_lod[0][idx]
            height = row_lod[0][idx]
            top_im_x = 0
            if width != 0:
                top_im_x = (width - 1) // stride_w + 1
            top_im_y = 0
            if height != 0:
                top_im_y = (height - 1) // stride_h + 1
            top_im_size = top_im_x * top_im_y
            out_lod[0].append(out_ch * top_im_size)
            if top_im_size == 0:
                out_tmp = np.zeros((out_ch * top_im_size, 1)).astype('float32')
            else:
102 103 104
                col_batch_data = col_res_data[
                    col_data_offset : col_data_offset + col_res_lod[0][idx]
                ]
K
Kevin 已提交
105 106 107 108 109 110 111 112 113
                gemm_shape = (in_ch * kernel_h * kernel_w, top_im_size)
                col_batch_data = col_batch_data.reshape(gemm_shape)
                out_tmp = np.dot(w_data, col_batch_data).reshape(-1, 1)
            out_data = np.vstack((out_data, out_tmp))

            col_data_offset += col_res_lod[0][idx]

        self.outputs = {
            'Out': (out_data.astype('float32'), out_lod),
114
            'Col': (col_res_data, col_res_lod),
K
Kevin 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        }

    def Im2Col(self):
        in_ch = self.attrs['InputChannel']
        kernel_h = self.attrs['KernelH']
        kernel_w = self.attrs['KernelW']
        stride_h = self.attrs['StrideH']
        stride_w = self.attrs['StrideW']
        row_data, row_lod = self.inputs['ROW']
        col_data, col_lod = self.inputs['COLUMN']
        x_data, x_lod = self.inputs['X']
        col_res_lod = [[]]
        top_size = 0
        batch_size = len(x_lod[0])
        for idx in range(batch_size):
            width = col_lod[0][idx]
            height = row_lod[0][idx]
            top_im_x = 0
            if width != 0:
                top_im_x = (width - 1) // stride_w + 1
            top_im_y = 0
            if height != 0:
                top_im_y = (height - 1) // stride_h + 1
            top_x = top_im_x * top_im_y
            top_y = in_ch * kernel_h * kernel_w
            col_res_lod[0].append(top_x * top_y)
            top_size += top_x * top_y

        col_res = np.zeros((top_size, 1)).astype('float32')

        kernel_win_size = kernel_h * kernel_w
        half_kernel_h = kernel_h // 2
        half_kernel_w = kernel_w // 2
        t_offset, b_offset = 0, 0
        for idx in range(batch_size):
            width = col_lod[0][idx]
            height = row_lod[0][idx]
            if width == 0 or height == 0:
                continue
            top_im_x = (width - 1) // stride_w + 1
            top_im_y = (height - 1) // stride_h + 1
            top_x = top_im_x * top_im_y
            for z in range(in_ch):
                row_offset = kernel_win_size * z
                im_offset = z * width * height
                for y in range(0, height, stride_h):
                    for x in range(0, width, stride_w):
                        col_offset = x // stride_w + y // stride_h * top_im_x
                        for ky in range(kernel_h):
                            for kx in range(kernel_w):
                                im_y = y + ky - half_kernel_h
                                im_x = x + kx - half_kernel_w
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                                if (
                                    im_x >= 0
                                    and im_x < width
                                    and im_y >= 0
                                    and im_y < height
                                ):
                                    col_res[
                                        t_offset
                                        + (row_offset + ky * kernel_w + kx)
                                        * top_x
                                        + col_offset
                                    ] = x_data[
                                        b_offset
                                        + im_offset
                                        + im_y * width
                                        + im_x
                                    ]
K
Kevin 已提交
184 185 186 187 188 189 190

            t_offset += col_res_lod[0][idx]
            b_offset += x_lod[0][idx]

        return col_res, col_res_lod

    def test_check_output(self):
H
hong 已提交
191
        self.check_output(check_dygraph=False)
K
Kevin 已提交
192 193

    def test_check_grad(self):
194 195 196
        self.check_grad(
            ['X'], 'Out', max_relative_error=0.005, check_dygraph=False
        )
K
Kevin 已提交
197 198


C
cnn 已提交
199
class TestVarConv2DOpCase1(TestVarConv2DOp):
K
Kevin 已提交
200 201 202 203 204 205
    def set_data(self):
        # set in_ch 1
        input_channel = 1
        output_channel = 2
        filter_size = [2, 3]
        stride = [1, 1]
Z
zhupengyang 已提交
206 207
        row = [1, 10]
        col = [40, 6]
208 209 210
        self.init_data(
            input_channel, output_channel, filter_size, stride, row, col
        )
K
Kevin 已提交
211 212


C
cnn 已提交
213
class TestVarConv2DOpCase2(TestVarConv2DOp):
K
Kevin 已提交
214 215 216 217 218 219
    def set_data(self):
        # set out_ch 1
        input_channel = 2
        output_channel = 1
        filter_size = [3, 3]
        stride = [2, 2]
Z
zhupengyang 已提交
220 221
        row = [6, 7]
        col = [8, 2]
222 223 224
        self.init_data(
            input_channel, output_channel, filter_size, stride, row, col
        )
K
Kevin 已提交
225 226


C
cnn 已提交
227
class TestVarConv2DOpCase3(TestVarConv2DOp):
K
Kevin 已提交
228 229 230 231 232 233
    def set_data(self):
        # set batch 1
        input_channel = 2
        output_channel = 1
        filter_size = [3, 3]
        stride = [2, 2]
Z
zhupengyang 已提交
234 235
        row = [14]
        col = [4]
236 237 238
        self.init_data(
            input_channel, output_channel, filter_size, stride, row, col
        )
K
Kevin 已提交
239 240


C
cnn 已提交
241
class TestVarConv2DOpCase4(TestVarConv2DOp):
K
Kevin 已提交
242 243 244 245 246 247 248 249
    def set_data(self):
        # set filter size very large
        input_channel = 3
        output_channel = 4
        filter_size = [6, 6]
        stride = [2, 2]
        row = [4, 7]
        col = [5, 2]
250 251 252
        self.init_data(
            input_channel, output_channel, filter_size, stride, row, col
        )
K
Kevin 已提交
253 254


C
cnn 已提交
255
class TestVarConv2DOpCase5(TestVarConv2DOp):
K
Kevin 已提交
256 257
    def set_data(self):
        # set input very small
Z
zhupengyang 已提交
258
        input_channel = 50
K
Kevin 已提交
259 260 261 262 263
        output_channel = 3
        filter_size = [3, 3]
        stride = [1, 1]
        row = [1, 1]
        col = [1, 1]
264 265 266
        self.init_data(
            input_channel, output_channel, filter_size, stride, row, col
        )
K
Kevin 已提交
267 268


269
@skip_check_grad_ci(
270
    reason="[skip shape check] Use shape of input_channel, row and col all is 1 to test special LoDTensor."
271
)
C
cnn 已提交
272
class TestVarConv2DOpCase6(TestVarConv2DOp):
K
Kevin 已提交
273 274 275 276 277 278 279
    def set_data(self):
        input_channel = 1
        output_channel = 3
        filter_size = [3, 3]
        stride = [1, 1]
        row = [1, 1]
        col = [1, 1]
280 281 282
        self.init_data(
            input_channel, output_channel, filter_size, stride, row, col
        )
K
Kevin 已提交
283 284


C
cnn 已提交
285
class TestVarConv2DOpCase7(TestVarConv2DOp):
K
Kevin 已提交
286 287 288 289 290 291 292
    def set_data(self):
        input_channel = 2
        output_channel = 3
        filter_size = [3, 3]
        stride = [1, 1]
        row = [5, 4]
        col = [6, 7]
293 294 295
        self.init_data(
            input_channel, output_channel, filter_size, stride, row, col
        )
K
Kevin 已提交
296 297 298 299


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