-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.py
192 lines (154 loc) · 7.66 KB
/
functions.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
# -*- coding: utf-8 -*-
# import matplotlib.pyplot as plt
import time
import keras.backend.tensorflow_backend as KTF
import tensorflow as tf
import os
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from model import get_model, get_callbacks
from data import *
from keras import backend as K
from data import *
init_ratio = 0.4 # gpu volatile ratio supporting parallel running
def cal_flops(model):
run_meta = tf.RunMetadata()
opts = tf.profiler.ProfileOptionBuilder.float_operation()
flops = tf.profiler.profile(graph=K.get_session().graph, run_meta=run_meta, cmd='op', options=opts)
params = tf.profiler.profile(graph=K.get_session().graph, run_meta=run_meta, cmd='op', options=tf.profiler.ProfileOptionBuilder.trainable_variables_parameter())
print('flops'+ str(flops.total_float_ops))
print('params'+ str(params.total_parameters))
def resolve_dict(hp):
return hp['pc'], hp['w'], hp['decay'], hp['bs'], hp['lr'], hp['epochs'], hp['disjoint'], hp['model_type']
def record_results(dataID, hp, results):
pass
# record_file_name = './' + data_name_dict[str(dataID)] + '_' + str(hp['w']) + '_' + '.txt'
# with open(record_file_name, 'w') as f:
# f.write('mean_oa:' + str(results[0]) + '\n')
# f.write('std_oa:' + str(results[1]))
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)
if reali[i] == 0:
print('Warnning!',str(i),'class have no samples')
producerA[i] = 1.0
else:
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))
return OA, Kappa, producerA
def loop_train_test(dataID=1, num_list=[], verbose=1, run_times=50, hyper_parameters={}, output_map=False, only_draw_label=False, model_save=False):
t = time.time()
gpu_setting(init_ratio=init_ratio)
num_PC, w, decay, batch_size, lr, epochs, disjoint, model_type = resolve_dict(hyper_parameters)
n_class = get_class_num(dataID=dataID)
results = np.zeros((n_class + 2, run_times))
run_time = np.zeros((3, run_times))
for run_i in range(run_times):
K.clear_session()
model = get_model(w, w, num_PC=num_PC, nb_classes=n_class, dataID=dataID, type=model_type, lr=lr)
# model.summary()
callbacks = get_callbacks(decay=decay)
if disjoint:
train_batch, train_step, test_batch, test_step = generate_fixed_train_test_batch(dataID=dataID,
num_PC=num_PC,
w=w,
batch_size=batch_size)
else:
train_batch, train_step, test_batch, test_step = generate_train_test_batch(dataID=dataID,
num_PC=num_PC,
num_list=num_list,
w=w,
batch_size=batch_size)
train_time = time.time()
# data = get_images(test_batch, step=test_step)
# gt = get_labels(test_batch, test_step, argmax=False)
model.fit_generator(train_batch, steps_per_epoch=train_step, epochs=epochs, verbose=verbose,
callbacks=callbacks)
truth = get_labels(test_batch, test_step, argmax=True)
train_time = time.time() - train_time
if model_save:
save_model(model, dataID)
test_time = time.time()
prediction = model.predict_generator(test_batch, steps=test_step, verbose=verbose)
prediction = prediction.argmax(axis=-1)
test_time = time.time() - test_time
OA, Kappa, ProducerA = CalAccuracy(prediction, truth)
results[0:n_class, run_i] = ProducerA
results[-2, run_i] = OA
results[-1, run_i] = Kappa
run_time[0, run_i] = train_time
run_time[1, run_i] = test_time
print('rand', run_i + 1, ' ' + data_name_dict[str(dataID)] + ' accuracy:', OA * 100)
whole_batch, steps = generate_whole_batch(dataID=dataID, num_PC=num_PC, w=w, batch_size=batch_size)
prediction_time = time.time()
prediction = model.predict_generator(whole_batch, steps=steps, verbose=verbose)
prediction_time = time.time() - prediction_time
run_time[2, run_i] = prediction_time
if output_map:
Map = prediction.argmax(axis=-1)
if only_draw_label:
Map = elimate_unlabeled_pixel(Map, dataID=dataID, fixed=False)
row = image_size_dict[str(dataID)][0]
col = image_size_dict[str(dataID)][1]
# sio.savemat('./' + data_name_dict[str(dataID)]+'_predict',{'y':np.reshape(Map, (row, col))})
#X_result = draw_result(Map, np.max(prediction, axis=-1), dataID=dataID)
#plt.imsave('./' + data_name_dict[str(dataID)] + '_OA_' + repr(int(OA * 10000)) + '.png', X_result)
mean_oa = np.mean(results[-2] * 100)
std_oa = np.std(results[-2] * 100)
aa = np.mean(results[0:n_class], axis=0)
mean_aa = np.mean(aa) * 100
std_aa = np.std(aa) * 100
mean_kappa = np.mean(results[-1])
std_kappa = np.std(results[-1])
for i in range(n_class):
print('Class ', str(i+1),' mean:', str(np.mean(results[i]) * 100), 'std:', str(np.std(results[i]) * 100))
print('OA mean:', str(mean_oa), 'std:', str(std_oa))
print('AA mean:', str(mean_aa), 'std:', str(std_aa))
print('Kappa mean:', str(mean_kappa * 100), 'std:', str(std_kappa * 100))
print('train_time:', str(np.mean(run_time[0])), 'std:', str(np.std(run_time[0])))
print('test_time:', str(np.mean(run_time[1])), 'std:', str(np.std(run_time[1])))
print('prediction_time:', str(np.mean(run_time[2])), 'std:', str(np.std(run_time[2])))
print('total_time:', str((time.time() - t) / run_times))
print('\n')
# record_results(dataID=dataID, hp=hyper_parameters, results=[mean_oa, std_oa])
def save_model(model, dataID, filepath='./weight.hdf5'):
# use model.save_weights can decrease storage
# but model.save can retraining
if(dataID==1):
filepath='./PU/weight.hdf5'
if(dataID==2):
filepath='./IP/weight.hdf5'
if(dataID==3):
filepath='./Houston/weight.hdf5'
if(dataID==4):
filepath='./Salinas/weight.hdf5'
if(dataID==5):
filepath='./Hanchuan/weight.hdf5'
if(dataID==6):
filepath='./HongHu/weight.hdf5'
if(dataID==7):
filepath='./Longkou/weight.hdf5'
if(dataID==8):
filepath='./Xiongan/weight.hdf5'
model.save(filepath)
# default 40% GPU
def gpu_setting(init_ratio=0.4):
if init_ratio > 0:
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
config = tf.ConfigProto()
# auto-increase
config.gpu_options.per_process_gpu_memory_fraction = init_ratio
config.gpu_options.allow_growth = True
# SET Session
KTF.set_session(tf.Session(config=config))
else:
# exchange to cpu mode
os.environ["CUDA_VISIBLE_DEVICES"] = ""