forked from open-mmlab/mmengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_visualizer.py
584 lines (498 loc) · 21.5 KB
/
test_visualizer.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import time
from typing import Any
from unittest import TestCase
import numpy as np
import pytest
import torch
import torch.nn as nn
from mmengine import VISBACKENDS, Config
from mmengine.logging import MMLogger
from mmengine.visualization import Visualizer
@VISBACKENDS.register_module()
class MockVisBackend:
def __init__(self, save_dir: str):
self._save_dir = save_dir
self._close = False
@property
def experiment(self) -> Any:
return self
def add_config(self, config, **kwargs) -> None:
self._add_config = True
def add_graph(self, model, data_batch, **kwargs) -> None:
self._add_graph = True
def add_image(self, name, image, step=0, **kwargs) -> None:
self._add_image = True
def add_scalar(self, name, value, step=0, **kwargs) -> None:
self._add_scalar = True
def add_scalars(self,
scalar_dict,
step=0,
file_path=None,
**kwargs) -> None:
self._add_scalars = True
def close(self) -> None:
"""close an opened object."""
self._close = True
class TestVisualizer(TestCase):
def setUp(self):
"""Setup the demo image in every test method.
TestCase calls functions in this order: setUp() -> testMethod() ->
tearDown() -> cleanUp()
"""
self.image = np.random.randint(
0, 256, size=(10, 10, 3)).astype('uint8')
self.vis_backend_cfg = [
dict(type='MockVisBackend', name='mock1'),
dict(type='MockVisBackend', name='mock2')
]
def test_init(self):
visualizer = Visualizer(image=self.image)
visualizer.get_image()
# test save_dir
# Warning should be raised since no backend is initialized.
with self.assertLogs(MMLogger.get_current_instance(), level='WARNING'):
Visualizer()
visualizer = Visualizer(
vis_backends=copy.deepcopy(self.vis_backend_cfg))
assert visualizer.get_backend('mock1') is None
visualizer = Visualizer(
vis_backends=copy.deepcopy(self.vis_backend_cfg),
save_dir='temp_dir')
assert isinstance(visualizer.get_backend('mock1'), MockVisBackend)
assert len(visualizer._vis_backends) == 2
# test empty list
with pytest.raises(AssertionError):
Visualizer(vis_backends=[], save_dir='temp_dir')
# test name
# If one of them has a name attribute, all backends must
# use the name attribute
with pytest.raises(RuntimeError):
Visualizer(
vis_backends=[
dict(type='MockVisBackend'),
dict(type='MockVisBackend', name='mock2')
],
save_dir='temp_dir')
# The name fields cannot be the same
with pytest.raises(RuntimeError):
Visualizer(
vis_backends=[
dict(type='MockVisBackend'),
dict(type='MockVisBackend')
],
save_dir='temp_dir')
with pytest.raises(RuntimeError):
Visualizer(
vis_backends=[
dict(type='MockVisBackend', name='mock1'),
dict(type='MockVisBackend', name='mock1')
],
save_dir='temp_dir')
# test global init
instance_name = 'visualizer' + str(time.time())
visualizer = Visualizer.get_instance(
instance_name,
vis_backends=copy.deepcopy(self.vis_backend_cfg),
save_dir='temp_dir')
assert len(visualizer._vis_backends) == 2
visualizer_any = Visualizer.get_instance(instance_name)
assert visualizer_any == visualizer
def test_set_image(self):
visualizer = Visualizer()
visualizer.set_image(self.image)
with pytest.raises(AssertionError):
visualizer.set_image(None)
def test_get_image(self):
visualizer = Visualizer(image=self.image)
visualizer.get_image()
def test_draw_bboxes(self):
visualizer = Visualizer(image=self.image)
# only support 4 or nx4 tensor and numpy
visualizer.draw_bboxes(torch.tensor([1, 1, 2, 2]))
# valid bbox
visualizer.draw_bboxes(torch.tensor([1, 1, 1, 2]))
bboxes = torch.tensor([[1, 1, 2, 2], [1, 2, 2, 2.5]])
visualizer.draw_bboxes(
bboxes, alpha=0.5, edge_colors=(255, 0, 0), line_styles='-')
bboxes = bboxes.numpy()
visualizer.draw_bboxes(bboxes)
# test invalid bbox
with pytest.raises(AssertionError):
# x1 > x2
visualizer.draw_bboxes(torch.tensor([5, 1, 2, 2]))
# test out of bounds
with pytest.warns(
UserWarning,
match='Warning: The bbox is out of bounds,'
' the drawn bbox may not be in the image'):
visualizer.draw_bboxes(torch.tensor([1, 1, 20, 2]))
# test incorrect bbox format
with pytest.raises(TypeError):
visualizer.draw_bboxes([1, 1, 2, 2])
def test_close(self):
visualizer = Visualizer(
image=self.image,
vis_backends=copy.deepcopy(self.vis_backend_cfg),
save_dir='temp_dir')
for name in ['mock1', 'mock2']:
assert visualizer.get_backend(name)._close is False
visualizer.close()
for name in ['mock1', 'mock2']:
assert visualizer.get_backend(name)._close is True
def test_draw_points(self):
visualizer = Visualizer(image=self.image)
with pytest.raises(TypeError):
visualizer.draw_points(positions=[1, 2])
with pytest.raises(AssertionError):
visualizer.draw_points(positions=np.array([1, 2, 3], dtype=object))
# test color
visualizer.draw_points(
positions=torch.tensor([[1, 1], [3, 3]]),
colors=['g', (255, 255, 0)])
visualizer.draw_points(
positions=torch.tensor([[1, 1], [3, 3]]),
colors=['g', (255, 255, 0)],
marker='.',
sizes=[1, 5])
def test_draw_texts(self):
visualizer = Visualizer(image=self.image)
# only support tensor and numpy
visualizer.draw_texts(
'text1', positions=torch.tensor([5, 5]), colors=(0, 255, 0))
visualizer.draw_texts(['text1', 'text2'],
positions=torch.tensor([[5, 5], [3, 3]]),
colors=[(255, 0, 0), (255, 0, 0)])
visualizer.draw_texts('text1', positions=np.array([5, 5]))
visualizer.draw_texts(['text1', 'text2'],
positions=np.array([[5, 5], [3, 3]]))
visualizer.draw_texts(
'text1',
positions=torch.tensor([5, 5]),
bboxes=dict(facecolor='r', alpha=0.6))
# test out of bounds
with pytest.warns(
UserWarning,
match='Warning: The text is out of bounds,'
' the drawn text may not be in the image'):
visualizer.draw_texts('text1', positions=torch.tensor([15, 5]))
# test incorrect format
with pytest.raises(TypeError):
visualizer.draw_texts('text', positions=[5, 5])
# test length mismatch
with pytest.raises(AssertionError):
visualizer.draw_texts(['text1', 'text2'],
positions=torch.tensor([5, 5]))
with pytest.raises(AssertionError):
visualizer.draw_texts(
'text1', positions=torch.tensor([[5, 5], [3, 3]]))
with pytest.raises(AssertionError):
visualizer.draw_texts(['text1', 'test2'],
positions=torch.tensor([[5, 5], [3, 3]]),
colors=['r'])
with pytest.raises(AssertionError):
visualizer.draw_texts(['text1', 'test2'],
positions=torch.tensor([[5, 5], [3, 3]]),
vertical_alignments=['top'])
with pytest.raises(AssertionError):
visualizer.draw_texts(['text1', 'test2'],
positions=torch.tensor([[5, 5], [3, 3]]),
horizontal_alignments=['left'])
with pytest.raises(AssertionError):
visualizer.draw_texts(['text1', 'test2'],
positions=torch.tensor([[5, 5], [3, 3]]),
font_sizes=[1])
# test type valid
with pytest.raises(TypeError):
visualizer.draw_texts(['text1', 'test2'],
positions=torch.tensor([[5, 5], [3, 3]]),
font_sizes='b')
def test_draw_lines(self):
visualizer = Visualizer(image=self.image)
# only support tensor and numpy
visualizer.draw_lines(
x_datas=torch.tensor([1, 5]), y_datas=torch.tensor([2, 6]))
visualizer.draw_lines(
x_datas=np.array([[1, 5], [2, 4]]),
y_datas=np.array([[2, 6], [4, 7]]))
visualizer.draw_lines(
x_datas=np.array([[1, 5], [2, 4]]),
y_datas=np.array([[2, 6], [4, 7]]),
colors='r',
line_styles=['-', '-.'],
line_widths=[1, 2])
# test out of bounds
with pytest.warns(
UserWarning,
match='Warning: The line is out of bounds,'
' the drawn line may not be in the image'):
visualizer.draw_lines(
x_datas=torch.tensor([12, 5]), y_datas=torch.tensor([2, 6]))
# test incorrect format
with pytest.raises(TypeError):
visualizer.draw_lines(x_datas=[5, 5], y_datas=torch.tensor([2, 6]))
with pytest.raises(TypeError):
visualizer.draw_lines(y_datas=[5, 5], x_datas=torch.tensor([2, 6]))
# test length mismatch
with pytest.raises(AssertionError):
visualizer.draw_lines(
x_datas=torch.tensor([1, 5]),
y_datas=torch.tensor([[2, 6], [4, 7]]))
def test_draw_circles(self):
visualizer = Visualizer(image=self.image)
# only support tensor and numpy
visualizer.draw_circles(torch.tensor([1, 5]), torch.tensor([1]))
visualizer.draw_circles(np.array([1, 5]), np.array([1]))
visualizer.draw_circles(
torch.tensor([[1, 5], [2, 6]]), radius=torch.tensor([1, 2]))
# test face_colors
visualizer.draw_circles(
torch.tensor([[1, 5], [2, 6]]),
radius=torch.tensor([1, 2]),
face_colors=(255, 0, 0),
edge_colors=(255, 0, 0))
# test config
visualizer.draw_circles(
torch.tensor([[1, 5], [2, 6]]),
radius=torch.tensor([1, 2]),
edge_colors=['g', 'r'],
line_styles=['-', '-.'],
line_widths=[1, 2])
# test out of bounds
with pytest.warns(
UserWarning,
match='Warning: The circle is out of bounds,'
' the drawn circle may not be in the image'):
visualizer.draw_circles(
torch.tensor([12, 5]), radius=torch.tensor([1]))
visualizer.draw_circles(
torch.tensor([1, 5]), radius=torch.tensor([10]))
# test incorrect format
with pytest.raises(TypeError):
visualizer.draw_circles([1, 5], radius=torch.tensor([1]))
with pytest.raises(TypeError):
visualizer.draw_circles(np.array([1, 5]), radius=10)
# test length mismatch
with pytest.raises(AssertionError):
visualizer.draw_circles(
torch.tensor([[1, 5]]), radius=torch.tensor([1, 2]))
def test_draw_polygons(self):
visualizer = Visualizer(image=self.image)
# shape Nx2 or list[Nx2]
visualizer.draw_polygons(torch.tensor([[1, 1], [2, 2], [3, 4]]))
visualizer.draw_polygons(np.array([[1, 1], [2, 2], [3, 4]]))
visualizer.draw_polygons([
np.array([[1, 1], [2, 2], [3, 4]]),
torch.tensor([[1, 1], [2, 2], [3, 4]])
])
visualizer.draw_polygons(
polygons=[
np.array([[1, 1], [2, 2], [3, 4]]),
torch.tensor([[1, 1], [2, 2], [3, 4]])
],
face_colors=(255, 0, 0),
edge_colors=(255, 0, 0))
visualizer.draw_polygons(
polygons=[
np.array([[1, 1], [2, 2], [3, 4]]),
torch.tensor([[1, 1], [2, 2], [3, 4]])
],
edge_colors=['r', 'g'],
line_styles='-',
line_widths=[2, 1])
# test out of bounds
with pytest.warns(
UserWarning,
match='Warning: The polygon is out of bounds,'
' the drawn polygon may not be in the image'):
visualizer.draw_polygons(torch.tensor([[1, 1], [2, 2], [16, 4]]))
def test_draw_binary_masks(self):
binary_mask = np.random.randint(0, 2, size=(10, 10)).astype(bool)
visualizer = Visualizer(image=self.image)
visualizer.draw_binary_masks(binary_mask)
visualizer.draw_binary_masks(torch.from_numpy(binary_mask))
# multi binary
binary_mask = np.random.randint(0, 2, size=(2, 10, 10)).astype(bool)
visualizer = Visualizer(image=self.image)
visualizer.draw_binary_masks(binary_mask, colors=['r', (0, 255, 0)])
# test the error that the size of mask and image are different.
with pytest.raises(AssertionError):
binary_mask = np.random.randint(0, 2, size=(8, 10)).astype(bool)
visualizer.draw_binary_masks(binary_mask)
# test non binary mask error
binary_mask = np.random.randint(0, 2, size=(10, 10, 3)).astype(bool)
with pytest.raises(AssertionError):
visualizer.draw_binary_masks(binary_mask)
# test color dim
with pytest.raises(AssertionError):
visualizer.draw_binary_masks(
binary_mask, colors=np.array([1, 22, 4, 45]))
binary_mask = np.random.randint(0, 2, size=(10, 10))
with pytest.raises(AssertionError):
visualizer.draw_binary_masks(binary_mask)
def test_draw_featmap(self):
visualizer = Visualizer()
image = np.random.randint(0, 256, size=(3, 3, 3), dtype='uint8')
# must be Tensor
with pytest.raises(
AssertionError,
match='`featmap` should be torch.Tensor, but got '
"<class 'numpy.ndarray'>"):
visualizer.draw_featmap(np.ones((3, 3, 3)))
# test tensor format
with pytest.raises(
AssertionError, match='Input dimension must be 3, but got 4'):
visualizer.draw_featmap(torch.randn(1, 1, 3, 3))
# test overlaid_image shape
with pytest.warns(Warning):
visualizer.draw_featmap(torch.randn(1, 4, 3), overlaid_image=image)
# test resize_shape
featmap = visualizer.draw_featmap(
torch.randn(1, 4, 3), resize_shape=(6, 7))
assert featmap.shape[:2] == (6, 7)
featmap = visualizer.draw_featmap(
torch.randn(1, 4, 3), overlaid_image=image, resize_shape=(6, 7))
assert featmap.shape[:2] == (6, 7)
# test channel_reduction parameter
# mode only supports 'squeeze_mean' and 'select_max'
with pytest.raises(AssertionError):
visualizer.draw_featmap(
torch.randn(2, 3, 3), channel_reduction='xx')
featmap = visualizer.draw_featmap(
torch.randn(2, 3, 3), channel_reduction='squeeze_mean')
assert featmap.shape[:2] == (3, 3)
featmap = visualizer.draw_featmap(
torch.randn(2, 3, 3), channel_reduction='select_max')
assert featmap.shape[:2] == (3, 3)
featmap = visualizer.draw_featmap(
torch.randn(2, 4, 3),
overlaid_image=image,
channel_reduction='select_max')
assert featmap.shape[:2] == (3, 3)
# test topk parameter
with pytest.raises(
AssertionError,
match='The input tensor channel dimension must be 1 or 3 '
'when topk is less than 1, but the channel '
'dimension you input is 6, you can use the '
'channel_reduction parameter or set topk '
'greater than 0 to solve the error'):
visualizer.draw_featmap(
torch.randn(6, 3, 3), channel_reduction=None, topk=0)
featmap = visualizer.draw_featmap(
torch.randn(6, 3, 3), channel_reduction='select_max', topk=10)
assert featmap.shape[:2] == (3, 3)
featmap = visualizer.draw_featmap(
torch.randn(1, 4, 3), channel_reduction=None, topk=-1)
assert featmap.shape[:2] == (4, 3)
featmap = visualizer.draw_featmap(
torch.randn(3, 4, 3),
overlaid_image=image,
channel_reduction=None,
topk=-1)
assert featmap.shape[:2] == (3, 3)
featmap = visualizer.draw_featmap(
torch.randn(6, 3, 3),
channel_reduction=None,
topk=4,
arrangement=(2, 2))
assert featmap.shape[:2] == (6, 6)
featmap = visualizer.draw_featmap(
torch.randn(6, 3, 3),
channel_reduction=None,
topk=4,
arrangement=(1, 4))
assert featmap.shape[:2] == (3, 12)
with pytest.raises(
AssertionError,
match='The product of row and col in the `arrangement` '
'is less than topk, please set '
'the `arrangement` correctly'):
visualizer.draw_featmap(
torch.randn(6, 3, 3),
channel_reduction=None,
topk=4,
arrangement=(1, 2))
# test gray
featmap = visualizer.draw_featmap(
torch.randn(6, 3, 3),
overlaid_image=np.random.randint(
0, 256, size=(3, 3), dtype='uint8'),
channel_reduction=None,
topk=4,
arrangement=(2, 2))
assert featmap.shape[:2] == (6, 6)
def test_chain_call(self):
visualizer = Visualizer(image=self.image)
binary_mask = np.random.randint(0, 2, size=(10, 10)).astype(bool)
visualizer.draw_bboxes(torch.tensor([1, 1, 2, 2])). \
draw_texts('test', torch.tensor([5, 5])). \
draw_lines(x_datas=torch.tensor([1, 5]),
y_datas=torch.tensor([2, 6])). \
draw_circles(torch.tensor([1, 5]), radius=torch.tensor([2])). \
draw_polygons(torch.tensor([[1, 1], [2, 2], [3, 4]])). \
draw_binary_masks(binary_mask)
def test_get_backend(self):
visualizer = Visualizer(
image=self.image,
vis_backends=copy.deepcopy(self.vis_backend_cfg),
save_dir='temp_dir')
for name in ['mock1', 'mock2']:
assert isinstance(visualizer.get_backend(name), MockVisBackend)
def test_add_config(self):
visualizer = Visualizer(
vis_backends=copy.deepcopy(self.vis_backend_cfg),
save_dir='temp_dir')
cfg = Config(dict(a=1, b=dict(b1=[0, 1])))
visualizer.add_config(cfg)
for name in ['mock1', 'mock2']:
assert visualizer.get_backend(name)._add_config is True
def test_add_graph(self):
visualizer = Visualizer(
vis_backends=copy.deepcopy(self.vis_backend_cfg),
save_dir='temp_dir')
class Model(nn.Module):
def __init__(self):
super().__init__()
self.conv = nn.Conv2d(1, 2, 1)
def forward(self, x, y=None):
return self.conv(x)
visualizer.add_graph(Model(), np.zeros([1, 1, 3, 3]))
for name in ['mock1', 'mock2']:
assert visualizer.get_backend(name)._add_graph is True
def test_add_image(self):
image = np.random.randint(0, 256, size=(10, 10, 3)).astype(np.uint8)
visualizer = Visualizer(
vis_backends=copy.deepcopy(self.vis_backend_cfg),
save_dir='temp_dir')
visualizer.add_image('img', image)
for name in ['mock1', 'mock2']:
assert visualizer.get_backend(name)._add_image is True
def test_add_scalar(self):
visualizer = Visualizer(
vis_backends=copy.deepcopy(self.vis_backend_cfg),
save_dir='temp_dir')
visualizer.add_scalar('map', 0.9, step=0)
for name in ['mock1', 'mock2']:
assert visualizer.get_backend(name)._add_scalar is True
def test_add_scalars(self):
visualizer = Visualizer(
vis_backends=copy.deepcopy(self.vis_backend_cfg),
save_dir='temp_dir')
input_dict = {'map': 0.7, 'acc': 0.9}
visualizer.add_scalars(input_dict)
for name in ['mock1', 'mock2']:
assert visualizer.get_backend(name)._add_scalars is True
def test_get_instance(self):
class DetLocalVisualizer(Visualizer):
def __init__(self, name):
super().__init__(name)
visualizer1 = DetLocalVisualizer.get_instance('name1')
visualizer2 = Visualizer.get_current_instance()
visualizer3 = DetLocalVisualizer.get_current_instance()
assert id(visualizer1) == id(visualizer2) == id(visualizer3)
def test_data_info(self):
visualizer = Visualizer()
visualizer.dataset_meta = {'class': 'cat'}
assert visualizer.dataset_meta['class'] == 'cat'