Spaces:
Runtime error
Runtime error
File size: 14,563 Bytes
2366e36 |
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# Copyright (c) OpenMMLab. All rights reserved.
from unittest import mock
import numpy as np
from mmdet.core import PolygonMasks
import mmocr.datasets.pipelines.custom_format_bundle as cf_bundle
import mmocr.datasets.pipelines.textdet_targets as textdet_targets
@mock.patch('%s.cf_bundle.show_feature' % __name__)
def test_gen_pannet_targets(mock_show_feature):
target_generator = textdet_targets.PANetTargets()
assert target_generator.max_shrink == 20
# test generate_kernels
img_size = (3, 10)
text_polys = [[np.array([0, 0, 1, 0, 1, 1, 0, 1])],
[np.array([2, 0, 3, 0, 3, 1, 2, 1])]]
shrink_ratio = 1.0
kernel = np.array([[1, 1, 2, 2, 0, 0, 0, 0, 0, 0],
[1, 1, 2, 2, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
output, _ = target_generator.generate_kernels(img_size, text_polys,
shrink_ratio)
print(output)
assert np.allclose(output, kernel)
# test generate_effective_mask
polys_ignore = text_polys
output = target_generator.generate_effective_mask((3, 10), polys_ignore)
target = np.array([[0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
assert np.allclose(output, target)
# test generate_targets
results = {}
results['img'] = np.zeros((3, 10, 3), np.uint8)
results['gt_masks'] = PolygonMasks(text_polys, 3, 10)
results['gt_masks_ignore'] = PolygonMasks([], 3, 10)
results['img_shape'] = (3, 10, 3)
results['mask_fields'] = []
output = target_generator(results)
assert len(output['gt_kernels']) == 2
assert len(output['gt_mask']) == 1
bundle = cf_bundle.CustomFormatBundle(
keys=['gt_kernels', 'gt_mask'],
visualize=dict(flag=True, boundary_key='gt_kernels'))
bundle(output)
assert 'gt_kernels' in output.keys()
assert 'gt_mask' in output.keys()
mock_show_feature.assert_called_once()
def test_gen_psenet_targets():
target_generator = textdet_targets.PSENetTargets()
assert target_generator.max_shrink == 20
assert target_generator.shrink_ratio == (1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4)
# Test DBNetTargets
def test_dbnet_targets_find_invalid():
target_generator = textdet_targets.DBNetTargets()
assert target_generator.shrink_ratio == 0.4
assert target_generator.thr_min == 0.3
assert target_generator.thr_max == 0.7
results = {}
text_polys = [[np.array([0, 0, 10, 0, 10, 10, 0, 10])],
[np.array([20, 0, 30, 0, 30, 10, 20, 10])]]
results['gt_masks'] = PolygonMasks(text_polys, 40, 40)
ignore_tags = target_generator.find_invalid(results)
assert np.allclose(ignore_tags, [False, False])
def test_dbnet_targets():
target_generator = textdet_targets.DBNetTargets()
assert target_generator.shrink_ratio == 0.4
assert target_generator.thr_min == 0.3
assert target_generator.thr_max == 0.7
def test_dbnet_ignore_texts():
target_generator = textdet_targets.DBNetTargets()
ignore_tags = [True, False]
results = {}
text_polys = [[np.array([0, 0, 10, 0, 10, 10, 0, 10])],
[np.array([20, 0, 30, 0, 30, 10, 20, 10])]]
text_polys_ignore = [[np.array([0, 0, 15, 0, 15, 10, 0, 10])]]
results['gt_masks_ignore'] = PolygonMasks(text_polys_ignore, 40, 40)
results['gt_masks'] = PolygonMasks(text_polys, 40, 40)
results['gt_bboxes'] = np.array([[0, 0, 10, 10], [20, 0, 30, 10]])
results['gt_labels'] = np.array([0, 1])
target_generator.ignore_texts(results, ignore_tags)
assert np.allclose(results['gt_labels'], np.array([1]))
assert len(results['gt_masks_ignore'].masks) == 2
assert np.allclose(results['gt_masks_ignore'].masks[1][0],
text_polys[0][0])
assert len(results['gt_masks'].masks) == 1
def test_dbnet_generate_thr_map():
target_generator = textdet_targets.DBNetTargets()
text_polys = [[np.array([0, 0, 10, 0, 10, 10, 0, 10])],
[np.array([20, 0, 30, 0, 30, 10, 20, 10])]]
thr_map, thr_mask = target_generator.generate_thr_map((40, 40), text_polys)
assert np.all((thr_map >= 0.29) * (thr_map <= 0.71))
def test_dbnet_draw_border_map():
target_generator = textdet_targets.DBNetTargets()
poly = np.array([[20, 21], [-14, 20], [-11, 30], [-22, 26]])
img_size = (40, 40)
thr_map = np.zeros(img_size, dtype=np.float32)
thr_mask = np.zeros(img_size, dtype=np.uint8)
target_generator.draw_border_map(poly, thr_map, thr_mask)
def test_dbnet_generate_targets():
target_generator = textdet_targets.DBNetTargets()
text_polys = [[np.array([0, 0, 10, 0, 10, 10, 0, 10])],
[np.array([20, 0, 30, 0, 30, 10, 20, 10])]]
text_polys_ignore = [[np.array([0, 0, 15, 0, 15, 10, 0, 10])]]
results = {}
results['mask_fields'] = []
results['img_shape'] = (40, 40, 3)
results['gt_masks_ignore'] = PolygonMasks(text_polys_ignore, 40, 40)
results['gt_masks'] = PolygonMasks(text_polys, 40, 40)
results['gt_bboxes'] = np.array([[0, 0, 10, 10], [20, 0, 30, 10]])
results['gt_labels'] = np.array([0, 1])
target_generator.generate_targets(results)
assert 'gt_shrink' in results['mask_fields']
assert 'gt_shrink_mask' in results['mask_fields']
assert 'gt_thr' in results['mask_fields']
assert 'gt_thr_mask' in results['mask_fields']
@mock.patch('%s.cf_bundle.show_feature' % __name__)
def test_gen_textsnake_targets(mock_show_feature):
target_generator = textdet_targets.TextSnakeTargets()
assert np.allclose(target_generator.orientation_thr, 2.0)
assert np.allclose(target_generator.resample_step, 4.0)
assert np.allclose(target_generator.center_region_shrink_ratio, 0.3)
# test vector_angle
vec1 = np.array([[-1, 0], [0, 1]])
vec2 = np.array([[1, 0], [0, 1]])
angles = target_generator.vector_angle(vec1, vec2)
assert np.allclose(angles, np.array([np.pi, 0]), atol=1e-3)
# test find_head_tail for quadrangle
polygon = np.array([[1.0, 1.0], [5.0, 1.0], [5.0, 3.0], [1.0, 3.0]])
head_inds, tail_inds = target_generator.find_head_tail(polygon, 2.0)
assert np.allclose(head_inds, [3, 0])
assert np.allclose(tail_inds, [1, 2])
polygon = np.array([[1.0, 1.0], [1.0, 3.0], [5.0, 3.0], [5.0, 1.0]])
head_inds, tail_inds = target_generator.find_head_tail(polygon, 2.0)
assert np.allclose(head_inds, [0, 1])
assert np.allclose(tail_inds, [2, 3])
# test find_head_tail for polygon
polygon = np.array([[0., 10.], [3., 3.], [10., 0.], [17., 3.], [20., 10.],
[15., 10.], [13.5, 6.5], [10., 5.], [6.5, 6.5],
[5., 10.]])
head_inds, tail_inds = target_generator.find_head_tail(polygon, 2.0)
assert np.allclose(head_inds, [9, 0])
assert np.allclose(tail_inds, [4, 5])
# test resample_line
line = np.array([[0, 0], [0, 1], [0, 3], [0, 4], [0, 7], [0, 8]])
resampled_line = target_generator.resample_line(line, 3)
assert len(resampled_line) == 3
assert np.allclose(resampled_line, np.array([[0, 0], [0, 4], [0, 8]]))
line = np.array([[0, 0], [0, 0]])
resampled_line = target_generator.resample_line(line, 4)
assert len(resampled_line) == 4
assert np.allclose(resampled_line,
np.array([[0, 0], [0, 0], [0, 0], [0, 0]]))
# test generate_text_region_mask
img_size = (3, 10)
text_polys = [[np.array([0, 0, 1, 0, 1, 1, 0, 1])],
[np.array([2, 0, 3, 0, 3, 1, 2, 1])]]
output = target_generator.generate_text_region_mask(img_size, text_polys)
target = np.array([[1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
assert np.allclose(output, target)
# test generate_center_region_mask
target_generator.center_region_shrink_ratio = 1.0
(center_region_mask, radius_map, sin_map,
cos_map) = target_generator.generate_center_mask_attrib_maps(
img_size, text_polys)
target = np.array([[1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
assert np.allclose(center_region_mask, target)
assert np.allclose(sin_map, np.zeros(img_size))
assert np.allclose(cos_map, target)
# test generate_effective_mask
polys_ignore = text_polys
output = target_generator.generate_effective_mask(img_size, polys_ignore)
target = np.array([[0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
assert np.allclose(output, target)
# test generate_targets
results = {}
results['img'] = np.zeros((3, 10, 3), np.uint8)
results['gt_masks'] = PolygonMasks(text_polys, 3, 10)
results['gt_masks_ignore'] = PolygonMasks([], 3, 10)
results['img_shape'] = (3, 10, 3)
results['mask_fields'] = []
output = target_generator(results)
assert len(output['gt_text_mask']) == 1
assert len(output['gt_center_region_mask']) == 1
assert len(output['gt_mask']) == 1
assert len(output['gt_radius_map']) == 1
assert len(output['gt_sin_map']) == 1
assert len(output['gt_cos_map']) == 1
bundle = cf_bundle.CustomFormatBundle(
keys=[
'gt_text_mask', 'gt_center_region_mask', 'gt_mask',
'gt_radius_map', 'gt_sin_map', 'gt_cos_map'
],
visualize=dict(flag=True, boundary_key='gt_text_mask'))
bundle(output)
assert 'gt_text_mask' in output.keys()
assert 'gt_center_region_mask' in output.keys()
assert 'gt_mask' in output.keys()
assert 'gt_radius_map' in output.keys()
assert 'gt_sin_map' in output.keys()
assert 'gt_cos_map' in output.keys()
mock_show_feature.assert_called_once()
def test_fcenet_generate_targets():
fourier_degree = 5
target_generator = textdet_targets.FCENetTargets(
fourier_degree=fourier_degree)
h, w, c = (64, 64, 3)
text_polys = [[np.array([0, 0, 10, 0, 10, 10, 0, 10])],
[np.array([20, 0, 30, 0, 30, 10, 20, 10])]]
text_polys_ignore = [[np.array([0, 0, 15, 0, 15, 10, 0, 10])]]
results = {}
results['mask_fields'] = []
results['img_shape'] = (h, w, c)
results['gt_masks_ignore'] = PolygonMasks(text_polys_ignore, h, w)
results['gt_masks'] = PolygonMasks(text_polys, h, w)
results['gt_bboxes'] = np.array([[0, 0, 10, 10], [20, 0, 30, 10]])
results['gt_labels'] = np.array([0, 1])
target_generator.generate_targets(results)
assert 'p3_maps' in results.keys()
assert 'p4_maps' in results.keys()
assert 'p5_maps' in results.keys()
def test_gen_drrg_targets():
target_generator = textdet_targets.DRRGTargets()
assert np.allclose(target_generator.orientation_thr, 2.0)
assert np.allclose(target_generator.resample_step, 8.0)
assert target_generator.num_min_comps == 9
assert target_generator.num_max_comps == 600
assert np.allclose(target_generator.min_width, 8.0)
assert np.allclose(target_generator.max_width, 24.0)
assert np.allclose(target_generator.center_region_shrink_ratio, 0.3)
assert np.allclose(target_generator.comp_shrink_ratio, 1.0)
assert np.allclose(target_generator.comp_w_h_ratio, 0.3)
assert np.allclose(target_generator.text_comp_nms_thr, 0.25)
assert np.allclose(target_generator.min_rand_half_height, 8.0)
assert np.allclose(target_generator.max_rand_half_height, 24.0)
assert np.allclose(target_generator.jitter_level, 0.2)
# test generate_targets
target_generator = textdet_targets.DRRGTargets(
min_width=2.,
max_width=4.,
min_rand_half_height=3.,
max_rand_half_height=5.)
results = {}
results['img'] = np.zeros((64, 64, 3), np.uint8)
text_polys = [[np.array([4, 2, 30, 2, 30, 10, 4, 10])],
[np.array([36, 12, 8, 12, 8, 22, 36, 22])],
[np.array([48, 20, 52, 20, 52, 50, 48, 50])],
[np.array([44, 50, 38, 50, 38, 20, 44, 20])]]
results['gt_masks'] = PolygonMasks(text_polys, 20, 30)
results['gt_masks_ignore'] = PolygonMasks([], 64, 64)
results['img_shape'] = (64, 64, 3)
results['mask_fields'] = []
output = target_generator(results)
assert len(output['gt_text_mask']) == 1
assert len(output['gt_center_region_mask']) == 1
assert len(output['gt_mask']) == 1
assert len(output['gt_top_height_map']) == 1
assert len(output['gt_bot_height_map']) == 1
assert len(output['gt_sin_map']) == 1
assert len(output['gt_cos_map']) == 1
assert output['gt_comp_attribs'].shape[-1] == 8
# test generate_targets with the number of proposed text components exceeds
# num_max_comps
target_generator = textdet_targets.DRRGTargets(
min_width=2.,
max_width=4.,
min_rand_half_height=3.,
max_rand_half_height=5.,
num_max_comps=6)
output = target_generator(results)
assert output['gt_comp_attribs'].ndim == 2
assert output['gt_comp_attribs'].shape[0] == 6
# test generate_targets with blank polygon masks
target_generator = textdet_targets.DRRGTargets(
min_width=2.,
max_width=4.,
min_rand_half_height=3.,
max_rand_half_height=5.)
results = {}
results['img'] = np.zeros((20, 30, 3), np.uint8)
results['gt_masks'] = PolygonMasks([], 20, 30)
results['gt_masks_ignore'] = PolygonMasks([], 20, 30)
results['img_shape'] = (20, 30, 3)
results['mask_fields'] = []
output = target_generator(results)
assert output['gt_comp_attribs'][0, 0] > 8
# test generate_targets with one proposed text component
text_polys = [[np.array([13, 6, 17, 6, 17, 14, 13, 14])]]
target_generator = textdet_targets.DRRGTargets(
min_width=4.,
max_width=8.,
min_rand_half_height=3.,
max_rand_half_height=5.)
results['gt_masks'] = PolygonMasks(text_polys, 20, 30)
output = target_generator(results)
assert output['gt_comp_attribs'][0, 0] > 8
# test generate_targets with shrunk margin in generate_rand_comp_attribs
target_generator = textdet_targets.DRRGTargets(
min_width=2.,
max_width=30.,
min_rand_half_height=3.,
max_rand_half_height=30.)
output = target_generator(results)
assert output['gt_comp_attribs'][0, 0] > 8
|