-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhowhardisIoU.py
94 lines (73 loc) · 2.67 KB
/
howhardisIoU.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
import os
import sys
import numpy as np
import PIL
from PIL import Image
from sklearn.metrics import confusion_matrix
import torch
import torch.backends.cudnn as cudnn
device = "cuda" if torch.cuda.is_available() else "cpu"
if device == "cuda":
torch.cuda.empty_cache()
cudnn.benchmark = True
whereIam = os.uname()[1]
print("massif benchmark")
import dataloader
if whereIam == "super":
miniworld = dataloader.MiniWorld(
flag="custom", custom=["potsdam/test", "bruges/test"]
)
else:
miniworld = dataloader.MiniWorld("test")
def accu(cm):
return 100.0 * (cm[0][0] + cm[1][1]) / np.sum(cm)
def f1(cm):
return 50.0 * cm[0][0] / (cm[0][0] + cm[1][0] + cm[0][1]) + 50.0 * cm[1][1] / (
cm[1][1] + cm[1][0] + cm[0][1]
)
cm = {}
with torch.no_grad():
for town in miniworld.towns:
print(town)
cm[town] = np.zeros((3, 3), dtype=int)
for i in range(miniworld.data[town].nbImages):
imageraw, label = miniworld.data[town].getImageAndLabel(i)
if False:
# 100%
pred = label.copy()
label = dataloader.convertIn3classNP(label)
if False:
# 100%
pred = dataloader.convertIn3classNP(label).astype(int)
pred = np.abs(pred - 1)
pred = np.uint8(np.abs(1 - pred))
label = dataloader.convertIn3classNP(label)
if True:
# ONLY AROUND 90%
pred = dataloader.convertIn3classNP(label).astype(int)
pred = np.abs(pred - 1)
pred = np.uint8(np.abs(1 - pred))
assert label.shape == pred.shape
cm[town] += confusion_matrix(
label.flatten(), pred.flatten(), labels=[0, 1, 2]
)
if True:
imageraw = PIL.Image.fromarray(np.uint8(imageraw))
imageraw.save("build/" + town[0:-5] + "_" + str(i) + "_x.png")
labelim = PIL.Image.fromarray(np.uint8(label) * 125)
labelim.save("build/" + town[0:-5] + "_" + str(i) + "_y.png")
predim = PIL.Image.fromarray(np.uint8(pred) * 125)
predim.save("build/" + town[0:-5] + "_" + str(i) + "_z.png")
cm[town] = cm[town][0:2, 0:2]
print(cm[town][0][0], cm[town][0][1], cm[town][1][0], cm[town][1][1])
print(
accu(cm[town]),
f1(cm[town]),
)
print("-------- results ----------")
for town in miniworld.towns:
print(town, accu(cm[town]), f1(cm[town]))
globalcm = np.zeros((2, 2), dtype=int)
for town in miniworld.towns:
globalcm += cm[town]
print("miniworld", accu(globalcm), f1(globalcm))