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

from __future__ import print_function

import unittest
import numpy as np

import paddle
21

22 23 24 25 26 27 28 29
paddle.enable_static()
import paddle.fluid.core as core
import paddle.fluid as fluid
from op_test import OpTest
from paddle.fluid import Program, program_guard


class TestConv2DAPI(unittest.TestCase):
30

31 32
    def test_api(self):

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
        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")
96 97 98

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


class TestConv2DAPI_Error(unittest.TestCase):
108

109
    def test_api(self):
110 111 112 113
        input = fluid.layers.data(name="input",
                                  shape=[2, 5, 5, 5],
                                  append_batch_size=False,
                                  dtype="float32")
114 115 116

        # ValueError: cudnn
        def run_1():
117 118 119 120 121 122 123 124 125
            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")
126 127 128 129 130

        self.assertRaises(ValueError, run_1)

        # ValueError: data_format
        def run_2():
131 132 133 134 135 136 137 138 139
            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")
140 141 142 143 144

        self.assertRaises(ValueError, run_2)

        # ValueError: padding
        def run_3():
145 146 147 148 149 150 151 152 153
            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")
154 155 156 157

        self.assertRaises(ValueError, run_3)

        def run_4():
158 159 160 161 162 163 164 165 166
            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")
167 168 169 170

        self.assertRaises(ValueError, run_4)

        def run_5():
171 172 173 174 175 176 177 178 179
            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")
180 181 182 183

        self.assertRaises(ValueError, run_5)

        # ValueError: channel dimmention
184 185 186 187
        x = fluid.layers.data(name="x",
                              shape=[2, 5, 5, -1],
                              append_batch_size=False,
                              dtype="float32")
188 189

        def run_6():
190 191 192 193 194 195 196 197 198
            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")
199 200 201 202 203

        self.assertRaises(ValueError, run_6)

        # ValueError: groups
        def run_7():
204 205 206 207 208 209 210 211 212
            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")
213 214 215 216 217

        self.assertRaises(ValueError, run_7)

        # ValueError: filter num
        def run_8():
218 219 220 221 222 223 224 225 226
            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")
227 228 229 230 231

        self.assertRaises(ValueError, run_8)

        # ValueError: groups
        def run_9():
232 233 234 235 236 237 238 239 240
            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")
241 242 243

        self.assertRaises(ValueError, run_9)

244
        # ValueError: stride
245
        def run_10():
246 247 248 249 250 251 252 253 254
            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")
255 256 257 258

        self.assertRaises(ValueError, run_10)

    def test_api_with_error_input(self):
259 260 261 262
        input = fluid.layers.data(name="error_input",
                                  shape=[1],
                                  append_batch_size=False,
                                  dtype="float32")
263 264 265

        # ValueError: cudnn
        def run_1():
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 278 279 280 281 282 283

        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):
284

285 286
    def run1(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
287 288 289 290 291 292 293 294 295 296 297 298
            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")
299 300 301 302 303 304 305 306 307
            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)
308 309 310 311
            conv = paddle.nn.Conv2D(in_channels=3,
                                    out_channels=4,
                                    kernel_size=(3, 3),
                                    data_format="NCHW")
312 313 314 315 316 317 318 319
            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,
320 321
                filter_size=(3, 3),
            )
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            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()