test_operator.py 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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
import os
import unittest
import logging
import numpy as np
import set_env
from data import transform as tf
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
        }]
        mapper = tf.build(ops_conf)
        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'}]
        mapper = tf.build(ops_conf)
        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}]
        mapper = tf.build(ops_conf)
        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'}]
        mapper = tf.build(ops_conf)
        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]]
        }]
        mapper = tf.build(ops_conf)
        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
        }]
        mapper = tf.build(ops_conf)
        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
        }]
        mapper = tf.build(ops_conf)
        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()