test_clip_op.py 9.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

W
wanghaoshuang 已提交
17 18
import unittest
import numpy as np
Y
Yang Zhang 已提交
19
import paddle
20 21
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
22
from op_test import OpTest
C
chentianyu03 已提交
23
from paddle.fluid.framework import _test_eager_guard
W
wanghaoshuang 已提交
24 25


26
class TestClipOp(OpTest):
W
wanghaoshuang 已提交
27
    def setUp(self):
28
        self.max_relative_error = 0.006
C
chentianyu03 已提交
29
        self.python_api = paddle.clip
30 31

        self.inputs = {}
32
        self.initTestCase()
33

34
        self.op_type = "clip"
W
wanghaoshuang 已提交
35
        self.attrs = {}
36 37
        self.attrs['min'] = self.min
        self.attrs['max'] = self.max
38 39 40 41 42 43 44 45 46 47
        if 'Min' in self.inputs:
            min_v = self.inputs['Min']
        else:
            min_v = self.attrs['min']

        if 'Max' in self.inputs:
            max_v = self.inputs['Max']
        else:
            max_v = self.attrs['max']

Z
zhangbo9674 已提交
48
        input = np.random.random(self.shape).astype(self.dtype)
49 50 51 52
        input[np.abs(input - min_v) < self.max_relative_error] = 0.5
        input[np.abs(input - max_v) < self.max_relative_error] = 0.5
        self.inputs['X'] = input
        self.outputs = {'Out': np.clip(self.inputs['X'], min_v, max_v)}
W
wanghaoshuang 已提交
53

54
    def test_check_output(self):
55
        paddle.enable_static()
C
chentianyu03 已提交
56
        self.check_output(check_eager=True)
57
        paddle.disable_static()
W
wanghaoshuang 已提交
58

59
    def test_check_grad_normal(self):
60
        paddle.enable_static()
C
chentianyu03 已提交
61
        self.check_grad(['X'], 'Out', check_eager=True)
62
        paddle.disable_static()
63 64

    def initTestCase(self):
Z
zhangbo9674 已提交
65
        self.dtype = np.float32
66 67 68
        self.shape = (4, 10, 10)
        self.max = 0.8
        self.min = 0.3
Z
zhangbo9674 已提交
69 70
        self.inputs['Max'] = np.array([0.8]).astype(self.dtype)
        self.inputs['Min'] = np.array([0.1]).astype(self.dtype)
71 72 73 74


class TestCase1(TestClipOp):
    def initTestCase(self):
Z
zhangbo9674 已提交
75
        self.dtype = np.float32
76 77
        self.shape = (8, 16, 8)
        self.max = 0.7
Y
Yang Yang(Tony) 已提交
78
        self.min = 0.0
79 80 81 82


class TestCase2(TestClipOp):
    def initTestCase(self):
Z
zhangbo9674 已提交
83
        self.dtype = np.float32
84
        self.shape = (8, 16)
Y
Yang Yang(Tony) 已提交
85 86
        self.max = 1.0
        self.min = 0.0
87

W
wanghaoshuang 已提交
88

89 90
class TestCase3(TestClipOp):
    def initTestCase(self):
Z
zhangbo9674 已提交
91
        self.dtype = np.float32
92 93 94
        self.shape = (4, 8, 16)
        self.max = 0.7
        self.min = 0.2
W
wanghaoshuang 已提交
95 96


97 98
class TestCase4(TestClipOp):
    def initTestCase(self):
Z
zhangbo9674 已提交
99
        self.dtype = np.float32
100 101 102
        self.shape = (4, 8, 8)
        self.max = 0.7
        self.min = 0.2
Z
zhangbo9674 已提交
103 104
        self.inputs['Max'] = np.array([0.8]).astype(self.dtype)
        self.inputs['Min'] = np.array([0.3]).astype(self.dtype)
105 106


Y
Yang Zhang 已提交
107 108
class TestCase5(TestClipOp):
    def initTestCase(self):
Z
zhangbo9674 已提交
109
        self.dtype = np.float32
Y
Yang Zhang 已提交
110 111 112 113 114
        self.shape = (4, 8, 16)
        self.max = 0.5
        self.min = 0.5


Z
zhangbo9674 已提交
115 116 117 118 119 120 121 122 123 124
class TestCase6(TestClipOp):
    def initTestCase(self):
        self.dtype == np.float16
        self.shape = (4, 8, 8)
        self.max = 0.7
        self.min = 0.2
        self.inputs['Max'] = np.array([0.8]).astype(self.dtype)
        self.inputs['Min'] = np.array([0.3]).astype(self.dtype)


