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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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
"""
Training pipeline for emotion recognition models.
"""
import os
import json
from pathlib import Path
from datetime import datetime
from typing import Dict, Optional, Tuple, Callable

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import (
    EarlyStopping, ModelCheckpoint, ReduceLROnPlateau,
    TensorBoard, Callback
)

import sys
sys.path.append(str(Path(__file__).parent.parent.parent))
from src.config import (
    EPOCHS, LEARNING_RATE, LEARNING_RATE_FINE_TUNE,
    EARLY_STOPPING_PATIENCE, REDUCE_LR_PATIENCE, REDUCE_LR_FACTOR,
    MODELS_DIR, CUSTOM_CNN_PATH, MOBILENET_PATH, VGG_PATH
)


class TrainingProgressCallback(Callback):
    """Custom callback to track and display training progress."""
    
    def __init__(self, total_epochs: int):
        super().__init__()
        self.total_epochs = total_epochs
    
    def on_epoch_end(self, epoch, logs=None):
        logs = logs or {}
        print(f"\nEpoch {epoch + 1}/{self.total_epochs}")
        print(f"  Loss: {logs.get('loss', 0):.4f} - Accuracy: {logs.get('accuracy', 0):.4f}")
        print(f"  Val Loss: {logs.get('val_loss', 0):.4f} - Val Accuracy: {logs.get('val_accuracy', 0):.4f}")


