test_conv3d_layer.py 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2020 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 numpy as np
from paddle import fluid, nn
import paddle.fluid.dygraph as dg
import paddle.nn.functional as F
import paddle.fluid.initializer as I
20 21
import paddle
from paddle.fluid.framework import _test_eager_guard
22 23 24 25
import unittest


class Conv3DTestCase(unittest.TestCase):
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    def __init__(
        self,
        methodName='runTest',
        batch_size=4,
        spartial_shape=(8, 8, 8),
        num_channels=6,
        num_filters=8,
        filter_size=3,
        padding=0,
        stride=1,
        dilation=1,
        groups=1,
        no_bias=False,
        data_format="NCDHW",
        dtype="float32",
    ):
42
        super().__init__(methodName)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        self.batch_size = batch_size
        self.num_channels = num_channels
        self.num_filters = num_filters
        self.spartial_shape = spartial_shape
        self.filter_size = filter_size

        self.padding = padding
        self.stride = stride
        self.dilation = dilation
        self.groups = groups
        self.no_bias = no_bias
        self.data_format = data_format
        self.dtype = dtype

    def setUp(self):
        self.channel_last = self.data_format == "NDHWC"
        if self.channel_last:
60 61 62
            input_shape = (
                (self.batch_size,) + self.spartial_shape + (self.num_channels,)
            )
63
        else:
64 65 66 67
            input_shape = (
                self.batch_size,
                self.num_channels,
            ) + self.spartial_shape
68 69 70 71 72 73
        self.input = np.random.randn(*input_shape).astype(self.dtype)

        if isinstance(self.filter_size, int):
            filter_size = [self.filter_size] * 3
        else:
            filter_size = self.filter_size
74 75 76 77 78 79 80
        self.weight_shape = weight_shape = (
            self.num_filters,
            self.num_channels // self.groups,
        ) + tuple(filter_size)
        self.weight = np.random.uniform(-1, 1, size=weight_shape).astype(
            self.dtype
        )
81 82
        if not self.no_bias:
            self.bias = np.random.uniform(
83 84
                -1, 1, size=(self.num_filters,)
            ).astype(self.dtype)
85 86 87 88 89 90 91 92
        else:
            self.bias = None

    def fluid_layer(self, place):
        main = fluid.Program()
        start = fluid.Program()
        with fluid.unique_name.guard():
            with fluid.program_guard(main, start):
93 94 95 96 97
                input_shape = (
                    (-1, -1, -1, -1, self.num_channels)
                    if self.channel_last
                    else (-1, self.num_channels, -1, -1, -1)
                )
98 99 100 101 102 103
                x_var = fluid.data("input", input_shape, dtype=self.dtype)
                weight_attr = I.NumpyArrayInitializer(self.weight)
                if self.bias is None:
                    bias_attr = False
                else:
                    bias_attr = I.NumpyArrayInitializer(self.bias)
104
                y_var = paddle.static.nn.conv3d(
105 106 107 108 109 110 111 112 113 114 115
                    x_var,
                    self.num_filters,
                    self.filter_size,
                    padding=self.padding,
                    stride=self.stride,
                    dilation=self.dilation,
                    groups=self.groups,
                    param_attr=weight_attr,
                    bias_attr=bias_attr,
                    data_format=self.data_format,
                )
116 117 118
        feed_dict = {"input": self.input}
        exe = fluid.Executor(place)
        exe.run(start)
119
        (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var])
120 121 122 123 124 125 126
        return y_np

    def functional(self, place):
        main = fluid.Program()
        start = fluid.Program()
        with fluid.unique_name.guard():
            with fluid.program_guard(main, start):
127 128 129 130 131
                input_shape = (
                    (-1, -1, -1, -1, self.num_channels)
                    if self.channel_last
                    else (-1, self.num_channels, -1, -1, -1)
                )
132
                x_var = fluid.data("input", input_shape, dtype=self.dtype)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                w_var = fluid.data(
                    "weight", self.weight_shape, dtype=self.dtype
                )
                b_var = fluid.data(
                    "bias", (self.num_filters,), dtype=self.dtype
                )
                y_var = F.conv3d(
                    x_var,
                    w_var,
                    None if self.no_bias else b_var,
                    padding=self.padding,
                    stride=self.stride,
                    dilation=self.dilation,
                    groups=self.groups,
                    data_format=self.data_format,
                )
