-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunconditional.py
162 lines (122 loc) · 4.96 KB
/
unconditional.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
# -*- coding: utf-8 -*-
### unconditional
"""Generating Piano Music with Transformer.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/notebooks/magenta/piano_transformer/piano_transformer.ipynb
##### Copyright 2019 Google LLC.
Licensed under the Apache License, Version 2.0 (the "License");
# Generating Piano Music with Transformer
### ___Ian Simon, Anna Huang, Jesse Engel, Curtis "Fjord" Hawthorne___
This Colab notebook lets you play with pretrained [Transformer](https://arxiv.org/abs/1706.03762) models for piano music generation, based on the [Music Transformer](http://g.co/magenta/music-transformer) model introduced by [Huang et al.](https://arxiv.org/abs/1809.04281) in 2018.
The models used here were trained on over 10,000 hours of piano recordings from YouTube, transcribed using [Onsets and Frames](http://g.co/magenta/onsets-frames) and represented using the event vocabulary from [Performance RNN](http://g.co/magenta/performance-rnn).
Unlike the original Music Transformer paper, this notebook uses attention based on absolute instead of relative position; we may add models that use relative attention at some point in the future.
# Environment Setup
"""
# Commented out IPython magic to ensure Python compatibility.
#@title Setup Environment
#@markdown Copy some auxiliary data from Google Cloud Storage.
#@markdown Also install and import Python dependencies needed
#@markdown for running the Transformer models.
# %tensorflow_version 1.x
print('Importing libraries...')
import numpy as np
import os
import tensorflow.compat.v1 as tf
from tensor2tensor import models
from tensor2tensor import problems
from tensor2tensor.data_generators import text_encoder
from tensor2tensor.utils import decoding
from tensor2tensor.utils import trainer_lib
from magenta.models.score2perf import score2perf
import note_seq
import sys
tf.disable_v2_behavior()
print('Done!')
#@title Definitions
#@markdown Define a few constants and helper functions.
SF2_PATH = './content/Yamaha-C5-Salamander-JNv5.1.sf2'
SAMPLE_RATE = 16000
# Decode a list of IDs.
def decode(ids, encoder):
ids = list(ids)
if text_encoder.EOS_ID in ids:
ids = ids[:ids.index(text_encoder.EOS_ID)]
return encoder.decode(ids)
"""# Piano Performance Language Model"""
if (len(sys.argv)) < 2:
output_name = 'unconditional.mid'
else:
output_name = sys.argv[1]
#@title Setup and Load Checkpoint
#@markdown Set up generation from an unconditional Transformer
#@markdown model.
print('Setup and load checkpoint')
model_name = 'transformer'
hparams_set = 'transformer_tpu'
ckpt_path = './checkpoints/unconditional_model_16.ckpt'
class PianoPerformanceLanguageModelProblem(score2perf.Score2PerfProblem):
@property
def add_eos_symbol(self):
return True
problem = PianoPerformanceLanguageModelProblem()
unconditional_encoders = problem.get_feature_encoders()
# Set up HParams.
print('setup HParams')
hparams = trainer_lib.create_hparams(hparams_set=hparams_set)
trainer_lib.add_problem_hparams(hparams, problem)
hparams.num_hidden_layers = 16
hparams.sampling_method = 'random'
# Set up decoding HParams.
print('setup decoding hparams')
decode_hparams = decoding.decode_hparams()
decode_hparams.alpha = 0.0
decode_hparams.beam_size = 1
# Create Estimator.
print('create estimator')
run_config = trainer_lib.create_run_config(hparams)
estimator = trainer_lib.create_estimator(
model_name, hparams, run_config,
decode_hparams=decode_hparams)
# Create input generator (so we can adjust priming and
# decode length on the fly).
def input_generator():
global targets
global decode_length
while True:
yield {
'targets': np.array([targets], dtype=np.int32),
'decode_length': np.array(decode_length, dtype=np.int32)
}
# These values will be changed by subsequent cells.
targets = []
decode_length = 0
# Start the Estimator, loading from the specified checkpoint.
print('start estimator')
input_fn = decoding.make_input_fn_from_generator(input_generator())
unconditional_samples = estimator.predict(
input_fn, checkpoint_path=ckpt_path)
# "Burn" one.
_ = next(unconditional_samples)
#@title Generate from Scratch
#@markdown Generate a piano performance from scratch.
#@markdown
#@markdown This can take a minute or so depending on the length
#@markdown of the performance the model ends up generating.
#@markdown Because we use a
#@markdown [representation](http://g.co/magenta/performance-rnn)
#@markdown where each event corresponds to a variable amount of
#@markdown time, the actual number of seconds generated may vary.
targets = []
decode_length = 1024
# Generate sample events.
print('generate sample events')
sample_ids = next(unconditional_samples)['outputs']
# Decode to NoteSequence.
print('decode to note sequence')
midi_filename = decode(
sample_ids,
encoder=unconditional_encoders['targets'])
#@title Download Performance as MIDI
#@markdown Download generated performance as MIDI (optional).
os.rename(midi_filename, output_name)