-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSHSDataset.py
274 lines (248 loc) · 8.17 KB
/
SHSDataset.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import numpy as np
import scipy.io as sio
import time
from CSMSSMTools import *
from BlockWindowFeatures import *
def getSHSIDDict():
"""
Get the dictionary of IDs to index numbers in
the features file
"""
m = {}
fin = open("SHSDataset/Chromas/msd_keys_mapping.cly")
for l in fin.readlines():
l = l.rstrip()
f = l.split(",")
m[f[0]] = int(f[1])
fin.close()
return m
def getSHSCliques():
"""
Return a dictionary of cliques of index numbers
"""
m = getSHSIDDict()
fin = open("SHSDataset/Chromas/shs_nodup.txt")
cliques = {}
currClique = ""
for l in fin.readlines():
l = l.rstrip()
if l[0] == '%':
currClique = l[1::]
cliques[currClique] = []
else:
cliques[currClique].append(m[l])
fin.close()
return cliques
def getSHSInfo():
database = {}
fin = open("SHSDataset/MFCC/info.cly")
fin.readline()
while True:
ID = fin.readline()
if not ID:
break
ID = int(ID)
artist = fin.readline()
songname = fin.readline()
year = int(fin.readline())
database[ID] = {'artist':artist, 'songname':songname, 'year':year}
fin.close()
return database
def loadSHSChromas(IDs):
"""
Load all of the 12-dim chroma features
"""
fin = open("SHSDataset/Chromas/btchromas.cly")
fin.readline() #First line is 'chroma'
chromas = {}
while True:
ID = fin.readline()
if not ID:
break
ID = int(ID)
if ID%1000 == 0:
print("Loaded chromas for %i songs..."%ID)
if not ID in IDs:
fin.readline()
continue
x = fin.readline().rstrip()
x = np.array([float(a) for a in x.split(",")])
x = np.reshape(x, (len(x)/12, 12))
chromas[ID] = x
fin.close()
return chromas
def loadSHSMFCCs(IDs):
"""
Load all of the 12-dim MFCC features
"""
IDDict = getSHSIDDict()
fin = open("SHSDataset/MFCC/bt_aligned_mfccs_shs.txt")
mfccs = {}
count = 0
while True:
ID = fin.readline().rstrip()
if not ID:
break
ID = IDDict[ID]
if count%1000 == 0:
print("Loaded mfccs for %i songs..."%count)
if not ID in IDs:
fin.readline()
count += 1
continue
x = fin.readline().rstrip()
x = x.split(",")
if len(x[-1]) == 0:
x = x[0:-1]
x = np.array([float(a) for a in x])
x = np.reshape(x, (len(x)/12, 12))
mfccs[ID] = x
count += 1
fin.close()
return mfccs
def getBeatsPerSong():
C = loadSHSChromas(np.arange(20000))
BeatsPerSong = np.zeros((len(C)))
for i in range(len(BeatsPerSong)):
BeatsPerSong[i] = len(C[i])
sio.savemat("SHSDataset/BeatsPerSong.mat", {"BeatsPerSong":BeatsPerSong})
def getSHSSubset(N, maxPerClique, minBeats = 100, maxBeats = 1000):
"""
Get a subset of the SHS dataset with N songs
formed of cliques of at most size "maxPerClique"
"""
BeatsPerSong = sio.loadmat("SHSDataset/BeatsPerSong.mat")['BeatsPerSong'].flatten()
cliques = getSHSCliques()
keys = cliques.keys()
idx = np.random.permutation(len(cliques))
n = 0
i = 0
IDs = []
Ks = []
while n < N:
clique = cliques[keys[idx[i]]]
K = len(clique)
if K > 4:
i += 1
continue
withinBeatRange = True
for s in cliques[keys[idx[i]]]:
if BeatsPerSong[s] < minBeats or BeatsPerSong[s] > maxBeats:
withinBeatRange = False
break
if not withinBeatRange:
i += 1
continue
n += K
IDs += clique
Ks += [K]
i += 1
return (IDs, Ks)
def getSHSBlockFeatures(c, m, BeatsPerBlock):
"""
Get normalized blocked chroma, mfcc, and SSM mfcc features
"""
N = m.shape[0]
NBlocks = N - BeatsPerBlock + 1
DPixels = BeatsPerBlock*(BeatsPerBlock-1)/2
print("N = %i, NBlocks = %i, BeatsPerBlock = %i"%(N, NBlocks, BeatsPerBlock))
cRet = np.zeros((NBlocks, 12*BeatsPerBlock))
mRet = np.zeros((NBlocks, 12*BeatsPerBlock))
dRet = np.zeros((NBlocks, DPixels))
[I, J] = np.meshgrid(np.arange(BeatsPerBlock), np.arange(BeatsPerBlock))
for i in range(NBlocks):
#MFCC Block
x = m[i:i+BeatsPerBlock, :]
x = x - np.mean(x, 0)
#Normalize x
xnorm = np.sqrt(np.sum(x**2, 1))[:, None]
xnorm[xnorm == 0] = 1
xn = x / xnorm
mRet[i, :] = xn.flatten()
D = getCSM(xn, xn)
dRet[i, :] = D[I < J]
#Chroma Block
x = c[i:i+BeatsPerBlock, :]
xnorm = np.sqrt(np.sum(x**2, 1))
xnorm[xnorm == 0] = 1
x = x/xnorm[:, None]
cRet[i, :] = x.flatten()
BlockFeatures = {'Chromas':cRet, 'SSMs':dRet, 'MFCCs':mRet}
#BlockFeatures = {'Chromas':cRet, 'SSMs':dRet}
OtherFeatures = {'ChromaMean':np.mean(c, 0)}
return (BlockFeatures, OtherFeatures)
def doSHSExperiment(IDs, Ks, CSMTypes, BeatsPerBlock, Kappa):
mfccs = loadSHSMFCCs(IDs)
chromas = loadSHSChromas(IDs)
AllFeatures = [] #{'Chromas':[], 'SSMs':[], 'MFCCs':[]}
AllOtherFeatures = []
N = len(IDs)
tic = time.time()
for i in range(len(IDs)):
(BlockFeatures, OtherFeatures) = getSHSBlockFeatures(chromas[IDs[i]], mfccs[IDs[i]], BeatsPerBlock)
AllFeatures.append(BlockFeatures)
AllOtherFeatures.append(OtherFeatures)
print("Elapsed time blocking: %g"%(time.time() - tic))
Results = {}
for FeatureName in AllFeatures[0]:
print("Doing %s"%FeatureName)
CSMType = 'Euclidean' #Euclidean comparison by default
if FeatureName in CSMTypes:
CSMType = CSMTypes[FeatureName]
Scores = np.zeros((N, N))
for i in range(N):
print("Doing %s %i of %i..."%(FeatureName, i, N))
Features1 = AllFeatures[i][FeatureName]
for j in range(i+1, N):
Features2 = AllFeatures[j][FeatureName]
Scores[i, j] = getCSMSmithWatermanScores([Features1, AllOtherFeatures[i], Features2, AllOtherFeatures[j], Kappa, CSMType])
Scores = Scores + Scores.T
Results[FeatureName] = Scores
sio.savemat("SHSDataset/SHSScores.mat", Results)
#Now do similarity fusion
Scores = np.zeros((N, N))
NIters = 10
K = 20
for i in range(N):
print("Doing SNF %i of %i..."%(i, N))
tic = time.time()
for j in range(i+1, N):
Scores[i, j] = getCSMSmithWatermanScoresEarlyFusion([AllFeatures[i], AllOtherFeatures[i], AllFeatures[j], AllOtherFeatures[j], Kappa, K, NIters, CSMTypes])
print("Elapsed Time: %g"%(time.time() - tic))
Results['SNF'] = Scores + Scores.T
sio.savemat("SHSDataset/SHSScores.mat", Results)
if __name__ == '__main__2':
CSMTypes = {'MFCCs':'Euclidean', 'SSMs':'Euclidean', 'Chromas':'CosineOTI'}
BeatsPerBlock = 25
Kappa = 0.1
N = 200
np.random.seed(100)
(IDs, Ks) = getSHSSubset(N, 4)
sio.savemat("SHSDataset/SHSIDs.mat", {"IDs":IDs, "Ks":Ks})
tic = time.time()
doSHSExperiment(IDs, Ks, CSMTypes, BeatsPerBlock, Kappa)
print("Elapsed Time All Comparisons: %g"%(time.time() - tic))
if __name__ == '__main__':
CSMTypes = {'MFCCs':'Euclidean', 'SSMs':'Euclidean', 'Chromas':'CosineOTI'}
BeatsPerBlock = 25
Kappa = 0.1
#Similarity fusion parameters
NIters = 10
K = 20
database = getSHSInfo()
song = "Hips Don't Lie"
cliques = getSHSCliques()
fout = open("SHSDataset/songs.txt", "w")
for s in cliques.keys():
fout.write("%s\n"%s)
fout.close()
c = cliques[song]
idx1 = c[0]
idx2 = c[1]
print(database[idx1])
print(database[idx2])
mfccs = loadSHSMFCCs(c)
chromas = loadSHSChromas(c)
(Features1, O1) = getSHSBlockFeatures(chromas[idx1], mfccs[idx1], BeatsPerBlock)
(Features2, O2) = getSHSBlockFeatures(chromas[idx2], mfccs[idx2], BeatsPerBlock)
compareTwoFeatureSets({}, Features1, O1, Features2, O2, CSMTypes, Kappa, "cocaine", NIters = NIters, K = K, song1name = database[idx1]['artist'], song2name = database[idx2]['artist'])