This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
class threadsafe_iter: | |
"""Takes an iterator/generator and makes it thread-safe by | |
serializing call to the `next` method of given iterator/generator. | |
""" | |
def __init__(self, it): | |
self.it = it | |
self.lock = threading.Lock() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gen(): | |
print('generator initiated') | |
idx = 0 | |
while True: | |
yield x_train[:32], y_train[:32] | |
print('generator yielded a batch %d' % idx) | |
idx += 1 | |
tr_gen = gen() | |
model.fit_generator(generator=tr_gen, steps_per_epoch=20, max_queue_size=10, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def gen(): | |
print('generator initiated') | |
idx = 0 | |
while True: | |
yield x_train[:32], y_train[:32] | |
print('generator yielded a batch %d' % idx) | |
idx += 1 | |
time.sleep(3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gen(): | |
print('generator initiated') | |
idx = 0 | |
while True: | |
yield x_train[:32], y_train[:32] | |
print('generator yielded a batch %d' % idx) | |
idx += 1 | |
tr_gen = gen() | |
model.fit_generator(generator=tr_gen, steps_per_epoch=20, max_queue_size=10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Keunwoo Choi | |
# This example crawl snoring sound by searching keyword 'snore'. | |
from __future__ import print_function | |
import freesound # $ git clone https://github.com/MTG/freesound-python | |
import os | |
import sys | |
api_key = 'YOUR_API_KEY' | |
folder = 'data_freesound/' # folder to save |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cnn_model(n_ch): | |
'''Assuming cifar-10 ''' | |
input_shape = (3, 32, 32) | |
model = Sequential() | |
for i in range(N_LAYER): | |
if i == 0: | |
model.add(Convolution2D(n_ch, 3, 3, border_mode='same', | |
input_shape=input_shape, | |
name='conv%d' % i)) | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential | |
from kapre.time_frequency import Melspectrogram | |
from kapre.utils import Normalization2D | |
from kapre.augmentation import AdditiveNoise | |
# 6 channels (!), maybe 1-sec audio signal | |
input_shape = (6, 44100) | |
sr = 44100 | |
model = Sequential() | |
# A mel-spectrogram layer with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParametricMel(Layer): | |
def __init__(self, n_mels, n_freqs, sr, scale=24., init='mel', **kwargs): | |
self.supports_masking = True | |
self.scale = scale # scaling | |
self.n_mels = n_mels | |
if init == 'mel': | |
self.means_init = np.array(_mel_frequencies(n_mels, fmin=0.0, fmax=sr/2), dtype='float32') | |
stds = self.means_init[1:] - self.means_init[:-1] | |
self.stds_init = 0.3 * np.hstack((stds[0:1], stds[:])) # 0.3: kinda make sense by the resulting images.. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cropping2D(Layer): | |
def __init__(self, cropping=((0, 0), (0, 0)), dim_ordering='default', **kwargs): | |
super(Cropping2D, self).__init__(**kwargs) | |
if dim_ordering == 'default': | |
dim_ordering = K.image_dim_ordering() | |
self.cropping = tuple(cropping) | |
assert len(self.cropping) == 2, 'cropping must be a tuple length of 2' | |
assert len(self.cropping[0]) == 2, 'cropping[0] must be a tuple length of 2' | |
assert len(self.cropping[1]) == 2, 'cropping[1] must be a tuple length of 2' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def Melspectrogram(n_dft, input_shape, trainable, n_hop=None, | |
border_mode='same', logamplitude=True, sr=22050, | |
n_mels=128, fmin=0.0, fmax=None, name='melgram'): | |
if input_shape is None: | |
raise RuntimeError('specify input shape') | |
Melgram = Sequential() | |
# Prepare STFT. | |
x, STFT_magnitude = get_spectrogram_tensors(n_dft, |