-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathreadSAT.py
86 lines (73 loc) · 3.7 KB
/
readSAT.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
import os
import numpy as np
import scipy.io as sio
from operator import truediv
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
def loadata(name, compressed=False):
comp = ''
if compressed:
comp = '_compressed'
if name == 'IP':
if compressed:
dat = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Data', 'Indian_pines_compressed.mat'))['indian_pines']
else:
dat = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Data', 'Indian_pines_corrected.mat'))['indian_pines_corrected']
label = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Data', 'Indian_pines_gt.mat'))['indian_pines_gt']
return dat, label
elif name == 'SA':
dat = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Data', 'Salinas_corrected' + comp + '.mat'))['salinas_corrected']
label = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Data', 'Salinas_gt.mat'))['salinas_gt']
return dat, label
elif name == 'PU':
dat = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Data', 'PaviaU' + comp + '.mat'))['paviaU']
label = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Data', 'PaviaU_gt.mat'))['paviaU_gt']
return dat, label
elif name == 'KSC':
dat = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Data', 'KSC' + comp + '.mat'))['KSC']
label = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Data', 'KSC_gt.mat'))['KSC_gt']
return dat, label
elif name == 'BSW':
dat = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Botswana' + comp + '.mat'))['Botswana']
label = sio.loadmat(os.path.join(os.path.dirname(__file__), 'Botswana_gt.mat'))['Botswana_gt']
return dat, label
def padWithZeros(Xc, margin=2):
newX = np.zeros((Xc.shape[0] + 2 * margin, Xc.shape[1] + 2 * margin, Xc.shape[2]))
x_offset = margin
y_offset = margin
newX[x_offset:Xc.shape[0] + x_offset, y_offset:Xc.shape[1] + y_offset, :] = Xc
return newX
def createImageCubes(Xc, yc, window=5, removeZeroLabels=True):
margin = int((window - 1) / 2)
zeroPaddedX = padWithZeros(Xc, margin=margin)
# split patches
patchesData = np.zeros((Xc.shape[0] * Xc.shape[1], window, window, Xc.shape[2]))
patchesLabels = np.zeros((Xc.shape[0] * Xc.shape[1]))
patchIndex = 0
for r in range(margin, zeroPaddedX.shape[0] - margin):
for c in range(margin, zeroPaddedX.shape[1] - margin):
patch = zeroPaddedX[r - margin:r + margin + 1, c - margin:c + margin + 1]
patchesData[patchIndex, :, :, :] = patch
patchesLabels[patchIndex] = yc[r - margin, c - margin]
patchIndex = patchIndex + 1
if removeZeroLabels:
patchesData = patchesData[patchesLabels > 0, :, :, :]
patchesLabels = patchesLabels[patchesLabels > 0]
patchesLabels -= 1
return patchesData, patchesLabels
def applyPCA(Xc, numComponents=75):
newX = np.reshape(Xc, (-1, Xc.shape[2]))
pcaC = PCA(n_components=numComponents, whiten=True)
newX = pcaC.fit_transform(newX)
newX = np.reshape(newX, (Xc.shape[0], Xc.shape[1], numComponents))
return newX, pcaC
def AA_andEachClassAccuracy(confusion_m):
list_diag = np.diag(confusion_m)
list_raw_sum = np.sum(confusion_m, axis=1)
each_ac = np.nan_to_num(truediv(list_diag, list_raw_sum))
average_acc = np.mean(each_ac)
return each_ac, average_acc
def splitraintestset(X, y, testRatio, randomState=345):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=testRatio, random_state=randomState,
stratify=y)
return X_train, X_test, y_train, y_test