text_plain_output_1.png
from keras.callbacks import EarlyStopping, ReduceLROnPlateau,ModelCheckpoint

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

from tensorflow.keras.models import Sequential

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.utils import to_categorical

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')

test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')

images = train.iloc[:, 1:].values.reshape(train.shape[0], 28, 28).astype('float32') / 255.0
images = images.reshape(train.shape[0], 28, 28, 1)
images_test = test.iloc[:, 1:].values.reshape(test.shape[0], 28, 28).astype('float32') / 255.0
images_test = images_test.reshape(test.shape[0], 28, 28, 1)
label = train.iloc[:, 0].astype('int').values
ids_test = test.iloc[:, 0].values

(images.shape, images_test.shape)

from tensorflow.keras.utils import to_categorical
label_train = to_categorical(label_train)

label_validation = to_categorical(label_validation)

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1), padding='same', activation='relu'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-05, gamma_initializer='uniform'))
model.add(MaxPooling2D())
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-05, gamma_initializer='uniform'))
model.add(MaxPooling2D())
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-05, gamma_initializer='uniform'))
model.add(MaxPooling2D())
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='sigmoid'))
model.summary()

from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint
earlystop = EarlyStopping(patience=10)
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=2, verbose=1, factor=0.5, min_lr=1e-05)
callbacks = [earlystop, learning_rate_reduction, ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]

from tensorflow.keras.optimizers import Adam
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy'])

model.fit(images_train, label_train, validation_data=(images_validation, label_validation), batch_size=64, epochs=50, callbacks=callbacks)

test_prediction = model.predict(images_test)

test_labels = []
for i in range(len(test_prediction)):
    test_labels.append(np.argmax(test_prediction[i]))

np.argmax(model.predict(images_test[2].reshape(1, 28, 28, 1)))