This file contains 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 design_residual_model(setting_dict): | |
'''''' | |
def design_for_mel(): | |
'''''' | |
model = keras.layers.containers.Sequential() # it's a CONTAINER, not MODEL. | |
# set numbers | |
image_patch_sizes = [[3,3]]*num_layers | |
vgg_modi_weight, pool_sizes = get_residual_weights(num_layers=num_layers) | |
setting_dict['vgg_modi_weight'] = vgg_modi_weight | |
height_input = height |
This file contains 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
# https://github.com/benanne/kaggle-ndsb/blob/master/buffering.py | |
import multiprocessing as mp | |
import Queue | |
import threading | |
def buffered_gen_mp(source_gen, buffer_size=2): | |
""" | |
Generator that runs a slow source generator in a separate process. | |
buffer_size: the maximal number of items to pre-generate (length of the buffer) | |
""" |
This file contains 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
#!/usr/bin/env python | |
import numpy as np | |
import pescador | |
def data_sampler(filename, n_samples): | |
# Load all the data from the file, somehow | |
data = load_data(filename) |
This file contains 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 keras | |
from keras.models import Sequential | |
from keras.layers.core import Dense, Activation, Dropout, TimeDistributedDense | |
from keras.layers.recurrent import LSTM, SimpleRNN | |
def build_model(num_layers=2, num_units=256, maxlen_rnn=50, dim_label=50): | |
''' | |
num_layers: in [2, 3] | |
num_units: in [256, 512, 1024] |
This file contains 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 output_of_lambda(input_shape): | |
return (input_shape[0], 1, input_shape[2]) | |
def mean(x): | |
return K.mean(x, axis=1, keepdims=True) | |
model.add(Lambda(mean, output_shape=output_of_lambda)) |
This file contains 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, |
This file contains 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 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 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 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: |
OlderNewer