125 126
class TestClipOpError(unittest.TestCase):
    def test_errors(self):
127
        paddle.enable_static()
128 129 130 131 132 133 134 135 136 137 138 139 140
        with program_guard(Program(), Program()):
            input_data = np.random.random((2, 4)).astype("float32")

            def test_Variable():
                fluid.layers.clip(x=input_data, min=-1.0, max=1.0)

            self.assertRaises(TypeError, test_Variable)

            def test_dtype():
                x2 = fluid.layers.data(name='x2', shape=[1], dtype='int32')
                fluid.layers.clip(x=x2, min=-1.0, max=1.0)

            self.assertRaises(TypeError, test_dtype)
141
        paddle.disable_static()
142 143


Y
Yang Zhang 已提交
144
class TestClipAPI(unittest.TestCase):
145 146 147
    def _executed_api(self, x, min=None, max=None):
        return paddle.clip(x, min, max)

Y
Yang Zhang 已提交
148
    def test_clip(self):
Y
Yang Zhang 已提交
149
        paddle.enable_static()
Y
Yang Zhang 已提交
150 151 152 153 154 155 156 157 158 159
        data_shape = [1, 9, 9, 4]
        data = np.random.random(data_shape).astype('float32')
        images = fluid.data(name='image', shape=data_shape, dtype='float32')
        min = fluid.data(name='min', shape=[1], dtype='float32')
        max = fluid.data(name='max', shape=[1], dtype='float32')

        place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
        ) else fluid.CPUPlace()
        exe = fluid.Executor(place)

160 161 162 163 164 165 166 167 168 169 170 171 172 173
        out_1 = self._executed_api(images, min=min, max=max)
        out_2 = self._executed_api(images, min=0.2, max=0.9)
        out_3 = self._executed_api(images, min=0.3)
        out_4 = self._executed_api(images, max=0.7)
        out_5 = self._executed_api(images, min=min)
        out_6 = self._executed_api(images, max=max)
        out_7 = self._executed_api(images, max=-1.)
        out_8 = self._executed_api(images)
        out_9 = self._executed_api(
            paddle.cast(images, 'float64'), min=0.2, max=0.9)
        out_10 = self._executed_api(
            paddle.cast(images * 10, 'int32'), min=2, max=8)
        out_11 = self._executed_api(
            paddle.cast(images * 10, 'int64'), min=2, max=8)
174 175

        res1, res2, res3, res4, res5, res6, res7, res8, res9, res10, res11 = exe.run(
Y
Yang Zhang 已提交
176 177 178 179 180 181
            fluid.default_main_program(),
            feed={
                "image": data,
                "min": np.array([0.2]).astype('float32'),
                "max": np.array([0.8]).astype('float32')
            },
Y
Yang Zhang 已提交
182
            fetch_list=[
183 184
                out_1, out_2, out_3, out_4, out_5, out_6, out_7, out_8, out_9,
                out_10, out_11
Y
Yang Zhang 已提交
185
            ])
Y
Yang Zhang 已提交
186 187 188 189 190 191 192

        self.assertTrue(np.allclose(res1, data.clip(0.2, 0.8)))
        self.assertTrue(np.allclose(res2, data.clip(0.2, 0.9)))
        self.assertTrue(np.allclose(res3, data.clip(min=0.3)))
        self.assertTrue(np.allclose(res4, data.clip(max=0.7)))
        self.assertTrue(np.allclose(res5, data.clip(min=0.2)))
        self.assertTrue(np.allclose(res6, data.clip(max=0.8)))
Y
Yang Zhang 已提交
193 194
        self.assertTrue(np.allclose(res7, data.clip(max=-1)))
        self.assertTrue(np.allclose(res8, data))
Y
Yang Zhang 已提交
195 196
        self.assertTrue(
            np.allclose(res9, data.astype(np.float64).clip(0.2, 0.9)))
197 198 199 200 201
        self.assertTrue(
            np.allclose(res10, (data * 10).astype(np.int32).clip(2, 8)))
        self.assertTrue(
            np.allclose(res11, (data * 10).astype(np.int64).clip(2, 8)))
        paddle.disable_static()
Y
Yang Zhang 已提交
202

203
    def func_clip_dygraph(self):
