forked from open-mmlab/mmtracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_transform.py
209 lines (171 loc) · 7.5 KB
/
test_transform.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import copy
import os.path as osp
import numpy as np
import pytest
from mmcv.utils import build_from_cfg
from mmdet.core.bbox.demodata import random_boxes
from mmtrack.datasets import PIPELINES
class TestTransforms(object):
@classmethod
def setup_class(cls):
cls.data_prefix = osp.join(osp.dirname(__file__), '../assets')
img_names = ['image_1.jpg', 'image_2.jpg']
results = [
dict(img_prefix=cls.data_prefix, img_info=dict(filename=name))
for name in img_names
]
load = build_from_cfg(
dict(type='LoadMultiImagesFromFile', to_float32=True), PIPELINES)
cls.results = load(results)
def test_seq_crop_like_siamfc(self):
results = copy.deepcopy(self.results)
for res in results:
res['gt_bboxes'] = random_boxes(1, 256)
res['bbox_fields'] = ['gt_bboxes']
transform = dict(
type='SeqCropLikeSiamFC',
context_amount=0.5,
exemplar_size=127,
crop_size=511)
seq_crop_like_siamfc = build_from_cfg(transform, PIPELINES)
results = seq_crop_like_siamfc(results)
assert results[0]['img'].shape == (511, 511, 3)
assert results[1]['img'].shape == (511, 511, 3)
def test_seq_shift_scale_aug(self):
results = copy.deepcopy(self.results)
for res in results:
res['gt_bboxes'] = random_boxes(1, 256).numpy()
res['bbox_fields'] = ['gt_bboxes']
transform = dict(
type='SeqShiftScaleAug',
target_size=[127, 255],
shift=[4, 64],
scale=[0.05, 0.18])
seq_shift_scale_aug = build_from_cfg(transform, PIPELINES)
results = seq_shift_scale_aug(results)
assert results[0]['img'].shape == (127, 127, 3)
assert results[1]['img'].shape == (255, 255, 3)
def test_seq_color_aug(self):
results = copy.deepcopy(self.results)
imgs_shape = [result['img'].shape for result in results]
transform = dict(
type='SeqColorAug',
prob=[1.0, 1.0],
rgb_var=[[-0.55919361, 0.98062831, -0.41940627],
[1.72091413, 0.19879334, -1.82968581],
[4.64467907, 4.73710203, 4.88324118]])
seq_color_aug = build_from_cfg(transform, PIPELINES)
results = seq_color_aug(results)
assert results[0]['img'].shape == imgs_shape[0]
assert results[1]['img'].shape == imgs_shape[0]
def test_seq_blur_aug(self):
results = copy.deepcopy(self.results)
imgs_shape = [result['img'].shape for result in results]
transform = dict(type='SeqBlurAug', prob=[0.0, 0.2])
seq_blur_aug = build_from_cfg(transform, PIPELINES)
results = seq_blur_aug(results)
assert results[0]['img'].shape == imgs_shape[0]
assert results[1]['img'].shape == imgs_shape[0]
def test_seq_resize(self):
results = copy.deepcopy(self.results)
transform = dict(
type='SeqResize', img_scale=(512, 1024), keep_ratio=True)
seq_resize = build_from_cfg(transform, PIPELINES)
results = seq_resize(results)
assert results[0]['img'].shape == (512, 1024, 3)
assert results[1]['img'].shape == (512, 1024, 3)
def test_seq_flip(self):
transform = dict(
type='SeqRandomFlip', share_params=True, flip_ratio=0.5)
flip_module = build_from_cfg(transform, PIPELINES)
for i in range(8):
results = copy.deepcopy(self.results)
results = flip_module(results)
assert results[0]['flip'] == results[1]['flip']
assert results[0]['flip_direction'] == results[1]['flip_direction']
cases = [False, False]
transform = dict(
type='SeqRandomFlip', share_params=False, flip_ratio=0.5)
flip_module = build_from_cfg(transform, PIPELINES)
for i in range(8):
results = copy.deepcopy(self.results)
results = flip_module(results)
if results[0]['flip'] == results[1]['flip']:
cases[0] = True
else:
cases[1] = True
assert cases[0] is True
assert cases[1] is True
def test_seq_pad(self):
results = copy.deepcopy(self.results)
transform = dict(type='SeqPad', size_divisor=32)
transform = build_from_cfg(transform, PIPELINES)
results = transform(results)
for result in results:
img_shape = result['img'].shape
assert img_shape[0] % 32 == 0
assert img_shape[1] % 32 == 0
resize_transform = dict(
type='SeqResize', img_scale=(1333, 800), keep_ratio=True)
resize_module = build_from_cfg(resize_transform, PIPELINES)
results = resize_module(results)
results = transform(results)
for result in results:
img_shape = result['img'].shape
assert img_shape[0] % 32 == 0
assert img_shape[1] % 32 == 0
def test_seq_normalize(self):
results = copy.deepcopy(self.results)
img_norm_cfg = dict(
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True)
transform = dict(type='SeqNormalize', **img_norm_cfg)
transform = build_from_cfg(transform, PIPELINES)
results = transform(results)
mean = np.array(img_norm_cfg['mean'])
std = np.array(img_norm_cfg['std'])
for i, result in enumerate(results):
converted_img = (self.results[i]['img'][..., ::-1] - mean) / std
assert np.allclose(result['img'], converted_img)
def test_seq_random_crop(self):
# test assertion for invalid random crop
with pytest.raises(AssertionError):
transform = dict(
type='SeqRandomCrop', crop_size=(-1, 0), share_params=False)
build_from_cfg(transform, PIPELINES)
crop_size = (256, 384)
transform = dict(
type='SeqRandomCrop', crop_size=crop_size, share_params=False)
crop_module = build_from_cfg(transform, PIPELINES)
results = copy.deepcopy(self.results)
for res in results:
res['gt_bboxes'] = random_boxes(8, 256)
res['gt_labels'] = np.random.randint(8)
res['gt_instance_ids'] = np.random.randint(8)
res['gt_bboxes_ignore'] = random_boxes(2, 256)
outs = crop_module(results)
assert len(outs) == len(results)
for res in results:
assert res['img'].shape[:2] == crop_size
# All bboxes should be reserved after crop
assert res['img_shape'][:2] == crop_size
assert res['gt_bboxes'].shape[0] == 8
assert res['gt_bboxes_ignore'].shape[0] == 2
assert outs[0]['img_info']['crop_offsets'] != outs[1]['img_info'][
'crop_offsets']
crop_module.share_params = True
outs = crop_module(results)
assert outs[0]['img_info']['crop_offsets'] == outs[1]['img_info'][
'crop_offsets']
def test_seq_color_jitter(self):
results = self.results.copy()
transform = dict(type='SeqPhotoMetricDistortion', share_params=False)
transform = build_from_cfg(transform, PIPELINES)
outs = transform(results)
assert outs[0]['img_info']['color_jitter'] != outs[1]['img_info'][
'color_jitter']
transform.share_params = True
outs = transform(results)
assert outs[0]['img_info']['color_jitter'] == outs[1]['img_info'][
'color_jitter']