-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_transfer_models.py
More file actions
373 lines (296 loc) · 12 KB
/
train_transfer_models.py
File metadata and controls
373 lines (296 loc) · 12 KB
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
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
import os
import sys
from pathlib import Path
import argparse
import json
import numpy as np
import matplotlib
matplotlib.use('Agg')
import tensorflow as tf
from preprocessing import DataPreprocessor
from balancing import DataBalancer
from transfer_learning_classifier import TransferLearningModel
np.random.seed(42)
tf.random.set_seed(42)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, required=True,
help='Model to use', default='{MODEL_NAME.upper()}')
parser.add_argument('--dataset', type=str, required=True,
help='Path to dataset', default='chest_xray')
args = parser.parse_args()
DATA_DIR = args.dataset
PROJECT_DIR = '.'
CHECKPOINT_DIR = f'{PROJECT_DIR}/checkpoints'
MODEL_DIR = f'{PROJECT_DIR}/models'
RESULTS_DIR = f'{PROJECT_DIR}/results'
MODEL_NAME = args.model
Path(CHECKPOINT_DIR).mkdir(parents=True, exist_ok=True)
Path(MODEL_DIR).mkdir(parents=True, exist_ok=True)
Path(RESULTS_DIR).mkdir(parents=True, exist_ok=True)
IMG_SIZE = (224, 224)
BATCH_SIZE = 32
INITIAL_EPOCHS = 30
FINE_TUNE_EPOCHS = 20
LEARNING_RATE = 0.0001
BALANCE_DATASET = True
CHECKPOINT_PATH = f'{CHECKPOINT_DIR}/{MODEL_NAME}_best.h5'
MODEL_SAVE_PATH = f'{MODEL_DIR}/{MODEL_NAME}_final.h5'
print("="*70)
print(f'PNEUMONIA DETECTION - {MODEL_NAME.upper()} TRAINING PIPELINE')
print("="*70)
print("\n" + "="*70)
print("SYSTEM CHECK")
print("="*70)
print(f"TensorFlow version: {tf.__version__}")
gpus = tf.config.list_physical_devices('GPU')
if gpus:
print(f"✓ GPU available: {len(gpus)} device(s)")
for gpu in gpus:
print(f" - {gpu}")
else:
print("⚠ No GPU found - training will use CPU (slower)")
print(f"Python version: {sys.version}")
print(f"Working directory: {os.getcwd()}")
if not Path(DATA_DIR).exists():
print(f"\n❌ ERROR: Dataset directory not found at {DATA_DIR}")
print("Please create the dataset directory with train/val/test folders!")
return
else:
print(f"\n✓ Dataset directory found: {DATA_DIR}")
print("\n" + "="*70)
print("CONFIGURATION")
print("="*70)
print(f"Data directory: {DATA_DIR}")
print(f"Image size: {IMG_SIZE}")
print(f"Batch size: {BATCH_SIZE}")
print(f"Initial epochs: {INITIAL_EPOCHS}")
print(f"Fine-tune epochs: {FINE_TUNE_EPOCHS}")
print(f"Learning rate: {LEARNING_RATE}")
print(f"Balance dataset: {BALANCE_DATASET}")
print(f"Checkpoint path: {CHECKPOINT_PATH}")
print(f"Model save path: {MODEL_SAVE_PATH}")
print(f"Results directory: {RESULTS_DIR}")
print("\n" + "="*70)
print("STEP 1: DATASET ANALYSIS")
print("="*70)
preprocessor = DataPreprocessor(
data_dir=DATA_DIR,
img_size=IMG_SIZE,
batch_size=BATCH_SIZE
)
stats = preprocessor.analyze_dataset()
print("\nVisualizing dataset samples...")
preprocessor.visualize_samples(num_samples=8)
print("\nVisualizing augmentation effects...")
preprocessor.visualize_augmentation(num_examples=5)
print("\n" + "="*70)
print("STEP 2: CREATING DATA GENERATORS")
print("="*70)
train_datagen, val_test_datagen = preprocessor.create_augmentation_generators()
train_generator, val_generator, test_generator = preprocessor.create_data_generators(
train_datagen,
val_test_datagen
)
print(f"\n✓ Train generator: {train_generator.samples} images")
print(f"✓ Validation generator: {val_generator.samples} images")
print(f"✓ Test generator: {test_generator.samples} images")
if BALANCE_DATASET:
print("\n" + "="*70)
print("STEP 3: DATASET BALANCING")
print("="*70)
train_balanced_path = preprocessor.prepare_for_oversampling()
balancer = DataBalancer(
train_balanced_path=train_balanced_path,
img_size=IMG_SIZE
)
balancer.get_balance_statistics()
print("\n⚠ This may take several minutes...")
balancer.oversample_minority_class(target_balance='equal', verbose=True)
print("\nVisualizing augmented samples...")
balancer.visualize_augmented_samples(num_samples=8)
train_datagen_balanced = train_datagen
train_generator = train_datagen_balanced.flow_from_directory(
train_balanced_path,
target_size=IMG_SIZE,
batch_size=BATCH_SIZE,
class_mode='categorical',
shuffle=True,
seed=42
)
print(f"\n✓ Balanced train generator: {train_generator.samples} images")
else:
print("\n" + "="*70)
print("STEP 3: DATASET BALANCING - SKIPPED")
print("="*70)
print("Using original imbalanced dataset")
print("\n" + "="*70)
print("STEP 4: BUILDING {MODEL_NAME.upper()} MODEL")
print("="*70)
model = TransferLearningModel(
input_shape=(*IMG_SIZE, 3),
num_classes=2,
freeze_base=True,
model_name=MODEL_NAME,
)
model.compile_model(learning_rate=LEARNING_RATE)
print("\nModel Summary:")
model.model.summary()
trainable_params = sum([tf.size(w).numpy() for w in model.model.trainable_weights])
total_params = sum([tf.size(w).numpy() for w in model.model.weights])
frozen_params = total_params - trainable_params
print("\n" + "-"*70)
print("MODEL PARAMETERS")
print("-"*70)
print(f"Trainable parameters: {trainable_params:,}")
print(f"Frozen parameters: {frozen_params:,}")
print(f"Total parameters: {total_params:,}")
print(f"Trainable percentage: {trainable_params/total_params*100:.2f}%")
print("\n" + "="*70)
print("STEP 5: TRAINING MODEL")
print("="*70)
print("\n⚠ This will take a while... Go grab a coffee! ☕")
try:
history = model.train(
train_generator=train_generator,
val_generator=val_generator,
epochs=INITIAL_EPOCHS,
checkpoint_path=CHECKPOINT_PATH,
fine_tune=True,
fine_tune_epochs=FINE_TUNE_EPOCHS
)
print("\n✓ Training completed successfully!")
except KeyboardInterrupt:
print("\n\n⚠ Training interrupted by user!")
print("Saving current model state...")
model.save_model(filepath=f'{MODEL_DIR}/{MODEL_NAME.upper()}_interrupted.h5')
print("Model saved. You can resume training later.")
return
except Exception as e:
print(f"\n❌ ERROR during training: {e}")
print("Saving current model state...")
model.save_model(filepath=f'{MODEL_DIR}/{MODEL_NAME.upper()}_error.h5')
raise
print("\nGenerating training history plots...")
model.plot_training_history(save_path=f'{RESULTS_DIR}/training_history.png')
print("\n" + "="*70)
print("STEP 6: EVALUATION ON TEST SET")
print("="*70)
metrics = model.evaluate(
test_generator=test_generator,
save_dir=RESULTS_DIR
)
print("\n" + "="*70)
print("FINAL TEST METRICS")
print("="*70)
for metric_name, metric_value in metrics.items():
print(f"{metric_name:25s}: {metric_value}")
print("\n" + "="*70)
print("STEP 7: SAVING MODEL")
print("="*70)
model.save_model(filepath=MODEL_SAVE_PATH)
print(f"\n✓ Model saved to: {MODEL_SAVE_PATH}")
print(f"✓ Results saved to: {RESULTS_DIR}/")
print(f"✓ Checkpoints saved to: {CHECKPOINT_PATH}")
print("\n" + "="*70)
print("STEP 8: ADDITIONAL ANALYSIS")
print("="*70)
print("\nLoading best model from checkpoint...")
try:
model.load_model(CHECKPOINT_PATH)
print("\nRe-evaluating with best checkpoint...")
best_metrics = model.evaluate(
test_generator=test_generator,
save_dir=f'{RESULTS_DIR}/best_checkpoint'
)
print("\n" + "="*70)
print("COMPARISON: Final Model vs Best Checkpoint")
print("="*70)
print(f"{'Metric':<25} {'Final Model':<15} {'Best Checkpoint':<15} {'Difference':<15}")
print("-"*70)
for key in metrics.keys():
diff = best_metrics[key] - metrics[key]
diff_str = f"{diff:+.4f}"
print(f"{key:<25} {metrics[key]:<15.4f} {best_metrics[key]:<15.4f} {diff_str:<15}")
comparison = {
'final_model': metrics,
'best_checkpoint': best_metrics,
'differences': {k: best_metrics[k] - metrics[k] for k in metrics.keys()}
}
with open(f'{RESULTS_DIR}/model_comparison.json', 'w') as f:
json.dump(comparison, f, indent=4)
print(f"\n✓ Comparison saved to: {RESULTS_DIR}/model_comparison.json")
except Exception as e:
print(f"\n⚠ Could not load checkpoint for comparison: {e}")
print("Continuing with final model only...")
print("\n" + "="*70)
print("TRAINING SUMMARY")
print("="*70)
summary = {
'dataset': {
'data_dir': DATA_DIR,
'train_samples': train_generator.samples,
'val_samples': val_generator.samples,
'test_samples': test_generator.samples,
'balanced': BALANCE_DATASET
},
'configuration': {
'img_size': IMG_SIZE,
'batch_size': BATCH_SIZE,
'initial_epochs': INITIAL_EPOCHS,
'fine_tune_epochs': FINE_TUNE_EPOCHS,
'learning_rate': LEARNING_RATE
},
'model': {
'architecture': '{MODEL_NAME.upper()}',
'trainable_params': int(trainable_params),
'total_params': int(total_params)
},
'results': {
'test_metrics': metrics,
'model_path': MODEL_SAVE_PATH,
'checkpoint_path': CHECKPOINT_PATH,
'results_dir': RESULTS_DIR
}
}
with open(f'{RESULTS_DIR}/training_summary.json', 'w') as f:
json.dump(summary, f, indent=4)
print("\nDataset:")
print(f" • Train samples: {train_generator.samples}")
print(f" • Validation samples: {val_generator.samples}")
print(f" • Test samples: {test_generator.samples}")
print(f" • Balanced: {BALANCE_DATASET}")
print("\nModel:")
print(f" • Architecture: {MODEL_NAME.upper()}")
print(f" • Total parameters: {total_params:,}")
print(f" • Trainable params: {trainable_params:,}")
print("\nBest Test Metrics:")
for metric_name, metric_value in metrics.items():
print(f" • {metric_name:20s}: {metric_value}")
print("\nFiles Created:")
print(f" • Model: {MODEL_SAVE_PATH}")
print(f" • Checkpoint: {CHECKPOINT_PATH}")
print(f" • Results: {RESULTS_DIR}/")
print(f" • Summary: {RESULTS_DIR}/training_summary.json")
print("\n" + "="*70)
print("🎉 TRAINING PIPELINE COMPLETE! 🎉")
print("="*70)
print("\n✓ All steps completed successfully!")
print(f"\n📁 Check your results in: {RESULTS_DIR}/")
print(f"🤖 Your trained model: {MODEL_SAVE_PATH}")
print("\n💡 You can now use this model for predictions on new X-ray images!")
print("\nThank you for using this training pipeline! 🚀")
if __name__ == "__main__":
print("\n" + "="*70)
print("STARTING PNEUMONIA DETECTION TRAINING PIPELINE")
print("="*70)
try:
main()
except KeyboardInterrupt:
print("\n\n⚠ Program interrupted by user. Exiting...")
except Exception as e:
print(f"\n\n❌ FATAL ERROR: {e}")
import traceback
traceback.print_exc()
print("\nPlease check the error message above and fix the issue.")
sys.exit(1)