test_conv2d_api.py 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2021 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

17 18 19
import numpy as np

import paddle
20

21 22
paddle.enable_static()
import paddle.fluid as fluid
23
import paddle.fluid.core as core
24 25 26 27 28


class TestConv2DAPI(unittest.TestCase):
    def test_api(self):

29 30 31 32 33 34 35 36 37 38 39 40 41 42
        input_NHWC = fluid.layers.data(
            name="input_NHWC",
            shape=[2, 5, 5, 3],
            append_batch_size=False,
            dtype="float32",
        )

        input_NCHW = fluid.layers.data(
            name="input_NCHW",
            shape=[2, 3, 5, 5],
            append_batch_size=False,
            dtype="float32",
        )

43
        paddle.static.nn.conv2d(
44 45 46 47 48 49 50 51 52 53
            input=input_NHWC,
            num_filters=3,
            filter_size=[3, 3],
            stride=[1, 1],
            padding=0,
            dilation=[1, 1],
            groups=1,
            data_format="NCHW",
        )

54
        paddle.static.nn.conv2d(
55 56 57 58 59 60 61 62 63 64
            input=input_NCHW,
            num_filters=3,
            filter_size=[3, 3],
            stride=[1, 1],
            padding=[1, 2, 1, 0],
            dilation=[1, 1],
            groups=1,
            data_format="NCHW",
        )

65
        paddle.static.nn.conv2d(
66 67 68 69 70 71 72 73 74 75
            input=input_NCHW,
            num_filters=3,
            filter_size=[3, 3],
            stride=[1, 1],
            padding=[[0, 0], [0, 0], [1, 1], [1, 1]],
            dilation=[1, 1],
            groups=1,
            data_format="NCHW",
        )

76
        paddle.static.nn.conv2d(
77 78 79 80 81 82 83 84 85 86
            input=input_NHWC,
            num_filters=3,
            filter_size=[3, 3],
            stride=[1, 1],
            padding=[[0, 0], [1, 1], [1, 1], [0, 0]],
            dilation=[1, 1],
            groups=1,
            data_format="NHWC",
        )

87
        paddle.static.nn.conv2d(
88 89 90 91 92 93 94 95 96 97
            input=input_NCHW,
            num_filters=3,
            filter_size=[3, 3],
            stride=[1, 1],
            padding="SAME",
            dilation=[1, 1],
            groups=1,
            data_format="NCHW",
        )

98
        paddle.static.nn.conv2d(
99 100 101 102 103 104 105 106 107
            input=input_NCHW,
            num_filters=3,
            filter_size=[3, 3],
            stride=[1, 1],
            padding="VALID",
            dilation=[1, 1],
            groups=1,
            data_format="NCHW",
        )
108 109

    def test_depthwise_conv2d(self):
110 111 112 113 114 115 116 117
        x_var = paddle.uniform((2, 8, 8, 4), dtype='float32', min=-1.0, max=1.0)
        conv = paddle.nn.Conv2D(
            in_channels=4,
            out_channels=4,
            kernel_size=(3, 3),
            groups=4,
            data_format='NHWC',
        )
118 119 120 121 122
        y_var = conv(x_var)


class TestConv2DAPI_Error(unittest.TestCase):
    def test_api(self):
123 124 125 126 127 128
        input = fluid.layers.data(
            name="input",
            shape=[2, 5, 5, 5],
            append_batch_size=False,
            dtype="float32",
        )
129 130 131

        # ValueError: cudnn
        def run_1():
132
            paddle.static.nn.conv2d(
133 134 135 136 137 138 139 140 141 142
                input=input,
                num_filters=3,
                filter_size=[3, 3],
                stride=[1, 1],
                padding=0,
                dilation=[1, 1],
                groups=1,
                use_cudnn=[0],
                data_format="NCHW",
            )
143 144 145 146 147

        self.assertRaises(ValueError, run_1)

        # ValueError: data_format
        def run_2():
148
            paddle.static.nn.conv2d(
149 150 151 152 153 154 155 156 157 158
                input=input,
                num_filters=3,
                filter_size=[3, 3],
                stride=[1, 1],
                padding=0,
                dilation=[1, 1],
                groups=1,
                use_cudnn=False,
                data_format="NCHWC",
            )
159 160 161 162 163

        self.assertRaises(ValueError, run_2)

        # ValueError: padding
        def run_3():
164
            paddle.static.nn.conv2d(
165 166 167 168 169 170 171 172 173 174
                input=input,
                num_filters=3,
                filter_size=[3, 3],
                stride=[1, 1],
                padding="SAMEE",
                dilation=[1, 1],
                groups=1,
                use_cudnn=False,
                data_format="NCHW",
            )
175 176 177 178

        self.assertRaises(ValueError, run_3)

        def run_4():
179
            paddle.static.nn.conv2d(
180 181 182 183 184 185 186 187 188 189
                input=input,
                num_filters=3,
                filter_size=[3, 3],
                stride=[1, 1],
                padding=[[0, 1], [0, 1], [0, 1], [0, 1]],
                dilation=[1, 1],
                groups=1,
                use_cudnn=False,
                data_format="NCHW",
            )
190 191 192 193

        self.assertRaises(ValueError, run_4)

        def run_5():
