forked from rajatkuls/lena-clean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrap-c.py
208 lines (139 loc) · 5.13 KB
/
wrap-c.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
# Given silence/nonsilence classifier, use it to learn within speech
# for long, it already has segments so it is fine. this is for short.
import numpy as np
import os
import glob
import cPickle as pickle
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.metrics import f1_score
from sklearn.model_selection import ParameterGrid
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
import sys
try:
speechModel = sys.argv[1]
except:
speechModel = 'models/a_classifier_all.p'
try:
labelType = sys.argv[2]
except:
labelType = 'class'
try:
outputFile = sys.argv[3]
except:
outputFile = 'models/c_classifier_all.p'
scores = config_c.scores
param_grid = config_c.param_grid
FEATUREPATH = config_c.FEATUREPATH
FOLDS_PATH = config_c.FOLDS_PATH
FEAT_DIM = config_c.FEAT_DIM
labelType = config_c.labelType
def getFilesFromPortion(portion):
t1 = open(portion,'r').read().split('\n')[:-1]
return [x[:x.find('.')] for x in t1]
foldFileList = []
for portion in glob.glob(FOLDS_PATH):
foldFileList.append([x for x in getFilesFromPortion(portion)])
folders = glob.glob(FEATUREPATH+'*')
clfSpeech = pickle.load(open(speechModel,'r'))
X = np.array([]).reshape(FEAT_DIM,0)
y = np.array([])
lengths = []
for i in xrange(len(foldFileList)):
length=0
for f in foldFileList[i]:
print 'Reading '+str(f)
filename = FEATUREPATH+str(i)+'/'+f
y1 = pickle.load(open(filename+'_y'+labelType,'r'))
x1 = pickle.load(open(filename+'_X','r'))
y1 = np.asarray(y1)
if y1.shape[0] < x1.shape[1]:
x1 = x1[:,:y1.shape[0]]
else:
y1 = y1[:x1.shape[1]]
y_speech = clfSpeech.predict(x1.T)
y1 = y1[y_speech>0]
x1 = x1[:,y_speech>0]
x1 = x1[:,y1>0]
y1 = y1[y1>0]
length+=y1.shape[0]
X = np.hstack((X,x1))
y = np.concatenate((y,y1))
lengths.append(length)
boundaries = [sum(lengths[:x]) for x in xrange(len(lengths)+1)]
idxs = [(boundaries[i],boundaries[i+1]) for i in xrange(len(boundaries)-1)]
X = X.T
def yieldIdxs(foldFileList,idxs):
for i in xrange(len(foldFileList)):
trainidxs = []
for j in xrange(len(foldFileList)):
if i!=j:
trainidxs += range(idxs[j][0],idxs[j][1])
testidxs = range(idxs[i][0],idxs[i][1])
yield trainidxs,testidxs
def featureListToVectors(featureList):
X = np.array([])
Y = np.array([])
for i, f in enumerate(featureList):
if i == 0:
X = f
Y = i * np.ones((len(f), 1))
else:
X = np.vstack((X, f))
Y = np.append(Y, i * np.ones((len(f), 1)))
return (X, Y)
param_list = list(ParameterGrid(param_grid))
best_f1 = -1
best_f1_i = -1
param_score_dict = {}
for i in scores:
param_score_dict[i] = {}
print 'Hyperparametrizing for class'
for p,params in enumerate(param_list):
print p,
params['n_jobs'] = 28
splitGenerator = yieldIdxs(foldFileList,idxs)
f1_list = []
precision_list = []
recall_list = []
for i in xrange(len(foldFileList)):
clf = pickle.load(open('models/c_classifier_template.p','r'))
clf.set_params(**params)
clf_test = pickle.load(open('models/c_classifier_template.p','r'))
clf_test.set_params(**params)
trainIdx,testIdx = splitGenerator.next()
X_train = X[trainIdx]
X_test = X[testIdx]
y_train = y[trainIdx]
y_test = y[testIdx]
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
f1_list.append(f1_score(y_test,y_pred,average='weighted'))
if f1_list[-1] > best_f1:
best_f1 = f1_list[-1]
best_f1_i = i
precision_list.append(precision_score(y_test,y_pred,average='weighted'))
recall_list.append(recall_score(y_test,y_pred,average='weighted'))
param_score_dict['f1'][p] = np.mean(f1_list)
param_score_dict['precision'][p]= np.mean(precision_list)
param_score_dict['recall'][p] = np.mean(recall_list)
print param_score_dict['f1'][p]
tempMax = -1
best_param_idx = None
for k,v in param_score_dict['f1'].items():
if v>tempMax:
tempMax=v
best_param_idx = k
print 'Best params over F1 are: ' + str(param_list[best_param_idx])
print 'Corresponding F1 : ' + str(param_score_dict['f1'][best_param_idx])
print 'Corresponding precision: ' + str(param_score_dict['precision'][best_param_idx])
print 'Corresponding recall : ' + str(param_score_dict['recall'][best_param_idx])
print 'Training global model with these parameters.'
all_classifier_class = pickle.load(open('models/c_classifier_template.p','r'))
all_classifier_class.set_params(**param_list[best_param_idx])
all_classifier_class.fit(X,y)
pickle.dump(class_rfc,open(outputFile,'w'))
print 'Model '+outputFile+' saved.'