File size: 9,578 Bytes
9ea3b1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
"""
Training script for all three IDS models.
Trains MLP, LSTM, and 1D-CNN on NSL-KDD with full evaluation.
"""

import os
import sys
import json
import time
import random
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import TensorDataset, DataLoader
from sklearn.metrics import (classification_report, confusion_matrix,
                             roc_auc_score, average_precision_score)

# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from models.mlp_baseline import MLP_IDS
from models.lstm_model import LSTM_IDS
from models.cnn1d_model import CNN1D_IDS
from data.preprocess import load_nsl_kdd, preprocess, save_preprocessed, FEATURE_NAMES

# ========================
# Reproducibility
# ========================
SEED = 42
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

# ========================
# Config
# ========================
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
NUM_CLASSES = 2  # Binary classification
EPOCHS = 50
BATCH_SIZE = 256
LR = 1e-3
WEIGHT_DECAY = 1e-4
RESULTS_DIR = 'results'
MODELS_DIR = 'saved_models'


def compute_class_weights(y_train):
    """Compute inverse-frequency class weights."""
    counts = np.bincount(y_train)
    weights = 1.0 / counts.astype(np.float32)
    weights = weights / weights.sum() * len(weights)  # Normalize
    return torch.FloatTensor(weights).to(DEVICE)


def train_one_epoch(model, loader, criterion, optimizer):
    """Train for one epoch."""
    model.train()
    total_loss = 0
    correct = 0
    total = 0
    
    for X_batch, y_batch in loader:
        X_batch, y_batch = X_batch.to(DEVICE), y_batch.to(DEVICE)
        
        optimizer.zero_grad()
        outputs = model(X_batch)
        loss = criterion(outputs, y_batch)
        loss.backward()
        optimizer.step()
        
        total_loss += loss.item() * len(y_batch)
        preds = outputs.argmax(dim=1)
        correct += (preds == y_batch).sum().item()
        total += len(y_batch)
    
    return total_loss / total, correct / total


@torch.no_grad()
def evaluate(model, loader, criterion):
    """Evaluate model on dataset."""
    model.eval()
    total_loss = 0
    all_preds = []
    all_probs = []
    all_labels = []
    
    for X_batch, y_batch in loader:
        X_batch, y_batch = X_batch.to(DEVICE), y_batch.to(DEVICE)
        
        outputs = model(X_batch)
        loss = criterion(outputs, y_batch)
        
        total_loss += loss.item() * len(y_batch)
        probs = torch.softmax(outputs, dim=1)
        all_preds.append(outputs.argmax(dim=1).cpu().numpy())
        all_probs.append(probs.cpu().numpy())
        all_labels.append(y_batch.cpu().numpy())
    
    all_preds = np.concatenate(all_preds)
    all_probs = np.concatenate(all_probs)
    all_labels = np.concatenate(all_labels)
    
    avg_loss = total_loss / len(all_labels)
    
    return avg_loss, all_preds, all_probs, all_labels


def full_evaluation(y_true, y_pred, y_probs, class_names):
    """Compute all metrics."""
    results = {}
    
    # Classification report
    report = classification_report(y_true, y_pred, target_names=class_names, output_dict=True)
    results['classification_report'] = report
    
    # ROC-AUC (binary)
    if len(class_names) == 2:
        results['roc_auc'] = roc_auc_score(y_true, y_probs[:, 1])
        results['pr_auc'] = average_precision_score(y_true, y_probs[:, 1])
    
    # Confusion matrix
    cm = confusion_matrix(y_true, y_pred)
    results['confusion_matrix'] = cm.tolist()
    
    return results


