-
Notifications
You must be signed in to change notification settings - Fork 378
/
test_semantic_segmentation_label_store.py
129 lines (105 loc) · 4.51 KB
/
test_semantic_segmentation_label_store.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
from typing import Callable
from os.path import join
import unittest
import numpy as np
from rastervision.pipeline.file_system.utils import get_tmp_dir, file_exists
from rastervision.core.box import Box
from rastervision.core.data import (
BuildingVectorOutputConfig, ClassConfig, IdentityCRSTransformer,
PolygonVectorOutputConfig, SemanticSegmentationLabelStore,
SemanticSegmentationSmoothLabels, VectorOutputConfig)
from tests.core.data.label.test_semantic_segmentation_labels import (
make_random_scores)
class TestVectorOutputConfig(unittest.TestCase):
def test_get_uri(self):
# w/o ClasConfig
cfg = VectorOutputConfig(class_id=0)
self.assertEqual(cfg.get_uri('abc/def'), 'abc/def/class-0.json')
# w/ ClasConfig
class_config = ClassConfig(names=['a', 'b'])
cfg = VectorOutputConfig(class_id=0)
self.assertEqual(
cfg.get_uri('abc/def', class_config), 'abc/def/class-0-a.json')
cfg = VectorOutputConfig(class_id=1)
self.assertEqual(
cfg.get_uri('abc/def', class_config), 'abc/def/class-1-b.json')
class TestPolygonVectorOutputConfig(unittest.TestCase):
def test_denoise(self):
mask = np.zeros((100, 100), dtype=np.uint8)
mask[10:20, 10:20] = 1
mask[60:65, 60:65] = 1
# denoise = 0
cfg = PolygonVectorOutputConfig(class_id=0, denoise=0)
polys = list(cfg.vectorize(mask))
self.assertEqual(len(polys), 2)
# denoise = 8
cfg = PolygonVectorOutputConfig(class_id=0, denoise=8)
polys = list(cfg.vectorize(mask))
self.assertEqual(len(polys), 1)
class TestBuildingVectorOutputConfig(unittest.TestCase):
def test_denoise(self):
mask = np.zeros((100, 100), dtype=np.uint8)
mask[10:20, 10:20] = 1
mask[60:65, 60:65] = 1
# denoise = 0
cfg = BuildingVectorOutputConfig(class_id=0, denoise=0)
polys = list(cfg.vectorize(mask))
self.assertEqual(len(polys), 2)
# denoise = 8
cfg = BuildingVectorOutputConfig(class_id=0, denoise=8)
polys = list(cfg.vectorize(mask))
self.assertEqual(len(polys), 1)
class TestSemanticSegmentationLabelStore(unittest.TestCase):
def assertNoError(self, fn: Callable, msg: str = ''):
try:
fn()
except Exception:
self.fail(msg)
def test_saving_and_loading(self):
with get_tmp_dir() as tmp_dir:
class_config = ClassConfig(names=['bg', 'fg'], null_class='bg')
label_store = SemanticSegmentationLabelStore(
uri=tmp_dir,
crs_transformer=IdentityCRSTransformer(),
class_config=class_config,
bbox=None,
smooth_output=True,
smooth_as_uint8=True,
vector_outputs=[
PolygonVectorOutputConfig(class_id=1, threshold=0.3)
])
labels = SemanticSegmentationSmoothLabels(
extent=Box(0, 0, 10, 10), num_classes=len(class_config))
labels.pixel_scores += make_random_scores(
len(class_config), 10, 10)
labels.pixel_hits += 1
label_store.save(labels)
self.assertTrue(file_exists(join(tmp_dir, 'labels.tif')))
self.assertTrue(file_exists(join(tmp_dir, 'scores.tif')))
self.assertTrue(file_exists(join(tmp_dir, 'pixel_hits.npy')))
del label_store
# test compatibility validation
args = dict(
uri=tmp_dir,
crs_transformer=IdentityCRSTransformer(),
class_config=ClassConfig(names=['bg', 'fg', 'null']),
smooth_output=True,
smooth_as_uint8=True,
)
with self.assertRaises(FileExistsError):
label_store = SemanticSegmentationLabelStore(**args)
args = dict(
uri=tmp_dir,
crs_transformer=IdentityCRSTransformer(),
class_config=class_config,
smooth_output=True,
smooth_as_uint8=True,
)
label_store = SemanticSegmentationLabelStore(**args)
self.assertIsNotNone(label_store.label_source)
self.assertIsNotNone(label_store.score_source)
self.assertNoError(lambda: label_store.get_labels())
self.assertNoError(lambda: label_store.get_scores())
self.assertNoError(lambda: label_store.save(labels))
if __name__ == '__main__':
unittest.main()