forked from glenncameron2/Performance-RNN-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
238 lines (192 loc) · 7.68 KB
/
generate.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
import torch
import numpy as np
import os, sys, optparse
import config, utils
from config import device, model as model_config
from model import PerformanceRNN
from sequence import EventSeq, Control, ControlSeq
from quantize import Quantizer
# pylint: disable=E1101,E1102
#========================================================================
# Settings
#========================================================================
def getopt():
parser = optparse.OptionParser()
parser.add_option('-c', '--control',
dest='control',
type='string',
default=None,
help=('control or a processed data file path, '
'e.g., "PITCH_HISTOGRAM;NOTE_DENSITY" like '
'"2,0,1,1,0,1,0,1,1,0,0,1;4", or '
'";3" (which gives all pitches the same probability), '
'or "/path/to/processed/midi/file.data" '
'(uses control sequence from the given processed data)'))
parser.add_option('-b', '--batch-size',
dest='batch_size',
type='int',
default=8)
parser.add_option('-s', '--session',
dest='sess_path',
type='string',
default='save/train.sess',
help='session file containing the trained model')
parser.add_option('-o', '--output-dir',
dest='output_dir',
type='string',
default='output/')
parser.add_option('-l', '--max-length',
dest='max_len',
type='int',
default=0)
parser.add_option('-g', '--greedy-ratio',
dest='greedy_ratio',
type='float',
default=1.0)
parser.add_option('-B', '--beam-size',
dest='beam_size',
type='int',
default=0)
parser.add_option('-T', '--temperature',
dest='temperature',
type='float',
default=1.0)
parser.add_option('-z', '--init-zero',
dest='init_zero',
action='store_true',
default=False)
# Distiller Begin.
parser.add_option('-i', '--input-midi-file',
dest='input_midi_file',
type='string',
default=None,
help='path to MIDI file containing user input')
parser.add_option('-q', '--stats-file',
dest='stats_file',
type='string',
default=None,
help='path to YAML file containing quantization stats')
# Distiller End.
return parser.parse_args()[0]
opt = getopt()
# Distiller Begin.
import preprocess
input_midi_file = opt.input_midi_file
if input_midi_file is not None:
assert os.path.isfile(input_midi_file), f'"{input_midi_file}" is not a file'
user_events, user_control = preprocess.preprocess_midi(input_midi_file)
else:
user_events = None
user_control = None
stats_file = opt.stats_file
use_quantization = stats_file is not None
# Distiller End.
#------------------------------------------------------------------------
output_dir = opt.output_dir
sess_path = opt.sess_path
batch_size = opt.batch_size
max_len = opt.max_len
greedy_ratio = opt.greedy_ratio
control = opt.control
use_beam_search = opt.beam_size > 0
beam_size = opt.beam_size
temperature = opt.temperature
init_zero = opt.init_zero
if use_beam_search:
greedy_ratio = 'DISABLED'
else:
beam_size = 'DISABLED'
assert os.path.isfile(sess_path), f'"{sess_path}" is not a file'
if control is not None:
if os.path.isfile(control) or os.path.isdir(control):
if os.path.isdir(control):
files = list(utils.find_files_by_extensions(control))
assert len(files) > 0, f'no file in "{control}"'
control = np.random.choice(files)
_, compressed_controls = torch.load(control)
controls = ControlSeq.recover_compressed_array(compressed_controls)
if max_len == 0:
max_len = controls.shape[0]
controls = torch.tensor(controls, dtype=torch.float32)
controls = controls.unsqueeze(1).repeat(1, batch_size, 1).to(device)
control = f'control sequence from "{control}"'
else:
pitch_histogram, note_density = control.split(';')
pitch_histogram = list(filter(len, pitch_histogram.split(',')))
if len(pitch_histogram) == 0:
pitch_histogram = np.ones(12) / 12
else:
pitch_histogram = np.array(list(map(float, pitch_histogram)))
assert pitch_histogram.size == 12
assert np.all(pitch_histogram >= 0)
pitch_histogram = pitch_histogram / pitch_histogram.sum() \
if pitch_histogram.sum() else np.ones(12) / 12
note_density = int(note_density)
assert note_density in range(len(ControlSeq.note_density_bins))
control = Control(pitch_histogram, note_density)
controls = torch.tensor(control.to_array(), dtype=torch.float32)
controls = controls.repeat(1, batch_size, 1).to(device)
control = repr(control)
else:
controls = None
control = 'NONE'
assert max_len > 0, 'either max length or control sequence length should be given'
#------------------------------------------------------------------------
print('-' * 70)
print('Session:', sess_path)
print('Batch size:', batch_size)
print('Max length:', max_len)
print('Greedy ratio:', greedy_ratio)
print('Beam size:', beam_size)
print('Output directory:', output_dir)
print('Controls:', control)
print('Temperature:', temperature)
print('Init zero:', init_zero)
print('-' * 70)
#========================================================================
# Generating
#========================================================================
state = torch.load(sess_path)
model = PerformanceRNN(**state['model_config']).to(device)
model.load_state_dict(state['model_state'])
# Distiller begin.
if use_quantization:
# Quantizer.model.
Q = Quantizer(model)
quantizer = Q.quantize(stats_file)
model = quantizer.model.to(device)
# Distiller end.
model.eval()
print(model)
print('-' * 70)
if init_zero:
init = torch.zeros(batch_size, model.init_dim).to(device)
else:
init = torch.randn(batch_size, model.init_dim).to(device)
with torch.no_grad():
if use_beam_search:
outputs = model.beam_search(init, max_len, beam_size,
controls=controls,
temperature=temperature,
verbose=True)
else:
outputs = model.generate(init, max_len,
controls=controls,
user_events=user_events if user_events is not None else None, # Added.
greedy=greedy_ratio,
temperature=temperature,
verbose=True)
outputs = outputs.cpu().numpy().T # [batch, steps]
#========================================================================
# Saving
#========================================================================
os.makedirs(output_dir, exist_ok=True)
for i, output in enumerate(outputs):
# FIXME
#import pdb
#pdb.set_trace()
# FIXME
name = f'output-{i:03d}.mid'
path = os.path.join(output_dir, name)
n_notes = utils.event_indeces_to_midi_file(output, path)
print(f'===> {path} ({n_notes} notes)')