forked from magenta/magenta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
melody_rnn_create_dataset.py
135 lines (109 loc) · 4.69 KB
/
melody_rnn_create_dataset.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
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Create a dataset of SequenceExamples from NoteSequence protos.
This script will extract melodies from NoteSequence protos and save them to
TensorFlow's SequenceExample protos for input to the melody RNN models.
"""
import os
# internal imports
import tensorflow as tf
import magenta
from magenta.models.melody_rnn import melody_rnn_config_flags
from magenta.pipelines import dag_pipeline
from magenta.pipelines import melody_pipelines
from magenta.pipelines import note_sequence_pipelines
from magenta.pipelines import pipeline
from magenta.pipelines import pipelines_common
from magenta.protobuf import music_pb2
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('input', None,
'TFRecord to read NoteSequence protos from.')
tf.app.flags.DEFINE_string('output_dir', None,
'Directory to write training and eval TFRecord '
'files. The TFRecord files are populated with '
'SequenceExample protos.')
tf.app.flags.DEFINE_float('eval_ratio', 0.1,
'Fraction of input to set aside for eval set. '
'Partition is randomly selected.')
tf.app.flags.DEFINE_string('log', 'INFO',
'The threshold for what messages will be logged '
'DEBUG, INFO, WARN, ERROR, or FATAL.')
class EncoderPipeline(pipeline.Pipeline):
"""A Module that converts monophonic melodies to a model specific encoding."""
def __init__(self, config, name):
"""Constructs an EncoderPipeline.
Args:
config: A MelodyRnnConfig that specifies the encoder/decoder, pitch range,
and what key to transpose into.
name: A unique pipeline name.
"""
super(EncoderPipeline, self).__init__(
input_type=magenta.music.Melody,
output_type=tf.train.SequenceExample,
name=name)
self._melody_encoder_decoder = config.encoder_decoder
self._min_note = config.min_note
self._max_note = config.max_note
self._transpose_to_key = config.transpose_to_key
def transform(self, melody):
melody.squash(
self._min_note,
self._max_note,
self._transpose_to_key)
encoded = self._melody_encoder_decoder.encode(melody)
return [encoded]
def get_pipeline(config, eval_ratio):
"""Returns the Pipeline instance which creates the RNN dataset.
Args:
config: A MelodyRnnConfig object.
eval_ratio: Fraction of input to set aside for evaluation set.
Returns:
A pipeline.Pipeline instance.
"""
partitioner = pipelines_common.RandomPartition(
music_pb2.NoteSequence,
['eval_melodies', 'training_melodies'],
[eval_ratio])
dag = {partitioner: dag_pipeline.DagInput(music_pb2.NoteSequence)}
for mode in ['eval', 'training']:
time_change_splitter = note_sequence_pipelines.TimeChangeSplitter(
name='TimeChangeSplitter_' + mode)
quantizer = note_sequence_pipelines.Quantizer(
steps_per_quarter=config.steps_per_quarter, name='Quantizer_' + mode)
melody_extractor = melody_pipelines.MelodyExtractor(
min_bars=7, max_steps=512, min_unique_pitches=5,
gap_bars=1.0, ignore_polyphonic_notes=False,
name='MelodyExtractor_' + mode)
encoder_pipeline = EncoderPipeline(config, name='EncoderPipeline_' + mode)
dag[time_change_splitter] = partitioner[mode + '_melodies']
dag[quantizer] = time_change_splitter
dag[melody_extractor] = quantizer
dag[encoder_pipeline] = melody_extractor
dag[dag_pipeline.DagOutput(mode + '_melodies')] = encoder_pipeline
return dag_pipeline.DAGPipeline(dag)
def main(unused_argv):
tf.logging.set_verbosity(FLAGS.log)
config = melody_rnn_config_flags.config_from_flags()
pipeline_instance = get_pipeline(
config, FLAGS.eval_ratio)
FLAGS.input = os.path.expanduser(FLAGS.input)
FLAGS.output_dir = os.path.expanduser(FLAGS.output_dir)
pipeline.run_pipeline_serial(
pipeline_instance,
pipeline.tf_record_iterator(FLAGS.input, pipeline_instance.input_type),
FLAGS.output_dir)
def console_entry_point():
tf.app.run(main)
if __name__ == '__main__':
console_entry_point()