-
Notifications
You must be signed in to change notification settings - Fork 0
/
HyperTools.py
222 lines (202 loc) · 7.98 KB
/
HyperTools.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
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio
def featureNormalize(X,type):
#type==1 x = (x-mean)/std(x)
#type==2 x = (x-max(x))/(max(x)-min(x))
if type==1:
mu = np.mean(X,0)
X_norm = X-mu
sigma = np.std(X_norm,0)
X_norm = X_norm/sigma
return X_norm
elif type==2:
minX = np.min(X,0)
maxX = np.max(X,0)
X_norm = X-minX
X_norm = X_norm/(maxX-minX)
return X_norm
def DrawResult(labels,imageID):
# DataName = {1:'PaviaU',2:'Salinas',3:'IndinaP',4:'HoustonU',5:'xqh';6:KSC}
num_class = int(labels.max())
if imageID == 1:
row = 610
col = 340
# palette = np.array([[216,191,216],
# [0,255,0],
# [0,255,255],
# [45,138,86],
# [255,0,255],
# [255,165,0],
# [159,31,239],
# [255,0,0],
# [255,255,0]])
palette = np.array([[192,192,192],
[0,255,0],
[0,255,255],
[0,128,0],
[255,0,255],
[165,82,41],
[128,0,128],
[255,0,0],
[255,255,0]])
palette = palette*1.0/255
elif imageID ==2:
row = 512
col = 217
# palette = np.array([[37, 58, 150],
# [47, 78, 161],
# [56, 87, 166],
# [56, 116, 186],
# [51, 181, 232],
# [112, 204, 216],
# [119, 201, 168],
# [148, 204, 120],
# [188, 215, 78],
# [238, 234, 63],
# [246, 187, 31],
# [244, 127, 33],
# [239, 71, 34],
# [238, 33, 35],
# [180, 31, 35],
# [123, 18, 20]])
palette = np.array([[0, 0, 255],
[255, 100, 0],
[0, 255, 135],
[150, 70, 150],
[100, 150, 255],
[60, 90, 115],
[255, 255, 125],
[255, 0, 255],
[100, 0, 255],
[0, 170, 255],
[0, 255, 3],
[175, 175, 80],
[100, 190, 55],
[140, 70, 50],
[115, 255, 170],
[255, 255, 0]])
palette = palette*1.0/255
elif imageID ==3:
row = 145
col = 145
# palette = np.array([[37, 58, 150],
# [47, 78, 161],
# [56, 87, 166],
# [56, 116, 186],
# [51, 181, 232],
# [112, 204, 216],
# [119, 201, 168],
# [148, 204, 120],
# [188, 215, 78],
# [238, 234, 63],
# [246, 187, 31],
# [244, 127, 33],
# [239, 71, 34],
# [238, 33, 35],
# [180, 31, 35],
# [123, 18, 20]])
palette = np.array([[140, 67, 46],
[0, 0, 255],
[255, 100, 0],
[0, 255, 123],
[164, 75, 155],
[101, 174, 255],
[118, 254, 172],
[60, 91, 112],
[255, 255, 0],
[255, 255, 125],
[255, 0, 255],
[100, 0, 255],
[0, 172, 254],
[0, 255, 0],
[171, 175, 80],
[101, 193, 60]])
palette = palette*1.0/255
elif imageID ==5:
row = 456
col = 352
palette = np.array([[0,172,255],
[255,0,255],
[0,255,122],
[255,255,122],
[255,100,0],
[140,67,45]])
palette = palette*1.0/255
X_result = np.zeros((labels.shape[0],3))
for i in range(1,num_class+1):
X_result[np.where(labels==i),0] = palette[i-1,0]
X_result[np.where(labels==i),1] = palette[i-1,1]
X_result[np.where(labels==i),2] = palette[i-1,2]
X_result = np.reshape(X_result,(row,col,3))
plt.axis ( "off" )
plt.imshow(X_result)
return X_result
def CalAccuracy(predict,label):
n = label.shape[0]
OA = np.sum(predict==label)*1.0/n
correct_sum = np.zeros((max(label)+1))
reali = np.zeros((max(label)+1))
predicti = np.zeros((max(label)+1))
producerA = np.zeros((max(label)+1))
for i in range(0,max(label)+1):
correct_sum[i] = np.sum(label[np.where(predict==i)]==i)
reali[i] = np.sum(label==i)
predicti[i] = np.sum(predict==i)
producerA[i] = correct_sum[i] / reali[i]
Kappa = (n*np.sum(correct_sum) - np.sum(reali * predicti)) *1.0/ (n*n - np.sum(reali * predicti))
AA = np.average(producerA)
return OA,AA,Kappa,producerA
def LoadHSI(dataID=1,num_label=60):
#ID=1:Pavia University
#ID=2:Salinas
if dataID==1:
data = sio.loadmat('./Data/PaviaU.mat')
X = data['paviaU']
data = sio.loadmat('./Data/PaviaU_gt.mat')
Y = data['paviaU_gt']
elif dataID==2:
data = sio.loadmat('./Data/Salinas_corrected.mat')
X = data['salinas_corrected']
data = sio.loadmat('./Data/Salinas_gt.mat')
Y = data['salinas_gt']
elif dataID == 3:
data = sio.loadmat('./Data/Indian_pines_corrected.mat')
X = data['indian_pines_corrected']
data = sio.loadmat('./Data/Indian_pines_gt.mat')
Y = data['indian_pines_gt']
num_label = [20,80,80,80,80,80,15,80,10,80,80,80,80,80,50,50]
elif dataID == 4:
data = sio.loadmat('./Data/Houston.mat')
X = data['Houston']
data = sio.loadmat('./Data/Houston_gt.mat')
Y = data['Houston_gt']
elif dataID == 5:
data = sio.loadmat('./Data/xqh.mat')
X = data['xqh']
data = sio.loadmat('./Data/xqh_gt.mat')
Y = data['xqh_gt']
[row,col, n_feature] = X.shape
K = row * col
X = X.reshape(K, n_feature)
n_class = Y.max()
X = featureNormalize(X,2)
X = np.reshape(X,(row, col, n_feature))
X = np.moveaxis(X,-1,0)
Y = Y.reshape(K,).astype('int')
for i in range(1,n_class+1):
index = np.where(Y==i)[0]
n_data = index.shape[0]
np.random.seed(12345)
randomArray_label = np.random.permutation(n_data)
if dataID == 3:
train_num = num_label[i-1]
else:
train_num = num_label
if i==1:
train_array = index[randomArray_label[0:train_num]]
test_array = index[randomArray_label[train_num:n_data]]
else:
train_array = np.append(train_array,index[randomArray_label[0:train_num]])
test_array = np.append(test_array,index[randomArray_label[train_num:n_data]])
return X, Y, train_array, test_array