def train_model(model, model_name, X_train, y_train, X_test, y_test, class_names):
    """Full training pipeline for one model."""
    print(f"\n{'='*60}")
    print(f"Training {model_name}")
    print(f"{'='*60}")
    print(f"Parameters: {model.count_parameters():,}")
    print(f"Device: {DEVICE}")
    
    # Data loaders
    train_ds = TensorDataset(torch.FloatTensor(X_train), torch.LongTensor(y_train))
    test_ds = TensorDataset(torch.FloatTensor(X_test), torch.LongTensor(y_test))
    train_loader = DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True)
    test_loader = DataLoader(test_ds, batch_size=BATCH_SIZE, shuffle=False)
    
    # Loss with class weights
    class_weights = compute_class_weights(y_train)
    criterion = nn.CrossEntropyLoss(weight=class_weights)
    
    # Optimizer
    optimizer = torch.optim.Adam(model.parameters(), lr=LR, weight_decay=WEIGHT_DECAY)
    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=5, factor=0.5)
    
    # Training loop
    model.to(DEVICE)
    best_f1 = 0
    history = {'train_loss': [], 'train_acc': [], 'test_loss': [], 'test_acc': []}
    
    start_time = time.time()
    
    for epoch in range(EPOCHS):
        train_loss, train_acc = train_one_epoch(model, train_loader, criterion, optimizer)
        test_loss, test_preds, test_probs, test_labels = evaluate(model, test_loader, criterion)
        test_acc = (test_preds == test_labels).mean()
        
        scheduler.step(test_loss)
        
        history['train_loss'].append(train_loss)
        history['train_acc'].append(train_acc)
        history['test_loss'].append(test_loss)
        history['test_acc'].append(test_acc)
        
        # Check for best model
        report = classification_report(test_labels, test_preds, output_dict=True)
        weighted_f1 = report['weighted avg']['f1-score']
        
        if weighted_f1 > best_f1:
            best_f1 = weighted_f1
            os.makedirs(MODELS_DIR, exist_ok=True)
            torch.save(model.state_dict(), os.path.join(MODELS_DIR, f'{model_name}_best.pt'))
        
        if (epoch + 1) % 10 == 0 or epoch == 0:
            print(f"  Epoch {epoch+1:3d}/{EPOCHS} | "
                  f"Train Loss: {train_loss:.4f} Acc: {train_acc:.4f} | "
                  f"Test Loss: {test_loss:.4f} Acc: {test_acc:.4f} F1: {weighted_f1:.4f}")
    
    train_time = time.time() - start_time
    print(f"\n  Training time: {train_time:.1f}s")
    
    # Load best model and final evaluation
    model.load_state_dict(torch.load(os.path.join(MODELS_DIR, f'{model_name}_best.pt'),
                                      weights_only=True))
    _, final_preds, final_probs, final_labels = evaluate(model, test_loader, criterion)
    
    results = full_evaluation(final_labels, final_preds, final_probs, class_names)
    results['training_time'] = train_time
    results['best_weighted_f1'] = best_f1
    results['history'] = history
    results['parameters'] = model.count_parameters()
    
    # Print final results
    print(f"\n  Final Results ({model_name}):")
    print(f"  {'='*50}")
    print(classification_report(final_labels, final_preds, target_names=class_names))
    
    if 'roc_auc' in results:
        print(f"  ROC-AUC: {results['roc_auc']:.4f}")
        print(f"  PR-AUC:  {results['pr_auc']:.4f}")
    
    print(f"  Confusion Matrix:\n{confusion_matrix(final_labels, final_preds)}")
    
    return model, results


def main():
    # ========================
    # Data
    # ========================
    df_train, df_test = load_nsl_kdd()
    X_train, X_test, y_train, y_test, le, scaler, class_names = preprocess(
        df_train, df_test, binary=True
    )
    save_preprocessed(X_train, X_test, y_train, y_test, le, scaler, class_names)
    
    # ========================
    # Train all models
    # ========================
    all_results = {}
    
    # 1. MLP Baseline
    mlp = MLP_IDS(in_dim=41, num_classes=NUM_CLASSES)
    mlp, mlp_results = train_model(mlp, 'mlp', X_train, y_train, X_test, y_test, class_names)
    all_results['mlp'] = mlp_results
    
    # 2. LSTM
    lstm = LSTM_IDS(in_dim=41, num_classes=NUM_CLASSES)
    lstm, lstm_results = train_model(lstm, 'lstm', X_train, y_train, X_test, y_test, class_names)
    all_results['lstm'] = lstm_results
    
    # 3. 1D-CNN
    cnn = CNN1D_IDS(in_dim=41, num_classes=NUM_CLASSES)
    cnn, cnn_results = train_model(cnn, 'cnn1d', X_train, y_train, X_test, y_test, class_names)
    all_results['cnn1d'] = cnn_results
    
    # ========================
    # Save results
    # ========================
    os.makedirs(RESULTS_DIR, exist_ok=True)
    
    def convert(o):
        if isinstance(o, np.floating): return float(o)
        if isinstance(o, np.integer): return int(o)
        if isinstance(o, np.ndarray): return o.tolist()
        return o
    
    with open(os.path.join(RESULTS_DIR, 'training_results.json'), 'w') as f:
        json.dump(all_results, f, indent=2, default=convert)
    
    # ========================
    # Summary comparison
    # ========================
    print("\n" + "="*60)
    print("MODEL COMPARISON SUMMARY")
    print("="*60)
    print(f"{'Model':<10} {'Params':>8} {'Accuracy':>10} {'W-F1':>8} {'ROC-AUC':>9} {'PR-AUC':>8} {'Time':>8}")
    print("-"*60)
    
    for name, res in all_results.items():
        acc = res['classification_report']['accuracy']
        wf1 = res['best_weighted_f1']
        roc = res.get('roc_auc', 0)
        pr = res.get('pr_auc', 0)
        t = res['training_time']
        p = res['parameters']
        print(f"{name:<10} {p:>8,} {acc:>10.4f} {wf1:>8.4f} {roc:>9.4f} {pr:>8.4f} {t:>7.1f}s")
    
    print("\nAll models trained successfully!")
    print(f"Results saved to {RESULTS_DIR}/training_results.json")
    print(f"Models saved to {MODELS_DIR}/")


if __name__ == '__main__':
    main()