Commit f6a46d22 authored by Lisa Casino's avatar Lisa Casino
Browse files

add fichier loading

parent b3bc45b1
%% Cell type:code id:tough-exposure tags:
``` python
import numpy as np
import os
import pandas as pd
from scipy.io import wavfile
import librosa
from tqdm import tqdm
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
from tensorflow.keras import regularizers, activations
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, MaxPooling2D, GlobalAveragePooling2D
from keras.utils import np_utils, to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from datetime import datetime
from matplotlib import pyplot as plt
```
%% Cell type:code id:polished-tongue tags:
``` python
us8k_df = pd.read_pickle("us8k_df.pkl")
```
%% Cell type:code id:elementary-congress tags:
``` python
def init_model():
model1 = Sequential()
#layer-1
model1.add(Conv2D(filters=24, kernel_size=5, input_shape=(128, 128, 1),
kernel_regularizer=regularizers.l2(1e-3)))
model1.add(MaxPooling2D(pool_size=(3,3), strides=3))
model1.add(Activation(activations.relu))
#layer-2
model1.add(Conv2D(filters=36, kernel_size=4, padding='valid', kernel_regularizer=regularizers.l2(1e-3)))
model1.add(MaxPooling2D(pool_size=(2,2), strides=2))
model1.add(Activation(activations.relu))
#layer-3
model1.add(Conv2D(filters=48, kernel_size=3, padding='valid'))
model1.add(Activation(activations.relu))
model1.add(GlobalAveragePooling2D())
#layer-4 (1st dense layer)
model1.add(Dense(60, activation='relu'))
model1.add(Dropout(0.5))
#layer-5 (2nd dense layer)
model1.add(Dense(10, activation='softmax'))
# compile
model1.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
return model1
```
%% Cell type:code id:afraid-louisville tags:
``` python
model = init_model()
model.summary()
```
%% Output
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 124, 124, 24) 624
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 41, 41, 24) 0
_________________________________________________________________
activation (Activation) (None, 41, 41, 24) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 38, 38, 36) 13860
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 19, 19, 36) 0
_________________________________________________________________
activation_1 (Activation) (None, 19, 19, 36) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 17, 17, 48) 15600
_________________________________________________________________
activation_2 (Activation) (None, 17, 17, 48) 0
_________________________________________________________________
global_average_pooling2d (Gl (None, 48) 0
_________________________________________________________________
dense (Dense) (None, 60) 2940
_________________________________________________________________
dropout (Dropout) (None, 60) 0
_________________________________________________________________
dense_1 (Dense) (None, 10) 610
=================================================================
Total params: 33,634
Trainable params: 33,634
Non-trainable params: 0
_________________________________________________________________
%% Cell type:code id:altered-vermont tags:
``` python
def train_test_split(fold_k, data, X_dim=(128, 128, 1)):
X_train = np.stack(data[data.fold != fold_k].melspectrogram.to_numpy())
X_test = np.stack(data[data.fold == fold_k].melspectrogram.to_numpy())
y_train = data[data.fold != fold_k].label.to_numpy()
y_test = data[data.fold == fold_k].label.to_numpy()
XX_train = X_train.reshape(X_train.shape[0], *X_dim)
XX_test = X_test.reshape(X_test.shape[0], *X_dim)
yy_train = to_categorical(y_train)
yy_test = to_categorical(y_test)
return XX_train, XX_test, yy_train, yy_test
```
%% Cell type:code id:phantom-strap tags:
``` python
def process_fold(fold_k, data, epochs=100, num_batch_size=32):
# split the data
X_train, X_test, y_train, y_test = train_test_split(fold_k, data)
# init data augmention
#train_datagen, val_datagen = init_data_aug()
# fit augmentation
#train_datagen.fit(X_train)
#val_datagen.fit(X_train)
# init model
model = init_model()
# pre-training accuracy
score = model.evaluate(X_test, y_test, batch_size=num_batch_size, verbose=0)
print("Pre-training accuracy: %.4f%%\n" % (100 * score[1]))
# train the model
start = datetime.now()
history = model.fit(X_train,y_train, epochs=epochs,validation_data=(X_test,y_test),batch_size=num_batch_size)
end = datetime.now()
print("Training completed in time: ", end - start, '\n')
return history
```
%% Cell type:code id:raised-puppy tags:
``` python
def show_results(tot_history):
"""Show accuracy and loss graphs for train and test sets."""
for i, history in enumerate(tot_history):
print('\n({})'.format(i+1))