test_transforms_loading.py 4.33 KB
Newer Older
1
2
3
4
5
6
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import os.path as osp

import numpy as np

7
from mmcv.transforms import LoadAnnotations, LoadImageFromFile
8
9
10
11
12
13
14
15
16
17
18
19
20


class TestLoadImageFromFile:

    def test_load_img(self):
        data_prefix = osp.join(osp.dirname(__file__), '../data')

        results = dict(img_path=osp.join(data_prefix, 'color.jpg'))
        transform = LoadImageFromFile()
        results = transform(copy.deepcopy(results))
        assert results['img_path'] == osp.join(data_prefix, 'color.jpg')
        assert results['img'].shape == (300, 400, 3)
        assert results['img'].dtype == np.uint8
21
22
        assert results['img_shape'] == (300, 400)
        assert results['ori_shape'] == (300, 400)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
        assert repr(transform) == transform.__class__.__name__ + \
            "(to_float32=False, color_type='color', " + \
            "imdecode_backend='cv2', file_client_args={'backend': 'disk'})"

        # to_float32
        transform = LoadImageFromFile(to_float32=True)
        results = transform(copy.deepcopy(results))
        assert results['img'].dtype == np.float32

        # gray image
        results = dict(img_path=osp.join(data_prefix, 'grayscale.jpg'))
        transform = LoadImageFromFile()
        results = transform(copy.deepcopy(results))
        assert results['img'].shape == (300, 400, 3)
        assert results['img'].dtype == np.uint8

        transform = LoadImageFromFile(color_type='unchanged')
        results = transform(copy.deepcopy(results))
        assert results['img'].shape == (300, 400)
        assert results['img'].dtype == np.uint8


45
class TestLoadAnnotations:
46
47
48
49
50

    def setup_class(cls):
        data_prefix = osp.join(osp.dirname(__file__), '../data')
        seg_map = osp.join(data_prefix, 'grayscale.jpg')
        cls.results = {
51
            'seg_map_path':
52
53
54
55
56
57
58
59
60
61
62
63
64
            seg_map,
            'instances': [{
                'bbox': [0, 0, 10, 20],
                'bbox_label': 1,
                'keypoints': [1, 2, 3]
            }, {
                'bbox': [10, 10, 110, 120],
                'bbox_label': 2,
                'keypoints': [4, 5, 6]
            }]
        }

    def test_load_bboxes(self):
65
        transform = LoadAnnotations(
66
67
68
            with_bbox=True,
            with_label=False,
            with_seg=False,
69
            with_keypoints=False,
70
71
72
73
74
        )
        results = transform(copy.deepcopy(self.results))
        assert 'gt_bboxes' in results
        assert (results['gt_bboxes'] == np.array([[0, 0, 10, 20],
                                                  [10, 10, 110, 120]])).all()
liukuikun's avatar
liukuikun committed
75
        assert results['gt_bboxes'].dtype == np.float32
76
77

    def test_load_labels(self):
78
        transform = LoadAnnotations(
79
80
81
            with_bbox=False,
            with_label=True,
            with_seg=False,
82
            with_keypoints=False,
83
84
85
86
        )
        results = transform(copy.deepcopy(self.results))
        assert 'gt_bboxes_labels' in results
        assert (results['gt_bboxes_labels'] == np.array([1, 2])).all()
87
        assert results['gt_bboxes_labels'].dtype == np.int64
88
89

    def test_load_kps(self):
90
        transform = LoadAnnotations(
91
92
93
            with_bbox=False,
            with_label=False,
            with_seg=False,
94
            with_keypoints=True,
95
96
97
98
99
        )
        results = transform(copy.deepcopy(self.results))
        assert 'gt_keypoints' in results
        assert (results['gt_keypoints'] == np.array([[[1, 2, 3]],
                                                     [[4, 5, 6]]])).all()
liukuikun's avatar
liukuikun committed
100
        assert results['gt_keypoints'].dtype == np.float32
101
102

    def test_load_seg_map(self):
103
        transform = LoadAnnotations(
104
105
106
            with_bbox=False,
            with_label=False,
            with_seg=True,
107
            with_keypoints=False,
108
109
        )
        results = transform(copy.deepcopy(self.results))
110
111
        assert 'gt_seg_map' in results
        assert results['gt_seg_map'].shape[:2] == (300, 400)
liukuikun's avatar
liukuikun committed
112
        assert results['gt_seg_map'].dtype == np.uint8
113
114

    def test_repr(self):
115
        transform = LoadAnnotations(
116
117
118
            with_bbox=True,
            with_label=False,
            with_seg=False,
119
            with_keypoints=False,
120
        )
121
        assert repr(transform) == (
122
            'LoadAnnotations(with_bbox=True, '
123
124
125
            'with_label=False, with_seg=False, '
            "with_keypoints=False, imdecode_backend='cv2', "
            "file_client_args={'backend': 'disk'})")