{"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":"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","execution":{"iopub.status.busy":"2022-02-12T02:56:33.974717Z","iopub.execute_input":"2022-02-12T02:56:33.97497Z","iopub.status.idle":"2022-02-12T02:56:33.9875Z","shell.execute_reply.started":"2022-02-12T02:56:33.974941Z","shell.execute_reply":"2022-02-12T02:56:33.986637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfrom IPython.display import Image\nimport matplotlib.pyplot as plt\nimport os\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:36.045946Z","iopub.execute_input":"2022-02-12T02:56:36.046765Z","iopub.status.idle":"2022-02-12T02:56:36.05357Z","shell.execute_reply.started":"2022-02-12T02:56:36.046719Z","shell.execute_reply":"2022-02-12T02:56:36.052578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"../input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:37.824301Z","iopub.execute_input":"2022-02-12T02:56:37.825048Z","iopub.status.idle":"2022-02-12T02:56:40.076946Z","shell.execute_reply.started":"2022-02-12T02:56:37.824981Z","shell.execute_reply":"2022-02-12T02:56:40.076191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = data.label","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:40.078394Z","iopub.execute_input":"2022-02-12T02:56:40.078624Z","iopub.status.idle":"2022-02-12T02:56:40.084609Z","shell.execute_reply.started":"2022-02-12T02:56:40.078593Z","shell.execute_reply":"2022-02-12T02:56:40.083925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Figure size\nplt.figure(figsize=(9,9))\nplt.suptitle('Training set images', fontsize=20)\n\n# Subplot \nfor i in range(9,18):\n    img = np.asarray(data.iloc[i,1:].values.reshape((28,28))/255);\n    ax=plt.subplot(3, 3, i+1-9)\n    ax.grid(False)\n    ax.get_xaxis().set_visible(False)\n    ax.get_yaxis().set_visible(False)\n    ax.title.set_text(f'{data.iloc[i,0]}')\n    plt.imshow(img, cmap='gray')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:41.348177Z","iopub.execute_input":"2022-02-12T02:56:41.348714Z","iopub.status.idle":"2022-02-12T02:56:41.770486Z","shell.execute_reply.started":"2022-02-12T02:56:41.348678Z","shell.execute_reply":"2022-02-12T02:56:41.769777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.hist(x='label', data=data,rwidth = 0.75)\nplt.title('Distribution of digits in dataset')","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:45.001487Z","iopub.execute_input":"2022-02-12T02:56:45.001747Z","iopub.status.idle":"2022-02-12T02:56:45.198847Z","shell.execute_reply.started":"2022-02-12T02:56:45.001718Z","shell.execute_reply":"2022-02-12T02:56:45.198209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Labels\ny=data.label\n\n# Scale features to be in [0,1]\nX=data.drop('label', axis=1)/255\n\n# Reshape (-1 means unspecified)\nX = X.values.reshape(-1, 28, 28, 1)\n\n# One-hot encode target\ny=pd.get_dummies(y)","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:46.186508Z","iopub.execute_input":"2022-02-12T02:56:46.187162Z","iopub.status.idle":"2022-02-12T02:56:46.378527Z","shell.execute_reply.started":"2022-02-12T02:56:46.187126Z","shell.execute_reply":"2022-02-12T02:56:46.377714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a validation set\nX_train, X_valid, y_train, y_valid = train_test_split(X,y,train_size=0.8, test_size=0.2,stratify=y, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:47.428645Z","iopub.execute_input":"2022-02-12T02:56:47.432056Z","iopub.status.idle":"2022-02-12T02:56:48.541553Z","shell.execute_reply.started":"2022-02-12T02:56:47.43201Z","shell.execute_reply":"2022-02-12T02:56:48.54081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:50.026312Z","iopub.execute_input":"2022-02-12T02:56:50.026565Z","iopub.status.idle":"2022-02-12T02:56:50.031878Z","shell.execute_reply.started":"2022-02-12T02:56:50.026538Z","shell.execute_reply":"2022-02-12T02:56:50.03119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model_gen():\n    model=tf.keras.Sequential()\n    ################################### FEATURE EXTRACTOR \n    model.add(layers.Conv2D(32,2,activation='relu',input_shape=(28, 28, 1)))\n    model.add(layers.Conv2D(32,4,activation='relu'))\n    model.add(layers.MaxPooling2D(2))\n    model.add(layers.Dropout(0.3))\n    model.add(layers.Conv2D(64,2,activation='relu'))\n    model.add(layers.Conv2D(64,4,activation='relu'))\n    model.add(layers.MaxPooling2D(2))\n    model.add(layers.Dropout(0.3))\n    model.add(layers.Conv2D(84,2,activation='relu'))\n    model.add(layers.Dropout(0.3))\n    #################################### Classifier \n    model.add(layers.Flatten())\n    model.add(layers.Dense(32,activation='relu'))\n    model.add(layers.Dense(32,activation='relu'))\n    model.add(layers.Dense(32,activation='relu'))\n    model.add(layers.Dense(10, activation='softmax',name='Garbage'))\n    # optimizer=> HOW MY MODEL WILL LEARN \n    # loss     => HOW TO CALCULATE MY MODEL ERROR \n    # metrics  => HOW TO EVALUATE MY MODEL RESULTS \n    model.compile(optimizer='Adamax',loss=['binary_crossentropy'],\n                  metrics=['categorical_accuracy'])\n    tf.keras.utils.plot_model(model, 'model.png',show_shapes=True)\n    \n    return model\n\nmodel=model_gen()\nImage('model.png')","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:52.439798Z","iopub.execute_input":"2022-02-12T02:56:52.440622Z","iopub.status.idle":"2022-02-12T02:56:52.745736Z","shell.execute_reply.started":"2022-02-12T02:56:52.440572Z","shell.execute_reply":"2022-02-12T02:56:52.745001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:56:58.132764Z","iopub.execute_input":"2022-02-12T02:56:58.133056Z","iopub.status.idle":"2022-02-12T02:56:58.148367Z","shell.execute_reply.started":"2022-02-12T02:56:58.133023Z","shell.execute_reply":"2022-02-12T02:56:58.146103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def scheduler(epoch, lr):\n    if epoch < 10:\n        return lr\n    else:\n        return lr * tf.math.exp(-0.1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n    tf.keras.callbacks.EarlyStopping(patience=75, monitor='val_loss',restore_best_weights=True) ]","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:57:00.391625Z","iopub.execute_input":"2022-02-12T02:57:00.392156Z","iopub.status.idle":"2022-02-12T02:57:00.396503Z","shell.execute_reply.started":"2022-02-12T02:57:00.392118Z","shell.execute_reply":"2022-02-12T02:57:00.395494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(X_train, y_train, epochs=90,batch_size=64,\n          validation_data=(X_valid,y_valid),callbacks=callbacks, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-12T02:58:23.605545Z","iopub.execute_input":"2022-02-12T02:58:23.605806Z","iopub.status.idle":"2022-02-12T03:02:45.684266Z","shell.execute_reply.started":"2022-02-12T02:58:23.605777Z","shell.execute_reply":"2022-02-12T03:02:45.683477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['categorical_accuracy'])\nplt.plot(history.history['val_categorical_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-12T03:03:03.158728Z","iopub.execute_input":"2022-02-12T03:03:03.159339Z","iopub.status.idle":"2022-02-12T03:03:03.354903Z","shell.execute_reply.started":"2022-02-12T03:03:03.159303Z","shell.execute_reply":"2022-02-12T03:03:03.354189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-12T03:03:16.246262Z","iopub.execute_input":"2022-02-12T03:03:16.246636Z","iopub.status.idle":"2022-02-12T03:03:18.102894Z","shell.execute_reply.started":"2022-02-12T03:03:16.246602Z","shell.execute_reply":"2022-02-12T03:03:18.102068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test /= 255","metadata":{"execution":{"iopub.status.busy":"2022-02-12T03:07:02.52558Z","iopub.execute_input":"2022-02-12T03:07:02.525851Z","iopub.status.idle":"2022-02-12T03:07:02.604751Z","shell.execute_reply.started":"2022-02-12T03:07:02.525821Z","shell.execute_reply":"2022-02-12T03:07:02.60402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-12T03:07:45.00706Z","iopub.execute_input":"2022-02-12T03:07:45.007728Z","iopub.status.idle":"2022-02-12T03:07:45.040075Z","shell.execute_reply.started":"2022-02-12T03:07:45.007692Z","shell.execute_reply":"2022-02-12T03:07:45.039231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-02-12T03:08:15.122129Z","iopub.execute_input":"2022-02-12T03:08:15.122648Z","iopub.status.idle":"2022-02-12T03:08:15.126814Z","shell.execute_reply.started":"2022-02-12T03:08:15.122611Z","shell.execute_reply":"2022-02-12T03:08:15.126037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2022-02-12T03:08:17.573689Z","iopub.execute_input":"2022-02-12T03:08:17.573967Z","iopub.status.idle":"2022-02-12T03:08:20.349815Z","shell.execute_reply.started":"2022-02-12T03:08:17.573935Z","shell.execute_reply":"2022-02-12T03:08:20.349058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-12T03:08:45.675413Z","iopub.execute_input":"2022-02-12T03:08:45.675698Z","iopub.status.idle":"2022-02-12T03:08:45.72531Z","shell.execute_reply.started":"2022-02-12T03:08:45.675667Z","shell.execute_reply":"2022-02-12T03:08:45.724546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}