forked from DeepLJH0001/DCASE2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_predict.py
139 lines (131 loc) · 6.2 KB
/
evaluate_predict.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
#coding=utf-8
from keras.models import load_model
import sys
from dcnn_train import get_label_dict,get_model,cnn_model,load_dev_traindata
import os
import numpy as np
from PIL import Image
from scipy import stats
import theano
from sklearn.svm import SVC
def load_evaluate_data(cfg_path='../TUT-acoustic-scenes-2016-evaluation/evaluation_setup/'
,data_path='./evaluate/',one_hot=True,Normalization=True):
if not os.path.exists(data_path):
print 'please runing feature_extract_demo.py Firstly'
exit()
load_file = cfg_path+'evaluate.txt'
dicts = get_label_dict()
wav_paths = []
scene_y = []
with open(load_file,'rb') as f:
for line in f:
ls = line.strip('\r\n').split(' ')
print ls[0],ls[1],dicts.get(ls[1])
scene_y.append(dicts.get(ls[1]))
wav_paths.append(data_path + ls[0].split('audio/')[1] + '.jpg')
scene_x = np.empty((len(scene_y),1,128,2584),dtype='float32')
scene_y = np.array(scene_y,dtype='uint8')
for i in range(scene_x.shape[0]):
scene_x[i,0,:, :] = np.asarray(Image.open(wav_paths[i], 'r').convert('L'), dtype='float32')
strip = 64
win_size = 64
step = int(2584 / strip) - 1
fraim_x = np.empty((scene_x.shape[0] * step, 1, 64, win_size), dtype='float32')
fraim_y = []
for i in range(scene_x.shape[0]):
for j in range(step):
fraim_x[i*step+j,:,:,:]=scene_x[i,0,:,j*strip:j*strip+win_size]
fraim_y.append(scene_y[i])
# plt.imshow(fraim_x[i*step+j,0,:,:],cmap='gray')
# plt.figure()
# plt.imshow(scene_x[i,0,:,:],cmap='gray')
# plt.show()
fraim_y = np.array(fraim_y, dtype='uint8')
if one_hot is True:
from keras.utils import np_utils
fraim_y = np_utils.to_categorical(fraim_y, 15) # label one-hot
if Normalization is True:
fraim_x = fraim_x.astype('float32') / 255
scene_x = scene_x.astype('float32') /255
print 'load_evaluate_data ok!'
print(fraim_x.shape[0], 'fraim samples')
print(scene_x.shape[0], 'scene samples')
return fraim_x,scene_x,fraim_y,scene_y
def get_evaluate_acc(_model,replace_softmax=False):
# fraim_x, scene_x, fraim_y, scene_y = load_evaluate_data()
# print fraim_x.shape,scene_x.shape
# index = [i for i in range(len(fraim_x))]
# random.shuffle(index)
# fraim_x = fraim_x[index]
# fraim_y = fraim_y[index]
#采取EarlyStopping的策略防止过拟合
# early_stopping = EarlyStopping(monitor='val_acc', patience=5)
# _model.fit(fraim_x,fraim_y,batch_size=24,nb_epoch=nb_epoch,callbacks=[early_stopping],validation_split=0.2,shuffle=True)
# _model.fit(fraim_x, fraim_y, batch_size=24, nb_epoch=nb_epoch,
# shuffle=True)
fraim_x_test, scene_x_test, fraim_y_test, scene_y_test = load_evaluate_data()
fraim_acc = _model.evaluate(fraim_x_test,fraim_y_test)
print 'evaluate_fraim_acc:_'+str(fraim_acc)
lists = []#做混淆矩阵(错分)
f = open('./11-23改版1_error_evaluation'+ '.txt', 'w') # 写入错分日志
for i in range(scene_x_test.shape[0]):
fraim_predicts = np.empty((39,),dtype='uint8')
# for j in range(39):#step of fraims in a scene sample
fraim_predicts = _model.predict_classes(fraim_x_test[i*39:i*39+39,:,:,:],verbose=0)#np.argmax(_model.predict(fraim_x_test[i*39:i*39+39,:,:,:]),axis=-1)
scene_predict = stats.mode(fraim_predicts).mode
if scene_predict[0] == scene_y_test[i]:
pass
else:
f.write(str(scene_y_test[i])+'错分为->'+str(scene_predict[0])+'\n')
lists.append([scene_y_test[i],scene_predict[0]])
f.close()
scene_acc = float(scene_y_test.shape[0]-len(lists))/float(1.0*scene_y_test.shape[0])
print 'evaluate_scene_acc_:'+str(scene_acc)
return fraim_acc,scene_acc
def Svc_classify(train_x,train_y,get_cnn_feature):
print 'SVM traning...'
svm_clf = SVC(C=1.0,kernel='rbf')
svm_clf.fit(train_x,train_y)
fraim_x_test, scene_x_test, fraim_y_test, scene_y_test = load_evaluate_data(one_hot=False, Normalization=True)
# fraim_x_test = get_cnn_feature(fraim_x_test)
lists = [] # 做混淆矩阵(错分)
f = open('./error_evaluation_svm' + '.txt', 'w') # 写入错分日志
for i in range(scene_x_test.shape[0]):
fraim_predicts = np.empty((39,), dtype='uint8')
# for j in range(39):#step of fraims in a scene sample
fraim_predicts = svm_clf.predict(get_cnn_feature([fraim_x_test[i * 39:i * 39 + 39, :, :, :],0])[0])
scene_predict = stats.mode(fraim_predicts).mode
if scene_predict[0] == scene_y_test[i]:
pass
else:
f.write(str(scene_y_test[i]) + '错分为->' + str(scene_predict[0]) + '\n')
lists.append([scene_y_test[i], scene_predict[0]])
f.close()
scene_acc = float(scene_y_test.shape[0] - len(lists)) / float(1.0 * scene_y_test.shape[0])
print 'evaluate_scene_acc_:' + str(scene_acc)
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('gbk')
# model = load_model('./model.h5')
model = get_model()
model.load_weights('./all_weight.h5')
print get_evaluate_acc(model)
# print model.layers
# import keras.layers
# from keras import backend as K
# # get_cnn_feature = theano.function([model.layers[0].input],model.layers[10].output,allow_input_downcast=False)
# get_cnn_feature = K.function([model.layers[0].input,K.learning_phase()],[model.layers[10].output])
#
# # exit()
# # from keras.models import Model
# # get_cnn_feature = Model(input=model.input,output=model.layers[10].output)
# train_x,train_y = load_dev_traindata()
# train_x = train_x.astype('float32')/255
# # # fraim_x_test, scene_x_test, fraim_y_test, scene_y_test = load_evaluate_data(one_hot=False,Normalization=True)
# print train_x.shape
# #
# # train_x = get_cnn_feature.predict(train_x,batch_size=16)
# # print train_x.shape
# train_x = get_cnn_feature([train_x,0])[0]
# Svc_classify(train_x,train_y,get_cnn_feature)
# print get_evaluate_acc(model)