File size: 6,710 Bytes
3bbb319 |
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 210 211 212 213 214 215 216 217 218 219 |
# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
import numpy as np
import pytest
from mmcv import bgr2rgb, build_from_cfg
from mmpose.datasets import PIPELINES
from mmpose.datasets.pipelines import Compose
def check_keys_equal(result_keys, target_keys):
"""Check if all elements in target_keys is in result_keys."""
return set(target_keys) == set(result_keys)
def check_keys_contain(result_keys, target_keys):
"""Check if elements in target_keys is in result_keys."""
return set(target_keys).issubset(set(result_keys))
def test_compose():
with pytest.raises(TypeError):
# transform must be callable or a dict
Compose('LoadImageFromFile')
target_keys = ['img', 'img_rename', 'img_metas']
# test Compose given a data pipeline
img = np.random.randn(256, 256, 3)
results = dict(img=img, img_file='test_image.png')
test_pipeline = [
dict(
type='Collect',
keys=['img', ('img', 'img_rename')],
meta_keys=['img_file'])
]
compose = Compose(test_pipeline)
compose_results = compose(results)
assert check_keys_equal(compose_results.keys(), target_keys)
assert check_keys_equal(compose_results['img_metas'].data.keys(),
['img_file'])
# test Compose when forward data is None
results = None
class ExamplePipeline:
def __call__(self, results):
return None
nonePipeline = ExamplePipeline()
test_pipeline = [nonePipeline]
compose = Compose(test_pipeline)
compose_results = compose(results)
assert compose_results is None
assert repr(compose) == compose.__class__.__name__ + \
f'(\n {nonePipeline}\n)'
def test_load_image_from_file():
# Define simple pipeline
load = dict(type='LoadImageFromFile')
load = build_from_cfg(load, PIPELINES)
data_prefix = 'tests/data/coco/'
image_file = osp.join(data_prefix, '00000000078.jpg')
results = dict(image_file=image_file)
# load an image that doesn't exist
with pytest.raises(FileNotFoundError):
results = load(results)
# mormal loading
image_file = osp.join(data_prefix, '000000000785.jpg')
results = dict(image_file=image_file)
results = load(results)
assert results['img'].shape == (425, 640, 3)
# load a single image from a list
image_file = [osp.join(data_prefix, '000000000785.jpg')]
results = dict(image_file=image_file)
results = load(results)
assert len(results['img']) == 1
# test loading multi images from a list
image_file = [
osp.join(data_prefix, '000000000785.jpg'),
osp.join(data_prefix, '00000004008.jpg'),
]
results = dict(image_file=image_file)
with pytest.raises(FileNotFoundError):
results = load(results)
image_file = [
osp.join(data_prefix, '000000000785.jpg'),
osp.join(data_prefix, '000000040083.jpg'),
]
results = dict(image_file=image_file)
results = load(results)
assert len(results['img']) == 2
# manually set image outside the pipeline
img = np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8)
results = load(dict(img=img))
np.testing.assert_equal(results['img'], bgr2rgb(img))
imgs = np.random.randint(0, 255, (2, 32, 32, 3), dtype=np.uint8)
desired = np.concatenate([bgr2rgb(img) for img in imgs], axis=0)
results = load(dict(img=imgs))
np.testing.assert_equal(results['img'], desired)
# neither 'image_file' or valid 'img' is given
results = dict()
with pytest.raises(KeyError):
_ = load(results)
results = dict(img=np.random.randint(0, 255, (32, 32), dtype=np.uint8))
with pytest.raises(ValueError):
_ = load(results)
def test_albu_transform():
data_prefix = 'tests/data/coco/'
results = dict(image_file=osp.join(data_prefix, '000000000785.jpg'))
# Define simple pipeline
load = dict(type='LoadImageFromFile')
load = build_from_cfg(load, PIPELINES)
albu_transform = dict(
type='Albumentation',
transforms=[
dict(type='RandomBrightnessContrast', p=0.2),
dict(type='ToFloat')
])
albu_transform = build_from_cfg(albu_transform, PIPELINES)
# Execute transforms
results = load(results)
results = albu_transform(results)
assert results['img'].dtype == np.float32
def test_photometric_distortion_transform():
data_prefix = 'tests/data/coco/'
results = dict(image_file=osp.join(data_prefix, '000000000785.jpg'))
# Define simple pipeline
load = dict(type='LoadImageFromFile')
load = build_from_cfg(load, PIPELINES)
photo_transform = dict(type='PhotometricDistortion')
photo_transform = build_from_cfg(photo_transform, PIPELINES)
# Execute transforms
results = load(results)
results = photo_transform(results)
assert results['img'].dtype == np.uint8
def test_multitask_gather():
ann_info = dict(
image_size=np.array([256, 256]),
heatmap_size=np.array([64, 64]),
num_joints=17,
joint_weights=np.ones((17, 1), dtype=np.float32),
use_different_joint_weights=False)
results = dict(
joints_3d=np.zeros([17, 3]),
joints_3d_visible=np.ones([17, 3]),
ann_info=ann_info)
pipeline_list = [[dict(type='TopDownGenerateTarget', sigma=2)],
[dict(type='TopDownGenerateTargetRegression')]]
pipeline = dict(
type='MultitaskGatherTarget',
pipeline_list=pipeline_list,
pipeline_indices=[0, 1, 0],
)
pipeline = build_from_cfg(pipeline, PIPELINES)
results = pipeline(results)
target = results['target']
target_weight = results['target_weight']
assert isinstance(target, list)
assert isinstance(target_weight, list)
assert target[0].shape == (17, 64, 64)
assert target_weight[0].shape == (17, 1)
assert target[1].shape == (17, 2)
assert target_weight[1].shape == (17, 2)
assert target[2].shape == (17, 64, 64)
assert target_weight[2].shape == (17, 1)
def test_rename_keys():
results = dict(
joints_3d=np.ones([17, 3]), joints_3d_visible=np.ones([17, 3]))
pipeline = dict(
type='RenameKeys',
key_pairs=[('joints_3d', 'target'),
('joints_3d_visible', 'target_weight')])
pipeline = build_from_cfg(pipeline, PIPELINES)
results = pipeline(results)
assert 'joints_3d' not in results
assert 'joints_3d_visible' not in results
assert 'target' in results
assert 'target_weight' in results
assert results['target'].shape == (17, 3)
assert results['target_weight'].shape == (17, 3)
|