{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport numpy as np\nimport pandas as pd\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-07T10:37:05.120293Z","iopub.execute_input":"2023-09-07T10:37:05.120673Z","iopub.status.idle":"2023-09-07T10:37:05.125916Z","shell.execute_reply.started":"2023-09-07T10:37:05.120624Z","shell.execute_reply":"2023-09-07T10:37:05.124732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/digit-recognizer/'\nimage_shape = (28, 28, 1)\nn_class = 10\nbatch_size = 128\nepochs = 30","metadata":{"execution":{"iopub.status.busy":"2023-09-07T10:37:05.143885Z","iopub.execute_input":"2023-09-07T10:37:05.144435Z","iopub.status.idle":"2023-09-07T10:37:05.149875Z","shell.execute_reply.started":"2023-09-07T10:37:05.144399Z","shell.execute_reply":"2023-09-07T10:37:05.148651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading the Data","metadata":{}},{"cell_type":"code","source":"train_csv = pd.read_csv(path + 'train.csv')\ntrain_n_images = train_csv.shape[0] \n\ntest_csv = pd.read_csv(path + 'test.csv')\ntest_n_images = test_csv.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-09-07T10:37:05.151950Z","iopub.execute_input":"2023-09-07T10:37:05.152287Z","iopub.status.idle":"2023-09-07T10:37:09.685819Z","shell.execute_reply.started":"2023-09-07T10:37:05.152255Z","shell.execute_reply":"2023-09-07T10:37:09.684847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = train_csv['label'];\ntrain_y = keras.utils.to_categorical(train_y, n_class)\n\n\ntrain_x = train_csv.drop(['label'], axis=1);\ntrain_x = tf.reshape(tf.convert_to_tensor(train_x), (train_n_images, image_shape[0], image_shape[1], image_shape[2]))\n\ntest_x = tf.reshape(tf.convert_to_tensor(test_csv), (test_n_images, image_shape[0], image_shape[1], image_shape[2]))","metadata":{"execution":{"iopub.status.busy":"2023-09-07T10:37:09.689864Z","iopub.execute_input":"2023-09-07T10:37:09.690173Z","iopub.status.idle":"2023-09-07T10:37:10.425079Z","shell.execute_reply.started":"2023-09-07T10:37:09.690144Z","shell.execute_reply":"2023-09-07T10:37:10.424071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-07T10:37:10.426597Z","iopub.execute_input":"2023-09-07T10:37:10.427193Z","iopub.status.idle":"2023-09-07T10:37:10.433207Z","shell.execute_reply.started":"2023-09-07T10:37:10.427158Z","shell.execute_reply":"2023-09-07T10:37:10.432296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize Data","metadata":{}},{"cell_type":"code","source":"from matplotlib import pyplot as plt\nfrom random import randint\n\nnrows, ncols = 4, 7\nfig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(10,6))\nindexes = [randint(0, train_n_images) for i in range(nrows * ncols)]\n\ni = 0\nfor _ in axes:\n    for ax in _:\n        ax.imshow(train_x[indexes[i]], cmap='gray')\n        ax.set_xticklabels([])\n        ax.set_yticklabels([])\n        i += 1","metadata":{"execution":{"iopub.status.busy":"2023-09-07T10:37:10.435625Z","iopub.execute_input":"2023-09-07T10:37:10.436198Z","iopub.status.idle":"2023-09-07T10:37:13.195795Z","shell.execute_reply.started":"2023-09-07T10:37:10.436166Z","shell.execute_reply":"2023-09-07T10:37:13.194699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Creation","metadata":{}},{"cell_type":"code","source":"model = keras.Sequential(\n    [\n        keras.Input(shape=image_shape),\n        \n        layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),\n        layers.MaxPool2D(pool_size=(2, 2)),\n        \n        layers.Conv2D(filters=128, kernel_size=(3, 3), activation='relu'),\n        layers.MaxPool2D(pool_size=(2, 2)),\n        \n        layers.Flatten(),\n        layers.Dropout(0.4),\n        layers.Dense(n_class, activation='softmax'),\n    ]\n\n)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-07T10:37:13.197339Z","iopub.execute_input":"2023-09-07T10:37:13.197725Z","iopub.status.idle":"2023-09-07T10:37:13.276684Z","shell.execute_reply.started":"2023-09-07T10:37:13.197689Z","shell.execute_reply":"2023-09-07T10:37:13.275927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Training","metadata":{}},{"cell_type":"code","source":"callback = tf.keras.callbacks.EarlyStopping(patience=3)\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nmodel.fit(train_x\n          , train_y\n          , batch_size=batch_size\n          , epochs=epochs\n          , validation_split=0.1\n          , callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2023-09-07T10:37:13.277690Z","iopub.execute_input":"2023-09-07T10:37:13.278015Z","iopub.status.idle":"2023-09-07T10:37:32.607323Z","shell.execute_reply.started":"2023-09-07T10:37:13.277982Z","shell.execute_reply":"2023-09-07T10:37:32.605955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"predict = np.argmax(model.predict(test_x), axis=1)\nresults = pd.Series(predict, name='Label')\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T10:37:32.608568Z","iopub.execute_input":"2023-09-07T10:37:32.608946Z","iopub.status.idle":"2023-09-07T10:37:36.629381Z","shell.execute_reply.started":"2023-09-07T10:37:32.608912Z","shell.execute_reply":"2023-09-07T10:37:36.628345Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}