forked from purvaten/feel-the-music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_dance.py
375 lines (317 loc) · 14.6 KB
/
generate_dance.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""Script to generate all dances for a song."""
from visualize import save_matrices, save_dance
from multiprocessing import Pool
from PIL import Image
import numpy as np
import itertools
import argparse
import librosa
import madmom
import random
import scipy
random.seed(123)
# parse arguments
parser = argparse.ArgumentParser(description='Process arguments.')
parser.add_argument('-songpath', '--songpath', type=str, default='./audio_files/flutesong.mp3',
help='Path to .mp3 song')
parser.add_argument('-songname', '--songname', type=str, default='flutesong',
help='Name of song')
parser.add_argument('-steps', '--steps', type=int, default=100,
help='Number of equidistant LRS steps the agent should take')
parser.add_argument('-type', '--type', type=str, default='action',
help='Type of dance -- state, action, stateplusaction')
parser.add_argument('-baseline', '--baseline', type=str, default='none',
help='Generate baseline -- none, unsync_random, unsync_sequential, sync_sequential, sync_random')
parser.add_argument('-visfolder', '--visfolder', type=str, default='./vis_num_steps_20/dancing_color_blob_20',
help='path to folder containing agent visualizations')
args = parser.parse_args()
# global variables
GRID_SIZE = 20
REWARD_INTERVAL = 5
ALL_ACTION_COMBS = list(set(itertools.permutations([-1 for _ in range(REWARD_INTERVAL)] + [1 for _ in range(REWARD_INTERVAL)] + [0 for _ in range(REWARD_INTERVAL)], REWARD_INTERVAL)))
START_POSITION = int(GRID_SIZE / 2)
ACTION_MAPPING = {-1: 'L', 1: 'R', 0: 'S'} # L: left, R: right, S; stay
MUSIC_MODE = 'affinity'
MUSIC_METRIC = 'euclidean'
HOP_LENGTH = 512
# **************************************************************************************************************** #
# BASELINES
def unsync_random(num_steps):
"""Baseline 1 : unsynced - random."""
states, actions = [], []
curr = START_POSITION
# get state and action sequences
for i in range(num_steps):
act = random.choice([-1, 0, 1])
newcurr = curr + act
if newcurr < 0:
curr = 0
elif newcurr == GRID_SIZE:
curr = GRID_SIZE - 1
else:
curr = newcurr
states.append(curr)
actions.append(act)
return [states, actions]
def unsync_sequential(num_steps):
"""Baseline 2 : unsynced - left2right."""
states, actions = [], []
curr = START_POSITION
curr, prev = START_POSITION, START_POSITION - 1
# get state and action sequences
for i in range(num_steps):
act = (curr - prev)
newcurr = curr + act
if newcurr < 0:
prev = 0
curr = 1
elif newcurr == GRID_SIZE:
prev = GRID_SIZE - 1
curr = GRID_SIZE - 2
else:
prev = curr
curr = newcurr
states.append(curr)
actions.append(act)
return [states, actions]
def sync_sequential(num_steps, filename, duration):
"""Baseline 3 : synced - left2right."""
# get beat information
proc = madmom.features.beats.DBNBeatTrackingProcessor(fps=100)
act = madmom.features.beats.RNNBeatProcessor()(filename)
beat_times = np.around(proc(act) * num_steps / duration)
states, actions = [], []
curr = START_POSITION
curr, prev = START_POSITION, START_POSITION - 1
for i in range(num_steps):
if i in beat_times:
act = (curr - prev)
newcurr = curr + act
if newcurr < 0:
prev = 0
curr = 1
elif newcurr == GRID_SIZE:
prev = GRID_SIZE - 1
curr = GRID_SIZE - 2
else:
prev = curr
curr = newcurr
else:
act = 0
states.append(curr)
actions.append(act)
return [states, actions]
def sync_random(num_steps, filename, duration):
"""Baseline 4 : synced - random."""
# get beat information
proc = madmom.features.beats.DBNBeatTrackingProcessor(fps=100)
act = madmom.features.beats.RNNBeatProcessor()(filename)
beat_times = np.around(proc(act) * num_steps / duration)
states, actions = [], []
curr = START_POSITION
for i in range(num_steps):
if i in beat_times:
act = random.choice([-1, 0, 1])
newcurr = curr + act
if newcurr < 0:
curr = 0
elif newcurr == GRID_SIZE:
curr = GRID_SIZE - 1
else:
curr = newcurr
else:
act = 0
states.append(curr)
actions.append(act)
return [states, actions]
# **************************************************************************************************************** #
# DANCE MATRIX CREATION
def fill_dance_aff_matrix_diststate(states):
"""Fill state action affinity matrix - relative distance based states."""
s = len(states)
rowtile = np.tile(states, (s, 1))
coltile = rowtile.T
sa_aff = 1. - np.abs(rowtile-coltile) / (GRID_SIZE-1)
# sa_aff = (sa_aff - np.min(sa_aff)) / (np.max(sa_aff) - np.min(sa_aff)) # normalize
return sa_aff
def fill_dance_aff_matrix_action(actions):
"""Fill state action affinity matrix - action based."""
s = len(actions)
rowtile = np.tile(actions, (s, 1))
coltile = rowtile.T
sa_aff = np.maximum(1. - np.abs(rowtile-coltile), 0.)
# sa_aff = (sa_aff - np.min(sa_aff)) / (np.max(sa_aff) - np.min(sa_aff)) # normalize
return sa_aff
def fill_dance_aff_matrix_diststateplusaction(states, actions):
"""Fill state action affinity matrix - action based."""
state_matrix = fill_dance_aff_matrix_diststate(states)
action_matrix = fill_dance_aff_matrix_action(actions)
sa_aff = (state_matrix + action_matrix) / 2.
# sa_aff = (sa_aff - np.min(sa_aff)) / (np.max(sa_aff) - np.min(sa_aff)) # normalize
return sa_aff
def get_dance_matrix(states, actions, dance_matrix_type, music_matrix_full):
"""Pass to appropriate dance matrix generation function based on dance_matrix_type."""
if dance_matrix_type == 'state':
dance_matrix = fill_dance_aff_matrix_diststate(states)
elif dance_matrix_type == 'action':
dance_matrix = fill_dance_aff_matrix_action(actions)
elif dance_matrix_type == 'stateplusaction':
dance_matrix = fill_dance_aff_matrix_diststateplusaction(states, actions)
else:
print("err")
dance_matrix = np.array(Image.fromarray(np.uint8(dance_matrix * 255)).resize(music_matrix_full.shape, Image.NEAREST)) / 255.
return dance_matrix
# **************************************************************************************************************** #
# MUSIC MATRIX COMPUTATION
def compute_music_matrix(y, sr, mode, metric):
"""Return music affinity matrix based on mode."""
lifter = 0
n_mfcc = 20
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc, lifter=lifter, hop_length=HOP_LENGTH)
R = librosa.segment.recurrence_matrix(mfcc, metric=metric, mode=mode, sym=True).T.astype(float) # already normalized in 0-1
np.fill_diagonal(R, 1) # make diagonal entries 1
return R
# **************************************************************************************************************** #
# REWARD COMPUTATION
def music_reward(music_matrix, dance_matrix, mtype):
"""Return the reward given music matrix and dance matrix."""
# compute distance based on mtype
if mtype == 'pearson':
if np.array(music_matrix).std() == 0 or np.array(dance_matrix).std() == 0:
reward = 0
else:
reward, p_val = scipy.stats.pearsonr(music_matrix.flatten(), dance_matrix.flatten())
elif mtype == 'spearman':
reward, p_val = scipy.stats.spearmanr(music_matrix.flatten(), dance_matrix.flatten())
else:
print("err")
return reward
# **************************************************************************************************************** #
# BRUTE FORCE METHODS
def get_rsa_for_actionset(args):
"""Return reward, state, action set."""
actionset, music_matrix, loc, num_actions, prev_states, prev_actions, dance_matrix_type = args
curr_states = []
curr_actions = []
start_loc = loc
for action in list(actionset):
newpos = start_loc + action
if newpos == 0 or newpos == GRID_SIZE:
# hit wall
break
curr_states.append(newpos)
curr_actions.append(action)
start_loc = newpos
# if not completed, ignore
if len(curr_actions) != num_actions:
return False, [], []
# get dance up till now
states = prev_states + curr_states
actions = prev_actions + curr_actions
# get dance matrix
if dance_matrix_type == 'state':
dance_matrix = fill_dance_aff_matrix_diststate(states)
elif dance_matrix_type == 'action':
dance_matrix = fill_dance_aff_matrix_action(actions)
else:
dance_matrix = fill_dance_aff_matrix_diststateplusaction(states, actions)
dance_matrix = np.array(Image.fromarray(np.uint8(dance_matrix * 255)).resize(music_matrix.shape, Image.NEAREST)) / 255.
# check how good dance up till now is by computing reward
curr_reward = music_reward(music_matrix, dance_matrix, 'pearson')
return curr_reward, states, actions
def getbest(loc, num_actions, prev_states, prev_actions, music_matrix_full, num_steps, dance_matrix_type):
"""Return best combination of size num_actions.
Start from `loc` in grid of size `GRID_SIZE`.
"""
scale = int(music_matrix_full.shape[0] * (len(prev_states)+num_actions) / num_steps)
music_matrix = np.array([music_matrix_full[i][:scale] for i in range(scale)])
# get best dance for this music matrix
bestreward = 0
#p = Pool()
args = ((actionset, music_matrix, loc, num_actions, prev_states, prev_actions, dance_matrix_type) for actionset in ALL_ACTION_COMBS)
res = map(get_rsa_for_actionset, args)
# p.close()
for curr_reward, states, actions in res:
if curr_reward is not False and curr_reward > bestreward:
bestreward = curr_reward
beststates = states
bestactions = actions
return beststates, bestactions, bestreward
# **************************************************************************************************************** #
# MAIN
if __name__ == "__main__":
# get args
songname = args.songname
baseline = args.baseline
dance_matrix_type = args.type
num_steps = args.steps
visfolder = args.visfolder
songpath = args.songpath
# load song
y, sr = librosa.load(songpath) # default sampling rate 22050
duration = librosa.get_duration(y=y, sr=sr)
# get music matrix
music_matrix_full = compute_music_matrix(y, sr, MUSIC_MODE, MUSIC_METRIC)
# main code
if baseline in ['unsync_random', 'unsync_sequential', 'sync_sequential', 'sync_random']:
# check baselines
if baseline == 'unsync_random':
states, actions = unsync_random(num_steps=num_steps)
if baseline == 'unsync_sequential':
states, actions = unsync_sequential(num_steps=num_steps)
if baseline == 'sync_sequential':
states, actions = sync_sequential(num_steps=num_steps, filename=songpath, duration=duration)
else:
states, actions = sync_random(num_steps=num_steps, filename=songpath, duration=duration)
# compute dance matrix
dance_matrix = get_dance_matrix(states, actions, dance_matrix_type, music_matrix_full)
# compute correlation
reward = music_reward(music_matrix_full, dance_matrix, 'pearson')
else:
# our approach
# try out all combinations of `REWARD_INTERVAL` actions and compute reward
prev_states = []
prev_actions = []
for i in range(num_steps):
# apply greedy algo to get dance matrix with best reward
if len(prev_actions) == 0:
prev_states, prev_actions, reward = getbest(loc=START_POSITION,
num_actions=REWARD_INTERVAL,
prev_states=prev_states,
prev_actions=prev_actions,
music_matrix_full=music_matrix_full,
num_steps=num_steps,
dance_matrix_type=dance_matrix_type)
elif not i % REWARD_INTERVAL:
prev_states, prev_actions, reward = getbest(loc=prev_states[-1],
num_actions=REWARD_INTERVAL,
prev_states=prev_states,
prev_actions=prev_actions,
music_matrix_full=music_matrix_full,
num_steps=num_steps,
dance_matrix_type=dance_matrix_type)
elif num_steps - len(prev_actions) != 0 and num_steps - len(prev_actions) < REWARD_INTERVAL:
prev_states, prev_actions, reward = getbest(loc=prev_states[-1],
num_actions=num_steps-len(prev_states),
prev_states=prev_states,
prev_actions=prev_actions,
music_matrix_full=music_matrix_full,
num_steps=num_steps,
dance_matrix_type=dance_matrix_type)
else:
continue
# get best dance matrix
dance_matrix = get_dance_matrix(prev_states, prev_actions, dance_matrix_type, music_matrix_full)
# assign states and actions correctly correctly
states = prev_states
actions = prev_actions
# map actions correctly
actions = [ACTION_MAPPING[a] for a in actions]
# print results
print("Correlation = ", reward)
print("State sequence = ", states)
print("Action sequence = ", actions)
# visualize results
save_matrices(music_matrix=music_matrix_full, dance_matrix=dance_matrix, duration=duration)
save_dance(states=states, visfolder=visfolder, songname=songname, duration=duration, num_steps=num_steps)
print(songname, " :: DONE!")