test_conv2d_fusion_op.py 11.2 KB
Newer Older
Q
qingqing01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#   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.

from __future__ import print_function

import unittest
import numpy as np

import paddle.fluid.core as core
from op_test import OpTest

from test_conv2d_op import conv2d_forward_naive


26
def create_test_padding_SAME_class(parent):
27

28
    class TestPaddingSAMECase(parent):
29

30 31 32 33 34
        def init_paddings(self):
            self.pad = [0, 0]
            self.padding_algorithm = "SAME"

    cls_name = "{0}_{1}".format(parent.__name__, "PaddingSAMEOp")
35 36
    TestPaddingSAMECase.__name__ = cls_name
    globals()[cls_name] = TestPaddingSAMECase
37 38 39


def create_test_padding_VALID_class(parent):
40

41
    class TestPaddingVALIDCase(parent):
42

43 44 45 46 47 48 49 50 51
        def init_paddings(self):
            self.pad = [1, 1]
            self.padding_algorithm = "VALID"

    cls_name = "{0}_{1}".format(parent.__name__, "PaddingVALIDOp")
    TestPaddingVALIDCase.__name__ = cls_name
    globals()[cls_name] = TestPaddingVALIDCase


C
cnn 已提交
52
class TestConv2DFusionOp(OpTest):
53

Q
qingqing01 已提交
54 55 56
    def setUp(self):
        self.op_type = "conv2d_fusion"
        self.exhaustive_search = False
57
        self.data_format = "NCHW"
Q
qingqing01 已提交
58 59 60
        self.dtype = np.float32
        self.activation = 'relu'
        self.add_residual_data = True
61
        self.split_channels = None
Q
qingqing01 已提交
62
        self.outputs = None
63
        self.padding_algorithm = "EXIPLICIT"
Q
qingqing01 已提交
64 65 66 67

        self.init_group()
        self.init_dilation()
        self.init_test_case()
68
        self.init_residual()
Q
qingqing01 已提交
69
        self.init_activation()
70
        self.init_paddings()
Q
qingqing01 已提交
71 72 73 74 75 76 77 78 79 80
        self.set_search_method()

        conv2d_param = {
            'stride': self.stride,
            'pad': self.pad,
            'dilation': self.dilations
        }

        input = np.random.random(self.input_size).astype(self.dtype)
        filter = np.random.random(self.filter_size).astype(self.dtype)
81
        bias = np.random.random(self.filter_size[0]).astype(self.dtype)
Q
qingqing01 已提交
82

83 84 85 86 87
        self.output, _, _, _, _ = conv2d_forward_naive(input, filter,
                                                       self.groups,
                                                       conv2d_param,
                                                       self.padding_algorithm,
                                                       self.data_format)
88

89
        self.output = self.output.astype(self.dtype)
Q
qingqing01 已提交
90 91 92

        self.inputs = {
            'Input': OpTest.np_dtype_to_fluid_dtype(input),
93 94
            'Filter': OpTest.np_dtype_to_fluid_dtype(filter),
            'Bias': OpTest.np_dtype_to_fluid_dtype(bias)
Q
qingqing01 已提交
95 96 97
        }

        if self.add_residual_data:
Q
qingqing01 已提交
98 99
            residual_data = np.random.random(self.output.shape).astype(
                self.dtype)
Q
qingqing01 已提交
100 101
            self.inputs['ResidualData'] = OpTest.np_dtype_to_fluid_dtype(
                residual_data)
Q
qingqing01 已提交
102
            self.output += residual_data
Q
qingqing01 已提交
103

104 105
        # Add bias
        self.output = self.output + bias.reshape((1, bias.size, 1, 1))
Q
qingqing01 已提交
106 107 108

        assert self.activation in ['relu', 'identity']
        if self.activation == 'relu':
Q
qingqing01 已提交
109
            self.output = np.maximum(self.output, 0)
Q
qingqing01 已提交
110 111 112 113 114 115 116 117

        self.attrs = {
            'strides': self.stride,
            'paddings': self.pad,
            'groups': self.groups,
            'dilations': self.dilations,
            'data_format': self.data_format,
            'exhaustive_search': self.exhaustive_search,
Q
qingqing01 已提交
118
            'activation': self.activation,
119
            'padding_algorithm': self.padding_algorithm
Q
qingqing01 已提交
120
        }
