forked from qiuqiangkong/DCASE2016_Task4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_dev_recognize.py
More file actions
78 lines (66 loc) · 2.38 KB
/
Copy pathmain_dev_recognize.py
File metadata and controls
78 lines (66 loc) · 2.38 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
'''
SUMMARY: Dcase 2016 Task 4. Audio Tagging
Recognize and evaluate the f value or EER
AUTHOR: Qiuqiang Kong
Created: 2016.05.29
Modified: 2016.10.11 modify variable name
--------------------------------------
'''
import pickle
import numpy as np
np.random.seed(1515)
import scipy.stats
from hat.models import Sequential
from hat.layers.core import InputLayer, Flatten, Dense, Dropout
from hat.callbacks import SaveModel, Validation
from hat.preprocessing import sparse_to_categorical, mat_2d_to_3d
from hat.optimizers import Rmsprop
from hat.metrics import prec_recall_fvalue
from hat import serializations
import hat.backend as K
import config as cfg
import prepare_dev_data as pp_dev_data
import csv
import cPickle
from main_dev_dnn import fe_fd, agg_num, hop, n_hid, fold
# hyper-params
n_labels = len( cfg.labels )
# load model
md = serializations.load( cfg.dev_md_fd + '/md10.p' )
def recognize():
# prepare data
_, _, _, te_X, te_y, _ = pp_dev_data.GetAllData( fe_fd, agg_num, hop, fold )
# do recognize and evaluation
thres = 0.4 # thres, tune to prec=recall
n_labels = len( cfg.labels )
gt_roll = []
pred_roll = []
with open( cfg.dev_cv_csv_path, 'rb') as f:
reader = csv.reader(f)
lis = list(reader)
# read one line
for li in lis:
na = li[1]
curr_fold = int(li[2])
if fold==curr_fold:
# get features, tags
fe_path = fe_fd + '/' + na + '.f'
info_path = cfg.dev_wav_fd + '/' + na + '.csv'
tags = pp_dev_data.GetTags( info_path )
y = pp_dev_data.TagsToCategory( tags )
X = cPickle.load( open( fe_path, 'rb' ) )
# aggregate data
X3d = mat_2d_to_3d( X, agg_num, hop )
p_y_pred = md.predict( X3d )
p_y_pred = np.mean( p_y_pred, axis=0 ) # shape:(n_label)
pred = np.zeros(n_labels)
pred[ np.where(p_y_pred>thres) ] = 1
pred_roll.append( pred )
gt_roll.append( y )
pred_roll = np.array( pred_roll )
gt_roll = np.array( gt_roll )
# calculate prec, recall, fvalue
prec, recall, fvalue = prec_recall_fvalue( pred_roll, gt_roll, thres )
print prec, recall, fvalue
if __name__ == '__main__':
recognize()