{"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 numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom tensorflow import keras\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, BatchNormalization, MaxPooling2D, Flatten, Dense, Dropout, InputLayer\nfrom keras.preprocessing.image import ImageDataGenerator\n\nimport tensorflow as tf\nfrom tensorflow.keras.layers.experimental import preprocessing\nfrom tensorflow.keras.callbacks import EarlyStopping\n\nimport sklearn\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:22.439506Z","iopub.execute_input":"2023-10-17T09:39:22.439873Z","iopub.status.idle":"2023-10-17T09:39:30.699176Z","shell.execute_reply.started":"2023-10-17T09:39:22.439831Z","shell.execute_reply":"2023-10-17T09:39:30.698157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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# 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-10-17T09:39:30.701217Z","iopub.execute_input":"2023-10-17T09:39:30.702254Z","iopub.status.idle":"2023-10-17T09:39:30.714893Z","shell.execute_reply.started":"2023-10-17T09:39:30.702221Z","shell.execute_reply":"2023-10-17T09:39:30.713787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"data_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndata_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:30.716613Z","iopub.execute_input":"2023-10-17T09:39:30.717632Z","iopub.status.idle":"2023-10-17T09:39:35.046192Z","shell.execute_reply.started":"2023-10-17T09:39:30.717561Z","shell.execute_reply":"2023-10-17T09:39:35.045297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = data_train.label\nX_train = data_train.drop(columns='label')\nX_test = data_test","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:35.047593Z","iopub.execute_input":"2023-10-17T09:39:35.047924Z","iopub.status.idle":"2023-10-17T09:39:35.139899Z","shell.execute_reply.started":"2023-10-17T09:39:35.047891Z","shell.execute_reply":"2023-10-17T09:39:35.139004Z"},"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":"2023-10-17T09:39:35.142557Z","iopub.execute_input":"2023-10-17T09:39:35.143373Z","iopub.status.idle":"2023-10-17T09:39:35.328740Z","shell.execute_reply.started":"2023-10-17T09:39:35.143339Z","shell.execute_reply":"2023-10-17T09:39:35.327830Z"},"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":"2023-10-17T09:39:35.330178Z","iopub.execute_input":"2023-10-17T09:39:35.330493Z","iopub.status.idle":"2023-10-17T09:39:35.337906Z","shell.execute_reply.started":"2023-10-17T09:39:35.330459Z","shell.execute_reply":"2023-10-17T09:39:35.334497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=data_train,x='label')","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:35.339139Z","iopub.execute_input":"2023-10-17T09:39:35.339867Z","iopub.status.idle":"2023-10-17T09:39:35.761845Z","shell.execute_reply.started":"2023-10-17T09:39:35.339835Z","shell.execute_reply":"2023-10-17T09:39:35.761021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f,ax = plt.subplots(3,4, figsize=(25,15))\nfor i,ax in zip(range(12), ax.reshape(-1)):\n    ax.imshow(X_train[i])\n    ax.set_title(y_train[i])\n    ax.axis(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:35.763174Z","iopub.execute_input":"2023-10-17T09:39:35.763671Z","iopub.status.idle":"2023-10-17T09:39:36.584290Z","shell.execute_reply.started":"2023-10-17T09:39:35.763640Z","shell.execute_reply":"2023-10-17T09:39:36.583420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Process Data","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.3, random_state=10)\n\ny_train = to_categorical(y_train)\n\ny_val = to_categorical(y_val)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:36.585755Z","iopub.execute_input":"2023-10-17T09:39:36.586748Z","iopub.status.idle":"2023-10-17T09:39:36.963786Z","shell.execute_reply.started":"2023-10-17T09:39:36.586714Z","shell.execute_reply":"2023-10-17T09:39:36.962850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Augementation","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.2, # Randomly zoom image \n        width_shift_range=0.2,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.2,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:36.965198Z","iopub.execute_input":"2023-10-17T09:39:36.965756Z","iopub.status.idle":"2023-10-17T09:39:37.042927Z","shell.execute_reply.started":"2023-10-17T09:39:36.965719Z","shell.execute_reply":"2023-10-17T09:39:37.042017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"epochs = 100\nbatch_size = 64\noptimizer = keras.optimizers.Adam(learning_rate=0.001)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:37.044460Z","iopub.execute_input":"2023-10-17T09:39:37.044802Z","iopub.status.idle":"2023-10-17T09:39:40.168918Z","shell.execute_reply.started":"2023-10-17T09:39:37.044769Z","shell.execute_reply":"2023-10-17T09:39:40.168003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, (3,3), padding = 'Same',input_shape=([28, 28, 1]), activation='relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(64, (3,3), padding = 'Same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(128, (3,3), padding = 'Same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(256, (3,3), padding = 'Same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(units=512, activation='relu'))\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(units=10, activation='softmax'))\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:40.170409Z","iopub.execute_input":"2023-10-17T09:39:40.170926Z","iopub.status.idle":"2023-10-17T09:39:40.375057Z","shell.execute_reply.started":"2023-10-17T09:39:40.170892Z","shell.execute_reply":"2023-10-17T09:39:40.374396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(datagen.flow(x=X_train, y=y_train, batch_size=batch_size),\n          batch_size=64, \n          validation_data=(X_val, y_val),\n          epochs=epochs,\n          steps_per_epoch=len(X_train)//batch_size,\n          verbose=0,\n         )","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:39:40.376028Z","iopub.execute_input":"2023-10-17T09:39:40.376349Z","iopub.status.idle":"2023-10-17T09:55:24.316140Z","shell.execute_reply.started":"2023-10-17T09:39:40.376317Z","shell.execute_reply":"2023-10-17T09:55:24.315197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert the training history to a dataframe\nhistory_df = pd.DataFrame(history.history)\n# use Pandas native plot method\nhistory_df.plot()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:55:24.319849Z","iopub.execute_input":"2023-10-17T09:55:24.320220Z","iopub.status.idle":"2023-10-17T09:55:24.607034Z","shell.execute_reply.started":"2023-10-17T09:55:24.320194Z","shell.execute_reply":"2023-10-17T09:55:24.605065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('model_digit_recognizer.h5')","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:55:24.610407Z","iopub.execute_input":"2023-10-17T09:55:24.611108Z","iopub.status.idle":"2023-10-17T09:55:24.714908Z","shell.execute_reply.started":"2023-10-17T09:55:24.611066Z","shell.execute_reply":"2023-10-17T09:55:24.714008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(X_test)\nsub_labells = [np.argmax(i) for i in y_pred]","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:55:24.716372Z","iopub.execute_input":"2023-10-17T09:55:24.716699Z","iopub.status.idle":"2023-10-17T09:55:27.132537Z","shell.execute_reply.started":"2023-10-17T09:55:24.716654Z","shell.execute_reply":"2023-10-17T09:55:27.131586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f,ax = plt.subplots(3,5, figsize=(25,15))\nfor i,ax in zip(range(15), ax.reshape(-1)):\n    ax.imshow(X_test[i])\n    ax.set_title(f\"{sub_labells[i]}\")\n    ax.axis(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:55:27.133804Z","iopub.execute_input":"2023-10-17T09:55:27.134413Z","iopub.status.idle":"2023-10-17T09:55:28.398850Z","shell.execute_reply.started":"2023-10-17T09:55:27.134377Z","shell.execute_reply":"2023-10-17T09:55:28.397827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsub.Label = sub_labells\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:55:28.400238Z","iopub.execute_input":"2023-10-17T09:55:28.401199Z","iopub.status.idle":"2023-10-17T09:55:28.523473Z","shell.execute_reply.started":"2023-10-17T09:55:28.401148Z","shell.execute_reply":"2023-10-17T09:55:28.522476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T09:55:28.524872Z","iopub.execute_input":"2023-10-17T09:55:28.525211Z","iopub.status.idle":"2023-10-17T09:55:28.538310Z","shell.execute_reply.started":"2023-10-17T09:55:28.525178Z","shell.execute_reply":"2023-10-17T09:55:28.537261Z"},"trusted":true},"execution_count":null,"outputs":[]}]}