-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexamples.py
298 lines (225 loc) · 8.13 KB
/
examples.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
"""
Module examples
Requires:
numpy
"""
from __future__ import annotations
import numpy as np
from matplotlib.colors import ListedColormap
from lapixdl.evaluation.evaluate import evaluate_classification
from lapixdl.evaluation.evaluate import evaluate_detection
from lapixdl.evaluation.evaluate import evaluate_segmentation
from lapixdl.evaluation.model import Classification
from lapixdl.evaluation.model import Result
from lapixdl.evaluation.visualize import show_classifications
from lapixdl.evaluation.visualize import show_detections
from lapixdl.evaluation.visualize import show_segmentations
from lapixdl.formats.annotation import BBox
def main():
# Model evaluation examples
evaluate_segmentation_example()
evaluate_detection_example()
evaluate_classification_example()
# Results visualization examples
show_classification_example()
show_segmentation_example()
show_detection_example()
def evaluate_segmentation_example():
# Class names - Background must be at 0 index
classes = ['bkg', 'kite', 'person']
# Image shape
mask_shape = (480, 640)
# Creating fake data
# Creates a rectangle of 1s in a 0s array
gt_bbox_1 = BBox(10, 10, 10, 10, 1)
mask_bin_GT_1 = draw_rectangle(
np.zeros(mask_shape, np.int),
gt_bbox_1.upper_left_point,
gt_bbox_1.bottom_right_point,
1,
)
pred_bbox_1 = BBox(10, 10, 10, 10, 1)
mask_bin_pred_1 = draw_rectangle(
np.zeros(mask_shape, np.int),
pred_bbox_1.upper_left_point,
pred_bbox_1.bottom_right_point,
1,
)
# Creates a rectangle of 2s in a 0s array
gt_bbox_2 = BBox(110, 110, 320, 280, 2)
mask_bin_GT_2 = draw_rectangle(
np.zeros(mask_shape, np.int),
gt_bbox_2.upper_left_point,
gt_bbox_2.bottom_right_point,
2,
)
pred_bbox_2 = BBox(70, 50, 240, 220, 2)
mask_bin_pred_2 = draw_rectangle(
np.zeros(mask_shape, np.int),
pred_bbox_2.upper_left_point,
pred_bbox_2.bottom_right_point,
2,
)
# Merging masks
mask_GT = np.maximum(mask_bin_GT_1, mask_bin_GT_2)
mask_pred = np.maximum(mask_bin_pred_1, mask_bin_pred_2)
# Creating data suplier iterator
# It is not necessary here, but it's useful if you want to yield data
# from the disk i.e. from a Pytorch DataLoader
it_gt_masks = identity_iterator(mask_GT)
it_pred_masks = identity_iterator(mask_pred)
# Calculates and shows metrics
metrics = evaluate_segmentation(it_gt_masks, it_pred_masks, classes)
# Shows confusion matrix and returns its Figure and Axes
fig, axes = metrics.show_confusion_matrix()
# Shows confusion matrix for class `a`
metrics.by_class[0].show_confusion_matrix()
def evaluate_detection_example():
# Class names
classes = ['kite', 'person']
# Image shape
# mask_shape = (480, 640)
# Creating fake data
gt_bbox_1 = BBox(10, 10, 10, 10, 0, 1)
pred_bbox_1 = BBox(10, 10, 10, 10, 0, 1)
gt_bbox_2 = BBox(110, 110, 320, 280, 1, 1)
pred_bbox_2 = BBox(70, 50, 240, 220, 1, 1)
# Creating data suplier iterator
# It is not necessary here, but it's useful if you want to yield data
# from the disk i.e. from a Pytorch DataLoader
it_gt_masks = identity_iterator([gt_bbox_1, gt_bbox_2])
it_pred_masks = identity_iterator([pred_bbox_1, pred_bbox_2])
# Calculates and shows metrics
metrics = evaluate_detection(it_gt_masks, it_pred_masks, classes)
# Shows confusion matrix and returns its Figure and Axes
# fig, axes = metrics.show_confusion_matrix()
# Shows confusion matrix for class `a`
metrics.by_class[0].show_confusion_matrix()
def evaluate_classification_example():
# Class names
classes = ['a', 'b', 'c']
# Classifications based in array
gt_class = [
Classification(x) for x in [
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
]
]
# All predictions with .8 score
pred_class = [
Classification(x, .8) for x in [
0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 2, 2, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2, 2,
]
]
# Calculates and shows metrics
metrics = evaluate_classification(gt_class, pred_class, classes)
# Shows confusion matrix and returns its Figure and Axes
fig, axes = metrics.show_confusion_matrix()
# Shows confusion matrix for class `a`
metrics.by_class[0].show_confusion_matrix()
def show_classification_example():
# Class names
classes = ['a', 'b', 'c']
# Classifications based in array
gt_class = [
Classification(x) for x in [
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
]
]
# All predictions with .8 score
pred_class = [
Classification(x, .8) for x in [
0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 2, 2, 1, 1, 0, 0, 0, 2, 2,
]
]
# Convert to results
results = [
Result(random_image(), gt, pred)
for gt, pred in zip(gt_class, pred_class)
]
# GT only result
results = [Result(random_image(), Classification(2))] + results
# Shows results and returns its Figure and Axes
fig, axes = show_classifications(results, classes, 5)
def show_segmentation_example():
# Class names
classes = ['bkg', 'kite', 'person', 'car']
# Image/mask shape
mask_shape = (480, 640)
# Multiclass mask creation
gt_bbox_1 = BBox(10, 10, 100, 100, 1)
mask_bin_GT_1 = draw_bboxes(mask_shape, [gt_bbox_1])
pred_bbox_1 = BBox(10, 10, 100, 100, 1)
mask_bin_pred_1 = draw_bboxes(mask_shape, [pred_bbox_1])
gt_bbox_2 = BBox(110, 110, 320, 280, 2)
mask_bin_GT_2 = draw_bboxes(mask_shape, [gt_bbox_2])
pred_bbox_2 = BBox(70, 50, 240, 220, 2)
mask_bin_pred_2 = draw_bboxes(mask_shape, [pred_bbox_2])
gt_bbox_3 = BBox(300, 300, 100, 100, 3)
mask_bin_GT_3 = draw_bboxes(mask_shape, [gt_bbox_3])
mask_multi_GT = np.maximum(
np.maximum(
mask_bin_GT_1, mask_bin_GT_2 * 2,
), mask_bin_GT_3 * 3,
)
mask_multi_pred = np.maximum(mask_bin_pred_1, mask_bin_pred_2 * 2)
# Convert to results
results = [
Result(
random_image(mask_shape[0], mask_shape[1]),
mask_multi_GT, mask_multi_pred,
),
]
# GT only result
results = [
Result(random_image(mask_shape[0], mask_shape[1]), mask_multi_GT),
] + results
# Custom color map
cmap = ListedColormap(['#ff0000', '#00ff00', '#0000ff', '#ffffff'])
# Shows results and returns its Figure and Axes
fig, axes = show_segmentations(results, classes, cmap=cmap)
def show_detection_example():
# Class names
classes = ['kite', 'person', 'car']
# Image shape
img_shape = (480, 640)
# Bboxes creation
gt_bbox_1 = BBox(10, 10, 100, 100, 0)
pred_bbox_1 = BBox(10, 10, 100, 100, 0, .8212158464)
gt_bbox_2 = BBox(110, 110, 320, 280, 1)
pred_bbox_2 = BBox(70, 50, 240, 220, 1, .34844545)
gt_bbox_3 = BBox(300, 300, 100, 100, 2)
# Convert to results
results = [
Result(
random_image(img_shape[0], img_shape[1]), [
gt_bbox_1, gt_bbox_2, gt_bbox_3,
], [pred_bbox_1, pred_bbox_2],
),
]
# GT only result
results = [
Result(
random_image(img_shape[0], img_shape[1]), [
gt_bbox_1, gt_bbox_2, gt_bbox_3,
],
),
] + results
# Shows results and returns its Figure and Axes
fig, axes = show_detections(results, classes, show_bbox_label=False)
def identity_iterator(value):
yield value
def random_image(h=None, w=None):
return (np.random.rand(h or 200, w or 400, 3) * 125).astype(np.int8)
def draw_bboxes(mask_shape, bboxes):
mask = np.zeros(mask_shape, np.int)
for bbox in bboxes:
mask[
bbox.upper_left_point[0]:bbox.bottom_right_point[0] + 1,
bbox.upper_left_point[1]:bbox.bottom_right_point[1] + 1,
] = 1
return mask
def draw_rectangle(img: np.ndarray, pt1: tuple[int, int], pt2: tuple[int, int], fill: int):
cp = img.copy()
cp[slice(pt1[0], pt2[0] + 1), slice(pt1[1], pt2[1] + 1)] = fill
return cp
main()