test_affine_channel_op.py 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#   Copyright (c) 2018 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.
14 15 16
"""
Unit testing for affine_channel_op
"""
17 18 19 20 21 22 23

from __future__ import print_function

import unittest
import numpy as np
from op_test import OpTest
import paddle.fluid.core as core
24
import paddle.fluid as fluid
25 26 27 28 29 30 31 32 33 34 35 36 37 38


def affine_channel(x, scale, bias, layout):
    C = x.shape[1] if layout == 'NCHW' else x.shape[-1]
    if len(x.shape) == 4:
        new_shape = (1, C, 1, 1) if layout == 'NCHW' else (1, 1, 1, C)
    else:
        new_shape = (1, C)
    scale = scale.reshape(new_shape)
    bias = bias.reshape(new_shape)
    return x * scale + bias


class TestAffineChannelOp(OpTest):
39

40 41 42 43
    def setUp(self):
        self.op_type = "affine_channel"
        self.init_test_case()

44 45 46
        x = np.random.random(self.shape).astype("float64")
        scale = np.random.random(self.C).astype("float64")
        bias = np.random.random(self.C).astype("float64")
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

        y = affine_channel(x, scale, bias, self.layout)

        self.inputs = {'X': x, 'Scale': scale, 'Bias': bias}
        self.attrs = {'data_layout': self.layout}
        self.outputs = {'Out': y}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X', 'Scale', 'Bias'], 'Out')

    def test_check_grad_stopgrad_dx(self):
        self.check_grad(['Scale', 'Bias'], 'Out', no_grad_set=set('X'))

    def test_check_grad_stopgrad_dscale_dbias(self):
        self.check_grad(['X'], 'Out', no_grad_set=set(['Scale', 'Bias']))

    def init_test_case(self):
67
        self.shape = [2, 100, 3, 3]
Z
zhupengyang 已提交
68
        self.C = 100
69 70 71
        self.layout = 'NCHW'


72
class TestAffineChannelOpError(unittest.TestCase):
73

74 75 76 77 78 79 80 81 82 83
    def test_errors(self):
        with fluid.program_guard(fluid.Program()):

            def test_x_type():
                input_data = np.random.random(2, 1, 2, 2).astype("float32")
                fluid.layers.affine_channel(input_data)

            self.assertRaises(TypeError, test_x_type)

            def test_x_dtype():
84 85 86
                x2 = fluid.layers.data(name='x2',
                                       shape=[None, 1, 2, 2],
                                       dtype='int32')
87 88 89 90 91
                fluid.layers.affine_channel(x2)

            self.assertRaises(TypeError, test_x_dtype)

            def test_scale_type():
92 93 94
                x3 = fluid.layers.data(name='x3',
                                       shape=[None, 1, 2, 2],
                                       dtype='float32')
95 96 97 98 99
                fluid.layers.affine_channel(x3, scale=1)

            self.assertRaises(TypeError, test_scale_type)

            def test_bias_type():
100 101 102
                x4 = fluid.layers.data(name='x4',
                                       shape=[None, 1, 2, 2],
                                       dtype='float32')
103 104 105 106 107
                fluid.layers.affine_channel(x4, bias=1)

            self.assertRaises(TypeError, test_bias_type)


108
class TestAffineChannelNHWC(TestAffineChannelOp):
109

110
    def init_test_case(self):
111
        self.shape = [2, 3, 3, 100]
Z
zhupengyang 已提交
112
        self.C = 100
113 114
        self.layout = 'NHWC'

Q
qingqing01 已提交
115 116 117 118 119 120
    def test_check_grad_stopgrad_dx(self):
        return

    def test_check_grad_stopgrad_dscale_dbias(self):
        return

121 122

class TestAffineChannel2D(TestAffineChannelOp):
123

124
    def init_test_case(self):
125
        self.shape = [2, 100]
Z
zhupengyang 已提交
126
        self.C = 100
127 128
        self.layout = 'NCHW'

Q
qingqing01 已提交
129 130 131 132 133 134
    def test_check_grad_stopgrad_dx(self):
        return

    def test_check_grad_stopgrad_dscale_dbias(self):
        return

135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
# TODO(qingqing): disable unit testing for large shape
#class TestAffineChannelNCHWLargeShape(TestAffineChannelOp):
#    def init_test_case(self):
#        self.shape = [4, 128, 112, 112]
#        self.C = 128
#        self.layout = 'NCHW'
#
#    # since the gradient check is very slow in large shape, so skip check_grad
#    def test_check_grad(self):
#        pass
#
#    def test_check_grad_stopgrad_dx(self):
#        pass
#
#    def test_check_grad_stopgrad_dscale_dbias(self):
#        pass

#class TestAffineChannelNHWCLargeShape(TestAffineChannelNCHWLargeShape):
#    def init_test_case(self):
#        self.shape = [64, 32, 32, 128]
#        self.C = 128
#        self.layout = 'NHWC'
158 159 160

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