image_output_1.png
from tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping, Callback

from tensorflow.keras.layers import Dropout, Dense, Activation, Flatten, Conv2D, MaxPool2D, BatchNormalization

from tensorflow.keras.models import Sequential

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt

import pandas as pd

train_dir = '/kaggle/input/hackerearth/dataset/Train Images'
test_dir = '/kaggle/input/hackerearth/dataset/Test Images'

train = pd.read_csv('/kaggle/input/hackerearth/dataset/train.csv')

num = len(train['Class'].unique())

datagen = ImageDataGenerator(rescale=1.0 / 255.0, validation_split=0.2)

train_it = datagen.flow_from_dataframe(train, directory=train_dir, x_col='Image', y_col='Class', target_size=(150, 150), class_mode='categorical', batch_size=32, subset='training', shuffle=True)
valid_it = datagen.flow_from_dataframe(train, directory=train_dir, x_col='Image', y_col='Class', target_size=(150, 150), class_mode='categorical', subset='validation', batch_size=32, shuffle=True)

model = Sequential()
model.add(Conv2D(16, kernel_size=(5, 5), activation='relu', input_shape=(150, 150, 3)))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(32, kernel_size=(5, 5), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(num, activation='softmax'))

model.summary()

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

class myCallback(Callback):
    def on_epoch_end(self, epoch, logs={}):
        if logs.get('accuracy') >= 0.98:
            self.model.stop_training = True
callback = myCallback()

hist = model.fit(train_it, epochs=1500, validation_data=valid_it, callbacks=callback)

plt.figure()
plt.plot(hist.history['loss'], label='Train Loss', color='black')
plt.plot(hist.history['val_loss'], label='Validation Loss', color='mediumvioletred', linestyle='dashed', markeredgecolor='purple', markeredgewidth=2)
plt.title('Model Loss', color='darkred', size=13)
plt.legend()
plt.show()