{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\n\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten, BatchNormalization\nfrom keras.utils import np_utils, plot_model\nfrom keras import regularizers, optimizers","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load Data"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/Kannada-MNIST/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\ndf_test = df_test.drop(columns=\"id\")\n\n# Shuffle\ndf_train = df_train.sample(frac=1).reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Normalize"},{"metadata":{"trusted":true},"cell_type":"code","source":"def normalize(X):\n    X = X.astype(\"float\")\n    mean = X.mean()\n    std = X.std() + 10**(-8)\n    X -= mean\n    X /= std\n    return X\n\ndef preprocess(df):\n    X = df.drop(columns=\"label\").to_numpy()\n    # Normalize\n    X = normalize(X)\n    # Reshape to fit in Conv2D\n    X = X.reshape(X.shape[0], 28, 28, 1)\n    # One hot encode labels\n    y = np_utils.to_categorical(df[\"label\"].to_numpy())\n    return X,y\n\nXtrain, ytrain = preprocess(df_train)\n\nXtest = df_test.to_numpy()\nXtest = normalize(Xtest)\nXtest = Xtest.reshape(Xtest.shape[0], 28, 28, 1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualize an image"},{"metadata":{"trusted":true},"cell_type":"code","source":"img = Xtrain[0]\nimg = np.array(img, dtype='float')\npixels = img.reshape((28, 28))\nplt.imshow(pixels, cmap='gray')\nplt.show()\nprint(\"Label: {}\".format(ytrain[0]))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Initialize Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model Architecture\nimg_width, img_height = Xtrain.shape[1], Xtrain[2]\nn_classes = ytrain.shape[1]\nmodel = Sequential()\nmodel.add(Conv2D(64,# Number of filters\n                (3,3), # Kernel Size\n                input_shape=(img_width,img_width,1),\n                activation=\"relu\",\n                padding=\"same\"))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dropout(0.4))\nmodel.add(Dense(50, activation=\"relu\"))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(n_classes, activation=\"softmax\"))\n\n\nmodel.compile(loss='categorical_crossentropy', optimizer=\"adam\", metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualize Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_model(model)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Fit the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(Xtrain, ytrain, validation_split=0.2, batch_size=64, epochs=10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualize Results"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot training & validation accuracy values\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='best')\nplt.show()\n\n# Plot training & validation loss values\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='best')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Predict Testdata"},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict(Xtest)\npredictions = np.argmax(predictions,axis=1)\nsubmission = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')\nsubmission['label'] = predictions\nsubmission.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}