{"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\nfrom keras.datasets import mnist\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm\nfrom keras.utils import np_utils\nfrom matplotlib import pyplot\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":"2023-03-02T03:37:51.054981Z","iopub.execute_input":"2023-03-02T03:37:51.055394Z","iopub.status.idle":"2023-03-02T03:37:58.230000Z","shell.execute_reply.started":"2023-03-02T03:37:51.055292Z","shell.execute_reply":"2023-03-02T03:37:58.228951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(X_train, y_train), (X_test, y_test) = mnist.load_data()\nX_train = np.vstack((X_train, X_test))\ny_train = np.concatenate([y_train, y_test])\nX_train = X_train.reshape(-1, 28, 28, 1)\nprint(X_train.shape, y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:38:01.062405Z","iopub.execute_input":"2023-03-02T03:38:01.064097Z","iopub.status.idle":"2023-03-02T03:38:01.502993Z","shell.execute_reply.started":"2023-03-02T03:38:01.064054Z","shell.execute_reply":"2023-03-02T03:38:01.501807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv').values\ny_val = train[:,0].astype('int32')\nX_val = train[:,1:].astype('float32')\nX_val = X_val.reshape(-1,28,28,1)\nprint(X_val.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:38:01.669531Z","iopub.execute_input":"2023-03-02T03:38:01.669839Z","iopub.status.idle":"2023-03-02T03:38:04.807910Z","shell.execute_reply.started":"2023-03-02T03:38:01.669811Z","shell.execute_reply":"2023-03-02T03:38:04.806156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_input(img, ax):\n    ax.imshow(img, cmap='gray')\n    width, height = img.shape\n    thresh = img.max()/2.5\n    for x in range(width):\n        for y in range(height):\n            ax.annotate(str(round(img[x][y],2)), xy=(y,x),\n                        horizontalalignment='center',\n                        verticalalignment='center',\n                        color='white' if img[x][y]<thresh else 'black')\n\nfig = plt.figure(figsize = (12,12)) \nax = fig.add_subplot(111)\nvisualize_input(X_train[0].reshape(28,28), ax)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:38:04.810132Z","iopub.execute_input":"2023-03-02T03:38:04.810527Z","iopub.status.idle":"2023-03-02T03:38:09.646906Z","shell.execute_reply.started":"2023-03-02T03:38:04.810488Z","shell.execute_reply":"2023-03-02T03:38:09.645907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.astype('float32')/255\nX_val = X_val.astype('float32')/255\nX_test = X_test.astype('float32')/255 ","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:38:09.648150Z","iopub.execute_input":"2023-03-02T03:38:09.648483Z","iopub.status.idle":"2023-03-02T03:38:09.797143Z","shell.execute_reply.started":"2023-03-02T03:38:09.648455Z","shell.execute_reply":"2023-03-02T03:38:09.795938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Integer-valued labels:')\nprint(y_train[:10])","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:38:09.799599Z","iopub.execute_input":"2023-03-02T03:38:09.800225Z","iopub.status.idle":"2023-03-02T03:38:09.806080Z","shell.execute_reply.started":"2023-03-02T03:38:09.800176Z","shell.execute_reply":"2023-03-02T03:38:09.805078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = np_utils.to_categorical(y_train, 10)\ny_val = np_utils.to_categorical(y_val, 10)\n\n# print first ten (one-hot) training labels\nprint('One-hot Encoding:')\nprint(y_train[:10])","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:38:09.807877Z","iopub.execute_input":"2023-03-02T03:38:09.808647Z","iopub.status.idle":"2023-03-02T03:38:09.822089Z","shell.execute_reply.started":"2023-03-02T03:38:09.808598Z","shell.execute_reply":"2023-03-02T03:38:09.820410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\n\n\nmodel = Sequential([\n    Conv2D(filters=32,kernel_size=(5,5),input_shape=(28,28,1),activation='relu'),\n    MaxPool2D(pool_size=2,strides=1),\n    Conv2D(filters=64,kernel_size=(5,5),activation='relu'),\n    MaxPool2D(pool_size=2,strides=1),\n    Conv2D(filters=128,kernel_size=(5,5),activation='relu'),\n    MaxPool2D(pool_size=2,strides=2),\n    Conv2D(filters=256,kernel_size=(3,3),activation='relu'),\n    MaxPool2D(pool_size=2,strides=1),\n    Conv2D(filters=1024,kernel_size=(3,3),activation='relu'),\n    MaxPool2D(pool_size=2,strides=1),\n    BatchNormalization(),\n    Dropout(0.25),\n    Flatten(),\n    Dense(units=256,activation='relu'),\n    BatchNormalization(),\n    Dropout(0.25),\n    Dense(units=128,activation='relu'),\n    BatchNormalization(),\n    Dropout(0.25),\n    Dense(units=64,activation='relu'),\n    BatchNormalization(),\n    Dropout(0.25),\n    Dense(units=32,activation='relu'),\n    BatchNormalization(),\n    Dropout(0.25),\n    Dense(units=10,activation='softmax'),\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:39:24.835641Z","iopub.execute_input":"2023-03-02T03:39:24.836007Z","iopub.status.idle":"2023-03-02T03:39:28.941178Z","shell.execute_reply.started":"2023-03-02T03:39:24.835977Z","shell.execute_reply":"2023-03-02T03:39:28.940366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:39:30.980280Z","iopub.execute_input":"2023-03-02T03:39:30.980642Z","iopub.status.idle":"2023-03-02T03:39:30.997288Z","shell.execute_reply.started":"2023-03-02T03:39:30.980612Z","shell.execute_reply":"2023-03-02T03:39:30.996154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint   \nfrom keras.callbacks import ReduceLROnPlateau\n\n# reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.3, verbose=1, patience=2, min_lr=0.00000001)\n\nhistory = model.fit(X_train, y_train, batch_size= 100, epochs=10, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:40:57.329743Z","iopub.execute_input":"2023-03-02T03:40:57.330107Z","iopub.status.idle":"2023-03-02T03:44:19.719744Z","shell.execute_reply.started":"2023-03-02T03:40:57.330077Z","shell.execute_reply":"2023-03-02T03:44:19.718362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)\ny_predict = [np.argmax(y_pred[i]) for i in range(len(y_pred))]","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:49:09.018291Z","iopub.execute_input":"2023-03-02T03:49:09.018644Z","iopub.status.idle":"2023-03-02T03:49:10.393276Z","shell.execute_reply.started":"2023-03-02T03:49:09.018613Z","shell.execute_reply":"2023-03-02T03:49:10.392214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame()\n\npredicted_val=[]\nfor i in range(len(y_predict)):\n    predicted_val.append(np.argmax(y_predict[i]))\n\nimgid=[]\nfor i in range(1, len(predicted_val)+1):\n    imgid.append(i)\n    \nsubmission[\"Imageid\"]=imgid\nsubmission[\"Label\"]=predicted_val","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:49:35.823217Z","iopub.execute_input":"2023-03-02T03:49:35.823604Z","iopub.status.idle":"2023-03-02T03:49:35.878395Z","shell.execute_reply.started":"2023-03-02T03:49:35.823570Z","shell.execute_reply":"2023-03-02T03:49:35.877433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:49:37.158183Z","iopub.execute_input":"2023-03-02T03:49:37.158573Z","iopub.status.idle":"2023-03-02T03:49:37.173364Z","shell.execute_reply.started":"2023-03-02T03:49:37.158544Z","shell.execute_reply":"2023-03-02T03:49:37.172272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-02T03:49:39.420513Z","iopub.execute_input":"2023-03-02T03:49:39.420915Z","iopub.status.idle":"2023-03-02T03:49:39.443305Z","shell.execute_reply.started":"2023-03-02T03:49:39.420882Z","shell.execute_reply":"2023-03-02T03:49:39.442369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}