-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterFeatures.py
217 lines (179 loc) · 8.75 KB
/
clusterFeatures.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import os, sys
from os import walk
import json
from scipy.cluster.vq import vq, kmeans, whiten
# from scipy.stats import mode
descriptorMapping = [ 'lowLevel.spectral_centroid.mean',# 0
'lowLevel.spectral_centroid.stdev', #1
'lowLevel.dissonance.mean', #2
'lowLevel.dissonance.stdev', #3
'sfx.logattacktime.mean', #4
'sfx.logattacktime.stdev', #5
'sfx.inharmonicity.mean', #6
'sfx.inharmonicity.stdev', #7
'lowLevel.spectral_contrast.mean', #8
'lowLevel.spectral_contrast.stdev', #9
'lowLevel.mfcc.mean', #10
'lowLevel.barkbands_kurtosis.mean', #11
'lowLevel.barkbands_skewness.mean', #12
'lowLevel.barkbands_spread.mean', #13
'lowLevel.spectral_complexity.mean', #14
'lowLevel.spectral_crest.mean', #15
'lowLevel.spectral_energyband_high.mean', #16
'lowLevel.spectral_energyband_low.mean', #17
'lowLevel.spectral_energy.mean', #18
'lowLevel.spectral_energy.stdev', #19
'lowLevel.spectral_kurtosis.mean', #20
'lowLevel.spectral_flatness_db.mean', #21
'lowLevel.spectral_flux.mean', #22
'lowLevel.spectral_flux.stdev', #23
'lowLevel.spectral_spread.stdev', #24
'lowLevel.spectral_spread.mean', #25
'lowLevel.sccoeffs.mean', #26
'lowLevel.spectral_rolloff.mean', #27
'lowLevel.zerocrossingrate.mean', #28
'lowLevel.zerocrossingrate.stdev', #29
'lowLevel.strongpeak.mean' ,#30
'lowLevel.mfcc.mean', # 31
'lowLevel.hfc.mean' ,# 32
'lowLevel.silence_rate_30dB.mean', #33
'lowLevel.silence_rate_20dB.mean' , #34
'lowLevel.silence_rate_60dB.mean', #35
'lowLevel.spectral_rms.mean', # 36
'sfx.oddtoevenharmonicenergyratio.mean', # 37
'lowLevel.pitch_salience.mean', #38
'lowLevel.strongpeak.mean' #39
]
descriptorMapping = np.array(descriptorMapping)
def fetchFeatures(inputPath):
dataDetails = {}
for path, dname, fnames in walk(inputPath):
for fname in fnames:
if 'features' in fname:
filename = path+'/'+fname
remain, rname, cname, sname = path.split('/')[:-3], path.split('/')[-3], path.split('/')[-2], path.split('/')[-1]
if not dataDetails.has_key(cname):
dataDetails[cname]={}
fDict = json.load(open(filename))
dataDetails[cname][sname]={'file':fname, 'feature': fDict}
return dataDetails
def convFtrDict2List(ftrDict, descriptorsToSelect):
"""
Select the desired descriptors, given by the variable descriptors
"""
ftr = []
# print ftrDict.keys()
try:
for feature in ftrDict['lowLevel'].keys():
feature_ = 'lowLevel.'+str(feature)+'.mean'
if feature_ in descriptorMapping[descriptorsToSelect]:
if hasattr(ftrDict['lowLevel'][feature]['mean'], '__len__'):
for val in ftrDict['lowLevel'][feature]['mean']:
ftr.append(float(val))
else:
ftr.append(float(ftrDict['lowLevel'][feature]['mean']))
for feature in ftrDict['sfx'].keys():
feature_ = 'sfx.' + str(feature) + '.mean'
if feature_ in descriptorMapping[descriptorsToSelect]:
if hasattr(ftrDict['sfx'][feature]['mean'], '__len__') > 1:
for val in ftrDict['sfx'][feature]['mean']:
ftr.append(float(val))
else:
ftr.append(float(ftrDict['sfx'][feature]['mean']))
except:
for feature in ftrDict['features']['lowLevel'].keys():
feature_ = 'lowLevel.'+str(feature)+'.mean'
if feature_ in descriptorMapping[descriptorsToSelect]:
if hasattr(ftrDict['features']['lowLevel'][feature]['mean'], '__len__'):
for val in ftrDict['features']['lowLevel'][feature]['mean']:
ftr.append(float(val))
else:
ftr.append(float(ftrDict['features']['lowLevel'][feature]['mean']))
for feature in ftrDict['features']['sfx'].keys():
feature_ = 'sfx.' + str(feature) + '.mean'
if feature_ in descriptorMapping[descriptorsToSelect]:
if hasattr(ftrDict['features']['sfx'][feature]['mean'], '__len__') > 1:
for val in ftrDict['features']['sfx'][feature]['mean']:
ftr.append(float(val))
else:
ftr.append(float(ftrDict['features']['sfx'][feature]['mean']))
return np.array(ftr)
#!/usr/bin/python
# -*- coding: utf-8 -*-
def clusterSounds(targetDir, nCluster=-1, descInput=[]):
"""
This function clusters all the sounds in targetDir using kmeans clustering.
Input:
targetDir (string): Directory where sound descriptors are stored (all the sounds in this
directory will be used for clustering)
nCluster (int): Number of clusters to be used for kmeans clustering.
descInput (list) : List of indices of the descriptors to be used for similarity/distance
computation (see descriptorMapping)
Output:
Prints the class of each cluster (computed by a majority vote), number of sounds in each
cluster and information (sound-id, sound-class and classification decision) of the sounds
in each cluster. Optionally, you can uncomment the return statement to return the same data.
"""
dataDetails = fetchFeatures(targetDir)
# print convFtrDict2List(dataDetails['cello_note']['358222'])
# print dataDetails
ftrArr = []
infoArr = []
if nCluster == -1:
nCluster = len(dataDetails.keys())
for cname in dataDetails.keys():
# iterating over sounds
for sname in dataDetails[cname].keys():
# print dataDetails[cname][sname]['feature']
ftrArr.append(convFtrDict2List(dataDetails[cname][sname]['feature'], descInput))
infoArr.append([sname, cname])
# print ('out ')
# print (ftrArr)
ftrArr = np.array(ftrArr)
infoArr = np.array(infoArr)
ftrArrWhite = whiten(ftrArr)
(centroids, distortion) = kmeans(ftrArrWhite, nCluster)
clusResults = -1 * np.ones(ftrArrWhite.shape[0])
for ii in range(ftrArrWhite.shape[0]):
diff = centroids - ftrArrWhite[ii, :]
diff = np.sum(np.power(diff, 2), axis=1)
indMin = np.argmin(diff)
clusResults[ii] = indMin
ClusterOut = []
classCluster = []
globalDecisions = []
for ii in range(nCluster):
ind = np.where(clusResults == ii)[0]
freqCnt = []
for elem in infoArr[ind, 1]:
freqCnt.append(infoArr[ind, 1].tolist().count(elem))
indMax = np.argmax(freqCnt)
classCluster.append(infoArr[ind, 1][indMax])
#print '\n(Cluster: ' + str(ii) \
# + ') Using majority voting as a criterion this cluster belongs to ' \
# + 'class: ' + classCluster[-1]
#print 'Number of sounds in this cluster are: ' + str(len(ind))
decisions = []
for jj in ind:
if infoArr[jj, 1] == classCluster[-1]:
decisions.append(1)
else:
decisions.append(0)
globalDecisions.extend(decisions)
#print 'sound-id, sound-class, classification decision'
ClusterOut.append(np.hstack((infoArr[ind],
np.array([decisions]).T)))
#print ClusterOut[-1]
globalDecisions = np.array(globalDecisions)
totalSounds = len(globalDecisions)
nIncorrectClassified = len(np.where(globalDecisions == 0)[0])
#print 'Out of %d sounds, %d sounds are incorrectly classified considering that one cluster should ideally contain sounds from only a single class' \
# % (totalSounds, nIncorrectClassified)
#print 'You obtain a classification (based on obtained clusters and majority voting) accuracy of %.2f percentage' \
# % round(float(100.0 * float(totalSounds - nIncorrectClassified)
# / totalSounds), 2)
return (totalSounds, nIncorrectClassified)
# return ClusterOut