<jupyter_start><jupyter_text>Ouverture des différents fichiers csv<jupyter_code>df_fan_train = pd.read_csv('fichier_fan_train.csv', index_col=0)
display(df_fan_train.head())
print(df_fan_train.shape)
df_pump_train = pd.read_csv('fichier_pump_train.csv', index_col=0)
display(df_pump_train.head())
print(df_pump_train.shape)
print(df_pump_train['label'].value_counts())
df_pump_train['label'] = df_pump_train['label'].replace( {0 : 1})  
display(df_pump_train.head())
print(df_pump_train['label'].value_counts())
df_valve_train = pd.read_csv('fichier_valve_train.csv', index_col=0)
display(df_valve_train.head())
print(df_valve_train.shape)
df_slider_train = pd.read_csv('fichier_slider_train.csv', index_col=0)
display(df_slider_train.head())
print(df_slider_train.shape)
print(df_slider_train['machine_class'].value_counts())
df_slider_train['label'] = df_slider_train['machine_class'].replace( {'normal': 0, 'anomaly': 1})  
display(df_slider_train.head())
print(df_slider_train['label'].value_counts())
# Création du fichier d'entrainnement contenants les données sons des machines fan, pump, slider et valve
df = pd.concat((df_fan_train, df_slider_train, df_pump_train, df_valve_train), axis=0)
df = df.to_csv('fichier_entrainnement_slider_normal.csv')
df = pd.read_csv('fichier_entrainnement_slider_normal.csv', index_col=0)
df.head()
df.label.value_counts()
df.label.value_counts(normalize=True)
# Ecouter le son émis par un fichier audio

from IPython.display import Audio
audio = tfio.audio.AudioIOTensor(df.iloc[0,0], dtype='int16')
 # convert to tensor
audio_tensor = audio.to_tensor()
# remove last dimension
audio_tensor = tf.squeeze(audio_tensor, axis=[-1])
Audio(audio_tensor.numpy(), rate=audio.rate.numpy())


def spectro_gram(filepath):
    # lecture du fichier audio
    audio = tfio.audio.AudioIOTensor(filepath, dtype='int16')
    
    #audio rate
    #audioSR = int(audio.rate.numpy())
    
    # convert to tensor
    audio_tensor = audio.to_tensor()
    
    # remove last dimension
    audio_tensor = tf.squeeze(audio_tensor, axis=[-1])
    
    # convert audio tensor in float32
    audio_tensor = tf.cast(audio_tensor, tf.float32) / 32768.0
    
    #frame_step = int(audioSR * 0.008)
    
    # Convert to spectrogram
    spectrogram = tfio.audio.spectrogram(audio_tensor, nfft=4096, window=4096, stride=512)

    # Convert to mel-spectrogram
    mel_spectrogram = tfio.audio.melscale(spectrogram, rate=16000, mels=128, fmin=0, fmax=8000)

    # Convert to db scale mel-spectrogram
    dbscale_mel_spectrogram = tfio.audio.dbscale(mel_spectrogram, top_db=80)
        
    # Freq masking
    #dbscale_mel_spectrogram = tfio.audio.freq_mask(dbscale_mel_spectrogram, param=10)
    
    # Time masking
    #dbscale_mel_spectrogram = tfio.audio.time_mask(dbscale_mel_spectrogram, param=10)
    
    # redimensionnement
    dbscale_mel_spectrogram = tf.reshape(dbscale_mel_spectrogram, (313, 128, 1))
    
    return dbscale_mel_spectrogram / 255
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import numpy as np

X_train_path, X_eval_path, y_train, y_eval = train_test_split(df.audio_file, df.label, train_size=0.7, random_state=1234)
y_train.value_counts()
y_train.value_counts(normalize=True)
y_eval.value_counts()
y_eval.value_counts(normalize=True)
X_eval = []
for filepath in tqdm(X_eval_path):
    # Lecture du fichier
    spec = spectro_gram(filepath)

    X_eval.append([spec])
    
X_eval = tf.concat(X_eval, axis=0)

