test_conv2d_api.py 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#   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
import numpy as np

import paddle
19

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


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

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 102 103 104 105 106
        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",
        )

        fluid.layers.conv2d(
            input=input_NHWC,
            num_filters=3,
            filter_size=[3, 3],
            stride=[1, 1],
            padding=0,
            dilation=[1, 1],
            groups=1,
            data_format="NCHW",
        )

        fluid.layers.conv2d(
            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",
        )

        fluid.layers.conv2d(
            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",
        )

        fluid.layers.conv2d(
            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",
        )

        fluid.layers.conv2d(
            input=input_NCHW,
            num_filters=3,
            filter_size=[3, 3],
            stride=[1, 1],
            padding="SAME",
            dilation=[1, 1],
            groups=1,
            data_format="NCHW",
        )

        fluid.layers.conv2d(
            input=input_NCHW,
            num_filters=3,
            filter_size=[3, 3],
            stride=[1, 1],
            padding="VALID",
            dilation=[1, 1],
            groups=1,
            data_format="NCHW",
        )
107 108

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


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

        # ValueError: cudnn
        def run_1():
131 132 133 134 135 136 137 138 139 140 141
            fluid.layers.conv2d(
                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",
            )
142 143 144 145 146

        self.assertRaises(ValueError, run_1)

        # ValueError: data_format
        def run_2():
147 148 149 150 151 152 153 154 155 156 157
            fluid.layers.conv2d(
                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",
            )
158 159 160 161 162

        self.assertRaises(ValueError, run_2)

        # ValueError: padding
        def run_3():
163 164 165 166 167 168 169 170 171 172 173
            fluid.layers.conv2d(
                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",
            )
174 175 176 177

        self.assertRaises(ValueError, run_3)

        def run_4():
178 179 180 181 182 183 184 185 186 187 188
            fluid.layers.conv2d(
                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",
            )
189 190 191 192

        self.assertRaises(ValueError, run_4)

        def run_5():
193 194 195 196 197 198 199 200 201 202 203
            fluid.layers.conv2d(
                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",
            )
204 205 206 207

        self.assertRaises(ValueError, run_5)

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

        def run_6():
216 217 218 219 220 221 222 223 224 225 226
            fluid.layers.conv2d(
                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",
            )
227 228 229 230 231

        self.assertRaises(ValueError, run_6)

        # ValueError: groups
        def run_7():
232 233 234 235 236 237 238 239 240 241 242
            fluid.layers.conv2d(
                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",
            )
243 244 245 246 247

        self.assertRaises(ValueError, run_7)

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

        self.assertRaises(ValueError, run_8)

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

        self.assertRaises(ValueError, run_9)

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

        self.assertRaises(ValueError, run_10)

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

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

        self.assertRaises(ValueError, run_1)


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

    def run2(self, place):
        with fluid.dygraph.guard(place):
            inputs = fluid.dygraph.to_variable(self.input_np)
354 355 356 357 358 359
            conv = paddle.nn.Conv2D(
                in_channels=3,
                out_channels=4,
                kernel_size=(3, 3),
                data_format="NCHW",
            )
360 361 362 363 364 365 366 367
            result = conv(inputs)

    def run3(self, place):
        with fluid.dygraph.guard(place):
            inputs = fluid.dygraph.to_variable(self.input_np)
            conv = paddle.fluid.dygraph.nn.Conv2D(
                num_channels=3,
                num_filters=4,
368 369
                filter_size=(3, 3),
            )
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
            result = conv(inputs)

    def run_all(self, place):
        self.run1(place)
        self.run2(place)
        self.run3(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()