File size: 8,314 Bytes
45742a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import pandas as pd
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.metrics import AUC
import matplotlib.pyplot as plt


# --- BEGIN ROBUST GPU FIX ---
# We must do this before any other TensorFlow operations
import tensorflow as tf
print("Applying robust GPU configuration...")
try:
    # Get all GPUs that TensorFlow can see
    gpus = tf.config.list_physical_devices('GPU')
    if gpus:
        for gpu in gpus:
            # Set memory growth to True for each GPU
            tf.config.experimental.set_memory_growth(gpu, True)
            print(f"  > Enabled memory growth for: {gpu.name}")
    else:
        print("  > No GPUs found by TensorFlow. Will run on CPU.")
except Exception as e:
    print(f"  > Error applying GPU configuration: {e}")
# --- END ROBUST GPU FIX ---


# 1. --- Import from our project files ---
try:
    import config
    from model import build_baseline_model
except ImportError:
    print("Error: Could not import config.py or model.py.")
    print("Make sure they are in the 'src/' directory.")
    sys.exit(1)
    
# Suppress TensorFlow logs
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 

def create_balanced_dataframe():
    """
    Scans the training directory and creates a balanced DataFrame
    of 'real' and 'fake' image paths. This is our undersampling step.
    """
    print("Creating balanced training dataframe...")
    
    # Get lists of all real and fake training images
    real_paths = [os.path.join(config.TRAIN_REAL_DIR, f) for f in os.listdir(config.TRAIN_REAL_DIR) if f.endswith('.jpg')]
    fake_paths = [os.path.join(config.TRAIN_FAKE_DIR, f) for f in os.listdir(config.TRAIN_FAKE_DIR) if f.endswith('.jpg')]
    
    # Create DataFrames
    df_real = pd.DataFrame({'filepath': real_paths, 'label': 'real'})
    df_fake = pd.DataFrame({'filepath': fake_paths, 'label': 'fake'})
    
    # --- This is the key undersampling step ---
    # We sample the 'fake' DataFrame to have the same number of
    # images as the 'real' DataFrame.
    df_fake_sampled = df_fake.sample(n=len(df_real), random_state=42)
    
    # Combine and shuffle
    df_train_balanced = pd.concat([df_real, df_fake_sampled]).sample(frac=1, random_state=42).reset_index(drop=True)
    
    print(f"Balanced training set created: {len(df_train_balanced)} total images")
    print(f"  Real: {len(df_real)} images")
    print(f"  Fake: {len(df_fake_sampled)} images")
    
    return df_train_balanced

def create_generators(train_df):
    """
    Creates the Keras Data Generators for training and validation.
    """
    print("Creating Data Generators...")
    
    # --- Training Generator with Data Augmentation ---
    # Data augmentation creates "new" versions of our images on-the-fly
    # (flipped, rotated, etc.) to make our model more robust.
    train_datagen = ImageDataGenerator(
        rescale=1./255,          # Normalize pixel values
        rotation_range=20,
        width_shift_range=0.1,
        height_shift_range=0.1,
        horizontal_flip=True,
        fill_mode='nearest',
        validation_split=0.2     # We'll use 20% of our training data for validation
    )
    
    # --- Test/Validation Generator (No Augmentation) ---
    # We *never* augment our validation or test data.
    test_datagen = ImageDataGenerator(rescale=1./255)
    
    # --- Create the generators from our DataFrames ---
    
    # Training Generator (from the balanced DataFrame)
    train_generator = train_datagen.flow_from_dataframe(
        dataframe=train_df,
        x_col='filepath',
        y_col='label',
        target_size=(config.TARGET_IMAGE_SIZE, config.TARGET_IMAGE_SIZE),
        batch_size=config.BATCH_SIZE,
        class_mode='binary',
        subset='training',
        shuffle=True
    )
    
    # Validation Generator (also from the balanced DataFrame)
    validation_generator = train_datagen.flow_from_dataframe(
        dataframe=train_df,
        x_col='filepath',
        y_col='label',
        target_size=(config.TARGET_IMAGE_SIZE, config.TARGET_IMAGE_SIZE),
        batch_size=config.BATCH_SIZE,
        class_mode='binary',
        subset='validation',
        shuffle=False  # No need to shuffle validation data
    )
    
    # Test Generator (from the *unbalanced* test directory)
    # This is our real-world test.
    test_generator = test_datagen.flow_from_directory(
        directory=config.TEST_DIR,
        target_size=(config.TARGET_IMAGE_SIZE, config.TARGET_IMAGE_SIZE),
        batch_size=config.BATCH_SIZE,
        class_mode='binary',
        shuffle=False
    )
    
    return train_generator, validation_generator, test_generator

