test_conv2d_api.py 10.0 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):

G
GGBond8488 已提交
29
        input_NHWC = paddle.static.data(
30 31 32 33 34
            name="input_NHWC",
            shape=[2, 5, 5, 3],
            dtype="float32",
        )

G
GGBond8488 已提交
35
        input_NCHW = paddle.static.data(
36 37 38 39 40
            name="input_NCHW",
            shape=[2, 3, 5, 5],
            dtype="float32",
        )

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

52
        paddle.static.nn.conv2d(
53 54 55 56 57 58 59 60 61 62
            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",
        )

63
        paddle.static.nn.conv2d(
64 65 66 67 68 69 70 71 72 73
            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",
        )

74
        paddle.static.nn.conv2d(
75 76 77 78 79 80 81 82 83 84
            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",
        )

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

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

    def test_depthwise_conv2d(self):
108 109 110 111 112 113 114 115
        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',
        )
116 117 118 119 120
        y_var = conv(x_var)


class TestConv2DAPI_Error(unittest.TestCase):
    def test_api(self):
G
GGBond8488 已提交
121
        input = paddle.static.data(
122 123 124 125
            name="input",
            shape=[2, 5, 5, 5],
            dtype="float32",
        )
126 127 128

        # ValueError: cudnn
        def run_1():
129
            paddle.static.nn.conv2d(
130 131 132 133 134 135 136 137 138 139
                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",
            )
140 141 142 143 144

        self.assertRaises(ValueError, run_1)

        # ValueError: data_format
        def run_2():
145
            paddle.static.nn.conv2d(
146 147 148 149 150 151 152 153 154 155
                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",
            )
156 157 158 159 160

        self.assertRaises(ValueError, run_2)

        # ValueError: padding
        def run_3():
161
            paddle.static.nn.conv2d(
162 163 164 165 166 167 168 169 170 171
                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",
            )
172 173 174 175

        self.assertRaises(ValueError, run_3)

        def run_4():
176
            paddle.static.nn.conv2d(
177 178 179 180 181 182 183 184 185 186
                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",
            )
187 188 189 190

        self.assertRaises(ValueError, run_4)

        def run_5():
191
            paddle.static.nn.conv2d(
192 193 194 195 196 197 198 199 200 201
                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",
            )
202 203 204 205

        self.assertRaises(ValueError, run_5)

        # ValueError: channel dimmention
G
GGBond8488 已提交
206
        x = paddle.static.data(
207 208 209 210
            name="x",
            shape=[2, 5, 5, -1],
            dtype="float32",
        )
211 212

        def run_6():
213
            paddle.static.nn.conv2d(
214 215 216 217 218 219 220 221 222 223
                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",
            )
224 225 226 227 228

        self.assertRaises(ValueError, run_6)

        # ValueError: groups
        def run_7():
229
            paddle.static.nn.conv2d(
230 231 232 233 234 235 236 237 238 239
                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",
            )
240 241 242 243 244

        self.assertRaises(ValueError, run_7)

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

        self.assertRaises(ValueError, run_8)

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

        self.assertRaises(ValueError, run_9)

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

        self.assertRaises(ValueError, run_10)

    def test_api_with_error_input(self):
G
GGBond8488 已提交
292
        input = paddle.static.data(
293 294 295 296
            name="error_input",
            shape=[1],
            dtype="float32",
        )
297 298 299

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

        self.assertRaises(ValueError, run_1)


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

    def run2(self, place):
        with fluid.dygraph.guard(place):
            inputs = fluid.dygraph.to_variable(self.input_np)
349 350 351 352 353 354
            conv = paddle.nn.Conv2D(
                in_channels=3,
                out_channels=4,
                kernel_size=(3, 3),
                data_format="NCHW",
            )
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
            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()