-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_melody_dataset.py
executable file
·95 lines (70 loc) · 3.13 KB
/
create_melody_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
from magenta.pipelines import note_sequence_pipelines
from magenta.pipelines import dag_pipeline
from magenta.pipelines import pipeline
from magenta.protobuf import music_pb2
from magenta.pipelines import statistics
from magenta.pipelines import pipelines_common
from melody_pipeline import MelodyExtractorInfo
from encoder_decoder import MelodyEncoderDecoder
from melody_lib import MelodySequence, STEPS_PER_SECOND
from encoder_decoder import LOWEST_MIDI_PITCH, HIGHEST_MIDI_PITCH
import tensorflow as tf
INPUT_DIR = '/home/hoppe/Code/tempo/dataset/out.tfrecord'
OUTPUT_DIR = 'training_data'
# Stretch by -10%, 0%, 10%, and 15%.
stretch_factors = [0.9, 1.0, 1.1, 1.15]
transposition_range = list(range(-5, 6)) + [-12, 12]
class EncoderPipeline(pipeline.Pipeline):
"""A Module that converts monophonic melodies to a model specific encoding."""
def __init__(self, name):
"""Constructs an EncoderPipeline.
name: A unique pipeline name.
"""
super(EncoderPipeline, self).__init__(
input_type=MelodySequence,
output_type=tf.train.SequenceExample,
name=name)
self._melody_encoder_decoder = MelodyEncoderDecoder()
def transform(self, melody):
encoded = self._melody_encoder_decoder.encode(melody)
return [encoded]
def get_pipeline(eval_ratio = 0.02):
"""Returns the Pipeline instance which creates the RNN dataset.
Args:
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']:
stretch_pipeline = note_sequence_pipelines.StretchPipeline(
stretch_factors, name='StretchPipeline_' + mode)
quantizer = note_sequence_pipelines.Quantizer(
steps_per_second=STEPS_PER_SECOND, name='Quantizer_' + mode)
transposition_pipeline = note_sequence_pipelines.TranspositionPipeline(
transposition_range, min_pitch=LOWEST_MIDI_PITCH,
max_pitch=HIGHEST_MIDI_PITCH, name= 'TranspositionPipeline_' + mode)
melody_extractor = MelodyExtractorInfo(name='MelodyExtractor_' + mode)
encoder_pipeline = EncoderPipeline(name='EncoderPipeline' + mode)
dag[stretch_pipeline] = partitioner[mode + '_melodies']
dag[quantizer] = stretch_pipeline
dag[transposition_pipeline] = quantizer
dag[melody_extractor] = transposition_pipeline
dag[encoder_pipeline] = melody_extractor
dag[dag_pipeline.DagOutput(mode + '_melodies')] = encoder_pipeline
return dag_pipeline.DAGPipeline(dag)
def main():
# tf.logging.set_verbosity(2)
pipeline_instance = get_pipeline()
input_dir = INPUT_DIR
output_dir = OUTPUT_DIR
pipeline.run_pipeline_serial(
pipeline_instance,
pipeline.tf_record_iterator(input_dir, pipeline_instance.input_type),
output_dir)
if __name__ == '__main__':
main()