forked from JasonZhang156/Sound-Recognition-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
55 lines (45 loc) · 1.66 KB
/
test.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
# -*- coding: utf-8 -*-
"""
@author: Jason Zhang
@github: https://github.com/JasonZhang156/Sound-Recognition-Tutorial
"""
from keras.models import load_model
import tensorflow as tf
import esc10_input
import numpy as np
import os
def use_gpu():
"""Configuration for GPU"""
from keras.backend.tensorflow_backend import set_session
os.environ['CUDA_VISIBLE_DEVICES'] = str(0)
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
config.gpu_options.allow_growth = True
set_session(tf.InteractiveSession(config=config))
def CNN_test(test_fold, feat):
"""
Test model using test set
:param test_fold: test fold of 5-fold cross validation
:param feat: which feature to use
"""
# 读取测试数据
_, _, test_features, test_labels = esc10_input.get_data(test_fold, feat)
# 导入训练好的模型
model = load_model('./saved_model/cnn_{}_fold{}.h5'.format(feat, test_fold))
# 输出训练好的模型在测试集上的表现
score = model.evaluate(test_features, test_labels)
print('Test score:', score[0])
print('Test accuracy:', score[1])
return score[1]
if __name__ == '__main__':
use_gpu() # 使用GPU
dict_acc = {}
print('### [Start] Test model for ESC10 dataset #####')
for fold in [1, 2, 3, 4, 5]:
print("## Start test fold{} model #####".format(fold))
acc = CNN_test(fold, 'mfcc')
dict_acc['fold{}'.format(fold)] = acc
print("## Finish test fold{} model #####".format(fold))
dict_acc['mean'] = np.mean(list(dict_acc.values()))
print(dict_acc)
print('### [Finish] Test model finished for ESC10 dataset #####')