-
Notifications
You must be signed in to change notification settings - Fork 28
/
Main.py
525 lines (461 loc) · 25.3 KB
/
Main.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt
import random
from matplotlib import cm
import spectral as spy
from sklearn import metrics
import time
from sklearn import preprocessing
import torch
import LDA_SLIC
import CEGCN
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# FLAG =1, indian
# FLAG =2, paviaU
# FLAG =3, salinas
samples_type = ['ratio', 'same_num'][0]
# for (FLAG, curr_train_ratio) in [(1,5),(1,10),(1,15),(1,20),(1,25),
# (2,5),(2,10),(2,15),(2,20),(2,25),
# (3,5),(3,10),(3,15),(3,20),(3,25)]:
for (FLAG, curr_train_ratio,Scale) in [(1,0.1,100)]:
# for (FLAG, curr_train_ratio,Scale) in [(2,0.01,100),(3,0.01,100)]:
torch.cuda.empty_cache()
OA_ALL = []
AA_ALL = []
KPP_ALL = []
AVG_ALL = []
Train_Time_ALL=[]
Test_Time_ALL=[]
Seed_List=[0,1,2,3,4]#随机种子点
if FLAG == 1:
data_mat = sio.loadmat('..\\HyperImage_data\\indian\\Indian_pines_corrected.mat')
data = data_mat['indian_pines_corrected']
gt_mat = sio.loadmat('..\\HyperImage_data\\indian\\Indian_pines_gt.mat')
gt = gt_mat['indian_pines_gt']
# 参数预设
# train_ratio = 0.05 # 训练集比例。注意,训练集为按照‘每类’随机选取
val_ratio = 0.01 # 测试集比例.注意,验证集选取为从测试集整体随机选取,非按照每类
class_count = 16 # 样本类别数
learning_rate = 5e-4 # 学习率
max_epoch =600 # 迭代次数
dataset_name = "indian_" # 数据集名称
# superpixel_scale=100
pass
if FLAG == 2:
data_mat = sio.loadmat('..\\HyperImage_data\\paviaU\\PaviaU.mat')
data = data_mat['paviaU']
gt_mat = sio.loadmat('..\\HyperImage_data\\paviaU\\Pavia_University_gt.mat')
gt = gt_mat['pavia_university_gt']
# 参数预设
# train_ratio = 0.01 # 训练集比例。注意,训练集为按照‘每类’随机选取
val_ratio = 0.01 # 测试集比例.注意,验证集选取为从测试集整体随机选取,非按照每类
class_count = 9 # 样本类别数
learning_rate = 5e-4 # 学习率
max_epoch = 600 # 迭代次数
dataset_name = "paviaU_" # 数据集名称
# superpixel_scale = 100
pass
if FLAG == 3:
data_mat = sio.loadmat('..\\HyperImage_data\\Salinas\\Salinas_corrected.mat')
data = data_mat['salinas_corrected']
gt_mat = sio.loadmat('..\\HyperImage_data\\Salinas\\Salinas_gt.mat')
gt = gt_mat['salinas_gt']
# 参数预设
# train_ratio = 0.01 # 训练集比例。注意,训练集为按照‘每类’随机选取
val_ratio = 0.01 # 测试集比例.注意,验证集选取为从测试集整体随机选取,非按照每类
class_count = 16 # 样本类别数
learning_rate = 5e-4 # 学习率
max_epoch = 600 # 迭代次数
dataset_name = "salinas_" # 数据集名称
# superpixel_scale = 100
pass
if FLAG == 4:
data_mat = sio.loadmat('..\\HyperImage_data\\KSC\\KSC.mat')
data = data_mat['KSC']
gt_mat = sio.loadmat('..\\HyperImage_data\\KSC\\KSC_gt.mat')
gt = gt_mat['KSC_gt']
# 参数预设
# train_ratio = 0.05 # 训练集比例。注意,训练集为按照‘每类’随机选取
val_ratio = 0.01 # 测试集比例.注意,验证集选取为从测试集整体随机选取,非按照每类
class_count = 13 # 样本类别数
learning_rate = 5e-4 # 学习率
max_epoch = 600 # 迭代次数
dataset_name = "KSC_" # 数据集名称
# superpixel_scale = 200
pass
###########
superpixel_scale=Scale#########################
train_samples_per_class = curr_train_ratio # 当定义为每类样本个数时,则该参数更改为训练样本数
val_samples = class_count
train_ratio = curr_train_ratio
cmap = cm.get_cmap('jet', class_count + 1)
plt.set_cmap(cmap)
m, n, d = data.shape # 高光谱数据的三个维度
# 数据standardization标准化,即提前全局BN
orig_data=data
height, width, bands = data.shape # 原始高光谱数据的三个维度
data = np.reshape(data, [height * width, bands])
minMax = preprocessing.StandardScaler()
data = minMax.fit_transform(data)
data = np.reshape(data, [height, width, bands])
# # 打印每类样本个数
# gt_reshape=np.reshape(gt, [-1])
# for i in range(class_count):
# idx = np.where(gt_reshape == i + 1)[-1]
# samplesCount = len(idx)
# print(samplesCount)
def Draw_Classification_Map(label, name: str, scale: float = 4.0, dpi: int = 400):
'''
get classification map , then save to given path
:param label: classification label, 2D
:param name: saving path and file's name
:param scale: scale of image. If equals to 1, then saving-size is just the label-size
:param dpi: default is OK
:return: null
'''
fig, ax = plt.subplots()
numlabel = np.array(label)
v = spy.imshow(classes=numlabel.astype(np.int16), fignum=fig.number)
ax.set_axis_off()
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
fig.set_size_inches(label.shape[1] * scale / dpi, label.shape[0] * scale / dpi)
foo_fig = plt.gcf() # 'get current figure'
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
foo_fig.savefig(name + '.png', format='png', transparent=True, dpi=dpi, pad_inches=0)
pass
def GT_To_One_Hot(gt, class_count):
'''
Convet Gt to one-hot labels
:param gt:
:param class_count:
:return:
'''
GT_One_Hot = [] # 转化为one-hot形式的标签
for i in range(gt.shape[0]):
for j in range(gt.shape[1]):
temp = np.zeros(class_count,dtype=np.float32)
if gt[i, j] != 0:
temp[int( gt[i, j]) - 1] = 1
GT_One_Hot.append(temp)
GT_One_Hot = np.reshape(GT_One_Hot, [height, width, class_count])
return GT_One_Hot
for curr_seed in Seed_List:
# step2:随机10%数据作为训练样本。方式:给出训练数据与测试数据的GT
random.seed(curr_seed)
gt_reshape = np.reshape(gt, [-1])
train_rand_idx = []
val_rand_idx = []
if samples_type == 'ratio':
for i in range(class_count):
idx = np.where(gt_reshape == i + 1)[-1]
samplesCount = len(idx)
rand_list = [i for i in range(samplesCount)] # 用于随机的列表
rand_idx = random.sample(rand_list,
np.ceil(samplesCount * train_ratio).astype('int32')) # 随机数数量 四舍五入(改为上取整)
rand_real_idx_per_class = idx[rand_idx]
train_rand_idx.append(rand_real_idx_per_class)
train_rand_idx = np.array(train_rand_idx)
train_data_index = []
for c in range(train_rand_idx.shape[0]):
a = train_rand_idx[c]
for j in range(a.shape[0]):
train_data_index.append(a[j])
train_data_index = np.array(train_data_index)
##将测试集(所有样本,包括训练样本)也转化为特定形式
train_data_index = set(train_data_index)
all_data_index = [i for i in range(len(gt_reshape))]
all_data_index = set(all_data_index)
# 背景像元的标签
background_idx = np.where(gt_reshape == 0)[-1]
background_idx = set(background_idx)
test_data_index = all_data_index - train_data_index - background_idx
# 从测试集中随机选取部分样本作为验证集
val_data_count = int(val_ratio * (len(test_data_index) + len(train_data_index))) # 验证集数量
val_data_index = random.sample(test_data_index, val_data_count)
val_data_index = set(val_data_index)
test_data_index = test_data_index - val_data_index # 由于验证集为从测试集分裂出,所以测试集应减去验证集
# 将训练集 验证集 测试集 整理
test_data_index = list(test_data_index)
train_data_index = list(train_data_index)
val_data_index = list(val_data_index)
if samples_type == 'same_num':
for i in range(class_count):
idx = np.where(gt_reshape == i + 1)[-1]
samplesCount = len(idx)
real_train_samples_per_class = train_samples_per_class
rand_list = [i for i in range(samplesCount)] # 用于随机的列表
if real_train_samples_per_class > samplesCount:
real_train_samples_per_class = samplesCount
rand_idx = random.sample(rand_list,
real_train_samples_per_class) # 随机数数量 四舍五入(改为上取整)
rand_real_idx_per_class_train = idx[rand_idx[0:real_train_samples_per_class]]
train_rand_idx.append(rand_real_idx_per_class_train)
train_rand_idx = np.array(train_rand_idx)
val_rand_idx = np.array(val_rand_idx)
train_data_index = []
for c in range(train_rand_idx.shape[0]):
a = train_rand_idx[c]
for j in range(a.shape[0]):
train_data_index.append(a[j])
train_data_index = np.array(train_data_index)
train_data_index = set(train_data_index)
all_data_index = [i for i in range(len(gt_reshape))]
all_data_index = set(all_data_index)
# 背景像元的标签
background_idx = np.where(gt_reshape == 0)[-1]
background_idx = set(background_idx)
test_data_index = all_data_index - train_data_index - background_idx
# 从测试集中随机选取部分样本作为验证集
val_data_count = int(val_samples) # 验证集数量
val_data_index = random.sample(test_data_index, val_data_count)
val_data_index = set(val_data_index)
test_data_index = test_data_index - val_data_index
# 将训练集 验证集 测试集 整理
test_data_index = list(test_data_index)
train_data_index = list(train_data_index)
val_data_index = list(val_data_index)
# 获取训练样本的标签图
train_samples_gt = np.zeros(gt_reshape.shape)
for i in range(len(train_data_index)):
train_samples_gt[train_data_index[i]] = gt_reshape[train_data_index[i]]
pass
# 获取测试样本的标签图
test_samples_gt = np.zeros(gt_reshape.shape)
for i in range(len(test_data_index)):
test_samples_gt[test_data_index[i]] = gt_reshape[test_data_index[i]]
pass
Test_GT = np.reshape(test_samples_gt, [m, n]) # 测试样本图
# 获取验证集样本的标签图
val_samples_gt = np.zeros(gt_reshape.shape)
for i in range(len(val_data_index)):
val_samples_gt[val_data_index[i]] = gt_reshape[val_data_index[i]]
pass
train_samples_gt=np.reshape(train_samples_gt,[height,width])
test_samples_gt=np.reshape(test_samples_gt,[height,width])
val_samples_gt=np.reshape(val_samples_gt,[height,width])
train_samples_gt_onehot=GT_To_One_Hot(train_samples_gt,class_count)
test_samples_gt_onehot=GT_To_One_Hot(test_samples_gt,class_count)
val_samples_gt_onehot=GT_To_One_Hot(val_samples_gt,class_count)
train_samples_gt_onehot=np.reshape(train_samples_gt_onehot,[-1,class_count]).astype(int)
test_samples_gt_onehot=np.reshape(test_samples_gt_onehot,[-1,class_count]).astype(int)
val_samples_gt_onehot=np.reshape(val_samples_gt_onehot,[-1,class_count]).astype(int)
############制作训练数据和测试数据的gt掩膜.根据GT将带有标签的像元设置为全1向量##############
# 训练集
train_label_mask = np.zeros([m * n, class_count])
temp_ones = np.ones([class_count])
train_samples_gt = np.reshape(train_samples_gt, [m * n])
for i in range(m * n):
if train_samples_gt[i] != 0:
train_label_mask[i] = temp_ones
train_label_mask = np.reshape(train_label_mask, [m* n, class_count])
# 测试集
test_label_mask = np.zeros([m * n, class_count])
temp_ones = np.ones([class_count])
test_samples_gt = np.reshape(test_samples_gt, [m * n])
for i in range(m * n):
if test_samples_gt[i] != 0:
test_label_mask[i] = temp_ones
test_label_mask = np.reshape(test_label_mask, [m* n, class_count])
# 验证集
val_label_mask = np.zeros([m * n, class_count])
temp_ones = np.ones([class_count])
val_samples_gt = np.reshape(val_samples_gt, [m * n])
for i in range(m * n):
if val_samples_gt[i] != 0:
val_label_mask[i] = temp_ones
val_label_mask = np.reshape(val_label_mask, [m* n, class_count])
ls = LDA_SLIC.LDA_SLIC(data, np.reshape( train_samples_gt,[height,width]), class_count-1)
tic0=time.time()
Q, S ,A,Seg= ls.simple_superpixel(scale=superpixel_scale)
toc0 = time.time()
LDA_SLIC_Time=toc0-tic0
# np.save(dataset_name+'Seg',Seg)
print("LDA-SLIC costs time: {}".format(LDA_SLIC_Time))
Q=torch.from_numpy(Q).to(device)
A=torch.from_numpy(A).to(device)
#转到GPU
train_samples_gt=torch.from_numpy(train_samples_gt.astype(np.float32)).to(device)
test_samples_gt=torch.from_numpy(test_samples_gt.astype(np.float32)).to(device)
val_samples_gt=torch.from_numpy(val_samples_gt.astype(np.float32)).to(device)
#转到GPU
train_samples_gt_onehot = torch.from_numpy(train_samples_gt_onehot.astype(np.float32)).to(device)
test_samples_gt_onehot = torch.from_numpy(test_samples_gt_onehot.astype(np.float32)).to(device)
val_samples_gt_onehot = torch.from_numpy(val_samples_gt_onehot.astype(np.float32)).to(device)
#转到GPU
train_label_mask = torch.from_numpy(train_label_mask.astype(np.float32)).to(device)
test_label_mask = torch.from_numpy(test_label_mask.astype(np.float32)).to(device)
val_label_mask = torch.from_numpy(val_label_mask.astype(np.float32)).to(device)
net_input=np.array( data,np.float32)
net_input=torch.from_numpy(net_input.astype(np.float32)).to(device)
if dataset_name == "indian_":
net = CEGCN.CEGCN(height, width, bands, class_count, Q, A, model='smoothed')
else:
net = CEGCN.CEGCN(height, width, bands, class_count, Q, A)
print("parameters", net.parameters(), len(list(net.parameters())))
net.to(device)
def compute_loss(predict: torch.Tensor, reallabel_onehot: torch.Tensor, reallabel_mask: torch.Tensor):
real_labels = reallabel_onehot
we = -torch.mul(real_labels,torch.log(predict))
we = torch.mul(we, reallabel_mask)
pool_cross_entropy = torch.sum(we)
return pool_cross_entropy
zeros = torch.zeros([m * n]).to(device).float()
def evaluate_performance(network_output,train_samples_gt,train_samples_gt_onehot, require_AA_KPP=False,printFlag=True):
if False==require_AA_KPP:
with torch.no_grad():
available_label_idx=(train_samples_gt!=0).float()#有效标签的坐标,用于排除背景
available_label_count=available_label_idx.sum()#有效标签的个数
correct_prediction =torch.where(torch.argmax(network_output, 1) ==torch.argmax(train_samples_gt_onehot, 1),available_label_idx,zeros).sum()
OA= correct_prediction.cpu()/available_label_count
return OA
else:
with torch.no_grad():
#计算OA
available_label_idx=(train_samples_gt!=0).float()#有效标签的坐标,用于排除背景
available_label_count=available_label_idx.sum()#有效标签的个数
correct_prediction =torch.where(torch.argmax(network_output, 1) ==torch.argmax(train_samples_gt_onehot, 1),available_label_idx,zeros).sum()
OA= correct_prediction.cpu()/available_label_count
OA=OA.cpu().numpy()
# 计算AA
zero_vector = np.zeros([class_count])
output_data=network_output.cpu().numpy()
train_samples_gt=train_samples_gt.cpu().numpy()
train_samples_gt_onehot=train_samples_gt_onehot.cpu().numpy()
output_data = np.reshape(output_data, [m * n, class_count])
idx = np.argmax(output_data, axis=-1)
for z in range(output_data.shape[0]):
if ~(zero_vector == output_data[z]).all():
idx[z] += 1
# idx = idx + train_samples_gt
count_perclass = np.zeros([class_count])
correct_perclass = np.zeros([class_count])
for x in range(len(train_samples_gt)):
if train_samples_gt[x] != 0:
count_perclass[int(train_samples_gt[x] - 1)] += 1
if train_samples_gt[x] == idx[x]:
correct_perclass[int(train_samples_gt[x] - 1)] += 1
test_AC_list = correct_perclass / count_perclass
test_AA = np.average(test_AC_list)
# 计算KPP
test_pre_label_list = []
test_real_label_list = []
output_data = np.reshape(output_data, [m * n, class_count])
idx = np.argmax(output_data, axis=-1)
idx = np.reshape(idx, [m, n])
for ii in range(m):
for jj in range(n):
if Test_GT[ii][jj] != 0:
test_pre_label_list.append(idx[ii][jj] + 1)
test_real_label_list.append(Test_GT[ii][jj])
test_pre_label_list = np.array(test_pre_label_list)
test_real_label_list = np.array(test_real_label_list)
kappa = metrics.cohen_kappa_score(test_pre_label_list.astype(np.int16),
test_real_label_list.astype(np.int16))
test_kpp = kappa
# 输出
if printFlag:
print("test OA=", OA, "AA=", test_AA, 'kpp=', test_kpp)
print('acc per class:')
print(test_AC_list)
OA_ALL.append(OA)
AA_ALL.append(test_AA)
KPP_ALL.append(test_kpp)
AVG_ALL.append(test_AC_list)
# 保存数据信息
f = open('results\\' + dataset_name + '_results.txt', 'a+')
str_results = '\n======================' \
+ " learning rate=" + str(learning_rate) \
+ " epochs=" + str(max_epoch) \
+ " train ratio=" + str(train_ratio) \
+ " val ratio=" + str(val_ratio) \
+ " ======================" \
+ "\nOA=" + str(OA) \
+ "\nAA=" + str(test_AA) \
+ '\nkpp=' + str(test_kpp) \
+ '\nacc per class:' + str(test_AC_list) + "\n"
# + '\ntrain time:' + str(time_train_end - time_train_start) \
# + '\ntest time:' + str(time_test_end - time_test_start) \
f.write(str_results)
f.close()
return OA
# 训练
optimizer = torch.optim.Adam(net.parameters(),lr=learning_rate)#,weight_decay=0.0001
best_loss=99999
net.train()
tic1 = time.clock()
for i in range(max_epoch+1):
optimizer.zero_grad() # zero the gradient buffers
output= net(net_input)
loss = compute_loss(output,train_samples_gt_onehot,train_label_mask)
loss.backward(retain_graph=False)
optimizer.step() # Does the update
if i%10==0:
with torch.no_grad():
net.eval()
output= net(net_input)
trainloss = compute_loss(output, train_samples_gt_onehot, train_label_mask)
trainOA = evaluate_performance(output, train_samples_gt, train_samples_gt_onehot)
valloss = compute_loss(output, val_samples_gt_onehot, val_label_mask)
valOA = evaluate_performance(output, val_samples_gt, val_samples_gt_onehot)
print("{}\ttrain loss={}\t train OA={} val loss={}\t val OA={}".format(str(i + 1), trainloss, trainOA, valloss, valOA))
if valloss < best_loss :
best_loss = valloss
torch.save(net.state_dict(),"model\\best_model.pt")
print('save model...')
torch.cuda.empty_cache()
net.train()
toc1 = time.clock()
print("\n\n====================training done. starting evaluation...========================\n")
training_time=toc1 - tic1 + LDA_SLIC_Time #分割耗时需要算进去
Train_Time_ALL.append(training_time)
torch.cuda.empty_cache()
with torch.no_grad():
net.load_state_dict(torch.load("model\\best_model.pt"))
net.eval()
tic2 = time.clock()
output = net(net_input)
toc2 = time.clock()
testloss = compute_loss(output, test_samples_gt_onehot, test_label_mask)
testOA = evaluate_performance(output, test_samples_gt, test_samples_gt_onehot,require_AA_KPP=True,printFlag=False)
print("{}\ttest loss={}\t test OA={}".format(str(i + 1), testloss, testOA))
#计算
classification_map=torch.argmax(output, 1).reshape([height,width]).cpu()+1
Draw_Classification_Map(classification_map,"results\\"+dataset_name+str(testOA))
testing_time=toc2 - tic2 + LDA_SLIC_Time #分割耗时需要算进去
Test_Time_ALL.append(testing_time)
## Saving data
# sio.savemat(dataset_name+"softmax",{'softmax':output.reshape([height,width,-1]).cpu().numpy()})
# np.save(dataset_name+"A_1", A_1.cpu().numpy())
# np.save(dataset_name+"A_2", A_2.cpu().numpy())
torch.cuda.empty_cache()
del net
OA_ALL = np.array(OA_ALL)
AA_ALL = np.array(AA_ALL)
KPP_ALL = np.array(KPP_ALL)
AVG_ALL = np.array(AVG_ALL)
Train_Time_ALL=np.array(Train_Time_ALL)
Test_Time_ALL=np.array(Test_Time_ALL)
print("\ntrain_ratio={}".format(curr_train_ratio),
"\n==============================================================================")
print('OA=', np.mean(OA_ALL), '+-', np.std(OA_ALL))
print('AA=', np.mean(AA_ALL), '+-', np.std(AA_ALL))
print('Kpp=', np.mean(KPP_ALL), '+-', np.std(KPP_ALL))
print('AVG=', np.mean(AVG_ALL, 0), '+-', np.std(AVG_ALL, 0))
print("Average training time:{}".format(np.mean(Train_Time_ALL)))
print("Average testing time:{}".format(np.mean(Test_Time_ALL)))
# 保存数据信息
f = open('results\\' + dataset_name + '_results.txt', 'a+')
str_results = '\n\n************************************************' \
+"\ntrain_ratio={}".format(curr_train_ratio) \
+'\nOA='+ str(np.mean(OA_ALL))+ '+-'+ str(np.std(OA_ALL)) \
+'\nAA='+ str(np.mean(AA_ALL))+ '+-'+ str(np.std(AA_ALL)) \
+'\nKpp='+ str(np.mean(KPP_ALL))+ '+-'+ str(np.std(KPP_ALL)) \
+'\nAVG='+ str(np.mean(AVG_ALL,0))+ '+-'+ str(np.std(AVG_ALL,0)) \
+"\nAverage training time:{}".format(np.mean(Train_Time_ALL)) \
+"\nAverage testing time:{}".format(np.mean(Test_Time_ALL))
f.write(str_results)
f.close()