Spaces:
Sleeping
Sleeping
File size: 8,914 Bytes
9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 9c6b905 279af50 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
import numpy as np
import logging
from keras.models import Sequential, Model
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D, Dense, Dropout, BatchNormalization, GRU, Input, add
from keras.optimizers import Adamax
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
import optuna
import mlflow
import mlflow.keras
from imblearn.over_sampling import RandomOverSampler
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from imblearn.over_sampling import SMOTE
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.layers import Conv1D, Conv2D, SeparableConv1D, MaxPooling1D, MaxPooling2D
from tensorflow.keras.layers import Input, add, Flatten, Dense, BatchNormalization, Dropout, LSTM, GRU
from tensorflow.keras.layers import GlobalMaxPooling1D, GlobalMaxPooling2D, Activation, LeakyReLU, ReLU
from tensorflow.keras import regularizers
from tensorflow.keras import backend as K
from tensorflow.keras.optimizers import Adamax
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.models import Sequential
from keras.layers import (
Conv1D, Conv2D, MaxPooling1D, MaxPooling2D,
GlobalAveragePooling1D, GlobalAveragePooling2D,
Dense, Dropout, BatchNormalization
)
from sklearn.model_selection import train_test_split
import numpy as np
# Initialize logger
model_logger = logging.getLogger("model_utils")
# ==========================
# MODEL BUILDING UTILITIES
# ==========================
def build_model(input_shape, n_filters, dense_units, dropout_rate, num_classes, model_type='1D', classification_mode='binary'):
"""
Build and compile a CNN model for 1D or 2D data.
Returns CNN model.
"""
print(f"Building the updated {model_type} CNN model with {classification_mode} classification.")
model = Sequential()
# Add convolutional layers based on the model type
if model_type == '1D':
# 1D CNN layers
model.add(Conv1D(n_filters, kernel_size=3, activation='relu', input_shape=input_shape))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(dropout_rate))
model.add(Conv1D(n_filters * 2, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=2))
model.add(Dropout(dropout_rate))
model.add(Conv1D(n_filters * 4, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
model.add(GlobalAveragePooling1D())
model.add(Dropout(dropout_rate))
elif model_type == '2D':
# 2D CNN layers
model.add(Conv2D(n_filters, (3, 3), activation='relu', input_shape=input_shape))
model.add(BatchNormalization())
if input_shape[0] >= 2:
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(dropout_rate))
model.add(Conv2D(n_filters * 2, (3, 3), activation='relu'))
model.add(BatchNormalization())
if input_shape[0] >= 4:
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(dropout_rate))
model.add(Conv2D(n_filters * 4, (3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(GlobalAveragePooling2D())
model.add(Dropout(dropout_rate))
else:
raise ValueError("Invalid model_type. Must be '1D' or '2D'.")
# Add fully connected layers
model.add(Dense(dense_units, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(dropout_rate))
# Add output layer dynamically based on classification mode
if classification_mode == 'binary':
# Binary classification: Single unit with sigmoid activation
model.add(Dense(1, activation='sigmoid'))
loss_function = 'binary_crossentropy'
else:
# Multi-class classification: num_classes units with softmax activation
model.add(Dense(num_classes, activation='softmax'))
loss_function = 'categorical_crossentropy'
# Compile the model
model.compile(optimizer='adam', loss=loss_function, metrics=['accuracy'])
print(f"{model_type} CNN model built and compiled successfully for {classification_mode} classification.")
return model
def track_experiment_with_mlflow_and_optuna(
mode,
num_classes,
model_type,
classification_mode,
X_train,
y_train,
X_val,
y_val,
n_trials=20,
):
"""
Optimize hyperparameters using Optuna and track experiments with MLflow.
Parameters:
- mode: Feature extraction mode (e.g., 'augmented', 'mfcc', 'log_mel').
- num_classes: Number of classes for classification.
- model_type: Type of model ('1D' for Conv1D, '2D' for Conv2D).
- classification_mode: 'binary' for binary classification, 'multi' for multi-class classification.
- X_train, y_train: Training data and labels.
- X_val, y_val: Validation data and labels.
- n_trials: Number of Optuna trials.
"""
def objective(trial):
with mlflow.start_run(nested=True):
# Hyperparameters to tune
n_filters = trial.suggest_categorical('n_filters', [16, 32, 64])
dense_units = trial.suggest_int('dense_units', 64, 256, step=32)
dropout_rate = trial.suggest_float('dropout_rate', 0.1, 0.5, step=0.1)
learning_rate = trial.suggest_loguniform('learning_rate', 1e-4, 1e-2)
# Build and compile the model
model = build_model(
input_shape=X_train.shape[1:],
n_filters=n_filters,
dense_units=dense_units,
dropout_rate=dropout_rate,
num_classes=num_classes,
model_type=model_type,
classification_mode=classification_mode
)
# Define EarlyStopping callback
early_stopping = EarlyStopping(
monitor='val_loss',
patience=5,
restore_best_weights=True
)
# Train the model
history = model.fit(
X_train,
y_train,
validation_data=(X_val, y_val),
epochs=50,
batch_size=32,
callbacks=[early_stopping],
verbose=0,
)
# Log hyperparameters and metrics to MLflow
mlflow.log_params({
'n_filters': n_filters,
'dense_units': dense_units,
'dropout_rate': dropout_rate,
'learning_rate': learning_rate,
'model_type': model_type,
'classification_mode': classification_mode,
})
mlflow.log_metric("best_val_accuracy", max(history.history['val_accuracy']))
# Save loss curves
plt.figure()
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.legend()
plt.title("Training and Validation Loss")
loss_curve_path = f"loss_curve_{trial.number}_{model_type}.png"
plt.savefig(loss_curve_path)
mlflow.log_artifact(loss_curve_path)
return max(history.history['val_accuracy'])
# Start Optuna study
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=n_trials)
# Retrieve the best trial and log results
best_trial = study.best_trial
model_logger.info(f"Best Trial for {mode} ({model_type}): {best_trial.params}")
# Build and return the best model
best_model = build_model(
input_shape=X_train.shape[1:],
n_filters=best_trial.params['n_filters'],
dense_units=best_trial.params['dense_units'],
dropout_rate=best_trial.params['dropout_rate'],
num_classes=num_classes,
model_type=model_type,
classification_mode=classification_mode
)
# Train the best model
early_stopping = EarlyStopping(
monitor='val_loss',
patience=5,
restore_best_weights=True,
)
best_model.fit(
X_train, y_train,
validation_data=(X_val, y_val),
epochs=50,
batch_size=32,
callbacks=[early_stopping],
verbose=1,
)
# Save the best model
best_model_path = f"best_model_{mode}_{model_type}.h5"
best_model.save(best_model_path)
mlflow.log_artifact(best_model_path)
model_logger.info(f"Best model for {mode} ({model_type}) saved successfully.")
return best_model
|