{"cells":[{"metadata":{},"cell_type":"markdown","source":"### 1.Data Pre-Processing"},{"metadata":{"trusted":true},"cell_type":"code","source":"import csv\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# configure based on your local data set path\nkaggle_path='/kaggle/input/Kannada-MNIST'\nlocal_path='/tmp/Kannada-MNIST'\nlocal_path=kaggle_path\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(local_path+'/train.csv')\ntrain, test = train_test_split(df, test_size=0.3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get(data):\n    temp_images = []\n    temp_labels = []\n    for index, row in data.iterrows():\n        temp_labels.append(row[0])\n        image_data = row[1:]\n        image_data_as_array = np.array_split(image_data, 28)\n        temp_images.append(image_data_as_array)\n    images = np.array(temp_images).astype('float')\n    labels = np.array(temp_labels).astype('float')\n    return images, labels\ntraining_images, training_label = get(train)\ntest_images, test_label = get(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_images = np.expand_dims(training_images, axis=3)\ntesting_images = np.expand_dims(test_images, axis=3)\n\ntrain_datagen = ImageDataGenerator(\n    rescale=1. / 255)\n\nvalidation_datagen = ImageDataGenerator(\n    rescale=1. / 255)\n\nprint(training_images.shape)\nprint(testing_images.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_images.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 2. Tensorflow 2.0 CNN Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"class myCallback(tf.keras.callbacks.Callback):\n      def on_epoch_end(self, epoch, logs={}):\n        if(logs.get('accuracy')>0.96):\n            print(\"\\nReached 99.99% accuracy so cancelling training!\")\n            self.model.stop_training = True\n\ncallbacks = myCallback()        \n        \nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(64, (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.Flatten(),\n    tf.keras.layers.Dense(128, activation=tf.nn.relu),\n    tf.keras.layers.Dense(10, activation=tf.nn.softmax)])\n\nmodel.compile(optimizer = tf.optimizers.Adam(),\n              loss = 'sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nhistory = model.fit_generator(train_datagen.flow(training_images, training_label, batch_size=32),\n                              steps_per_epoch=len(training_images) / 32,\n                              epochs=100,\n                              validation_data=validation_datagen.flow(testing_images, test_label, batch_size=100),\n                              validation_steps=len(testing_images) / 32,\n                             callbacks=[callbacks])\n\nmodel.evaluate(testing_images, test_label)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 0. Submission Code\nref: https://www.kaggle.com/chmarco97/kannada-mnist"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Reading the test File to genreate predictions\ntest_data = pd.read_csv(local_path+\"/test.csv\", sep=\",\")\ntest_data.pop(\"id\")\nx_test = np.array(test_data.values).astype('float')\nprint(x_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Preparint the test input for the predictions\nx_test = [i.reshape(28,28, 1) for i in x_test]\npredictions = model.predict([x_test], batch_size=5000)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# save and send to Kaggle \ndef to_csv(predictions):\n    with open(\"submission.csv\", \"w\") as out:\n        out.write(\"id,label\\n\")\n        for i in range(len(predictions)):\n            out.write(str(i)+\",\"+str(np.unravel_index(np.argmax(predictions[i], axis=None), predictions[i].shape)[0])+\"\\n\")\n            \n    return True\n\nto_csv(predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.5"}},"nbformat":4,"nbformat_minor":1}