{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":16017,"databundleVersionId":708189,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\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 read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:13.216753Z","iopub.execute_input":"2024-10-20T16:06:13.217175Z","iopub.status.idle":"2024-10-20T16:06:13.226750Z","shell.execute_reply.started":"2024-10-20T16:06:13.217136Z","shell.execute_reply":"2024-10-20T16:06:13.225649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom keras import layers,Input,Model,callbacks\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:13.228229Z","iopub.execute_input":"2024-10-20T16:06:13.228491Z","iopub.status.idle":"2024-10-20T16:06:13.234008Z","shell.execute_reply.started":"2024-10-20T16:06:13.228461Z","shell.execute_reply":"2024-10-20T16:06:13.233158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\nsample_submission = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')\nextra_samples = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:13.258801Z","iopub.execute_input":"2024-10-20T16:06:13.259124Z","iopub.status.idle":"2024-10-20T16:06:17.060571Z","shell.execute_reply.started":"2024-10-20T16:06:13.259091Z","shell.execute_reply":"2024-10-20T16:06:17.059492Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Prepare Data** ","metadata":{}},{"cell_type":"code","source":"# concatenating train and extra_sample to make more general data\nall = pd.concat([train,extra_samples],ignore_index=False)\nall = tf.random.shuffle(all,seed=313)\nimages = tf.cast(all[:,1:],tf.float32)\nlabels = tf.cast(all[:,0],tf.int8)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:17.062240Z","iopub.execute_input":"2024-10-20T16:06:17.062531Z","iopub.status.idle":"2024-10-20T16:06:18.661645Z","shell.execute_reply.started":"2024-10-20T16:06:17.062499Z","shell.execute_reply":"2024-10-20T16:06:18.660831Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# reshape data \ntrain_shape = (len(images),28,28,1)\ntest_shape = (len(test),28,28,1)\nimages = tf.reshape(images,train_shape)\n\n# prepare test data\ntest = test.drop('id',axis=1)\ntest = tf.constant(test,dtype=tf.float32)\ntest = tf.reshape(test,test_shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.662755Z","iopub.execute_input":"2024-10-20T16:06:18.663065Z","iopub.status.idle":"2024-10-20T16:06:18.690308Z","shell.execute_reply.started":"2024-10-20T16:06:18.663032Z","shell.execute_reply":"2024-10-20T16:06:18.689527Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.692195Z","iopub.execute_input":"2024-10-20T16:06:18.692502Z","iopub.status.idle":"2024-10-20T16:06:18.698780Z","shell.execute_reply.started":"2024-10-20T16:06:18.692469Z","shell.execute_reply":"2024-10-20T16:06:18.697880Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.699835Z","iopub.execute_input":"2024-10-20T16:06:18.700125Z","iopub.status.idle":"2024-10-20T16:06:18.709376Z","shell.execute_reply.started":"2024-10-20T16:06:18.700086Z","shell.execute_reply":"2024-10-20T16:06:18.708613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.710769Z","iopub.execute_input":"2024-10-20T16:06:18.711344Z","iopub.status.idle":"2024-10-20T16:06:18.721000Z","shell.execute_reply.started":"2024-10-20T16:06:18.711300Z","shell.execute_reply":"2024-10-20T16:06:18.720136Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Design Model**","metadata":{}},{"cell_type":"code","source":"def get_model():\n    input = Input((28,28,1))\n    x = layers.Rescaling(1/255.)(input)\n    x = layers.Conv2D(32,3,padding='same',activation='relu')(x)\n    x = layers.MaxPool2D()(x)\n    x = layers.Conv2D(64,3,padding='same',activation='relu')(x)\n    x = layers.MaxPool2D()(x)\n    x = layers.Conv2D(128,3,padding='same',activation='relu')(x)\n    x = layers.Dropout(0.5)(x)\n    x = layers.MaxPool2D()(x)\n    x = layers.Flatten()(x)\n    x = layers.Dense(256,activation='relu')(x)\n    x = layers.Dropout(0.5)(x)\n    output = layers.Dense(10,activation='softmax')(x)\n\n    return Model(input,output)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.722188Z","iopub.execute_input":"2024-10-20T16:06:18.722488Z","iopub.status.idle":"2024-10-20T16:06:18.730233Z","shell.execute_reply.started":"2024-10-20T16:06:18.722443Z","shell.execute_reply":"2024-10-20T16:06:18.729400Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_callbacks():\n    early_stopping = callbacks.EarlyStopping(\n        min_delta=0.01,\n        monitor='loss',\n        patience=5,\n        restore_best_weights=True)\n\n    # check_point = callbacks.ModelCheckpoint(\n    #     filepath='/best.keras',\n    #     monitor='val_loss',\n    #     save_best_only=True)\n    return [early_stopping]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.731414Z","iopub.execute_input":"2024-10-20T16:06:18.731865Z","iopub.status.idle":"2024-10-20T16:06:18.740259Z","shell.execute_reply.started":"2024-10-20T16:06:18.731822Z","shell.execute_reply":"2024-10-20T16:06:18.739369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_step(X,y):\n    model = get_model()\n    model.compile(\n        optimizer='rmsprop',\n        loss = 'sparse_categorical_crossentropy',\n        metrics = ['sparse_categorical_accuracy'])\n\n    res = model.fit(\n        X,y,\n        batch_size=64,\n        epochs=30,\n        callbacks = get_callbacks())\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.741233Z","iopub.execute_input":"2024-10-20T16:06:18.741487Z","iopub.status.idle":"2024-10-20T16:06:18.749256Z","shell.execute_reply.started":"2024-10-20T16:06:18.741458Z","shell.execute_reply":"2024-10-20T16:06:18.748442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def kfold_evaluate(k,images,labels):\n    fold_size = len(images)//k\n    val_accuracies = []\n    val_losses = []\n    for iter in range(k):\n        test_begin = iter*fold_size\n        test_end = iter*fold_size + fold_size\n        X_test = images[test_begin:test_end]\n        y_test = labels[test_begin:test_end]\n        X_train = tf.concat([images[:test_begin],images[test_end:]],axis=0)\n        y_train = tf.concat([labels[:test_begin],labels[test_end:]],axis=0)\n\n        result = train_step(X_train,y_train,X_test,y_test)\n        val_accuracies.append(result['val_sparse_categorical_accuracy'].max())\n        val_losses.append(result['val_loss'].min())\n\n    return val_accuracies,val_losses","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.752187Z","iopub.execute_input":"2024-10-20T16:06:18.752469Z","iopub.status.idle":"2024-10-20T16:06:18.762685Z","shell.execute_reply.started":"2024-10-20T16:06:18.752438Z","shell.execute_reply":"2024-10-20T16:06:18.761881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# acc,loss = kfold_evaluate(4,images,labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.763701Z","iopub.execute_input":"2024-10-20T16:06:18.763990Z","iopub.status.idle":"2024-10-20T16:06:18.771474Z","shell.execute_reply.started":"2024-10-20T16:06:18.763960Z","shell.execute_reply":"2024-10-20T16:06:18.770615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# fit entire data\nmodel = train_step(images,labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:06:18.772551Z","iopub.execute_input":"2024-10-20T16:06:18.772868Z","iopub.status.idle":"2024-10-20T16:07:09.623401Z","shell.execute_reply.started":"2024-10-20T16:06:18.772835Z","shell.execute_reply":"2024-10-20T16:07:09.622620Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# **Disply some of predictions**","metadata":{}},{"cell_type":"code","source":"def index_of_max(pred):\n    results = []\n    for sample in pred:\n        for i,v in enumerate(sample):\n            max_prop = np.max(sample)\n            if v == max_prop:\n                results.append(i)\n    return results","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:07:09.624656Z","iopub.execute_input":"2024-10-20T16:07:09.624965Z","iopub.status.idle":"2024-10-20T16:07:09.630339Z","shell.execute_reply.started":"2024-10-20T16:07:09.624931Z","shell.execute_reply":"2024-10-20T16:07:09.629364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def disply(images,predicted):\n    results = index_of_max(predicted)\n    columns = 3\n    rows = int(len(images)/columns)\n    fig,ax = plt.subplots(rows,columns,figsize=(10,10))\n\n    counter=0\n    for row in range(rows):\n        for column in range(columns):\n            ax[row,column].imshow(images[counter])\n            ax[row,column].title.set_text(f'Predicted by model: {results[counter]}')\n            fig.tight_layout()\n            counter+=1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:07:09.631556Z","iopub.execute_input":"2024-10-20T16:07:09.632210Z","iopub.status.idle":"2024-10-20T16:07:09.641246Z","shell.execute_reply.started":"2024-10-20T16:07:09.632166Z","shell.execute_reply":"2024-10-20T16:07:09.640389Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"disply(images[:16],model.predict(images[:16]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:07:09.642470Z","iopub.execute_input":"2024-10-20T16:07:09.643217Z","iopub.status.idle":"2024-10-20T16:07:16.829778Z","shell.execute_reply.started":"2024-10-20T16:07:09.643182Z","shell.execute_reply":"2024-10-20T16:07:16.828546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result = index_of_max(model.predict(test))\nsample_submission['label'] = result","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:07:16.831258Z","iopub.execute_input":"2024-10-20T16:07:16.831580Z","iopub.status.idle":"2024-10-20T16:07:17.889294Z","shell.execute_reply.started":"2024-10-20T16:07:16.831545Z","shell.execute_reply":"2024-10-20T16:07:17.888235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission.to_csv('/kaggle/working/submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-20T16:07:17.890667Z","iopub.execute_input":"2024-10-20T16:07:17.891020Z","iopub.status.idle":"2024-10-20T16:07:17.905679Z","shell.execute_reply.started":"2024-10-20T16:07:17.890983Z","shell.execute_reply":"2024-10-20T16:07:17.904922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}