from keras.utils import np_utils
y_train = np_utils.to_categorical(y_train, dtype='int')
y_eval = np_utils.to_categorical(y_eval, dtype='int')
y_train.shape
y_train
# Définition d'un dataset 'dataset_train' de (X_train_path, y_train) à l'aide de la fonction from_tensor_slices
dataset_train = tf.data.Dataset.from_tensor_slices((X_train_path, y_train))
# Appliquons la fonction spectro_gram à chaque valeur de X_train_path à l'aide de la méthode map. 
# Pour que le chargement s'effectue en multi-tasking, précisons l'argument num_parallel_calls = -1
# Regroupeons les observations sous forme de batchs de taille 64
dataset_train = dataset_train.map(lambda x, y : [spectro_gram(x), y], num_parallel_calls = -1).shuffle(100).batch(64)

from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils

import matplotlib.pyplot as plt
from matplotlib import cm

from sklearn import metrics
from keras import backend as K 
# Clear the old session.
K.clear_session()

# Architecture du modèle 
lenet = Sequential()

conv_1 = Conv2D(filters = 64,                     # Nombre de filtres
                kernel_size = (5, 5),            # Dimensions du noyau
                padding = 'valid',               # Mode de Dépassement
                input_shape = (313, 128, 1),       # Dimensions de l'image en entrée
                activation = 'relu')             # Fonction d'activation

max_pool_1 = MaxPooling2D(pool_size = (2, 2))

conv_2 = Conv2D(filters = 32,                    
                kernel_size = (3, 3),          
                padding = 'valid',             
                activation = 'relu')

max_pool_2 = MaxPooling2D(pool_size = (2, 2))

flatten = Flatten()

dropout = Dropout(rate = 0.2)

dense_1 = Dense(units = 128,
                activation = 'relu')

dense_2 = Dense(units = 2,
                activation = 'sigmoid')

lenet.add(conv_1)
lenet.add(max_pool_1)
lenet.add(conv_2)
lenet.add(max_pool_2)

lenet.add(dropout)
lenet.add(flatten)
lenet.add(dense_1)
lenet.add(dense_2)

# Compilation
lenet.compile(loss='binary_crossentropy',  # fonction de perte
              optimizer= 'adam',                 # algorithme de descente de gradient
              metrics=['accuracy'])             # métrique d'évaluation

lenet.summary()
from tensorflow.keras import callbacks
early_stopping = callbacks.EarlyStopping(
    monitor='val_loss',
   
    patience=3,
    
    mode='min',
  
    restore_best_weights=True,
)
history = lenet.fit(dataset_train, epochs=10, validation_data=(X_eval, y_eval))
#plot the training history

plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.legend()
plt.xlabel('Epochs')
plt.ylabel('binary_crossentropy')
plt.savefig('model_training_history')
plt.show()
df_slider_test = pd.read_csv('fichier_slider_test.csv')
df_slider_test.head()
X_test_path = df_slider_test['audio_file']
y_test = df_slider_test['label']
y_test.value_counts()
X_test = []
for filepath in tqdm(X_test_path):
    # Lecture du fichier
    spec = spectro_gram(filepath)

    X_test.append([spec])
    
X_test = tf.concat(X_test, axis=0)
y_pred = (lenet.predict(X_test).argmax(axis=1))
pd.crosstab(y_test, y_pred, rownames=['valeur réélle'], colnames=['valeur prédite'])
from sklearn.metrics import classification_report

print(classification_report(y_test, y_pred))
y_test = np_utils.to_categorical(y_test, dtype='int')
y_score = lenet.predict(X_test)
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(2):
    fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
    roc_auc[i] = auc(fpr[i], tpr[i])
plt.figure()
lw = 2
plt.plot(fpr[1], tpr[1], color='darkorange',
         lw=lw, label='ROC curve (auc = %0.2f)' % roc_auc[1])
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--', label = 'Modèle aléatoire (auc = 0.5)')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Taux vrais positives')
plt.ylabel('Taux faux positives')
plt.title('Courbe ROC pour la classe 1 => Sons anormaux')
plt.legend(loc="lower right")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11941821893340138, 'fraction_numerical': 0.02028578719060985, 'mean_word_length': 4.493342676944639, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '175758', 'license': 'MIT', 'n_tokens_mistral': 2996, 'n_tokens_neox': 2864, 'n_words': 574, 'path': 'Classification/model_classification _slider _V1.ipynb', 'repo_name': 'fbontron/Son_atyPyque', 'size': '7838'}