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

import numpy as np
liukuikun's avatar
liukuikun committed
6
import pytest
7

8
from mmcv.transforms import LoadAnnotations, LoadImageFromFile
9
10
11
12
13


class TestLoadImageFromFile:

    def test_load_img(self):
14
15
16
17
18
19
20
        # file_client_args and backend_args can not be both set
        with pytest.raises(
                ValueError,
                match='"file_client_args" and "backend_args" cannot be set'):
            LoadImageFromFile(
                file_client_args={'backend': 'disk'},
                backend_args={'backend': 'disk'})
21
22
23
24
25
26
27
28
        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
29
30
        assert results['img_shape'] == (300, 400)
        assert results['ori_shape'] == (300, 400)
31
        assert repr(transform) == transform.__class__.__name__ + \
liukuikun's avatar
liukuikun committed
32
            "(ignore_empty=False, to_float32=False, color_type='color', " + \
33
            "imdecode_backend='cv2', backend_args=None)"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

        # 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

liukuikun's avatar
liukuikun committed
52
53
54
55
56
57
58
59
60
        # test load empty
        fake_img_path = osp.join(data_prefix, 'fake.jpg')
        results['img_path'] = fake_img_path
        transform = LoadImageFromFile(ignore_empty=False)
        with pytest.raises(FileNotFoundError):
            transform(copy.deepcopy(results))
        transform = LoadImageFromFile(ignore_empty=True)
        assert transform(copy.deepcopy(results)) is None

61

62
class TestLoadAnnotations:
63
64
65
66
67

    def setup_class(cls):
        data_prefix = osp.join(osp.dirname(__file__), '../data')
        seg_map = osp.join(data_prefix, 'grayscale.jpg')
        cls.results = {
68
            'seg_map_path':
69
70
71
72
73
74
75
76
77
78
79
80
            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]
            }]
        }

81
82
83
84
85
86
87
88
89
    def test_init(self):
        # file_client_args and backend_args can not be both set
        with pytest.raises(
                ValueError,
                match='"file_client_args" and "backend_args" cannot be set'):
            LoadAnnotations(
                file_client_args={'backend': 'disk'},
                backend_args={'backend': 'disk'})

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

    def test_load_labels(self):
104
        transform = LoadAnnotations(
105
106
107
            with_bbox=False,
            with_label=True,
            with_seg=False,
108
            with_keypoints=False,
109
110
111
112
        )
        results = transform(copy.deepcopy(self.results))
        assert 'gt_bboxes_labels' in results
        assert (results['gt_bboxes_labels'] == np.array([1, 2])).all()
113
        assert results['gt_bboxes_labels'].dtype == np.int64
114
115

    def test_load_kps(self):
116
        transform = LoadAnnotations(
117
118
119
            with_bbox=False,
            with_label=False,
            with_seg=False,
120
            with_keypoints=True,
121
122
123
124
125
        )
        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
126
        assert results['gt_keypoints'].dtype == np.float32
127
128

    def test_load_seg_map(self):
129
        transform = LoadAnnotations(
130
131
132
            with_bbox=False,
            with_label=False,
            with_seg=True,
133
            with_keypoints=False,
134
135
        )
        results = transform(copy.deepcopy(self.results))
136
137
        assert 'gt_seg_map' in results
        assert results['gt_seg_map'].shape[:2] == (300, 400)
liukuikun's avatar
liukuikun committed
138
        assert results['gt_seg_map'].dtype == np.uint8
139
140

    def test_repr(self):
141
        transform = LoadAnnotations(
142
143
144
            with_bbox=True,
            with_label=False,
            with_seg=False,
145
            with_keypoints=False,
146
        )
147
        assert repr(transform) == (
148
            'LoadAnnotations(with_bbox=True, '
149
            'with_label=False, with_seg=False, '
150
151
            "with_keypoints=False, imdecode_backend='cv2', "
            'backend_args=None)')