class EmotionModelTrainer:
    """
    Trainer class for emotion recognition models.
    """
    
    def __init__(
        self,
        model: Model,
        model_name: str = "model",
        save_path: Optional[Path] = None,
        logs_dir: Optional[Path] = None
    ):
        """
        Initialize the trainer.
        
        Args:
            model: Keras model to train
            model_name: Name for the model (used for saving)
            save_path: Path to save the trained model
            logs_dir: Directory for TensorBoard logs
        """
        self.model = model
        self.model_name = model_name
        self.save_path = save_path or MODELS_DIR / f"{model_name}.h5"
        self.logs_dir = logs_dir or MODELS_DIR / "logs" / model_name
        
        self.history = None
        self.training_metadata = {}
        
        # Create directories
        Path(self.save_path).parent.mkdir(parents=True, exist_ok=True)
        Path(self.logs_dir).mkdir(parents=True, exist_ok=True)
    
    def compile(
        self,
        learning_rate: float = LEARNING_RATE,
        optimizer: Optional[tf.keras.optimizers.Optimizer] = None,
        loss: str = 'categorical_crossentropy',
        metrics: list = ['accuracy']
    ) -> None:
        """
        Compile the model.
        
        Args:
            learning_rate: Learning rate for optimizer
            optimizer: Custom optimizer (uses Adam if None)
            loss: Loss function
            metrics: Metrics to track
        """
        if optimizer is None:
            optimizer = Adam(learning_rate=learning_rate)
        
        self.model.compile(
            optimizer=optimizer,
            loss=loss,
            metrics=metrics
        )
        
        self.training_metadata['learning_rate'] = learning_rate
        self.training_metadata['loss_function'] = loss
        self.training_metadata['metrics'] = metrics
    
    def get_callbacks(
        self,
        use_early_stopping: bool = True,
        use_reduce_lr: bool = True,
        use_tensorboard: bool = True,
        use_checkpoint: bool = True,
        custom_callbacks: Optional[list] = None
    ) -> list:
        """
        Get training callbacks.
        
        Args:
            use_early_stopping: Whether to use early stopping
            use_reduce_lr: Whether to reduce LR on plateau
            use_tensorboard: Whether to log to TensorBoard
            use_checkpoint: Whether to save best model
            custom_callbacks: Additional custom callbacks
            
        Returns:
            List of callbacks
        """
        callbacks = []
        
        if use_early_stopping:
            callbacks.append(EarlyStopping(
                monitor='val_loss',
                patience=EARLY_STOPPING_PATIENCE,
                restore_best_weights=True,
                verbose=1
            ))
        
        if use_reduce_lr:
            callbacks.append(ReduceLROnPlateau(
                monitor='val_loss',
                factor=REDUCE_LR_FACTOR,
                patience=REDUCE_LR_PATIENCE,
                min_lr=1e-7,
                verbose=1
            ))
        
        if use_tensorboard:
            callbacks.append(TensorBoard(
                log_dir=str(self.logs_dir),
                histogram_freq=1,
                write_graph=True
            ))
        
        if use_checkpoint:
            callbacks.append(ModelCheckpoint(
                filepath=str(self.save_path),
                monitor='val_accuracy',
                save_best_only=True,
                mode='max',
                verbose=1
            ))
        
        if custom_callbacks:
            callbacks.extend(custom_callbacks)
        
        return callbacks
    
    def train(
        self,
        train_generator,
        val_generator,
        epochs: int = EPOCHS,
        class_weights: Optional[Dict] = None,
        callbacks: Optional[list] = None,
        verbose: int = 1
    ) -> Dict:
        """
        Train the model.
        
        Args:
            train_generator: Training data generator
            val_generator: Validation data generator
            epochs: Number of epochs
            class_weights: Optional class weights for imbalanced data
            callbacks: Optional custom callbacks (uses defaults if None)
            verbose: Verbosity mode
            
        Returns:
            Training history dictionary
        """
        if callbacks is None:
            callbacks = self.get_callbacks()
        
        # Add progress callback
        callbacks.append(TrainingProgressCallback(epochs))
        
        # Record training start
        start_time = datetime.now()
        self.training_metadata['training_started'] = start_time.isoformat()
        self.training_metadata['epochs_requested'] = epochs
        
        print(f"\n{'='*60}")
        print(f"Training {self.model_name}")
        print(f"{'='*60}")
        print(f"Epochs: {epochs}")
        print(f"Training samples: {train_generator.samples}")
        print(f"Validation samples: {val_generator.samples}")
        print(f"{'='*60}\n")
        
        # Train
        self.history = self.model.fit(
            train_generator,
            epochs=epochs,
            validation_data=val_generator,
            class_weight=class_weights,
            callbacks=callbacks,
            verbose=verbose
        )
        
        # Record training end
        end_time = datetime.now()
        duration = (end_time - start_time).total_seconds()
        
        self.training_metadata['training_ended'] = end_time.isoformat()
        self.training_metadata['training_duration_seconds'] = duration
        self.training_metadata['epochs_completed'] = len(self.history.history['loss'])
        self.training_metadata['final_accuracy'] = float(self.history.history['accuracy'][-1])
        self.training_metadata['final_val_accuracy'] = float(self.history.history['val_accuracy'][-1])
        self.training_metadata['best_val_accuracy'] = float(max(self.history.history['val_accuracy']))
        
        print(f"\n{'='*60}")
        print(f"Training Complete!")
        print(f"Duration: {duration/60:.2f} minutes")
        print(f"Best Validation Accuracy: {self.training_metadata['best_val_accuracy']:.4f}")
        print(f"{'='*60}\n")
        
        return self.history.history
    
    def fine_tune(
        self,
        train_generator,
        val_generator,
        epochs: int = 20,
        learning_rate: float = LEARNING_RATE_FINE_TUNE,
        unfreeze_layers: int = 30
    ) -> Dict:
        """
        Fine-tune a transfer learning model.
        
        Args:
            train_generator: Training data generator
            val_generator: Validation data generator
            epochs: Number of fine-tuning epochs
            learning_rate: Learning rate for fine-tuning
            unfreeze_layers: Number of layers to unfreeze
            
        Returns:
            Fine-tuning history
        """
        # Unfreeze top layers
        for layer in self.model.layers[-unfreeze_layers:]:
            layer.trainable = True
        
        # Recompile with lower learning rate
        self.compile(learning_rate=learning_rate)
        
        print(f"\nFine-tuning with learning rate: {learning_rate}")
        print(f"Unfrozen {unfreeze_layers} top layers")
        
        # Continue training
        return self.train(train_generator, val_generator, epochs=epochs)
    
    def save_training_history(self) -> None:
        """Save training history and metadata to files."""
        if self.history is None:
            print("No training history to save.")
            return
        
        # Save history as JSON
        history_path = self.save_path.with_suffix('.history.json')
        with open(history_path, 'w') as f:
            json.dump(self.history.history, f, indent=2)
        
        # Save metadata
        metadata_path = self.save_path.with_suffix('.meta.json')
        with open(metadata_path, 'w') as f:
            json.dump(self.training_metadata, f, indent=2)
        
        print(f"Training history saved to: {history_path}")
        print(f"Training metadata saved to: {metadata_path}")
    
    def get_training_summary(self) -> Dict:
        """
        Get a summary of the training results.
        
        Returns:
            Dictionary with training summary
        """
        if self.history is None:
            return {"status": "Not trained"}
        
        return {
            "model_name": self.model_name,
            "epochs_completed": len(self.history.history['loss']),
            "final_accuracy": self.history.history['accuracy'][-1],
            "final_val_accuracy": self.history.history['val_accuracy'][-1],
            "best_val_accuracy": max(self.history.history['val_accuracy']),
            "final_loss": self.history.history['loss'][-1],
            "final_val_loss": self.history.history['val_loss'][-1],
            "training_duration": self.training_metadata.get('training_duration_seconds', 0)
        }


