test_random_crop_op.py 2.6 KB
Newer Older
F
stash  
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

F
stash  
fengjiayi 已提交
17 18 19
import unittest
import numpy as np
import paddle.fluid.core as core
20
from op_test import OpTest
21
import paddle.fluid as fluid
F
stash  
fengjiayi 已提交
22 23 24 25


class TestRandomCropOp(OpTest):
    def setUp(self):
F
fengjiayi 已提交
26
        to_crop = np.array([[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]] *
M
minqiyang 已提交
27
                           5).astype(np.int32)
F
fengjiayi 已提交
28
        self.possible_res = [
29 30 31 32
            np.array([[1, 2, 3], [5, 6, 7]]).astype(np.int32),
            np.array([[2, 3, 4], [6, 7, 8]]).astype(np.int32),
            np.array([[5, 6, 7], [9, 10, 11]]).astype(np.int32),
            np.array([[6, 7, 8], [10, 11, 12]]).astype(np.int32)
F
fengjiayi 已提交
33
        ]
F
stash  
fengjiayi 已提交
34
        self.op_type = "random_crop"
W
wopeizl 已提交
35
        self.inputs = {'X': to_crop, 'Seed': np.array([10]).astype('int64')}
F
fengjiayi 已提交
36 37
        self.outputs = {'Out': np.array([]), 'SeedOut': np.array([])}
        self.attrs = {'shape': [2, 3]}
F
stash  
fengjiayi 已提交
38 39

    def test_check_output(self):
F
fengjiayi 已提交
40 41 42 43 44 45 46
        self.check_output_customized(self.verify_output)

    def verify_output(self, outs):
        out = np.array(outs[1])
        for ins in out[:]:
            is_equal = [(ins == res).all() for res in self.possible_res]
            self.assertIn(True, is_equal)
F
stash  
fengjiayi 已提交
47 48


49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
class TestRandomCropOpError(unittest.TestCase):
    def test_errors(self):
        with fluid.program_guard(fluid.Program()):

            def test_x_type():
                input_data = np.random.random(2, 3, 256, 256).astype("float32")
                fluid.layers.random_crop(input_data)

            self.assertRaises(TypeError, test_x_type)

            def test_x_dtype():
                x2 = fluid.layers.data(
                    name='x2', shape=[None, 3, 256, 256], dtype='float16')
                fluid.layers.random_crop(x2)

            self.assertRaises(TypeError, test_x_dtype)

            def test_shape_type():
                x3 = fluid.layers.data(
                    name='x3', shape=[None, 3, 256, 256], dtype='float32')
                fluid.layers.random_crop(x3, shape=1)

            self.assertRaises(TypeError, test_shape_type)


F
stash  
fengjiayi 已提交
74 75
if __name__ == "__main__":
    unittest.main()