test_transforms_loading.py 4.39 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
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


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
        assert results['height'] == 300
        assert results['width'] == 400
        assert results['ori_height'] == 300
        assert results['ori_width'] == 400
        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


47
class TestLoadAnnotations:
48
49
50
51
52

    def setup_class(cls):
        data_prefix = osp.join(osp.dirname(__file__), '../data')
        seg_map = osp.join(data_prefix, 'grayscale.jpg')
        cls.results = {
53
            'seg_map_path':
54
55
56
57
58
59
60
61
62
63
64
65
66
            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):
67
        transform = LoadAnnotations(
68
69
70
            with_bbox=True,
            with_label=False,
            with_seg=False,
71
            with_keypoints=False,
72
73
74
75
76
        )
        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
77
        assert results['gt_bboxes'].dtype == np.float32
78
79

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

    def test_load_kps(self):
92
        transform = LoadAnnotations(
93
94
95
            with_bbox=False,
            with_label=False,
            with_seg=False,
96
            with_keypoints=True,
97
98
99
100
101
        )
        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
102
        assert results['gt_keypoints'].dtype == np.float32
103
104

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

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