This commit is contained in:
2026-05-21 14:31:01 +02:00
parent 4192e5f2be
commit 0d69f340ae

View File

@@ -29,7 +29,7 @@ Original file is located at
import sys
IN_COLAB = 'google.colab' in sys.modules
# f IN_COLAB:
# if IN_COLAB:
# !pip install pandas numpy matplotlib seaborn pillow scikit-learn tensorflow
# !pip install --upgrade kagglehub[pandas-datasets,hf-datasets]
@@ -241,6 +241,12 @@ from tensorflow.keras.applications import DenseNet121
from tensorflow.keras.applications.densenet import preprocess_input
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
gpus = tf.config.list_physical_devices('GPU')
print("GPUs:", gpus)
strategy = tf.distribute.MirroredStrategy()
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip("horizontal"),
@@ -265,7 +271,9 @@ x = Dense(512, activation='relu')(x) # Added another Dense layer
x = Dense(256, activation='relu')(x) # Existing Dense layer
predictions = Dense(1, activation='sigmoid')(x) # Output layer for binary classification
model = Model(inputs=base_model.input, outputs=predictions)
with strategy.scope(): # Use all gpus
model = Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])
"""## 4. Data Generators
@@ -313,21 +321,7 @@ val_generator = val_datagen.flow_from_dataframe(
seed=42
)
"""## 5. Compile the Model
I will compile the model using the Adam optimizer, binary cross-entropy loss (suitable for binary classification), and track accuracy as a metric.
"""
from tensorflow.keras.optimizers import Adam
model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])
"""## 6. Train the Model
I will now train the model using the prepared data generators. I'll also add callbacks for early stopping to prevent overfitting and to save the best model.
"""
"""## 6. Train the Model"""
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
@@ -354,7 +348,7 @@ history = model.fit(
class_weight=class_weights # Use class weights to handle imbalance
)
"""## X. Evaluation
"""## 7. Evaluation
### Load best model
"""