-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_final_98.py
More file actions
86 lines (73 loc) · 2.79 KB
/
Copy pathtrain_final_98.py
File metadata and controls
86 lines (73 loc) · 2.79 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
import tensorflow as tf
from tensorflow.keras import layers, models, optimizers
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 1. THE "SECRET" PRE-PROCESSING (CLAHE)
# This function is applied to EVERY image before the model sees it.
def medical_preprocessing(img):
# Convert to 0-255 uint8 for OpenCV to work
img = img.astype(np.uint8)
# Apply CLAHE to make tumor edges "pop"
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
for i in range(3): # Apply to each color channel (R, G, B)
img[:,:,i] = clahe.apply(img[:,:,i])
return img.astype(np.float32) / 255.0
# 2. DATA LOADERS (With the new Pre-processing)
datagen = ImageDataGenerator(
preprocessing_function=medical_preprocessing,
validation_split=0.2
)
train_gen = datagen.flow_from_directory(
'data/Training',
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='training'
)
val_gen = datagen.flow_from_directory(
'data/Training',
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='validation'
)
# 3. BUILD THE FINE-TUNING MODEL
base_model = MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet')
# THE TRICK: Unfreeze only the top layers of the pre-trained brain
base_model.trainable = True
for layer in base_model.layers[:-30]: # Unfreeze the last 30 layers
layer.trainable = False
model = models.Sequential([
layers.GaussianNoise(0.01, input_shape=(224, 224, 3)), # Minimal noise defense
base_model,
layers.GlobalAveragePooling2D(),
layers.Dropout(0.4), # Increased dropout to stop overfitting (Xception lesson)
layers.Dense(4, activation='softmax')
])
# 4. VERY LOW LEARNING RATE (Crucial so we don't 'break' the brain)
model.compile(
optimizer=optimizers.Adam(learning_rate=0.00001),
loss='categorical_crossentropy',
metrics=['accuracy']
)
print("\n--- Starting Phase 3: Final Medical Fine-Tuning ---")
history = model.fit(train_gen, validation_data=val_gen, epochs=10)
# 5. SAVE EVERYTHING
model.save('models/v3_Final_Booster.h5')
# Save the new graphs for your presentation
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Train')
plt.plot(history.history['val_accuracy'], label='Val')
plt.title('Final Model Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train')
plt.plot(history.history['val_loss'], label='Val')
plt.title('Final Model Loss')
plt.legend()
plt.savefig('presentation_assets/final_v3_performance.png')
print("\nSuccess! Model saved as v3_Final_Booster.h5")