File size: 3,284 Bytes
cdf0064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Utility Module
Visualization and helper functions
"""
import matplotlib.pyplot as plt
import numpy as np


def plot_training_history(history):
    """
    Plot training and validation loss/accuracy curves
    
    Args:
        history: Keras training history object
    """
    # Create figure with 2 subplots side by side
    fig, axes = plt.subplots(1, 2, figsize=(14, 5))
    
    # Plot 1: Loss curves
    axes[0].plot(history.history['loss'], label='Training Loss', linewidth=2)
    axes[0].plot(history.history['val_loss'], label='Validation Loss', linewidth=2)
    axes[0].set_title('Model Loss Over Epochs', fontsize=14, fontweight='bold')
    axes[0].set_xlabel('Epoch', fontsize=12)
    axes[0].set_ylabel('Loss', fontsize=12)
    axes[0].legend(fontsize=10)
    axes[0].grid(True, alpha=0.3)
    
    # Plot 2: Accuracy curves
    axes[1].plot(history.history['accuracy'], label='Training Accuracy', linewidth=2)
    axes[1].plot(history.history['val_accuracy'], label='Validation Accuracy', linewidth=2)
    axes[1].set_title('Model Accuracy Over Epochs', fontsize=14, fontweight='bold')
    axes[1].set_xlabel('Epoch', fontsize=12)
    axes[1].set_ylabel('Accuracy', fontsize=12)
    axes[1].legend(fontsize=10)
    axes[1].grid(True, alpha=0.3)
    
    plt.tight_layout()
    plt.savefig('training_history.png', dpi=300, bbox_inches='tight')
    print("✓ Training history plots saved as 'training_history.png'")
    plt.show()


def visualize_results(model, noisy_images, clean_images, num_images=5):
    """
    Display comparison of noisy, original, and denoised images
    
    Args:
        model: Trained autoencoder model
        noisy_images: Noisy input images
        clean_images: Original clean images
        num_images: Number of images to display (default: 5)
    """
    # Generate denoised predictions
    denoised_images = model.predict(noisy_images[:num_images])
    
    # Create figure with 3 rows (Noisy, Original, Denoised) and num_images columns
    fig, axes = plt.subplots(3, num_images, figsize=(15, 6))
    
    for i in range(num_images):
        # Row 1: Noisy images
        axes[0, i].imshow(noisy_images[i].reshape(28, 28), cmap='gray')
        axes[0, i].axis('off')
        if i == 0:
            axes[0, i].set_title('Noisy Input', fontsize=12, fontweight='bold')
        
        # Row 2: Original clean images
        axes[1, i].imshow(clean_images[i].reshape(28, 28), cmap='gray')
        axes[1, i].axis('off')
        if i == 0:
            axes[1, i].set_title('Original Clean', fontsize=12, fontweight='bold')
        
        # Row 3: Denoised output from model
        axes[2, i].imshow(denoised_images[i].reshape(28, 28), cmap='gray')
        axes[2, i].axis('off')
        if i == 0:
            axes[2, i].set_title('Denoised Output', fontsize=12, fontweight='bold')
    
    plt.tight_layout()
    plt.savefig('denoising_results.png', dpi=300, bbox_inches='tight')
    print("✓ Denoising results saved as 'denoising_results.png'")
    plt.show()


def print_model_summary(model):
    """
    Print detailed model architecture summary
    
    Args:
        model: Keras model
    """
    print("\n" + "="*60)
    print("MODEL ARCHITECTURE SUMMARY")
    print("="*60)
    model.summary()
    print("="*60 + "\n")