Skip to content

Instantly share code, notes, and snippets.

View keunwoochoi's full-sized avatar

Keunwoo Choi keunwoochoi

View GitHub Profile
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()
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,
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)
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)
@keunwoochoi
keunwoochoi / freesound_crawler.py
Last active July 11, 2020 06:38
how to crawl freesound
# 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
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:
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
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..
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'
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,