-
Notifications
You must be signed in to change notification settings - Fork 150
/
kapre_helpers.py
executable file
·180 lines (159 loc) · 7.23 KB
/
kapre_helpers.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import keras
import kapre
from datetime import datetime
now = datetime.now()
import librosa
from librosa import display
import numpy as np
from keras.models import Sequential
from kapre.time_frequency import Spectrogram
from kapre.time_frequency import Melspectrogram
from keras.models import *
from keras.layers import Input, merge, Conv2D, MaxPooling2D, UpSampling2D, Dropout, Cropping2D, Concatenate
from keras.optimizers import *
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras import backend as keras
def plot_spect(y, name = 'testspect.png', show = False):
s = 1
plt.figure(figsize=(12, 8))
plt.plot(4*s, 2*s, 2*s)
try:
display.specshow(y[0, :, :, 0], y_axis='log')
except Exception as e:
display.specshow(y[0, :, :], y_axis='log')
else:
pass
finally:
pass
plt.colorbar(format='%+2.0f dB')
plt.title('Log-frequency power spectrogram')
plt.savefig(name)
if show == True:
plt.show()
def print_info():
print('%s/%s/%s' % (now.year, now.month, now.day))
print('librosa version: {}'.format(librosa.__version__))
print('Keras version: {}'.format(keras.__version__))
if keras.backend._BACKEND == 'tensorflow':
import tensorflow
print('Keras backend: {}: {}'.format(keras.backend._backend, tensorflow.__version__))
elif keras.backend._BACKEND == 'theano':
import theano
print('Keras backend: {}: {}'.format(keras.backend._backend, theano.__version__))
print('Keras image data format: {}'.format(keras.backend.image_data_format()))
print('Kapre version: {}'.format(kapre.__version__))
print('\nSampling rate: {} Hz'.format(SR))
def check_model(model):
model.summary(line_length=80, positions=[.33, .65, .8, 1.])
batch_input_shape = (2,) + model.input_shape[1:]
batch_output_shape = (2,) + model.output_shape[1:]
model.compile('sgd', 'mse')
model.fit(np.random.uniform(size=batch_input_shape), np.random.uniform(size=batch_output_shape), epochs=1)
def visualise_model(model, logam=False, sr=16000):
n_ch, nsp_src = model.input_shape[1:]
src, _ = librosa.load('/Users/admin/Dropbox/workspace/unet/data/audio/abjones_1_01.wav', sr, mono=True)
src = src[:nsp_src]
src_batch = src[np.newaxis, np.newaxis, :]
pred = model.predict(x=src_batch)
if keras.backend.image_data_format == 'channels_first':
result = pred[0, 0]
else:
result = pred[0, :, :, 0]
if logam:
result = librosa.amplitude_to_db(result)
display.specshow(result,
y_axis='linear', sr=sr)
def test_plot():
SR = 16000
src = np.random.random((1, SR * 3))
src_cute, _ = librosa.load('/Users/admin/Dropbox/workspace/unet/data/audio/abjones_1_01.wav', sr=SR, mono=True)
model = Sequential()
model.add(Melspectrogram(sr=SR, n_mels=128,
n_dft=512, n_hop=256, input_shape=src.shape,
return_decibel_melgram=True,
trainable_kernel=True, name='melgram'))
check_model(model)
visualise_model(model)
SR = 16000
src = np.random.random((1, SR * 3))
model = Sequential()
model.add(Spectrogram(n_dft=512, n_hop=256, input_shape=src.shape,
return_decibel_spectrogram=False, power_spectrogram=2.0,
trainable_kernel=False, name='static_stft'))
check_model(model)
plt.figure(figsize=(14, 4))
plt.subplot(1, 2, 1)
plt.title('log-Spectrogram by Kapre')
visualise_model(model, logam=True)
plt.subplot(1, 2, 2)
display.specshow(librosa.amplitude_to_db(np.abs(librosa.stft(src_cute[: SR * 3], 512, 256)) ** 2, ref=1.0),
y_axis='linear', sr=SR)
plt.title('log-Spectrogram by Librosa')
plt.show()
def unet(inputs):
conv1 = Conv2D(64, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(inputs)
conv1 = Conv2D(64, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(128, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(pool1)
conv2 = Conv2D(128, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(256, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(pool2)
conv3 = Conv2D(256, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(512, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(pool3)
conv4 = Conv2D(512, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv4)
drop4 = Dropout(0.5)(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
conv5 = Conv2D(1024, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(pool4)
conv5 = Conv2D(1024, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv5)
drop5 = Dropout(0.5)(conv5)
up6 = Conv2D(512, 2, activation='relu', padding='same',
kernel_initializer='he_normal'
)(UpSampling2D(size=(2, 2))(drop5))
merge6 = Concatenate(axis=3)([drop4, up6]) # usr add
conv6 = Conv2D(512, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(merge6)
conv6 = Conv2D(512, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv6)
up7 = Conv2D(256, 2, activation='relu', padding='same',
kernel_initializer='he_normal'
)(UpSampling2D(size=(2, 2))(conv6))
merge7 = Concatenate(axis=3)([conv3, up7]) # usr add
conv7 = Conv2D(256, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(merge7)
conv7 = Conv2D(256, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv7)
up8 = Conv2D(128, 2, activation='relu', padding='same',
kernel_initializer='he_normal'
)(UpSampling2D(size=(2, 2))(conv7))
merge8 = Concatenate(axis=3)([conv2, up8]) # usr add
conv8 = Conv2D(128, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(merge8)
conv8 = Conv2D(128, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv8)
up9 = Conv2D(64, 2, activation='relu', padding='same',
kernel_initializer='he_normal'
)(UpSampling2D(size=(2, 2))(conv8))
merge9 = Concatenate(axis=3)([conv1, up9]) # usr add
conv9 = Conv2D(64, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(merge9)
conv9 = Conv2D(64, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv9)
conv9 = Conv2D(2, 3, activation='relu', padding='same',
kernel_initializer='he_normal')(conv9)
conv10 = Conv2D(1, 1, activation='sigmoid')(conv9)
return conv10