def train_custom_cnn(
    train_generator,
    val_generator,
    epochs: int = EPOCHS,
    class_weights: Optional[Dict] = None
) -> Tuple[Model, Dict]:
    """
    Train the custom CNN model.
    
    Args:
        train_generator: Training data generator
        val_generator: Validation data generator
        epochs: Number of epochs
        class_weights: Optional class weights
        
    Returns:
        Tuple of (trained model, training history)
    """
    from src.models.custom_cnn import build_custom_cnn
    
    model = build_custom_cnn()
    trainer = EmotionModelTrainer(model, "custom_cnn", CUSTOM_CNN_PATH)
    trainer.compile()
    history = trainer.train(train_generator, val_generator, epochs, class_weights)
    trainer.save_training_history()
    
    return model, history


def train_mobilenet(
    train_generator,
    val_generator,
    epochs: int = EPOCHS,
    fine_tune_epochs: int = 20,
    class_weights: Optional[Dict] = None
) -> Tuple[Model, Dict]:
    """
    Train the MobileNetV2 model with fine-tuning.
    
    Args:
        train_generator: Training data generator (RGB, 96x96)
        val_generator: Validation data generator
        epochs: Initial training epochs
        fine_tune_epochs: Fine-tuning epochs
        class_weights: Optional class weights
        
    Returns:
        Tuple of (trained model, training history)
    """
    from src.models.mobilenet_model import build_mobilenet_model
    
    model = build_mobilenet_model()
    trainer = EmotionModelTrainer(model, "mobilenet_v2", MOBILENET_PATH)
    
    # Initial training with frozen base
    trainer.compile()
    history = trainer.train(train_generator, val_generator, epochs, class_weights)
    
    # Fine-tuning
    if fine_tune_epochs > 0:
        fine_tune_history = trainer.fine_tune(
            train_generator, val_generator, fine_tune_epochs
        )
        # Merge histories
        for key in history:
            history[key].extend(fine_tune_history[key])
    
    trainer.save_training_history()
    
    return model, history


def train_vgg(
    train_generator,
    val_generator,
    epochs: int = EPOCHS,
    fine_tune_epochs: int = 15,
    class_weights: Optional[Dict] = None
) -> Tuple[Model, Dict]:
    """
    Train the VGG-19 model with fine-tuning.
    
    Args:
        train_generator: Training data generator (RGB, 96x96)
        val_generator: Validation data generator
        epochs: Initial training epochs
        fine_tune_epochs: Fine-tuning epochs
        class_weights: Optional class weights
        
    Returns:
        Tuple of (trained model, training history)
    """
    from src.models.vgg_model import build_vgg_model
    
    model = build_vgg_model()
    trainer = EmotionModelTrainer(model, "vgg19", VGG_PATH)
    
    # Initial training with frozen base
    trainer.compile()
    history = trainer.train(train_generator, val_generator, epochs, class_weights)
    
    # Fine-tuning
    if fine_tune_epochs > 0:
        fine_tune_history = trainer.fine_tune(
            train_generator, val_generator, fine_tune_epochs
        )
        for key in history:
            history[key].extend(fine_tune_history[key])
    
    trainer.save_training_history()
    
    return model, history