test_conv3d_transpose_op.py 6.6 KB
Newer Older
C
chengduoZH 已提交
1 2
import unittest
import numpy as np
3 4

import paddle.v2.fluid.core as core
C
chengduoZH 已提交
5 6 7
from op_test import OpTest


C
chengduoZH 已提交
8
def conv3dtranspose_forward_naive(input_, filter_, attrs):
C
chengduoZH 已提交
9 10 11 12
    in_n, in_c, in_d, in_h, in_w = input_.shape
    f_c, out_c, f_d, f_h, f_w = filter_.shape
    assert in_c == f_c

C
chengduoZH 已提交
13 14 15 16 17 18 19 20 21
    stride, pad, dilations = attrs['strides'], attrs['paddings'], attrs[
        'dilations']

    d_bolck_d = dilations[0] * (f_d - 1) + 1
    d_bolck_h = dilations[1] * (f_h - 1) + 1
    d_bolck_w = dilations[2] * (f_w - 1) + 1
    out_d = (in_d - 1) * stride[0] + d_bolck_d
    out_h = (in_h - 1) * stride[1] + d_bolck_h
    out_w = (in_w - 1) * stride[2] + d_bolck_w
C
chengduoZH 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34
    out = np.zeros((in_n, out_c, out_d, out_h, out_w))

    for n in range(in_n):
        for d in range(in_d):
            for i in range(in_h):
                for j in range(in_w):
                    input_masked = input_[n, :, d, i, j]  # (c)
                    input_masked = np.reshape(input_masked, (in_c, 1, 1, 1))
                    input_masked = np.tile(input_masked, (1, f_d, f_h, f_w))

                    for k in range(out_c):
                        tmp_out = np.sum(input_masked * filter_[:, k, :, :, :],
                                         axis=0)
C
chengduoZH 已提交
35 36 37 38 39
                        d1, d2 = d * stride[0], d * stride[0] + d_bolck_d
                        i1, i2 = i * stride[1], i * stride[1] + d_bolck_h
                        j1, j2 = j * stride[2], j * stride[2] + d_bolck_w
                        out[n, k, d1:d2:dilations[0], i1:i2:dilations[1], j1:j2:
                            dilations[2]] += tmp_out
C
chengduoZH 已提交
40

C
chengduoZH 已提交
41 42
    out = out[:, :, pad[0]:out_d - pad[0], pad[1]:out_h - pad[1], pad[2]:out_w -
              pad[2]]
C
chengduoZH 已提交
43 44 45 46 47 48
    return out


class TestConv3dTransposeOp(OpTest):
    def setUp(self):
        # init as conv transpose
49
        self.use_cudnn = False
C
chengduoZH 已提交
50 51 52 53 54 55 56 57 58 59
        self.init_op_type()
        self.init_test_case()

        input_ = np.random.random(self.input_size).astype("float32")
        filter_ = np.random.random(self.filter_size).astype("float32")

        self.inputs = {'Input': input_, 'Filter': filter_}
        self.attrs = {
            'strides': self.stride,
            'paddings': self.pad,
60 61 62
            'dilations': self.dilations,
            'use_cudnn': self.use_cudnn,
            'data_format': 'AnyLayout'  # TODO(dzhwinter) : should be fix latter
C
chengduoZH 已提交
63
        }
C
chengduoZH 已提交
64 65 66 67

        output = conv3dtranspose_forward_naive(input_, filter_,
                                               self.attrs).astype("float32")

C
chengduoZH 已提交
68 69 70
        self.outputs = {'Output': output}

    def test_check_output(self):
71 72 73 74 75
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)
        else:
            self.check_output()
C
chengduoZH 已提交
76 77

    def test_check_grad(self):
78 79 80 81 82 83 84 85 86 87
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place,
                set(['Input', 'Filter']),
                'Output',
                max_relative_error=0.03)
        else:
            self.check_grad(
                set(['Input', 'Filter']), 'Output', max_relative_error=0.03)
C
chengduoZH 已提交
88 89

    def test_check_grad_no_filter(self):
90 91 92 93 94 95 96 97 98 99 100 101 102
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place, ['Input'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Filter']))
        else:
            self.check_grad(
                ['Input'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Filter']))
C
chengduoZH 已提交
103 104

    def test_check_grad_no_input(self):
105 106 107 108 109 110 111 112 113 114 115 116 117
        if self.use_cudnn:
            place = core.CUDAPlace(0)
            self.check_grad_with_place(
                place, ['Filter'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Input']))
        else:
            self.check_grad(
                ['Filter'],
                'Output',
                max_relative_error=0.03,
                no_grad_set=set(['Input']))
C
chengduoZH 已提交
118 119 120 121 122

    def init_test_case(self):
        self.pad = [0, 0, 0]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
C
chengduoZH 已提交
123
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
C
chengduoZH 已提交
124 125 126 127
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

    def init_op_type(self):
C
chengduoZH 已提交
128
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
129 130


C
chengduoZH 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
class TestWithPad(TestConv3dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]


class TestWithStride(TestConv3dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [2, 2, 2]
        self.dilations = [1, 1, 1]
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]


C
chengduoZH 已提交
151 152 153 154 155 156 157 158 159 160
class TestWithDilation(TestConv3dTransposeOp):
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [2, 2, 2]
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]


C
chengduoZH 已提交
161
# ------------ test_cudnn ------------
162
class TestCUDNN(TestConv3dTransposeOp):
C
chengduoZH 已提交
163
    def init_op_type(self):
164 165
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
166 167


168
class TestCUDNNWithPad(TestWithPad):
C
chengduoZH 已提交
169 170 171 172 173 174 175 176 177
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [1, 1, 1]
        self.dilations = [1, 1, 1]
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

    def init_op_type(self):
178 179
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
180 181


182
class TestCUDNNWithStride(TestWithStride):
C
chengduoZH 已提交
183 184 185 186 187 188 189 190 191
    def init_test_case(self):
        self.pad = [1, 1, 1]
        self.stride = [2, 2, 2]
        self.dilations = [1, 1, 1]
        self.input_size = [2, 3, 5, 5, 5]  # NCDHW
        f_c = self.input_size[1]
        self.filter_size = [f_c, 6, 3, 3, 3]

    def init_op_type(self):
192 193
        self.use_cudnn = True
        self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
194 195 196


# #cudnn v5 does not support dilation conv.
197
# class TestCUDNNWithDilation(TestWithDilation):
C
chengduoZH 已提交
198 199 200 201 202 203 204 205 206
#     def init_test_case(self):
#         self.pad = [1, 1, 1]
#         self.stride = [2, 2, 2]
#         self.dilations = [2, 2, 2]
#         self.input_size = [2, 3, 5, 5, 5]  # NCDHW
#         f_c = self.input_size[1]
#         self.filter_size = [f_c, 6, 3, 3, 3]
#
#     def init_op_type(self):
207
#         self.op_type = "conv3d_transpose"
C
chengduoZH 已提交
208

C
chengduoZH 已提交
209 210
if __name__ == '__main__':
    unittest.main()