121 122 123
        if self.split_channels is not None:
            self.attrs['split_channels'] = self.split_channels

Q
qingqing01 已提交
124 125 126
        self.outputs = {'Output': self.output}

        self.set_outputs()
Q
qingqing01 已提交
127

128
    def has_cuda(self):
Q
qingqing01 已提交
129 130 131
        return core.is_compiled_with_cuda()

    def test_check_output(self):
132
        if self.has_cuda():
Q
qingqing01 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)

    def init_test_case(self):
        self.pad = [0, 0]
        self.stride = [1, 1]
        self.input_size = [2, 3, 5, 5]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 3, 3]

    def init_dilation(self):
        self.dilations = [1, 1]

    def init_group(self):
        self.groups = 1

150
    def init_residual(self):
Q
qingqing01 已提交
151 152 153 154 155 156 157 158
        self.add_residual_data = True

    def init_activation(self):
        self.activation = 'relu'

    def set_search_method(self):
        self.exhaustive_search = False

Q
qingqing01 已提交
159 160 161
    def set_outputs(self):
        pass

162 163 164 165
    def init_paddings(self):
        self.pad = [0, 0]
        self.padding_algorithm = "EXPLICIT"

Q
qingqing01 已提交
166

C
cnn 已提交
167
class TestWithoutResidual(TestConv2DFusionOp):
168

169
    def init_residual(self):
Q
qingqing01 已提交
170 171 172
        self.add_residual_data = False


C
cnn 已提交
173
class TestIdentityActivation(TestConv2DFusionOp):
174

Q
qingqing01 已提交
175 176 177 178
    def init_activation(self):
        self.activation = 'identity'


Z
zhangchunle 已提交
179
class TestIdentityActivation1(TestConv2DFusionOp):
180

181 182 183 184 185
    def init_activation(self):
        self.activation = 'identity'
        self.add_residual_data = False


C
cnn 已提交
186
class TestWithGroup(TestConv2DFusionOp):
187

Q
qingqing01 已提交
188 189 190 191
    def init_group(self):
        self.groups = 3


C
cnn 已提交
192
class TestWithDilation(TestConv2DFusionOp):
193

Q
qingqing01 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    def init_test_case(self):
        self.pad = [0, 0]
        self.stride = [1, 1]
        self.input_size = [2, 3, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 3, 3]

    def init_dilation(self):
        self.dilations = [2, 2]

    def init_group(self):
        self.groups = 3


C
cnn 已提交
209
class TestCUDNNExhaustiveSearch(TestConv2DFusionOp):
210

Q
qingqing01 已提交
211 212 213 214
    def set_search_method(self):
        self.exhaustive_search = True


C
cnn 已提交
215
class TestMultipleOutputs(TestConv2DFusionOp):
216

Q
qingqing01 已提交
217 218 219 220 221 222 223
    def init_test_case(self):
        self.pad = [1, 1]
        self.stride = [1, 1]
        self.input_size = [1, 32, 17, 17]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [126, f_c, 3, 3]
224
        self.split_channels = [84, 42]
Q
qingqing01 已提交
225 226 227 228 229 230 231

    def set_outputs(self):
        out1 = self.output[:, 0:84, :, :]
        out2 = self.output[:, 84:126, :, :]
        self.outputs['Outputs'] = [('out1', out1), ('out2', out2)]


C
cnn 已提交
232
class TestAsyPadding(TestConv2DFusionOp):
233

234 235 236 237 238
    def init_paddings(self):
        self.pad = [0, 0, 1, 2]
        self.padding_algorithm = "EXPLICIT"


C
cnn 已提交
239
class TestWithPad_AsyPadding(TestConv2DFusionOp):
240

241 242 243 244 245 246 247 248 249 250 251 252
    def init_test_case(self):
        self.stride = [1, 1]
        self.input_size = [2, 3, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 3, 3]

    def init_paddings(self):
        self.pad = [2, 1, 3, 2]
        self.padding_algorithm = "EXPLICIT"


