-
Notifications
You must be signed in to change notification settings - Fork 8
/
compute_orig_feats.py
executable file
·222 lines (188 loc) · 6.75 KB
/
compute_orig_feats.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
#!/usr/bin/env python
"""
Computes various features from the SHS dataset.
----
Authors:
Uri Nieto ([email protected])
Eric J. Humphrey ([email protected])
----
License:
This code is distributed under the GNU LESSER PUBLIC LICENSE
(LGPL, see www.gnu.org).
Copyright (c) 2012-2013 MARL@NYU.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
a. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
b. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
c. Neither the name of MARL, NYU nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
"""
import os
import sys
import cPickle
import numpy as np
import argparse
import time
import random
# local stuff
import utils
import dan_tools
def compute_original_feats(maindir, tracks, cliques, output="originalfeats.pk"):
"""Computes the original features."""
X = []
I = []
cnt = 0
k = 0
for tid in tracks:
path = utils.path_from_tid(maindir, tid)
feats = utils.extract_feats(path)
if feats == None:
continue
x = np.zeros(feats.shape)
for i,feat in enumerate(feats):
#feat = dan_tools.chromnorm(feat.reshape(feat.shape[0], 1)).squeeze()
#feat = np.reshape(feat, (1,900))
x[i] = feat
X.append(x)
for i, clique in enumerate(cliques):
if tid in clique:
idx = i
break
I.append(idx)
if cnt % 50 == 0 and cnt != 0:
print "---Computing features: %d of %d" % (cnt, len(tracks))
f = open("/Volumes/Audio/SummaryCovers/originalfeats%d.pk"%k, 'w')
cPickle.dump((X,I), f)
f.close()
k += 1
X = []
cnt += 1
def compute_clique_idxs(cliques, tracks, output="clique_idx.pk"):
"""Computes the clique indeces."""
clique_idx = []
for t, tid in enumerate(tracks):
if t == 244: print tid
idx = -1
for i, clique in enumerate(cliques):
if tid in clique:
idx = i
break
clique_idx.append(idx)
assert idx != -1, "ERROR computing clique idxs"
f = open(output, 'w')
cPickle.dump(np.asarray(clique_idx), f)
f.close()
def compute_one_clique(maindir, cliques, mu, sd, clique_id=0,
output="clique_vs_nonclique.pk"):
"""Computes the features for one clique, and N other tracks as
non_cliques."""
X = dict()
X["cliques"] = []
X["non_cliques"] = []
for tid in cliques[clique_id]:
path = utils.path_from_tid(maindir, tid)
feats = utils.extract_feats(path)
if feats == None:
continue
x = np.zeros((feats.shape[0], feats.shape[1]/2))
for i,feat in enumerate(feats):
x[i] = feat[450:]
X["cliques"].append(x)
N = len(cliques[clique_id])
n = 0
checked_cliques = []
checked_cliques.append(clique_id)
while n < N:
idx = np.random.random_integers(0,len(cliques))
if idx in checked_cliques:
continue
path = utils.path_from_tid(maindir, cliques[idx][0])
feats = utils.extract_feats(path)
if feats == None:
continue
x = np.zeros((feats.shape[0], feats.shape[1]/2))
for i,feat in enumerate(feats):
x[i] = feat[450:]
n += 1
X["non_cliques"].append(x)
feats = np.empty((0,450))
bounds = []
for key in X:
print key
for x in X[key]:
x = standardize(x, mu, sd)
feats = np.concatenate((feats, x), axis=0)
try:
bounds.append(x.shape[0] + bounds[-1])
except:
bounds.append(x.shape[0])
plt.imshow(feats, interpolation="nearest", aspect="auto")
for bound in bounds:
plt.axhline(bound, color="magenta", linewidth=2.0)
plt.show()
f = open(output, 'w')
cPickle.dump(X, f)
f.close()
def compute_N_cliques(maindir, cliques, N=10, output="cliques.pk"):
"""Computes the features for N cliques."""
X = []
clique_ids = []
for i in xrange(N):
clique_id = random.randint(0, len(cliques)-1)
while clique_id in clique_ids:
clique_id = random.randint(0, len(cliques)-1)
clique_ids.append(clique_id)
x =[]
for tid in cliques[clique_id]:
path = utils.path_from_tid(maindir, tid)
feats = utils.extract_feats(path)
if feats == None:
continue
x.append(feats)
X.append(x)
f = open(output, 'w')
cPickle.dump(X, f)
f.close()
def main():
# Args parser
parser = argparse.ArgumentParser(description=
"Computes various features from the SHS dataset")
parser.add_argument("maindir", action="store",
help="/data directory")
parser.add_argument("shsf", action="store",
help="SHS dataset files")
args = parser.parse_args()
# sanity cheks
assert os.path.isdir(args.maindir)
assert os.path.isfile(args.shsf)
# read cliques and all tracks
cliques, all_tracks = utils.read_shs_file(args.shsf)
# Compute specific features
#compute_one_clique(args.maindir, cliques, clique_id=3753, mu=dict["mu"],
# sd=dict["sd"])
#print "Computing original features"
#compute_original_feats(args.maindir, all_tracks, cliques)
print "Computing clique indeces"
compute_clique_idxs(cliques, all_tracks)
#print "Computing N cliques"
#compute_N_cliques(args.maindir, cliques, N=10)
# done
print 'DONE!'
if __name__ == '__main__':
main()