test_clip_op.py 5.7 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
W
wanghaoshuang 已提交
23 24


25
class TestClipOp(OpTest):
W
wanghaoshuang 已提交
26
    def setUp(self):
27
        self.max_relative_error = 0.006
28 29

        self.inputs = {}
30
        self.initTestCase()
31

32
        self.op_type = "clip"
W
wanghaoshuang 已提交
33
        self.attrs = {}
34 35
        self.attrs['min'] = self.min
        self.attrs['max'] = self.max
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        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']

        input = np.random.random(self.shape).astype("float32")
        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 已提交
51

52 53
    def test_check_output(self):
        self.check_output()
W
wanghaoshuang 已提交
54

55
    def test_check_grad_normal(self):
56
        self.check_grad(['X'], 'Out')
57 58

    def initTestCase(self):
59 60 61 62 63
        self.shape = (4, 10, 10)
        self.max = 0.8
        self.min = 0.3
        self.inputs['Max'] = np.array([0.8]).astype('float32')
        self.inputs['Min'] = np.array([0.1]).astype('float32')
64 65 66 67 68 69


class TestCase1(TestClipOp):
    def initTestCase(self):
        self.shape = (8, 16, 8)
        self.max = 0.7
Y
Yang Yang(Tony) 已提交
70
        self.min = 0.0
71 72 73 74 75


class TestCase2(TestClipOp):
    def initTestCase(self):
        self.shape = (8, 16)
Y
Yang Yang(Tony) 已提交
76 77
        self.max = 1.0
        self.min = 0.0
78

W
wanghaoshuang 已提交
79

80 81 82 83 84
class TestCase3(TestClipOp):
    def initTestCase(self):
        self.shape = (4, 8, 16)
        self.max = 0.7
        self.min = 0.2
W
wanghaoshuang 已提交
85 86


87 88 89 90 91 92 93 94 95
class TestCase4(TestClipOp):
    def initTestCase(self):
        self.shape = (4, 8, 8)
        self.max = 0.7
        self.min = 0.2
        self.inputs['Max'] = np.array([0.8]).astype('float32')
        self.inputs['Min'] = np.array([0.3]).astype('float32')


96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
class TestClipOpError(unittest.TestCase):
    def test_errors(self):
        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)


Y
Yang Zhang 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
class TestClipAPI(unittest.TestCase):
    def test_clip(self):
        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)

        out_1 = paddle.clip(images, min=min, max=max)
        out_2 = paddle.clip(images, min=0.2, max=0.9)
        out_3 = paddle.clip(images, min=0.3)
        out_4 = paddle.clip(images, max=0.7)
        out_5 = paddle.clip(images, min=min)
        out_6 = paddle.clip(images, max=max)

        res1, res2, res3, res4, res5, res6 = exe.run(
            fluid.default_main_program(),
            feed={
                "image": data,
                "min": np.array([0.2]).astype('float32'),
                "max": np.array([0.8]).astype('float32')
            },
            fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6])

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

    def test_clip_dygraph(self):
        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')
        images = paddle.to_variable(data, dtype='float32')

        out_1 = paddle.clip(images, min=0.2, max=0.8)
        out_2 = paddle.clip(images, min=0.2, max=0.9)

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

    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")
        x3 = fluid.data(name='x3', shape=[1], dtype="float32")
        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)
        self.assertRaises(Exception, paddle.clip, x=x3)


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