qu penser vosu de ce code entrainemt de dat set python

Copier
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from emotional_deepfake_detector import preprocess_audio

def load_dataset(dataset_path):
    """
    Charge et prétraite les fichiers audio d'un dataset
    """
    X = []
    y = []
    try:
        for label in ['real', 'fake']:
            label_path = os.path.join(dataset_path, label)
            if not os.path.exists(label_path):
                print(f"Warning: Path {label_path} does not exist")
                continue
                
            for audio_file in os.listdir(label_path):
                if audio_file.endswith(('.wav', '.mp3')):  # Vérifier les extensions audio
                    audio_path = os.path.join(label_path, audio_file)
                    try:
                        features = preprocess_audio(audio_path)
                        if features is not None and len(features) > 0:
                            X.append(features)
                            y.append(1 if label == 'fake' else 0)
                    except Exception as e:
                        print(f"Error processing {audio_path}: {str(e)}")
                        
        if not X:
            raise ValueError(f"No valid audio files found in {dataset_path}")
            
        return np.array(X), np.array(y)
    except Exception as e:
        print(f"Error loading dataset from {dataset_path}: {str(e)}")
        return None, None

def create_model(input_shape):
    """
    Crée le modèle LSTM pour la détection de deepfake
    """
    model = Sequential([
        LSTM(128, input_shape=input_shape, return_sequences=True),
        Dropout(0.3),
        LSTM(64, return_sequences=True),
        Dropout(0.3),
        LSTM(32),
        Dropout(0.3),
        Dense(32, activation='relu'),
        Dropout(0.2),
        Dense(16, activation='relu'),
        Dense(1, activation='sigmoid')
    ])
    
    model.compile(
        optimizer=Adam(learning_rate=0.001),
        loss='binary_crossentropy',
        metrics=['accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall()]
    )
    return model

def train_model(batch_size=32, epochs=50):
    """
    Entraîne le modèle sur les deux datasets
    """
    # Chemins des datasets
    arabic_path = 'audio_service/datasets/Arabic_Audio_Deepfake'
    safeear_path = 'audio_service/datasets/SafeEarWeb'
    
    # Charger les datasets
    print("Chargement du dataset Arabic...")
    X_arabic, y_arabic = load_dataset(arabic_path)
    print("Chargement du dataset SafeEar...")
    X_safeear, y_safeear = load_dataset(safeear_path)
    
    # Vérifier si les datasets ont été chargés correctement
    if X_arabic is None or X_safeear is None:
        raise ValueError("Erreur lors du chargement des datasets")
    
    # Normalisation des features
    X_arabic = tf.keras.utils.normalize(X_arabic, axis=1)
    X_safeear = tf.keras.utils.normalize(X_safeear, axis=1)
    
    # Combiner les datasets
    X = np.concatenate((X_arabic, X_safeear), axis=0)
    y = np.concatenate((y_arabic, y_safeear), axis=0)
    
    # Mélanger les données
    indices = np.random.permutation(len(X))
    X = X[indices]
    y = y[indices]
    
    # Split train/test
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, 
        test_size=0.2, 
        random_state=42,
        stratify=y
    )
    
    # Créer le modèle
    model = create_model(input_shape=(X_train.shape[1], X_train.shape[2]))
    
    # Callbacks
    early_stopping = tf.keras.callbacks.EarlyStopping(
        monitor='val_loss',
        patience=5,
        restore_best_weights=True
    )
    
    checkpoint = tf.keras.callbacks.ModelCheckpoint(
        'audio_service/models/best_model.h5',
        monitor='val_accuracy',
        save_best_only=True
    )
    
    # Entraînement
    print("Début de l'entraînement...")
    history = model.fit(
        X_train, y_train,
        validation_data=(X_test, y_test),
        epochs=epochs,
        batch_size=batch_size,
        callbacks=[early_stopping, checkpoint],
        verbose=1
    )
    
    # Évaluation
    test_results = model.evaluate(X_test, y_test, verbose=1)
    print("\nRésultats de l'évaluation:")
    for metric_name, value in zip(model.metrics_names, test_results):
        print(f"{metric_name}: {value:.4f}")
    
    # Sauvegarder le modèle final
    model.save('audio_service/models/deepfake_detection_model_final.h5')
    
    return history, test_results

if __name__ == "__main__":
    try:
        history, results = train_model()
        print("\nEntraînement terminé avec succès!")
        print(f"Accuracy finale: {results[1]:.4f}")
    except Exception as e:
        print(f"Erreur lors de l'entraînement: {str(e)}")