-
Notifications
You must be signed in to change notification settings - Fork 0
/
DictionaryLearningLinux.py
178 lines (144 loc) · 7.93 KB
/
DictionaryLearningLinux.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
import logging
from iceberk import mpi, pipeline, classifier
import numpy as np
import BabelDataset
import Classifier
if __name__ == '__main__':
'''Loading Data: '''
print 'Rank of this process is ',mpi.RANK
mpi.root_log_level(logging.DEBUG)
logging.info('Loading Babel data...')
list_file = './data/20130307.dev.untightened.scp'
feat_range = None
posting_file = './data/word.kwlist.alignment.csv'
perc_pos = 0.2
babel = BabelDataset.BabelDataset(list_file, feat_range, posting_file, perc_pos)
list_file = './data/20130307.dev.post.untightened.scp'
feat_range = None
babel_post = BabelDataset.BabelDataset(list_file, feat_range, posting_file, perc_pos, keep_full_utt=True, posting_sampler=babel.posting_sampler)
list_file = './data/20130307.eval.untightened.scp'
posting_file = './data/word.cut_down_evalpart1.decision.kwlist.alignment.csv'
perc_pos = 0.2
babel_eval = BabelDataset.BabelDataset(list_file, feat_range, posting_file, perc_pos)
list_file = './data/20130307.eval.post.untightened.scp'
feat_range = None
babel_eval_post = BabelDataset.BabelDataset(list_file, feat_range, posting_file, perc_pos, keep_full_utt=True, posting_sampler=babel_eval.posting_sampler)
'''An example audio pipeline to extract features'''
conv = pipeline.ConvLayer([
pipeline.PatchExtractor([10,75], 1), # extracts patches
pipeline.MeanvarNormalizer({'reg': 10}), # normalizes the patches
pipeline.LinearEncoder({},
trainer = pipeline.ZcaTrainer({'reg': 0.1})), # Does whitening
pipeline.ThresholdEncoder({'alpha': 0.25, 'twoside': True},
trainer = pipeline.OMPTrainer(
{'k': 500, 'max_iter':100})), # does encoding
pipeline.SpatialPooler({'grid': (1,1), 'method': 'ave'})
])
logging.info('Training the pipeline...')
conv.train(babel, 100000)
logging.info('Extracting features...')
Xp_acoustic = conv.process_dataset(babel, as_2d = True)
'''An example for posterior features'''
babel_post.GetLocalFeatures(feat_type=['entropy'])
babel_post.GetGlobalFeatures(feat_type=['entropy'])
babel_post.GetUtteranceFeatures(feat_type=['entropy'])
Xp_post_local = np.asmatrix(babel_post._local_features)
Xp_post_glob = np.asmatrix(babel_post._glob_features)
Xp_post_utt = np.asmatrix(babel_post._utt_features)
'''Pipeline that just gets the score'''
Xp_score = np.asmatrix(babel._features).T
'''Pipeline that cheats'''
#Xp_cheat = np.asmatrix(babel.labels().astype(np.int)).T
'''Constructing Dictionary of Features'''
Xtrain_dict = {'Audio':Xp_acoustic, 'Local':Xp_post_local, 'Global':Xp_post_glob, 'Score':Xp_score, 'Utterance':Xp_post_utt}
Ytrain = babel.labels().astype(np.int)
Xp_t_a1 = conv.process_dataset(babel_eval, as_2d = True)
babel_eval_post.GetLocalFeatures(feat_type=['entropy'])
babel_eval_post.GetGlobalFeatures(feat_type=['entropy'])
babel_eval_post.GetUtteranceFeatures(feat_type=['entropy'])
Xp_t_entropy = np.asmatrix(babel_eval_post._local_features)
Xp_t_entropy_glob = np.asmatrix(babel_eval_post._glob_features)
Xp_t_entropy_utt = np.asmatrix(babel_eval_post._utt_features)
Xp_t_score = np.asmatrix(babel_eval._features).T
Xtest_dict = {'Audio':Xp_t_a1, 'Local':Xp_t_entropy, 'Global':Xp_t_entropy_glob, 'Score':Xp_t_score, 'Utterance':Xp_t_entropy_utt}
Ytest = babel_eval.labels().astype(np.int)
lr_classifier = Classifier.Classifier(Xtrain_dict, Ytrain)
'''Classifier stage'''
feat_list=['Audio','Local','Score','Global','Utterance']
w, b = lr_classifier.Train(feat_list=feat_list,type='logreg',gamma=0.0)
accu = lr_classifier.Accuracy(Xtrain_dict, Ytrain)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtrain_dict, Ytrain)
print 'Accuracy is ',accu
print 'Neg LogLikelihood is ',neg_ll
print 'Prior is ',np.sum(Ytrain==0)/float(len(Ytrain))
logging.info('Running Test...')
accu = lr_classifier.Accuracy(Xtest_dict, Ytest)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtest_dict, Ytest)
print 'Test Accuracy is ',accu
print 'Test Neg LogLikelihood is ',neg_ll
print 'Test Prior is ',np.sum(Ytest==0)/float(len(Ytest))
'''Classifier stage'''
feat_list=['Audio','Local','Score','Global']
w, b = lr_classifier.Train(feat_list=feat_list,type='logreg',gamma=0.0)
accu = lr_classifier.Accuracy(Xtrain_dict, Ytrain)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtrain_dict, Ytrain)
print 'Accuracy is ',accu
print 'No utterance Neg LogLikelihood is ',neg_ll
print 'Prior is ',np.sum(Ytrain==0)/float(len(Ytrain))
logging.info('Running Test...')
accu = lr_classifier.Accuracy(Xtest_dict, Ytest)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtest_dict, Ytest)
print 'Test Accuracy is ',accu
print 'Test Neg LogLikelihood is ',neg_ll
print 'Test Prior is ',np.sum(Ytest==0)/float(len(Ytest))
'''Classifier stage'''
feat_list=['Score']
w, b = lr_classifier.Train(feat_list=feat_list,type='logreg',gamma=0.0)
accu = lr_classifier.Accuracy(Xtrain_dict, Ytrain)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtrain_dict, Ytrain)
print 'Score only Accuracy is ',accu
print 'Score only Neg LogLikelihood is ',neg_ll
print 'Prior is ',np.sum(Ytrain==0)/float(len(Ytrain))
logging.info('Running Test...')
accu = lr_classifier.Accuracy(Xtest_dict, Ytest)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtest_dict, Ytest)
print 'Score only Test Accuracy is ',accu
print 'Score only Test Neg LogLikelihood is ',neg_ll
print 'Test Prior is ',np.sum(Ytest==0)/float(len(Ytest))
'''Classifier stage'''
feat_list=['Audio']
w, b = lr_classifier.Train(feat_list=feat_list,type='logreg',gamma=0.0)
accu = lr_classifier.Accuracy(Xtrain_dict, Ytrain)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtrain_dict, Ytrain)
print 'Audio only Accuracy is ',accu
print 'Prior is ',np.sum(Ytrain==0)/float(len(Ytrain))
logging.info('Running Test...')
accu = lr_classifier.Accuracy(Xtest_dict, Ytest)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtest_dict, Ytest)
print 'Audio only Test Accuracy is ',accu
print 'Score only Test Neg LogLikelihood is ',neg_ll
print 'Test Prior is ',np.sum(Ytest==0)/float(len(Ytest))
'''Classifier stage'''
feat_list=['Local']
w, b = lr_classifier.Train(feat_list=feat_list,type='logreg',gamma=0.0)
accu = lr_classifier.Accuracy(Xtrain_dict, Ytrain)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtrain_dict, Ytrain)
print 'Entropy only Accuracy is ',accu
print 'Prior is ',np.sum(Ytrain==0)/float(len(Ytrain))
logging.info('Running Test...')
accu = lr_classifier.Accuracy(Xtest_dict, Ytest)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtest_dict, Ytest)
print 'Entropy only Test Accuracy is ',accu
print 'Test Prior is ',np.sum(Ytest==0)/float(len(Ytest))
'''Classifier stage'''
feat_list=['Global']
w, b = lr_classifier.Train(feat_list=feat_list,type='logreg',gamma=0.0)
accu = lr_classifier.Accuracy(Xtrain_dict, Ytrain)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtrain_dict, Ytrain)
print 'Global Entropy only Accuracy is ',accu
print 'Prior Accuracy is ',np.sum(Ytrain==0)/float(len(Ytrain))
logging.info('Running Test...')
accu = lr_classifier.Accuracy(Xtest_dict, Ytest)
neg_ll = lr_classifier.loss_multiclass_logreg(Xtest_dict, Ytest)
print 'Global Entropy only Test Accuracy is ',accu
print 'Test Prior Accuracy is ',np.sum(Ytest==0)/float(len(Ytest))