C
cnn 已提交
253
class TestWithStride_AsyPadding(TestConv2DFusionOp):
254

255 256 257 258 259 260 261 262 263 264 265 266
    def init_test_case(self):
        self.stride = [2, 2]
        self.input_size = [2, 3, 6, 6]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 3, 3]

    def init_paddings(self):
        self.pad = [2, 1, 3, 2]
        self.padding_algorithm = "EXPLICIT"


C
cnn 已提交
267
class TestWith1x1_AsyPadding(TestConv2DFusionOp):
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    def init_test_case(self):
        self.stride = [1, 1]
        self.input_size = [2, 3, 5, 5]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 1, 1]

    def init_group(self):
        self.groups = 3

    def init_paddings(self):
        self.pad = [2, 2, 4, 0]
        self.padding_algorithm = "EXPLICIT"


C
cnn 已提交
284
class TestWithGroup_AsyPadding(TestConv2DFusionOp):
285

286 287 288 289
    def init_group(self):
        self.groups = 3


C
cnn 已提交
290
class TestWithDepthWise3x3_AsyPadding(TestConv2DFusionOp):
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    def init_test_case(self):
        self.stride = [1, 1]
        self.input_size = [3, 4, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [8, f_c, 3, 3]

    def init_dilation(self):
        self.dilations = [2, 2]

    def init_group(self):
        self.groups = 4

    def init_paddings(self):
        self.pad = [1, 3, 2, 1]
        self.padding_algorithm = "EXPLICIT"


C
cnn 已提交
310
class TestWithDepthWise5x5_AsyPadding(TestConv2DFusionOp):
311

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    def init_test_case(self):
        self.stride = [1, 1]
        self.input_size = [2, 4, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [8, f_c, 5, 5]

    def init_group(self):
        self.groups = 4

    def init_paddings(self):
        self.pad = [0, 1, 1, 0]
        self.padding_algorithm = "EXPLICIT"


C
cnn 已提交
327
class TestWithDepthWise7x7_AsyPadding(TestConv2DFusionOp):
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    def init_test_case(self):
        self.stride = [2, 2]
        self.input_size = [2, 8, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [16, f_c, 7, 7]

    def init_group(self):
        self.groups = 8

    def init_paddings(self):
        self.pad = [1, 3, 4, 1]
        self.padding_algorithm = "EXPLICIT"


C
cnn 已提交
344
class TestWithDilation_AsyPadding(TestConv2DFusionOp):
345

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    def init_test_case(self):
        self.stride = [1, 1]
        self.input_size = [2, 3, 10, 10]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 3, 3]

    def init_dilation(self):
        self.dilations = [2, 2]

    def init_group(self):
        self.groups = 3

    def init_paddings(self):
        self.pad = [0, 1, 3, 0]
        self.padding_algorithm = "EXPLICIT"


C
cnn 已提交
364
class TestWithInput1x1Filter1x1_AsyPadding(TestConv2DFusionOp):
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    def init_test_case(self):
        self.stride = [1, 1]
        self.input_size = [2, 3, 1, 1]  # NCHW
        assert np.mod(self.input_size[1], self.groups) == 0
        f_c = self.input_size[1] // self.groups
        self.filter_size = [6, f_c, 1, 1]

    def init_group(self):
        self.groups = 3

    def init_paddings(self):
        self.pad = [0, 3, 4, 0]
        self.padding_algorithm = "EXPLICIT"


create_test_padding_SAME_class(TestAsyPadding)
create_test_padding_SAME_class(TestWithPad_AsyPadding)
create_test_padding_SAME_class(TestWithStride_AsyPadding)
create_test_padding_SAME_class(TestWithGroup_AsyPadding)
create_test_padding_SAME_class(TestWithInput1x1Filter1x1_AsyPadding)

create_test_padding_VALID_class(TestAsyPadding)
create_test_padding_VALID_class(TestWithPad_AsyPadding)
create_test_padding_VALID_class(TestWithStride_AsyPadding)
create_test_padding_VALID_class(TestWithGroup_AsyPadding)
create_test_padding_VALID_class(TestWithInput1x1Filter1x1_AsyPadding)

Q
qingqing01 已提交
393 394
if __name__ == '__main__':
    unittest.main()