test_conv2d_api.py 12.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
paddle.enable_static()
import paddle.fluid.core as core
import paddle.fluid as fluid


class TestConv2DAPI(unittest.TestCase):
26

27 28
    def test_api(self):

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
        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")
92 93 94

    def test_depthwise_conv2d(self):
        x_var = paddle.uniform((2, 8, 8, 4), dtype='float32', min=-1., max=1.)
95 96 97 98 99
        conv = paddle.nn.Conv2D(in_channels=4,
                                out_channels=4,
                                kernel_size=(3, 3),
                                groups=4,
                                data_format='NHWC')
100 101 102 103
        y_var = conv(x_var)


class TestConv2DAPI_Error(unittest.TestCase):
104

105
    def test_api(self):
106 107 108 109
        input = fluid.layers.data(name="input",
                                  shape=[2, 5, 5, 5],
                                  append_batch_size=False,
                                  dtype="float32")
110 111 112

        # ValueError: cudnn
        def run_1():
113 114 115 116 117 118 119 120 121
            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")
122 123 124 125 126

        self.assertRaises(ValueError, run_1)

        # ValueError: data_format
        def run_2():
127 128 129 130 131 132 133 134 135
            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")
136 137 138 139 140

        self.assertRaises(ValueError, run_2)

        # ValueError: padding
        def run_3():
141 142 143 144 145 146 147 148 149
            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")
150 151 152 153

        self.assertRaises(ValueError, run_3)

        def run_4():
154 155 156 157 158 159 160 161 162
            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")
163 164 165 166

        self.assertRaises(ValueError, run_4)

        def run_5():
167 168 169 170 171 172 173 174 175
            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")
176 177 178 179

        self.assertRaises(ValueError, run_5)

        # ValueError: channel dimmention
180 181 182 183
        x = fluid.layers.data(name="x",
                              shape=[2, 5, 5, -1],
                              append_batch_size=False,
                              dtype="float32")
184 185

        def run_6():
186 187 188 189 190 191 192 193 194
            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")
195 196 197 198 199

        self.assertRaises(ValueError, run_6)

        # ValueError: groups
        def run_7():
200 201 202 203 204 205 206 207 208
            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")
209 210 211 212 213

        self.assertRaises(ValueError, run_7)

        # ValueError: filter num
        def run_8():
214 215 216 217 218 219 220 221 222
            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")
223 224 225 226 227

        self.assertRaises(ValueError, run_8)

        # ValueError: groups
        def run_9():
228 229 230 231 232 233 234 235 236
            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")
237 238 239

        self.assertRaises(ValueError, run_9)

240
        # ValueError: stride
241
        def run_10():
242 243 244 245 246 247 248 249 250
            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")
251 252 253 254

        self.assertRaises(ValueError, run_10)

    def test_api_with_error_input(self):
255 256 257 258
        input = fluid.layers.data(name="error_input",
                                  shape=[1],
                                  append_batch_size=False,
                                  dtype="float32")
259 260 261

        # ValueError: cudnn
        def run_1():
262 263 264 265 266 267 268 269 270
            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")
271 272 273 274 275 276 277 278 279

        self.assertRaises(ValueError, run_1)


# --------- test environment variable ------
@unittest.skipIf(
    not (core.is_compiled_with_cuda() or core.is_compiled_with_rocm()),
    "core is not compiled with CUDA or ROCM")
class TestConv2DEnviron(unittest.TestCase):
280

281 282
    def run1(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
283 284 285 286 287 288 289 290 291 292 293 294
            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")
295 296 297 298 299 300 301 302 303
            exe = fluid.Executor(place)
            exe.run(fluid.default_startup_program())
            fetches = exe.run(fluid.default_main_program(),
                              feed={"inputs": self.input_np},
                              fetch_list=[result])

    def run2(self, place):
        with fluid.dygraph.guard(place):
            inputs = fluid.dygraph.to_variable(self.input_np)
304 305 306 307
            conv = paddle.nn.Conv2D(in_channels=3,
                                    out_channels=4,
                                    kernel_size=(3, 3),
                                    data_format="NCHW")
308 309 310 311 312 313 314 315
            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,
316 317
                filter_size=(3, 3),
            )
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
            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()