{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten, BatchNormalization\nfrom keras.optimizers import RMSprop\nfrom keras.preprocessing.image import ImageDataGenerator\nimport seaborn as sns\nfrom keras.callbacks import ReduceLROnPlateau\n\n%matplotlib inline","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-10-02T10:30:50.065568Z","iopub.execute_input":"2021-10-02T10:30:50.065952Z","iopub.status.idle":"2021-10-02T10:30:56.509012Z","shell.execute_reply.started":"2021-10-02T10:30:50.065916Z","shell.execute_reply":"2021-10-02T10:30:56.508041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:30:56.511222Z","iopub.execute_input":"2021-10-02T10:30:56.511538Z","iopub.status.idle":"2021-10-02T10:31:02.425902Z","shell.execute_reply.started":"2021-10-02T10:30:56.511508Z","shell.execute_reply":"2021-10-02T10:31:02.424978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = df_train['label']\nX_train = df_train.drop('label', axis = 1)\nX_test = df_test","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:02.427321Z","iopub.execute_input":"2021-10-02T10:31:02.427768Z","iopub.status.idle":"2021-10-02T10:31:02.551270Z","shell.execute_reply.started":"2021-10-02T10:31:02.427723Z","shell.execute_reply":"2021-10-02T10:31:02.550275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = sns.countplot(y_train)\n\ny_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:02.552558Z","iopub.execute_input":"2021-10-02T10:31:02.552940Z","iopub.status.idle":"2021-10-02T10:31:02.769210Z","shell.execute_reply.started":"2021-10-02T10:31:02.552908Z","shell.execute_reply":"2021-10-02T10:31:02.768195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train/255.0\nX_test = X_test/255.0","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:02.772062Z","iopub.execute_input":"2021-10-02T10:31:02.772369Z","iopub.status.idle":"2021-10-02T10:31:02.899379Z","shell.execute_reply.started":"2021-10-02T10:31:02.772340Z","shell.execute_reply":"2021-10-02T10:31:02.898351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1, 28,28,1)\nX_test = X_test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:02.900884Z","iopub.execute_input":"2021-10-02T10:31:02.901154Z","iopub.status.idle":"2021-10-02T10:31:02.906578Z","shell.execute_reply.started":"2021-10-02T10:31:02.901128Z","shell.execute_reply":"2021-10-02T10:31:02.905572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:02.907928Z","iopub.execute_input":"2021-10-02T10:31:02.908231Z","iopub.status.idle":"2021-10-02T10:31:02.918519Z","shell.execute_reply.started":"2021-10-02T10:31:02.908201Z","shell.execute_reply":"2021-10-02T10:31:02.917400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state = 10)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:02.919814Z","iopub.execute_input":"2021-10-02T10:31:02.920096Z","iopub.status.idle":"2021-10-02T10:31:03.339311Z","shell.execute_reply.started":"2021-10-02T10:31:02.920069Z","shell.execute_reply":"2021-10-02T10:31:03.338526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = ImageDataGenerator(zoom_range=0.1, \n                               width_shift_range=0.1, \n                               height_shift_range=0.1,\n                               rotation_range=10,\n                               fill_mode='nearest'\n                              )\n\ntrain_gen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:03.340429Z","iopub.execute_input":"2021-10-02T10:31:03.340853Z","iopub.status.idle":"2021-10-02T10:31:03.440519Z","shell.execute_reply.started":"2021-10-02T10:31:03.340823Z","shell.execute_reply":"2021-10-02T10:31:03.439742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3), activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3), activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\n\n# model.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 128, kernel_size = (3,3), activation ='relu'))\nmodel.add(Conv2D(filters = 128, kernel_size = (3,3), activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\n\n# model.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters=256, kernel_size = (3,3), activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation = \"relu\"))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(10, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:03.441557Z","iopub.execute_input":"2021-10-02T10:31:03.442050Z","iopub.status.idle":"2021-10-02T10:31:03.721203Z","shell.execute_reply.started":"2021-10-02T10:31:03.442009Z","shell.execute_reply":"2021-10-02T10:31:03.720236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.0001)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:03.722709Z","iopub.execute_input":"2021-10-02T10:31:03.723116Z","iopub.status.idle":"2021-10-02T10:31:03.728318Z","shell.execute_reply.started":"2021-10-02T10:31:03.723078Z","shell.execute_reply":"2021-10-02T10:31:03.727235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:03.729852Z","iopub.execute_input":"2021-10-02T10:31:03.730264Z","iopub.status.idle":"2021-10-02T10:31:03.778486Z","shell.execute_reply.started":"2021-10-02T10:31:03.730222Z","shell.execute_reply":"2021-10-02T10:31:03.777699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nhistory = model.fit_generator(train_gen.flow(X_train, y_train, batch_size=batch_size),\n          steps_per_epoch=X_train.shape[0]//batch_size, epochs=40, validation_data=(X_val, y_val), \n                callbacks=[learning_rate_reduction])","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:31:03.779590Z","iopub.execute_input":"2021-10-02T10:31:03.780095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history.get('loss'), label = \"Training Loss\")\nplt.plot(history.history.get('val_loss'), label = \"Val Loss\")\nplt.legend()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history.get('accuracy'), label = \"Training Accuracy\")\nplt.plot(history.history.get('val_accuracy'), label = \"Val Accuracy\")\nplt.legend()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(X_test)\n\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results","metadata":{"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(\"cnn_mnist_datagen.csv\",index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}