-
Notifications
You must be signed in to change notification settings - Fork 8
/
spectrogram_replicate.py
150 lines (116 loc) · 7.07 KB
/
spectrogram_replicate.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
import tensorflow as tf
import numpy as np
import datasets
from model import NetworkMelody, AdjustVoicingHook
from collections import namedtuple
import sys
import common
import librosa
def create_model(self, args):
layer = self.spectrogram
print(layer.shape)
context_size = int(args.context_width/self.spectrogram_hop_size)
with tf.name_scope('model_pitch'):
layer = tf.layers.conv2d(layer, 8*args.capacity_multiplier, (5, 5), (1, 1), "same", activation=None, use_bias=False)
layer = tf.layers.batch_normalization(layer, training=self.is_training)
layer = tf.nn.relu(layer)
layer = tf.layers.dropout(layer, 0.25, training=self.is_training)
residual = layer
layer = tf.layers.conv2d(layer, 8*args.capacity_multiplier, (5, 5), (1, 1), "same", activation=None, use_bias=False)
layer = tf.layers.batch_normalization(layer, training=self.is_training)
layer = tf.nn.relu(layer)
layer = tf.layers.dropout(layer, 0.25, training=self.is_training)
residual += layer
layer = tf.layers.conv2d(layer, 8*args.capacity_multiplier, (9, 3), (1, 1), "same", activation=None, use_bias=False)
layer = tf.layers.batch_normalization(layer, training=self.is_training)
layer = tf.nn.relu(layer)
layer = tf.layers.dropout(layer, 0.25, training=self.is_training)
residual += layer
layer = tf.layers.conv2d(layer, 8*args.capacity_multiplier, (9, 3), (1, 1), "same", activation=None, use_bias=False)
layer = tf.layers.batch_normalization(layer, training=self.is_training)
layer = tf.nn.relu(layer)
layer = tf.layers.dropout(layer, 0.25, training=self.is_training)
residual += layer
layer = tf.layers.conv2d(layer, 8*args.capacity_multiplier, (5, 70), (1, 1), "same", activation=None, use_bias=False)
layer = tf.layers.batch_normalization(layer, training=self.is_training)
layer = tf.nn.relu(layer)
layer = tf.layers.dropout(layer, 0.25, training=self.is_training)
residual += layer
layer = residual
layer = tf.layers.batch_normalization(layer, training=self.is_training)
layer = tf.layers.conv2d(layer, 1, (10, 1), (1, 1), "same", activation=None, use_bias=False)
layer_cut = layer[:, context_size:-context_size, :, :]
# layer = tf.layers.conv2d(layer, 1, (10, 1), (1, 1), "same", activation=None, use_bias=True)
note_output = tf.squeeze(layer_cut, -1)
print(note_output.shape)
self.note_logits = note_output
if args.voicing:
with tf.name_scope('model_voicing'):
voicing_input = tf.concat([tf.stop_gradient(layer), self.spectrogram], axis=-1)
# voicing_input = spectrogram
print(voicing_input.shape)
voicing_layer = tf.layers.conv2d(voicing_input, 64, (5, 5), (1, 1), "same", activation=tf.nn.relu, use_bias=False)
voicing_layer = tf.layers.dropout(voicing_layer, 0.25, training=self.is_training)
voicing_layer = tf.layers.batch_normalization(voicing_layer, training=self.is_training)
voicing_layer = tf.layers.conv2d(voicing_layer, 64, (5, 70), (1, 5), "same", activation=tf.nn.relu, use_bias=False)
voicing_layer = tf.layers.dropout(voicing_layer, 0.25, training=self.is_training)
voicing_layer = tf.layers.batch_normalization(voicing_layer, training=self.is_training)
voicing_layer = tf.layers.conv2d(voicing_layer, 64, (5, 12), (1, 12), "same", activation=tf.nn.relu, use_bias=False)
voicing_layer = tf.layers.dropout(voicing_layer, 0.25, training=self.is_training)
voicing_layer = tf.layers.batch_normalization(voicing_layer, training=self.is_training)
voicing_layer = tf.layers.conv2d(voicing_layer, 64, (15, 3), (1, 1), "same", activation=tf.nn.relu, use_bias=False)
voicing_layer = tf.layers.dropout(voicing_layer, 0.25, training=self.is_training)
voicing_layer = tf.layers.batch_normalization(voicing_layer, training=self.is_training)
print(voicing_layer.shape)
voicing_layer = tf.layers.conv2d(voicing_layer, 1, (1, 6), (1, 1), "valid", activation=None, use_bias=True)
cut_layer = voicing_layer[:, context_size:-context_size, :, :]
print(cut_layer.shape)
self.voicing_logits = tf.squeeze(cut_layer)
else:
self.voicing_threshold = tf.Variable(0.15, trainable=False)
tf.summary.scalar("model/voicing_threshold", self.voicing_threshold)
self.loss = common.loss(self, args)
self.est_notes = common.est_notes(self, args)
self.training = common.optimizer(self, args)
def parse_args(argv):
parser = common.common_arguments_parser()
parser.add_argument("--spectrogram", type=str, help="Postprocessing layer")
parser.add_argument("--capacity_multiplier", default=8, type=int, help="Capacity")
parser.add_argument("--voicing", action='store_true', help="Add voicing model.")
args = parser.parse_args(argv)
hop_length = 512
defaults = {
"samplerate": 44100, "context_width": 14*hop_length, "annotations_per_window": 20, "hop_size": 1, "frame_width": hop_length,
"note_range": 72, "min_note": 24,
"batch_size": 8,
"evaluate_every": 5000,
"evaluate_small_every": 1000,
"annotation_smoothing": 0.177,
"spectrogram": "hcqt",
"capacity_multiplier": 8,
"voicing": False
}
specified_args = common.argument_defaults(args, defaults)
common.name(args, specified_args, "bittner")
# common.name(args, "cqt_voicing_residual_batchnorm")
return args
def construct(args):
network = NetworkMelody(args)
with network.session.graph.as_default():
spectrogram_function, spectrogram_thumb, spectrogram_info = common.spectrograms(args)
def preload_fn(aa):
aa.annotation = datasets.Annotation.from_time_series(*aa.annotation, args.frame_width*args.samplerate/44100)
aa.audio.load_resampled_audio(args.samplerate).load_spectrogram(spectrogram_function, spectrogram_thumb, spectrogram_info[2])
def dataset_transform(tf_dataset, dataset):
return tf_dataset.map(dataset.prepare_example, num_parallel_calls=args.threads).batch(args.batch_size_evaluation).prefetch(10)
def dataset_transform_train(tf_dataset, dataset):
return tf_dataset.shuffle(10**5).map(dataset.prepare_example, num_parallel_calls=args.threads).batch(args.batch_size).prefetch(10)
train_dataset, test_datasets, validation_datasets = common.prepare_datasets(args.datasets, args, preload_fn, dataset_transform, dataset_transform_train)
if not args.voicing:
for vd in validation_datasets:
if not vd.name.startswith("small_"):
vd.hooks.append(AdjustVoicingHook())
network.construct(args, create_model, train_dataset.dataset.output_types, train_dataset.dataset.output_shapes, spectrogram_info=spectrogram_info)
return network, train_dataset, validation_datasets, test_datasets
if __name__ == "__main__":
common.main(sys.argv[1:], construct, parse_args)