/
pipelines_common.py
105 lines (88 loc) · 3.93 KB
/
pipelines_common.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
# 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.
"""Common data processing pipelines."""
import random
# internal imports
import numpy as np
import tensorflow as tf
from magenta.music import sequences_lib
from magenta.pipelines import pipeline
from magenta.pipelines import statistics
from magenta.protobuf import music_pb2
class TimeChangeSplitter(pipeline.Pipeline):
"""A Pipeline that splits NoteSequences on time signature & tempo changes."""
def __init__(self, name=None):
super(TimeChangeSplitter, self).__init__(
input_type=music_pb2.NoteSequence,
output_type=music_pb2.NoteSequence,
name=name)
def transform(self, note_sequence):
return sequences_lib.split_note_sequence_on_time_changes(note_sequence)
class Quantizer(pipeline.Pipeline):
"""A Module that quantizes NoteSequence data."""
def __init__(self, steps_per_quarter=4, name=None):
super(Quantizer, self).__init__(
input_type=music_pb2.NoteSequence,
output_type=music_pb2.NoteSequence,
name=name)
self._steps_per_quarter = steps_per_quarter
def transform(self, note_sequence):
try:
quantized_sequence = sequences_lib.quantize_note_sequence(
note_sequence, self._steps_per_quarter)
return [quantized_sequence]
except sequences_lib.MultipleTimeSignatureException as e:
tf.logging.warning('Multiple time signatures in NoteSequence %s: %s',
note_sequence.filename, e)
self._set_stats([statistics.Counter(
'sequences_discarded_because_multiple_time_signatures', 1)])
return []
except sequences_lib.MultipleTempoException as e:
tf.logging.warning('Multiple tempos found in NoteSequence %s: %s',
note_sequence.filename, e)
self._set_stats([statistics.Counter(
'sequences_discarded_because_multiple_tempos', 1)])
return []
class RandomPartition(pipeline.Pipeline):
"""Outputs multiple datasets.
This Pipeline will take a single input feed and randomly partition the inputs
into multiple output datasets. The probabilities of an input landing in each
dataset are given by `partition_probabilities`. Use this Pipeline to partition
previous Pipeline outputs into training and test sets, or training, eval, and
test sets.
"""
def __init__(self, type_, partition_names, partition_probabilities):
super(RandomPartition, self).__init__(
type_, dict([(name, type_) for name in partition_names]))
if len(partition_probabilities) != len(partition_names) - 1:
raise ValueError('len(partition_probabilities) != '
'len(partition_names) - 1. '
'Last probability is implicity.')
self.partition_names = partition_names
self.cumulative_density = np.cumsum(partition_probabilities).tolist()
self.rand_func = random.random
def transform(self, input_object):
r = self.rand_func()
if r >= self.cumulative_density[-1]:
bucket = len(self.cumulative_density)
else:
for i, cpd in enumerate(self.cumulative_density):
if r < cpd:
bucket = i
break
self._set_stats(self._make_stats(self.partition_names[bucket]))
return dict([(name, [] if i != bucket else [input_object])
for i, name in enumerate(self.partition_names)])
def _make_stats(self, increment_partition=None):
return [statistics.Counter(increment_partition + '_count', 1)]