-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_cls_map.py
136 lines (121 loc) · 4.43 KB
/
get_cls_map.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
import numpy as np
import matplotlib.pyplot as plt
def get_classification_map(y_pred, y):
height = y.shape[0]
width = y.shape[1]
k = 0
cls_labels = np.zeros((height, width))
for i in range(height):
for j in range(width):
target = int(y[i, j])
if target == 0:
continue
else:
cls_labels[i][j] = y_pred[k]+1
k += 1
return cls_labels
def list_to_colormap(x_list):
y = np.zeros((x_list.shape[0], 3))
for index, item in enumerate(x_list):
'''
if item == 0:
y[index] = np.array([0, 0, 0]) / 255.
if item == 1:
y[index] = np.array([216, 191, 216]) / 255.
if item == 2:
y[index] = np.array([0, 255, 0]) / 255.
if item == 3:
y[index] = np.array([0, 255, 255]) / 255.
if item == 4:
y[index] = np.array([45, 138, 86]) / 255.
if item == 5:
y[index] = np.array([255, 0, 255]) / 255.
if item == 6:
y[index] = np.array([255, 165, 0]) / 255.
if item == 7:
y[index] = np.array([159, 31, 239]) / 255.
if item == 8:
y[index] = np.array([255, 0, 0]) / 255.
if item == 9:
y[index] = np.array([255, 255, 0]) / 255.
'''
if item == 0:
y[index] = np.array([0, 0, 0]) / 255.
if item == 1:
y[index] = np.array([140, 67, 46]) / 255.
if item == 2:
y[index] = np.array([0, 0, 255]) / 255.
if item == 3:
y[index] = np.array([255, 100, 0]) / 255.
if item == 4:
y[index] = np.array([0, 255, 200]) / 255.
if item == 5:
y[index] = np.array([164, 75, 155]) / 255.
if item == 6:
y[index] = np.array([101, 174, 255]) / 255.
if item == 7:
y[index] = np.array([118, 254, 254]) / 255.
if item == 8:
y[index] = np.array([60, 91, 112]) / 255.
if item == 9:
y[index] = np.array([255, 255, 0]) / 255.
if item == 10:
y[index] = np.array([255, 255, 255]) / 255.
if item == 11:
y[index] = np.array([255, 0, 255]) / 255.
if item == 12:
y[index] = np.array([100, 0, 255]) / 255.
if item == 13:
y[index] = np.array([0, 200, 254]) / 255.
if item == 14:
y[index] = np.array([0, 255, 0]) / 255.
if item == 15:
y[index] = np.array([171, 175, 80]) / 255.
if item == 16:
y[index] = np.array([101, 193, 60]) / 255.
return y
def classification_map(map, ground_truth, dpi, save_path):
fig = plt.figure(frameon=False)
fig.set_size_inches(ground_truth.shape[1]*2.0/dpi, ground_truth.shape[0]*2.0/dpi)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
fig.add_axes(ax)
ax.imshow(map)
fig.savefig(save_path, dpi=dpi)
return 0
def test(device, net, test_loader):
count = 0
# 模型测试
net.eval()
y_pred_test = 0
y_test = 0
for inputs, labels in test_loader:
inputs = inputs.to(device)
outputs,_ = net(inputs)
outputs = np.argmax(outputs.detach().cpu().numpy(), axis=1)
if count == 0:
y_pred_test = outputs
y_test = labels
count = 1
else:
y_pred_test = np.concatenate((y_pred_test, outputs))
y_test = np.concatenate((y_test, labels))
return y_pred_test, y_test
def get_cls_map(net, device, all_data_loader, y,oa):
y_pred, y_new = test(device, net, all_data_loader)
cls_labels = get_classification_map(y_pred, y)
x = np.ravel(cls_labels)
gt = y.flatten()
y_list = list_to_colormap(x)
y_gt = list_to_colormap(gt)
y_re = np.reshape(y_list, (y.shape[0], y.shape[1], 3))
gt_re = np.reshape(y_gt, (y.shape[0], y.shape[1], 3))
classification_map(y_re, y, 300,
'classification_maps/' + 'predictions.eps')
classification_map(y_re, y, 300,
'classification_maps/' + 'predictions_{}.png'.format(oa))
classification_map(gt_re, y, 300,
'classification_maps/' + 'NF_gt.png')
print('------Get classification maps successful-------')