{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1 align=center><font size = 4>Kannada MNIST Identification</font></h1>\n<h1 align=center><font size = 5>Image Recognition</font></h1>","metadata":{}},{"cell_type":"markdown","source":"# Table of Contents\n* [Setup](#setup)\n* [Get the Data](#get_data)\n* [Explore Kannada MNIST](#explore)\n* [Convolutional Neural Networks](#cnn)\n* [Make Predictions](#predictions)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"setup\"></a>\n# Setup","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-31T16:34:36.079572Z","iopub.execute_input":"2021-10-31T16:34:36.080194Z","iopub.status.idle":"2021-10-31T16:34:36.093957Z","shell.execute_reply.started":"2021-10-31T16:34:36.080129Z","shell.execute_reply":"2021-10-31T16:34:36.092998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.set(style=\"darkgrid\")\n\n# to make this notebook's output stable across runs\nnp.random.seed(42)\n\n# To plot figures\n%matplotlib inline\nimport matplotlib\nimport matplotlib.pyplot as plt\nplt.rcParams['axes.labelsize'] = 14\nplt.rcParams['xtick.labelsize'] = 12\nplt.rcParams['ytick.labelsize'] = 12\n\nimport warnings\nwarnings.filterwarnings(action=\"ignore\")","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-10-31T16:34:36.099152Z","iopub.execute_input":"2021-10-31T16:34:36.099398Z","iopub.status.idle":"2021-10-31T16:34:36.490332Z","shell.execute_reply.started":"2021-10-31T16:34:36.099370Z","shell.execute_reply":"2021-10-31T16:34:36.489414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"get_data\"></a>\n# Get the Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/Kannada-MNIST/train.csv\")\nX_train = train_data.drop(['label'], axis=1).values\ny_train = train_data['label'].values","metadata":{"execution":{"iopub.status.busy":"2021-10-31T16:34:36.491898Z","iopub.execute_input":"2021-10-31T16:34:36.492118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape, y_train.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"explore\"></a>\n# Explore Kannada MNIST","metadata":{}},{"cell_type":"code","source":"X_valid, X_train = X_train[:5000], X_train[5000:]\ny_valid, y_train = y_train[:5000], y_train[5000:]\n\nprint(np.unique(y_train))\nprint(np.unique(y_valid))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_digit(pixels, digit):\n    image = pixels.reshape(28, 28)\n    plt.imshow(image, cmap = matplotlib.cm.binary, interpolation=\"nearest\")\n    plt.title('Digit {}'.format(digit))\n    plt.axis(\"off\")\n    \nplot_digit(X_train[6], y_train[6])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_digits(instances, images_per_row=10, **options):\n    size = 28\n    images_per_row = min(len(instances), images_per_row)\n    images = [instance.reshape(size,size) for instance in instances]\n    n_rows = (len(instances) - 1) // images_per_row + 1\n    row_images = []\n    n_empty = n_rows * images_per_row - len(instances)\n    images.append(np.zeros((size, size * n_empty)))\n    for row in range(n_rows):\n        rimages = images[row * images_per_row : (row + 1) * images_per_row]\n        row_images.append(np.concatenate(rimages, axis=1))\n    image = np.concatenate(row_images, axis=0)\n    plt.imshow(image, cmap = matplotlib.cm.binary, **options)\n    plt.axis(\"off\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rnd_idx = np.random.permutation(len(X_train))\n\nplt.figure(figsize=(8, 8))\nexample_images = X_train[rnd_idx[:100]]\nplot_digits(example_images, images_per_row=10)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reshape and scale data\nX_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\nX_train = X_train / 255\n\nX_valid = X_valid.reshape(X_valid.shape[0], 28, 28, 1)\nX_valid = X_valid / 255","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"cnn\"></a>\n# Convolutional Neural Networks","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\ntf.__version__","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(200, activation='relu'),\n    tf.keras.layers.Dense(100, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n])\n\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping_cb = tf.keras.callbacks.EarlyStopping(patience=5)\ncheckpoint_cb = tf.keras.callbacks.ModelCheckpoint(\"cnn.h5\", save_best_only=True)\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\nhistory = model.fit(X_train, y_train, epochs=100, validation_data=(X_valid, y_valid), callbacks=[early_stopping_cb, checkpoint_cb])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend(loc=0)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"predictions\"></a>\n# Make Predictions","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\ntest_data.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"cnn.h5\") # rollback to best model\nmodel.evaluate(X_valid, y_valid)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_data.drop(['id'], axis=1).values\nX_test = X_test.reshape(X_test.shape[0], 28, 28, 1)\nX_test = X_test / 255\n\ny_pred = model.predict(X_test, batch_size=32)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_pred.shape)\ny_pred[:2]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_pred.shape)\ny_pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['label'] = np.argmax(y_pred, axis=1)\ntest_data[['id','label']].head(10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 4\nlabel = test_data['label'].values[idx]\nsome_digit = test_data.drop(['id','label'], axis=1).values[idx].reshape(28, 28)\nplt.imshow(some_digit, cmap = matplotlib.cm.binary, interpolation=\"nearest\")\nplt.title('Digit {}'.format(label))\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[['id','label']].to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}