-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple_tcn_eval.py
158 lines (148 loc) · 7.89 KB
/
simple_tcn_eval.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
from simple_tcn import TCNClassifier, NetworkInterface, N_MIDI_PITCH, CONTEXT_LENGTH
import numpy as np
from midi_structure import get_piano_roll, prepare_quantization, evaluate_result, evaluations_to_latex, get_split
import pretty_midi
import os
from settings import LMD_MATCHED_FOLDER, RWC_DATASET_PATH
import matplotlib.pyplot as plt
import torch
from crf import CRFDecoder
from metrical_crf import get_ternary_transition
def decode(log_observations):
log_observations = torch.tensor(log_observations)
# log_transitions = get_log_transitions(4)
log_transitions, indices = get_ternary_transition(np.array([-5.0, -4.0, -3.0, -2.0]), np.array([-8.0, -7.0, -6.0, -5.0]))
log_observations = log_observations[:, indices]
crf = CRFDecoder(torch.tensor(log_transitions))
result = crf.viterbi_decode(log_observations[None]).squeeze(0).numpy()
return indices[result]
def model_eval(model, midi_path, subbeat_count=4, drums=1, melody=1, others=1, visualize=True, tracks=None, crf=True):
# print('Evaluating:', midi_path)
try:
midi = pretty_midi.PrettyMIDI(midi_path)
except:
print('Midi load failed: %s' % midi_path)
return None
n_subbeat, downbeat_bins, boundaries, subbeat_time = prepare_quantization(midi, subbeat_count)
piano_rolls = [get_piano_roll(ins, boundaries, False, ignore_drums=True) for ins in midi.instruments]
onset_rolls = [get_piano_roll(ins, boundaries, True, ignore_drums=True) for ins in midi.instruments]
drum_rolls = [get_piano_roll(ins, boundaries, True, ignore_drums=False, ignore_non_drums=True) for ins in midi.instruments]
rolls = []
ins_names = []
# collect all drum tracks first
for j, ins in enumerate(midi.instruments):
if (ins.is_drum):
if (drums == 0 or (tracks is not None and j not in tracks)):
continue
roll = np.concatenate((onset_rolls[j], piano_rolls[j], drum_rolls[j]), axis=-1)
rolls.append(roll)
ins_names.append('drums:%d' % j)
if (len(rolls) > 1):
rolls = [np.max(rolls, axis=0)]
ins_names = ['drums:-1']
for j, ins in enumerate(midi.instruments):
if (ins.is_drum):
continue
if ('mel' in ins.name.lower() or 'vocal' in ins.name.lower()):
if (melody == 0 or (tracks is not None and j not in tracks)):
continue
ins_name = 'melody'
else:
ins_name = pretty_midi.program_to_instrument_name(ins.program) + '(%d)' % ins.program
if (others == 0 or (tracks is not None and j not in tracks)):
continue
roll = np.concatenate((onset_rolls[j], piano_rolls[j], drum_rolls[j]), axis=-1)
rolls.append(roll)
ins_names.append('%s:%d' % (ins_name, j))
# visualized_preds, _ = model.inference(roll.astype(np.float32))
# visualized_preds = np.cumsum(visualized_preds[:, ::-1], axis=1)
# plt.figure(figsize=(26, 6))
# plt.imshow(np.concatenate((piano_rolls[j][:, ::-1], np.repeat(visualized_preds, 16, axis=1)), axis=1).T, interpolation='nearest')
# plt.title(os.path.basename(midi_path))
# plt.show()
if (len(rolls) == 0):
print('No track!')
return None
# print('Tracks: %d' % (len(rolls)))
rolls = np.stack(rolls, axis=0)
log_final_pred, log_conf = model.inference_function('inference_song', rolls.astype(np.float32), return_log_prob=True)
used_downbeats = downbeat_bins[downbeat_bins < len(log_final_pred)]
log_downbeat_pred = log_final_pred[used_downbeats]
if (crf == True):
result = decode(log_downbeat_pred)
else:
result = np.argmax(log_downbeat_pred, axis=-1)
if (visualize):
onehot_result = np.eye(5)[result]
final_pred = np.exp(log_final_pred)
visualized_preds = np.cumsum(final_pred[:, ::-1], axis=1)
visualized_result = np.zeros((final_pred.shape[0], 5))
visualized_result[used_downbeats] = onehot_result
visualized_result = np.cumsum(visualized_result[:, ::-1], axis=1)
plt.figure(figsize=(26, 6))
plt.imshow(np.concatenate((rolls.max(axis=0)[:, ::-1], np.repeat(visualized_preds, 16, axis=1), np.repeat(visualized_result, 16, axis=1)), axis=1).T)
plt.title(os.path.basename(midi_path) + ' final')
plt.show()
gt_midi_path = 'annotation/%s_gt.mid' % os.path.basename(midi_path)
if (os.path.exists(gt_midi_path)):
evaluation = evaluate_result(result, gt_midi_path, downbeat_bins, subbeat_count, 4)
# print(('%s:\t' % gt_midi_path) + '\t'.join(str(x) for x in evaluation))
else:
evaluation = None
# print(conf)
output = pretty_midi.Instrument(program=0, is_drum=True, name='Layers')
for i, pred in enumerate(result):
for k in range(pred):
onset_time = subbeat_time[downbeat_bins[i]]
output.notes.append(pretty_midi.Note(velocity=100, pitch=40 + k, start=onset_time, end=onset_time + 0.5))
midi.instruments.append(output)
if not (os.path.exists('output/%s' % model.save_name)):
os.mkdir('output/%s' % model.save_name)
midi.write('output/%s/%s_crf.mid' % (model.save_name, os.path.basename(midi_path)))
np.savetxt('output/%s/%s_conf.txt' % (model.save_name, os.path.basename(midi_path)), log_conf)
f = open('output/%s/%s_conf_ins.txt' % (model.save_name, os.path.basename(midi_path)), 'w')
f.write(','.join(ins_names))
f.close()
return evaluation
def evaluate_lmd(model, count):
f = open('data/lmd_matched_usable_midi.txt', 'r')
lines = [line.strip() for line in f.readlines() if line.strip() != '']
f.close()
np.random.seed(6172)
np.random.shuffle(lines)
lines = lines[:count]
for line in lines:
model_eval(model, os.path.join(LMD_MATCHED_FOLDER, line), visualize=False)
def main(visualize, custom_midi):
model = NetworkInterface(TCNClassifier(384, 256, 6, 5, 0.1),
'simple_tcn_v2.0_filtered', load_checkpoint=False)
if custom_midi is not None:
model_eval(model, custom_midi, visualize=visualize)
else:
for split in ['val', 'test']:
split_files = get_split('rwc_multitrack_hierarchy_v6_supervised', split)
print(f'Dataset: RWC Pop {split}')
print(evaluations_to_latex('Proposed\nw/o CRF',
[model_eval(model, os.path.join(RWC_DATASET_PATH, 'AIST.RWC-MDB-P-2001.SMF_SYNC', file),
drums=1, melody=1, others=1, visualize=visualize, crf=False) for file in split_files]))
print(evaluations_to_latex('Proposed\n(mel. only)',
[model_eval(model, os.path.join(RWC_DATASET_PATH, 'AIST.RWC-MDB-P-2001.SMF_SYNC', file),
drums=0, melody=1, others=0, visualize=visualize) for file in split_files]))
print(evaluations_to_latex('Proposed\n(no drums)',
[model_eval(model, os.path.join(RWC_DATASET_PATH, 'AIST.RWC-MDB-P-2001.SMF_SYNC', file),
drums=0, melody=1, others=1, visualize=visualize) for file in split_files]))
print(evaluations_to_latex('Proposed',
[model_eval(model, os.path.join(RWC_DATASET_PATH, 'AIST.RWC-MDB-P-2001.SMF_SYNC', file),
drums=1, melody=1, others=1, visualize=visualize) for file in split_files]))
print('Dataset: POP909 test')
model_eval(model, R'E:\Dataset\lmd_matched\L\C\N\TRLCNWM128F423BB63\7596e59dea60afab6bbc7207aca8bd8c.mid')
print(evaluations_to_latex('Proposed\n(mel. only)',
[model_eval(model, R'input/POP909-%d.mid' % (i + 1), tracks=[0], visualize=visualize) for i in range(5)]))
print(evaluations_to_latex('Proposed',
[model_eval(model, R'input/POP909-%d.mid' % (i + 1), visualize=visualize) for i in range(5)]))
if __name__ == '__main__':
import sys
if (len(sys.argv) >= 2):
main(visualize=True, custom_midi=sys.argv[1])
else:
main(visualize=False, custom_midi=None)