-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnmf_test.py
More file actions
283 lines (235 loc) · 8.06 KB
/
nmf_test.py
File metadata and controls
283 lines (235 loc) · 8.06 KB
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
import numpy as np
import scipy.io as sio
from scipy.optimize import nnls
import os
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import time
from scipy.sparse.linalg import svds
# from sklearn.linear_model import LassoLars
# from scipy.linalg import svdvals
# Custom libraries
from getSynData import get_syn_data as getSynData
from mvcnmf import mvcnmf_secord as mvcnmf
from vca import vca
def print_summary(array, name):
print("---------------------------")
print(f"Size of {name}: {array.shape}")
print(f"Minimum value: {np.min(array):.2f}")
print(f"Maximum value: {np.max(array):.2f}")
print(f"Mean value: {np.mean(array):.2f}")
print(f"Standard deviation: {np.std(array):.2f}")
print("---------------------------")
np.random.seed(0) # Set the seed for reproducibility
# --- Parameters --- %
# input parameters
# Synthetic Data
# use_synthetic_data = True
# input_mat_name = "A.mat"
# bands_mat_name = "BANDS.mat"
# Landsat Data
use_synthetic_data = False
input_mat_name = 'Landsat_separate_images_BR_R002.mat'
# input_mat_name = 'Landsat.mat'
# mvcnmf parameters
c = 5 # number of endmembers
SNR = 20 # dB
tol = 1e-6
maxiter = 150
T = 0.015
showflag = False
verbose = True
# --- read data --- %
input_path = os.path.join("../inputs", input_mat_name)
# Load the list of variables in the .mat file
variables = [var for var in sio.whosmat(input_path)
if var not in ['__header__', '__version__', '__globals__']]
if c == 1:
raise ValueError("c must be greater than 1")
# start the timer
tic = time.time()
# loop through the variables
for i in range(len(variables)):
print("#########################################")
print(f"Processing {i+1}/{len(variables)} images")
print("#########################################")
# variable_name = variables[i][0]
variable_name = "BR_R002_23KPR00_2014_01_09"
# Load the first variable in the list
loaded_variable = sio.loadmat(input_path)
# Set variable A equal to the loaded variable
A = loaded_variable[variable_name].astype('float64')
if use_synthetic_data:
# Load bands
try:
bands_path = os.path.join("../inputs", bands_mat_name)
bands_mat = sio.loadmat(bands_path)
BANDS = bands_mat["BANDS"].reshape(-1)
A = A[BANDS, :c]
except NameError:
A = A[:, :c]
# --- process --- %
# print_summary(A, "A")
if use_synthetic_data:
[synthetic, abf] = getSynData(A, 7, 0)
M, N, D = synthetic.shape
mixed = synthetic.reshape(M * N, D)
# add noise
variance = np.sum(mixed**2) / 10 ** (SNR / 10) / M / N / D
n = np.sqrt(variance) * np.random.randn(D, M * N)
mixed = mixed.T + n
del n
# remove noise
UU, SS, VV = svds(mixed, k=c)
UU = UU[:, ::-1] # Reverse the column order to match MATLAB output
Lowmixed = UU.T @ mixed
# print_summary(UU, "UU")
# print_summary(Lowmixed, "A")
mixed = UU @ Lowmixed
EM = UU.T @ A
# vca algorithm
A_vca, EndIdx = vca(mixed, p=c, SNR=SNR, verbose=verbose)
else:
# load data
M, N, D = A.shape
mixed = A.reshape(M * N, D)
mixed = mixed.T
# create an empty var for UU
UU = np.empty((0, 0))
# vca algorithm
A_vca, EndIdx = vca(mixed, p=c, verbose=verbose)
# FCLS
AA = np.vstack([1e-5 * A_vca, np.ones((1, A_vca.shape[1]))])
s_fcls = np.zeros((A_vca.shape[1], M * N))
for j in range(M * N):
r = np.hstack([1e-5 * mixed[:, j], [1]])
# s_fcls[:, j] = np.linalg.lstsq(AA, r, rcond=None)[0]
s_fcls[:, j] = nnls(AA, r)[0]
# use vca to initiate
Ainit = A_vca
sinit = s_fcls
# PCA
pca = PCA()
pca_score = pca.fit_transform(mixed.T)
PrinComp = pca.components_
meanData = np.mean(pca_score, axis=0)[:, np.newaxis].T
# use conjugate gradient to find A can speed up the learning
maxiter_str = f"{maxiter}"
Aest, sest = mvcnmf(
mixed,
Ainit,
sinit,
A,
UU,
PrinComp,
meanData,
T,
tol,
maxiter,
showflag,
2,
1,
use_synthetic_data,
)
# visualize endmembers in scatterplots
if showflag:
d = 4
Anmf = UU.T @ Aest
fig, axes = plt.subplots(d - 2, d - 2)
axes = np.ravel(axes)
index = 0
for i in range(d - 1):
for j in range(i + 1, d - 1):
ax = axes[index]
ax.plot(Lowmixed[i, ::6], Lowmixed[j, ::6], 'rx')
ax.plot(EM[i, :], EM[j, :], 'go', markerfacecolor='g')
ax.plot(Anmf[i, :], Anmf[j, :], 'bo', markerfacecolor='b')
index += 1
plt.show()
if use_synthetic_data == 1:
# permute results
CRD = np.corrcoef(np.hstack([A, Aest]))
DD = np.abs(CRD[c : 2 * c, :c])
perm_mtx = np.zeros((c, c))
aux = np.zeros((c, 1))
for i in range(c):
ld, cd = np.unravel_index(np.argmax(DD), DD.shape)
perm_mtx[ld, cd] = 1
DD[:, cd] = aux.squeeze()
DD[ld, :] = aux.squeeze().T
Aest = Aest @ perm_mtx
sest = (sest.T @ perm_mtx)
Sest = np.reshape(sest, (M, N, c))
sest = sest.T
# show the estimations
if showflag:
fig, axs = plt.subplots(c, 4)
for i in range(c):
axs[i, 0].plot(A[:, i], "r", label="True endmembers")
axs[i, 0].set_ylim(0, 1)
if i == 0:
axs[i, 0].set_title("True end-members")
axs[i, 1].plot(Aest[:, i], "g", label="Estimated endmembers")
axs[i, 1].legend()
axs[i, 1].set_ylim(0, 1)
if i == 0:
axs[i, 1].set_title("Estimated end-members")
axs[i, 2].imshow(abf[i, :].reshape(M, N))
if i == 0:
axs[i, 2].set_title("True abundance")
axs[i, 3].imshow(sest[i, :].reshape(M, N))
if i == 0:
axs[i, 3].set_title("Estimated abundance")
plt.show()
# quantitative evaluation of spectral signature and abundance
if use_synthetic_data:
# rmse error of abundances
E_rmse = np.sqrt(np.sum((abf - sest) ** 2) / (M * N * c))
print("E_rmse", E_rmse)
# the angle between abundances
nabf = np.diag(abf @ abf.T)
nsest = np.diag(sest @ sest.T)
ang_beta = (
180
/ np.pi
* np.arccos(np.diag(abf @ sest.T) / np.sqrt(nabf * nsest))
)
E_aad = np.sqrt(np.mean(ang_beta**2))
print("E_aad", E_aad)
# cross entropy between abundance
E_entropy = np.sum(
abf * np.log((abf + 1e-9) / (sest + 1e-9))
) + np.sum(sest * np.log((sest + 1e-9) / (abf + 1e-9)))
E_aid = np.sqrt(np.mean(E_entropy**2))
print("E_aid", E_aid)
# the angle between material signatures
nA = np.diag(A.T @ A)
nAest = np.diag(Aest.T @ Aest)
ang_theta = (
180 / np.pi * np.arccos(np.diag(A.T @ Aest) / np.sqrt(nA * nAest))
)
E_sad = np.sqrt(np.mean(ang_theta**2))
print("E_sad", E_sad)
# the spectral information divergence
pA = A / (np.sum(A, axis=0))
qA = Aest / (np.sum(Aest, axis=0))
qA = np.abs(qA)
SID = np.sum(pA * np.log((pA + 1e-9) / (qA + 1e-9))) + np.sum(
qA * np.log((qA + 1e-9) / (pA + 1e-9))
)
E_sid = np.sqrt(np.mean(SID**2))
print("E_sid", E_sid)
# Save output
# keep only 2 digits after the decimal point
T_str = f"{T:.4f}"
outputFileName = (
f"../outputs/output_{variable_name}_max_iter{maxiter}_T{T_str}.mat"
)
if use_synthetic_data:
data = {"Aest": Aest, "sest": sest, "E_rmse": E_rmse,
"E_aad": E_aad, "E_aid": E_aid, "E_sad": E_sad,
"E_sid": E_sid}
else:
data = {"Aest": Aest, "sest": sest}
sio.savemat(outputFileName, data, do_compression=True)
break