test_operator.py 5.2 KB
Newer Older
X
xiaoting 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#   Copyright (c) 2019 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 17 18
import os
import unittest
import logging
import numpy as np
import set_env
19
import ppdet.data.transform as tf
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
logging.basicConfig(level=logging.INFO)


class TestBase(unittest.TestCase):
    """Test cases for dataset.transform.operator
    """

    @classmethod
    def setUpClass(cls, with_mixup=False):
        """ setup
        """
        roidb_fname = set_env.coco_data['TRAIN']['ANNO_FILE']
        image_dir = set_env.coco_data['TRAIN']['IMAGE_DIR']
        import pickle as pkl
        with open(roidb_fname, 'rb') as f:
            roidb = f.read()
            roidb = pkl.loads(roidb)
        fn = os.path.join(image_dir, roidb[0][0]['im_file'])
        with open(fn, 'rb') as f:
            roidb[0][0]['image'] = f.read()
        if with_mixup:
            mixup_fn = os.path.join(image_dir, roidb[0][1]['im_file'])
            roidb[0][0]['mixup'] = roidb[0][1]
            with open(fn, 'rb') as f:
                roidb[0][0]['mixup']['image'] = f.read()
        cls.sample = roidb[0][0]

    @classmethod
    def tearDownClass(cls):
        """ tearDownClass """
        pass

    def test_ops_all(self):
        """ test operators
        """
        # ResizeImage
        ops_conf = [{
            'op': 'DecodeImage'
        }, {
            'op': 'ResizeImage',
            'target_size': 300,
            'max_size': 1333
        }]
63
        mapper = tf.build_mapper(ops_conf)
64 65 66 67 68 69 70
        self.assertTrue(mapper is not None)
        data = self.sample.copy()
        result0 = mapper(data)
        self.assertIsNotNone(result0['image'])
        self.assertEqual(len(result0['image'].shape), 3)
        # RandFlipImage
        ops_conf = [{'op': 'RandomFlipImage'}]
71
        mapper = tf.build_mapper(ops_conf)
72 73 74 75 76 77
        self.assertTrue(mapper is not None)
        result1 = mapper(result0)
        self.assertEqual(result1['image'].shape, result0['image'].shape)
        self.assertEqual(result1['gt_bbox'].shape, result0['gt_bbox'].shape)
        # NormalizeImage
        ops_conf = [{'op': 'NormalizeImage', 'is_channel_first': False}]
78
        mapper = tf.build_mapper(ops_conf)
79 80 81 82 83 84 85 86
        self.assertTrue(mapper is not None)
        result2 = mapper(result1)
        im1 = result1['image']
        count = np.where(im1 <= 1)[0]
        if im1.dtype == 'float64':
            self.assertEqual(count, im1.shape[0] * im1.shape[1], im1.shape[2])
        # ArrangeSample
        ops_conf = [{'op': 'ArrangeRCNN'}]
87
        mapper = tf.build_mapper(ops_conf)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        self.assertTrue(mapper is not None)
        result3 = mapper(result2)
        self.assertEqual(type(result3), tuple)

    def test_ops_part1(self):
        """test Crop and Resize
        """
        ops_conf = [{
            'op': 'DecodeImage'
        }, {
            'op': 'NormalizeBox'
        }, {
            'op': 'CropImage',
            'batch_sampler': [[1, 1, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0],
                              [1, 50, 0.3, 1.0, 0.5, 2.0, 0.1, 0.0],
                              [1, 50, 0.3, 1.0, 0.5, 2.0, 0.3, 0.0],
                              [1, 50, 0.3, 1.0, 0.5, 2.0, 0.5, 0.0],
                              [1, 50, 0.3, 1.0, 0.5, 2.0, 0.7, 0.0],
                              [1, 50, 0.3, 1.0, 0.5, 2.0, 0.9, 0.0],
                              [1, 50, 0.3, 1.0, 0.5, 2.0, 0.0, 1.0]]
        }]
109
        mapper = tf.build_mapper(ops_conf)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        self.assertTrue(mapper is not None)
        data = self.sample.copy()
        result = mapper(data)
        self.assertEqual(len(result['image'].shape), 3)

    def test_ops_part2(self):
        """test Expand and RandomDistort
        """
        ops_conf = [{
            'op': 'DecodeImage'
        }, {
            'op': 'NormalizeBox'
        }, {
            'op': 'ExpandImage',
            'max_ratio': 1.5,
            'prob': 1
        }]
127
        mapper = tf.build_mapper(ops_conf)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        self.assertTrue(mapper is not None)
        data = self.sample.copy()
        result = mapper(data)
        self.assertEqual(len(result['image'].shape), 3)
        self.assertGreater(result['gt_bbox'].shape[0], 0)

    def test_ops_part3(self):
        """test Mixup and RandomInterp
        """
        ops_conf = [{
            'op': 'DecodeImage',
            'with_mixup': True,
        }, {
            'op': 'MixupImage',
        }, {
            'op': 'RandomInterpImage',
            'target_size': 608
        }]
146
        mapper = tf.build_mapper(ops_conf)
147 148 149 150 151 152 153 154 155 156
        self.assertTrue(mapper is not None)
        data = self.sample.copy()
        result = mapper(data)
        self.assertEqual(len(result['image'].shape), 3)
        self.assertGreater(result['gt_bbox'].shape[0], 0)
        #self.assertGreater(result['gt_score'].shape[0], 0)


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