194
            paddle.static.nn.conv2d(
195 196 197 198 199 200 201 202 203 204
                input=input,
                num_filters=3,
                filter_size=[3, 3],
                stride=[1, 1],
                padding=[[0, 1], [0, 1], [0, 1], [0, 1]],
                dilation=[1, 1],
                groups=1,
                use_cudnn=False,
                data_format="NHWC",
            )
205 206 207 208

        self.assertRaises(ValueError, run_5)

        # ValueError: channel dimmention
209 210 211 212 213 214
        x = fluid.layers.data(
            name="x",
            shape=[2, 5, 5, -1],
            append_batch_size=False,
            dtype="float32",
        )
215 216

        def run_6():
217
            paddle.static.nn.conv2d(
218 219 220 221 222 223 224 225 226 227
                input=x,
                num_filters=3,
                filter_size=[3, 3],
                stride=[1, 1],
                padding=0,
                dilation=[1, 1],
                groups=1,
                use_cudnn=False,
                data_format="NHWC",
            )
228 229 230 231 232

        self.assertRaises(ValueError, run_6)

        # ValueError: groups
        def run_7():
233
            paddle.static.nn.conv2d(
234 235 236 237 238 239 240 241 242 243
                input=input,
                num_filters=3,
                filter_size=[3, 3],
                stride=[1, 1],
                padding=0,
                dilation=[1, 1],
                groups=3,
                use_cudnn=False,
                data_format="NHWC",
            )
244 245 246 247 248

        self.assertRaises(ValueError, run_7)

        # ValueError: filter num
        def run_8():
249
            paddle.static.nn.conv2d(
250 251 252 253 254 255 256 257 258 259
                input=input,
                num_filters=0,
                filter_size=0,
                stride=0,
                padding=0,
                dilation=0,
                groups=1,
                use_cudnn=False,
                data_format="NCHW",
            )
260 261 262 263 264

        self.assertRaises(ValueError, run_8)

        # ValueError: groups
        def run_9():
265
            paddle.static.nn.conv2d(
266 267 268 269 270 271 272 273 274 275
                input=input,
                num_filters=0,
                filter_size=0,
                stride=0,
                padding=0,
                dilation=0,
                groups=0,
                use_cudnn=False,
                data_format="NCHW",
            )
276 277 278

        self.assertRaises(ValueError, run_9)

279
        # ValueError: stride
280
        def run_10():
281
            paddle.static.nn.conv2d(
282 283 284 285 286 287 288 289 290 291
                input=input,
                num_filters=1,
                filter_size=1,
                stride=0,
                padding=0,
                dilation=0,
                groups=1,
                use_cudnn=False,
                data_format="NCHW",
            )
292 293 294 295

        self.assertRaises(ValueError, run_10)

    def test_api_with_error_input(self):
296 297 298 299 300 301
        input = fluid.layers.data(
            name="error_input",
            shape=[1],
            append_batch_size=False,
            dtype="float32",
        )
302 303 304

        # ValueError: cudnn
        def run_1():
305
            paddle.static.nn.conv2d(
306 307 308 309 310 311 312 313 314 315
                input=input,
                num_filters=0,
                filter_size=0,
                stride=0,
                padding=0,
                dilation=0,
                groups=0,
                use_cudnn=False,
                data_format="NCHW",
            )
316 317 318 319 320 321 322

        self.assertRaises(ValueError, run_1)


# --------- test environment variable ------
@unittest.skipIf(
    not (core.is_compiled_with_cuda() or core.is_compiled_with_rocm()),
323 324
    "core is not compiled with CUDA or ROCM",
)
325 326 327
class TestConv2DEnviron(unittest.TestCase):
    def run1(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
328 329 330 331 332 333
            inputs = fluid.layers.data(
                shape=[2, 3, 5, 5],
                append_batch_size=False,
                name="inputs",
                dtype="float32",
            )
334
            result = paddle.static.nn.conv2d(
335 336 337 338 339 340 341 342 343
                input=inputs,
                num_filters=4,
                filter_size=[3, 3],
                stride=[1, 1],
                padding=0,
                dilation=[1, 1],
                groups=1,
                data_format="NCHW",
            )
344 345
            exe = fluid.Executor(place)
            exe.run(fluid.default_startup_program())
346 347 348 349 350
            fetches = exe.run(
                fluid.default_main_program(),
                feed={"inputs": self.input_np},
                fetch_list=[result],
            )
351 352 353 354

    def run2(self, place):
        with fluid.dygraph.guard(place):
            inputs = fluid.dygraph.to_variable(self.input_np)
355 356 357 358 359 360
            conv = paddle.nn.Conv2D(
                in_channels=3,
                out_channels=4,
                kernel_size=(3, 3),
                data_format="NCHW",
            )
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
            result = conv(inputs)

    def run_all(self, place):
        self.run1(place)
        self.run2(place)

    def test_environ(self):
        self.input_np = np.random.random([2, 3, 5, 5]).astype("float32")
        for place in [paddle.CPUPlace(), paddle.CUDAPlace(0)]:
            fluid.set_flags({'FLAGS_conv2d_disable_cudnn': False})
            self.run_all(place)
            fluid.set_flags({'FLAGS_conv2d_disable_cudnn': True})
            self.run_all(place)


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