im doing a retinopathy detection project with over 3500 images, 700 in each class. I've filtered the image like
It seems that my model isn't learning from the data, or is having trouble because the accuracy value climbs up really slowly, and the loss is still pretty high. is there a better way to train my model and to make it faster?
import albumentations as A
from albumentations.core.composition import OneOf
from albumentations.core.transforms_interface import ImageOnlyTransform
from albumentations.pytorch import ToTensorV2
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau
#data augmentation
def augmentations():
return A.Compose([
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.Rotate(limit=40, p=0.5),
A.RandomBrightnessContrast(p=0.5),
A.GaussianBlur(p=0.5)
])
# ImageDataGenerator
def create_generators(directory):
datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)
train_gen = datagen.flow_from_directory(
directory,
target_size=(224, 224),
batch_size=32,
class_mode='sparse',
subset='training'
)
val_gen = datagen.flow_from_directory(
directory,
target_size=(224, 224),
batch_size=32,
class_mode='sparse',
subset='validation'
)
return train_gen, val_gen
#CNN model
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
MaxPooling2D(pool_size=(2, 2)),
BatchNormalization(),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
BatchNormalization(),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
BatchNormalization(),
#Conv2D(256, (3, 3), activation='relu'),
#MaxPooling2D(pool_size=(2, 2)),
#BatchNormalization(),
#Conv2D(512, (3, 3), activation='relu'),
#MaxPooling2D(pool_size=(2, 2)),
#BatchNormalization(),
GlobalAveragePooling2D(),
Dense(512, activation='relu'),
Dropout(0.3), # original 0.2
Dense(5, activation='softmax')
])
# Compiling
model.compile(optimizer=Adam(learning_rate= 1e-3),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Callbacks
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, min_lr = 1e-3)
early_stopping = EarlyStopping(monitor='val_loss', patience=15)
model_checkpoint = ModelCheckpoint('best_model.keras', save_best_only=True, monitor='val_loss')
directory = "retina"
train_gen, val_gen = create_generators(directory)
# Training
history = model.fit(
train_gen,
epochs=40,
shuffle=True,
validation_data=val_gen,
callbacks=[early_stopping, model_checkpoint, reduce_lr]
)
import matplotlib.pyplot as plt
def plot_history(history):
plt.figure(figsize=(12, 4))
# Accuracy
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
# Loss
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plot_history(history)
# Saving the final model
model.save('diabetic_retinopathyfitered.keras')
print("Model Saved")
88/88 ━━━━━━━━━━━━━━━━━━━━ 82s 897ms/step - accuracy: 0.2186 - loss: 1.6166 - val_accuracy: 0.2000 - val_loss: 1.7761 - learning_rate: 0.0010
Epoch 2/40
88/88 ━━━━━━━━━━━━━━━━━━━━ 80s 900ms/step - accuracy: 0.2889 - loss: 1.5480 - val_accuracy: 0.2000 - val_loss: 1.7196 - learning_rate: 0.0010
Epoch 3/40
88/88 ━━━━━━━━━━━━━━━━━━━━ 84s 950ms/step - accuracy: 0.3572 - loss: 1.4783 - val_accuracy: 0.2357 - val_loss: 1.9541 - learning_rate: 0.0010
Epoch 4/40
88/88 ━━━━━━━━━━━━━━━━━━━━ 81s 913ms/step - accuracy: 0.3366 - loss: 1.4794 - val_accuracy: 0.2057 - val_loss: 2.7135 - learning_rate: 0.0010
Epoch 5/40
88/88 ━━━━━━━━━━━━━━━━━━━━ 82s 921ms/step - accuracy: 0.3597 - loss: 1.4316 - val_accuracy: 0.2000 - val_loss: 2.7310 - learning_rate: 0.0010
Epoch 6/40
88/88 ━━━━━━━━━━━━━━━━━━━━ 80s 900ms/step - accuracy: 0.3812 - loss: 1.4046 - val_accuracy: 0.2000 - val_loss: 2.9920 - learning_rate: 0.0010
Epoch 7/40
```