def plot_history(history, save_path):
    """
    Plots the training history (accuracy and loss) and saves it to a file.
    """
    print(f"Saving training history plot to {save_path}...")
    
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10))

    # Plot training & validation accuracy values
    ax1.plot(history.history['accuracy'])
    ax1.plot(history.history['val_accuracy'])
    ax1.set_title('Model Accuracy')
    ax1.set_ylabel('Accuracy')
    ax1.set_xlabel('Epoch')
    ax1.legend(['Train', 'Validation'], loc='upper left')

    # Plot training & validation loss values
    ax2.plot(history.history['loss'])
    ax2.plot(history.history['val_loss'])
    ax2.set_title('Model Loss')
    ax2.set_ylabel('Loss')
    ax2.set_xlabel('Epoch')
    ax2.legend(['Train', 'Validation'], loc='upper left')
    
    plt.tight_layout()
    plt.savefig(save_path)
    print("History plot saved.")


def main():
    """
    Main training function.
    """
    print("--- Phase 2: Starting Baseline Model Training ---")
    
    # 1. Handle class imbalance
    train_df = create_balanced_dataframe()
    
    # 2. Create data generators
    train_gen, val_gen, test_gen = create_generators(train_df)
    
    # 3. Build the model
    print("Building model...")
    model = build_baseline_model(config.TARGET_IMAGE_SIZE)
    
    # 4. Compile the model
    # We use AUC (Area Under the Curve) as our main metric.
    # It's much better than accuracy for imbalanced test sets.
    model.compile(
        optimizer=Adam(learning_rate=config.LEARNING_RATE),
        loss='binary_crossentropy',
        metrics=['accuracy', AUC(name='auc')]
    )
    
    model.summary()
    
    # 5. Define Callbacks
    # This will save the *best* model based on validation AUC
    checkpoint_path = os.path.join(config.MODEL_DIR, "baseline_model.h5")
    model_checkpoint = ModelCheckpoint(
        filepath=checkpoint_path,
        save_best_only=True,
        monitor='val_auc',
        mode='max',
        verbose=1
    )
    
    # This will stop training early if it stops improving
    early_stopping = EarlyStopping(
        monitor='val_auc',
        mode='max',
        patience=5,  # Stop after 5 epochs of no improvement
        verbose=1,
        restore_best_weights=True
    )
    
    # 6. Start Training
    print("Starting model training...")
    history = model.fit(
        train_gen,
        steps_per_epoch=train_gen.n // config.BATCH_SIZE,
        validation_data=val_gen,
        validation_steps=val_gen.n // config.BATCH_SIZE,
        epochs=config.EPOCHS,
        callbacks=[model_checkpoint, early_stopping]
    )
    
    print("Training complete.")
    
    # 7. Evaluate on the (imbalanced) Test Set
    print("Evaluating model on the test set...")
    results = model.evaluate(test_gen, steps=test_gen.n // config.BATCH_SIZE)
    
    print("\n--- Test Set Evaluation ---")
    print(f"Test Loss:     {results[0]:.4f}")
    print(f"Test Accuracy: {results[1]:.4f}")
    print(f"Test AUC:      {results[2]:.4f}")
    
    # 8. Save history plot
    plot_path = os.path.join(config.RESULTS_DIR, "baseline_training_history.png")
    plot_history(history, plot_path)
    
    print("\n--- Baseline Model Training Finished ---")
    print(f"Best model saved to: {checkpoint_path}")

if __name__ == "__main__":
    
    # Create models/ and results/ directories if they don't exist
    os.makedirs(config.MODEL_DIR, exist_ok=True)
    os.makedirs(config.RESULTS_DIR, exist_ok=True)
    
    main()