204
        paddle.disable_static()
Y
Yang Zhang 已提交
205 206 207 208 209
        place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
        ) else fluid.CPUPlace()
        paddle.disable_static(place)
        data_shape = [1, 9, 9, 4]
        data = np.random.random(data_shape).astype('float32')
Z
Zhou Wei 已提交
210 211 212
        images = paddle.to_tensor(data, dtype='float32')
        v_min = paddle.to_tensor(np.array([0.2], dtype=np.float32))
        v_max = paddle.to_tensor(np.array([0.8], dtype=np.float32))
Y
Yang Zhang 已提交
213

214 215 216 217 218
        out_1 = self._executed_api(images, min=0.2, max=0.8)
        images = paddle.to_tensor(data, dtype='float32')
        out_2 = self._executed_api(images, min=0.2, max=0.9)
        images = paddle.to_tensor(data, dtype='float32')
        out_3 = self._executed_api(images, min=v_min, max=v_max)
Y
Yang Zhang 已提交
219

220 221 222 223
        out_4 = self._executed_api(
            paddle.cast(images * 10, 'int32'), min=2, max=8)
        out_5 = self._executed_api(
            paddle.cast(images * 10, 'int64'), min=2, max=8)
224 225
        # test with numpy.generic
        out_6 = self._executed_api(images, min=np.abs(0.2), max=np.abs(0.8))
226

Y
Yang Zhang 已提交
227 228
        self.assertTrue(np.allclose(out_1.numpy(), data.clip(0.2, 0.8)))
        self.assertTrue(np.allclose(out_2.numpy(), data.clip(0.2, 0.9)))
229
        self.assertTrue(np.allclose(out_3.numpy(), data.clip(0.2, 0.8)))
230 231 232 233
        self.assertTrue(
            np.allclose(out_4.numpy(), (data * 10).astype(np.int32).clip(2, 8)))
        self.assertTrue(
            np.allclose(out_5.numpy(), (data * 10).astype(np.int64).clip(2, 8)))
234
        self.assertTrue(np.allclose(out_6.numpy(), data.clip(0.2, 0.8)))
Y
Yang Zhang 已提交
235

236 237 238 239 240 241 242
    def test_clip_dygraph(self):
        with _test_eager_guard():
            self.func_clip_dygraph()
        self.func_clip_dygraph()

    def test_clip_dygraph_default_max(self):
        paddle.disable_static()
C
chentianyu03 已提交
243
        with _test_eager_guard():
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
            x_int32 = paddle.to_tensor([1, 2, 3], dtype="int32")
            x_int64 = paddle.to_tensor([1, 2, 3], dtype="int64")
            x_f32 = paddle.to_tensor([1, 2, 3], dtype="float32")
            egr_out1 = paddle.clip(x_int32, min=1)
            egr_out2 = paddle.clip(x_int64, min=1)
            egr_out3 = paddle.clip(x_f32, min=1)
        x_int32 = paddle.to_tensor([1, 2, 3], dtype="int32")
        x_int64 = paddle.to_tensor([1, 2, 3], dtype="int64")
        x_f32 = paddle.to_tensor([1, 2, 3], dtype="float32")
        out1 = paddle.clip(x_int32, min=1)
        out2 = paddle.clip(x_int64, min=1)
        out3 = paddle.clip(x_f32, min=1)
        self.assertTrue(np.allclose(out1.numpy(), egr_out1.numpy()))
        self.assertTrue(np.allclose(out2.numpy(), egr_out2.numpy()))
        self.assertTrue(np.allclose(out3.numpy(), egr_out3.numpy()))
C
chentianyu03 已提交
259

Y
Yang Zhang 已提交
260 261 262 263 264 265
    def test_errors(self):
        paddle.enable_static()
        x1 = fluid.data(name='x1', shape=[1], dtype="int16")
        x2 = fluid.data(name='x2', shape=[1], dtype="int8")
        self.assertRaises(TypeError, paddle.clip, x=x1, min=0.2, max=0.8)
        self.assertRaises(TypeError, paddle.clip, x=x2, min=0.2, max=0.8)
266
        paddle.disable_static()
Y
Yang Zhang 已提交
267 268


269 270 271 272 273
class TestInplaceClipAPI(TestClipAPI):
    def _executed_api(self, x, min=None, max=None):
        return x.clip_(min, max)


W
wanghaoshuang 已提交
274 275
if __name__ == '__main__':
    unittest.main()