text_plain_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 numpy as np

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')

test = pd.read_csv('/kaggle/input/hackerearth/dataset/test.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)

test_datagen = ImageDataGenerator(rescale=1.0 / 255.0)
SIZE = (150, 150, 3)
test_generator = test_datagen.flow_from_dataframe(test, directory=test_dir, x_col='Image', y_col=None, class_mode=None, target_size=(150, 150))

preds = model.predict(test_generator)

y_pred = [np.argmax(probas) for probas in preds]

y_p = set(y_pred)
y_p