-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
212 lines (181 loc) · 7.86 KB
/
inference.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
from model_ead import TransformerXL
import pickle
import os
import torch
import yaml
import json
import make_loops as loops
import guitarpro
import numpy as np
import datetime
from dadagp import dadagp_decode, tokens2guitarpro
from primers import build_primer
from dataclasses import dataclass
@dataclass
class LoopExtractConfig:
loop_size: int = 4
min_length: int = 4
min_rep_beats: float = 2.0
density: float = 1.0
def load_model(model_config, inference_config, device):
# load dictionary
event2word = pickle.load(open(inference_config['vocab_data_path'], 'rb'))
word2event = pickle.load(open(inference_config['rev_vocab_data_path'], 'rb'))
model_path = get_model_path(inference_config)
# declare model
model = TransformerXL(
model_config,
model_path,
device,
event2word=event2word,
word2event=word2event,
is_training=False)
return model
def get_device(inference_config):
return inference_config['gpuID']
def get_model_path(inference_config):
# checkpoint information
CHECKPOINT_FOLDER = inference_config['experiment_dir']
checkpoint_type = inference_config['checkpoint_type']
if checkpoint_type == 'best_train':
model_path = os.path.join(CHECKPOINT_FOLDER, 'model_best.pth.tar')
elif checkpoint_type == 'best_val':
model_path = os.path.join(CHECKPOINT_FOLDER, 'model_best_val.pth.tar')
elif checkpoint_type == 'epoch_idx':
model_path = os.path.join(CHECKPOINT_FOLDER, 'ep_{}.pth.tar'.format(str(inference_config['model_epoch'])))
return model_path
def create_output_dir(inference_config, output_name):
parent_output_dir = inference_config['generated_dir']
if not os.path.exists(parent_output_dir):
os.mkdir(parent_output_dir)
experiment_dir = os.path.join(inference_config['generated_dir'], output_name)
if not os.path.exists(experiment_dir):
print('Creating experiment_dir:', experiment_dir)
os.mkdir(experiment_dir)
return experiment_dir
def decode_outputs(experiment_dir):
for item in os.listdir(experiment_dir):
full_path = os.path.join(experiment_dir, item)
if full_path.endswith(".txt"):
print("Decoding {}...".format(item))
output_path = os.path.join(experiment_dir, item[:-4] + ".gp5")
dadagp_decode(full_path, output_path)
def extract_loops(experiment_dir):
for idx, item in enumerate(os.listdir(experiment_dir)):
full_path = os.path.join(experiment_dir, item)
if full_path.endswith(".txt"):
print("Extracting loops from {}...".format(item))
text_file = open(full_path, "r")
tokens = text_file.read().split("\n")
song = tokens2guitarpro(tokens, verbose=False)
extract_loops_from_song(song, tokens, idx, experiment_dir)
#calculate average number of notes per instrument in each measure
def calc_density(token_list):
num_meas = 0
timestamp = 0
num_notes = {}
for i in range(len(token_list)):
t = token_list[i]
if "note" in t:
instrument = t.split(":")[0]
if instrument not in num_notes:
num_notes[instrument] = 1
else:
num_notes[instrument] += 1
if t == "new_measure":
num_meas += 1
total_notes = 0
for inst in num_notes.keys():
total_notes += num_notes[inst]
curr_density = total_notes * 1.0 / len(num_notes)
return curr_density / num_meas
def extract_loops_from_song(song, generated_tokens, name, output_dir, loop_config):
#loop extraction parameters
LOOP_SIZE = loop_config.loop_size
MIN_LEN = loop_config.min_length
MIN_REP_BEATS = loop_config.min_rep_beats
DENSITY = loop_config.density
#extract loops
track_list, time_signatures = loops.create_track_list(song)
beats_per_bar = 4 #inference forces 4/4
min_beats = beats_per_bar * LOOP_SIZE
max_beats = beats_per_bar * LOOP_SIZE
lead_mat, lead_dur, melody_seq = loops.calc_correlation(track_list, 0)
_, loop_endpoints = loops.get_valid_loops(melody_seq, lead_mat, lead_dur, min_len=MIN_LEN, min_beats=min_beats, max_beats=max_beats, min_rep_beats=MIN_REP_BEATS)
token_list = loops.unify_loops(generated_tokens, loop_endpoints, density=DENSITY)
token_list_repeats = loops.get_repeats(generated_tokens, min_meas=LOOP_SIZE, max_meas=LOOP_SIZE, density=DENSITY)
token_list = token_list + token_list_repeats
if token_list[-1] != "end":
token_list.append("end")
loops_length = len(token_list)
if loops_length > 10:
header_data = token_list[:4]
main_data = token_list[4:]
num_measures = 0
split_loops = []
current_loop = []
for i,token in enumerate(main_data):
if token == "new_measure":
if num_measures > 0 and num_measures % LOOP_SIZE == 0: #end of a loop
split_loops.append(current_loop)
current_loop = []
num_measures += 1
if token == "end":
split_loops.append(current_loop)
break
current_loop.append(token)
num_segments = 0
for i,loop in enumerate(split_loops):
duplicate = False
current = " ". join(split_loops[i])
for j in range(0,i):
comparison = " ".join(split_loops[j])
if comparison == current:
duplicate = True
break
if not duplicate:
save_loop(loop, header_data, output_dir, name, num_segments)
num_segments += 1
print("Found {} loops in {}".format(num_segments, name))
else:
print("Found 0 loops in {}".format(name))
def save_loop(loop_tokens, header, output_dir, name, idx):
full_tokens = header + loop_tokens
full_tokens.append("end")
density = calc_density(full_tokens)
print("Exporting loop with {} density".format(density))
song = tokens2guitarpro(full_tokens, verbose=False)
song.artist = full_tokens[0]
song.album = 'Generated by DadaGP'
song.title = "untitled"
dadagp_path = os.path.join(output_dir, str(name) + "_loop" + str(idx) + ".gp5")
guitarpro.write(song, dadagp_path)
def run_single_inference(output_loc, name, model, num_bars, primer, temp, loop_config):
generated_tokens = model.inference_single_from_primer(['temperature', 'nucleus'], {'t': temp ,'p': 0.9, 'num_bars': num_bars}, primer)
text_output_path = os.path.join(output_loc, name + "_tokens" + ".txt")
with open(text_output_path, "w") as text_file:
text_file.write("\n".join(generated_tokens))
#save raw generation as GuitarPro file
song = tokens2guitarpro(generated_tokens, verbose=False)
song.artist = generated_tokens[0]
song.album = 'Generated by DadaGP'
song.title = "untitled"
dadagp_path = os.path.join(output_loc, name + "_full" + ".gp5")
guitarpro.write(song, dadagp_path)
extract_loops_from_song(song, generated_tokens, name, output_loc, loop_config)
def setup_inference(output_loc):
model_config = yaml.full_load(open("model_config.yaml", 'r'))
inference_config = yaml.full_load(open("inference_config.yaml", 'r'))
device = get_device(inference_config)
model = load_model(model_config, inference_config, device)
experiment_dir = create_output_dir(inference_config, output_loc)
return experiment_dir, model
if __name__ == '__main__':
output_loc = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
experiment_dir, model = setup_inference(output_loc)
for idx in range(5):
name = "example_" + str(idx)
primer = build_primer(120, key="e", duration=480)
bars_to_generate = 8
loop_extract_config = LoopExtractConfig(loop_size=2) #defaults
run_single_inference(experiment_dir, name, model, bars_to_generate, primer, loop_extract_config)