149 150 151 152 153
        feed_dict = {"input": self.input, "weight": self.weight}
        if self.bias is not None:
            feed_dict["bias"] = self.bias
        exe = fluid.Executor(place)
        exe.run(start)
154
        (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var])
155 156 157
        return y_np

    def paddle_nn_layer(self):
158 159
        x_var = paddle.to_tensor(self.input)
        x_var.stop_gradient = False
160 161 162 163 164 165 166 167 168 169
        conv = nn.Conv3D(
            self.num_channels,
            self.num_filters,
            self.filter_size,
            padding=self.padding,
            stride=self.stride,
            dilation=self.dilation,
            groups=self.groups,
            data_format=self.data_format,
        )
170 171 172 173
        conv.weight.set_value(self.weight)
        if not self.no_bias:
            conv.bias.set_value(self.bias)
        y_var = conv(x_var)
174
        y_var.backward()
175
        y_np = y_var.numpy()
176 177
        t1 = x_var.gradient()
        return y_np, t1
178 179 180 181 182 183

    def _test_equivalence(self, place):
        place = fluid.CPUPlace()
        result1 = self.fluid_layer(place)
        result2 = self.functional(place)
        with dg.guard(place):
184 185 186
            result3, g1 = self.paddle_nn_layer()
            with _test_eager_guard():
                res_eager, g2 = self.paddle_nn_layer()
187 188
        np.testing.assert_array_almost_equal(result1, result2)
        np.testing.assert_array_almost_equal(result2, result3)
189 190
        np.testing.assert_allclose(result3, res_eager, rtol=1e-05)
        np.testing.assert_allclose(g1, g2, rtol=1e-05)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

    def runTest(self):
        place = fluid.CPUPlace()
        self._test_equivalence(place)

        if fluid.core.is_compiled_with_cuda():
            place = fluid.CUDAPlace(0)
            self._test_equivalence(place)


class Conv3DErrorTestCase(Conv3DTestCase):
    def runTest(self):
        place = fluid.CPUPlace()
        with dg.guard(place):
            with self.assertRaises(ValueError):
                self.paddle_nn_layer()


def add_cases(suite):
    suite.addTest(Conv3DTestCase(methodName='runTest'))
    suite.addTest(
212 213
        Conv3DTestCase(methodName='runTest', stride=[1, 2, 1], dilation=2)
    )
214
    suite.addTest(
215 216
        Conv3DTestCase(methodName='runTest', stride=2, dilation=(2, 1, 2))
    )
217
    suite.addTest(
218 219
        Conv3DTestCase(methodName='runTest', padding="same", no_bias=True)
    )
220
    suite.addTest(
221 222 223 224
        Conv3DTestCase(
            methodName='runTest', filter_size=(3, 2, 3), padding='valid'
        )
    )
225 226
    suite.addTest(Conv3DTestCase(methodName='runTest', padding=(2, 3, 1)))
    suite.addTest(
227 228
        Conv3DTestCase(methodName='runTest', padding=[1, 2, 2, 1, 2, 3])
    )
229
    suite.addTest(
230 231 232 233 234
        Conv3DTestCase(
            methodName='runTest',
            padding=[[0, 0], [0, 0], [1, 2], [2, 1], [2, 2]],
        )
    )
235 236
    suite.addTest(Conv3DTestCase(methodName='runTest', data_format="NDHWC"))
    suite.addTest(
237 238 239 240 241 242
        Conv3DTestCase(
            methodName='runTest',
            data_format="NDHWC",
            padding=[[0, 0], [1, 1], [3, 3], [2, 2], [0, 0]],
        )
    )
243
    suite.addTest(
244 245
        Conv3DTestCase(methodName='runTest', groups=2, padding="valid")
    )
246
    suite.addTest(
247 248 249 250 251 252 253 254
        Conv3DTestCase(
            methodName='runTest',
            num_filters=6,
            num_channels=3,
            groups=3,
            padding="valid",
        )
    )
255 256 257 258


def add_error_cases(suite):
    suite.addTest(
259 260
        Conv3DErrorTestCase(methodName='runTest', num_channels=5, groups=2)
    )
261
    suite.addTest(
262 263 264 265
        Conv3DErrorTestCase(
            methodName='runTest', num_channels=5, groups=2, padding=[-1, 1, 3]
        )
    )
266 267 268 269 270 271 272 273 274 275 276


def load_tests(loader, standard_tests, pattern):
    suite = unittest.TestSuite()
    add_cases(suite)
    add_error_cases(suite)
    return suite


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