{"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 keras\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Flatten\nfrom keras import Model, Input, Sequential\nimport matplotlib.pyplot as plt\nimport numpy as np \nimport pandas as pd \nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2022-07-27T11:38:06.703572Z","iopub.execute_input":"2022-07-27T11:38:06.704107Z","iopub.status.idle":"2022-07-27T11:38:12.702463Z","shell.execute_reply.started":"2022-07-27T11:38:06.704056Z","shell.execute_reply":"2022-07-27T11:38:12.701149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:38:12.704577Z","iopub.execute_input":"2022-07-27T11:38:12.705164Z","iopub.status.idle":"2022-07-27T11:38:18.145667Z","shell.execute_reply.started":"2022-07-27T11:38:12.705129Z","shell.execute_reply":"2022-07-27T11:38:18.144638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:38:18.147157Z","iopub.execute_input":"2022-07-27T11:38:18.147651Z","iopub.status.idle":"2022-07-27T11:38:18.180231Z","shell.execute_reply.started":"2022-07-27T11:38:18.147605Z","shell.execute_reply":"2022-07-27T11:38:18.178989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixels = train.drop(columns=['label'], axis=1)\nlabels = train.label.values.astype('uint8')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:38:18.182732Z","iopub.execute_input":"2022-07-27T11:38:18.183084Z","iopub.status.idle":"2022-07-27T11:38:18.301164Z","shell.execute_reply.started":"2022-07-27T11:38:18.183051Z","shell.execute_reply":"2022-07-27T11:38:18.300353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(pixels):\n    \n    data = list()\n    for i in range(len(pixels)):\n        data.append((pixels.iloc[i].values.reshape(28,28,1)).astype('float32'))\n    data = np.asarray(data)\n    data /= 255.0\n    return data\n    \nimg = preprocess(pixels)    ","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-27T11:38:18.302279Z","iopub.execute_input":"2022-07-27T11:38:18.303275Z","iopub.status.idle":"2022-07-27T11:38:22.093241Z","shell.execute_reply.started":"2022-07-27T11:38:18.303222Z","shell.execute_reply":"2022-07-27T11:38:22.092080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(9):\n    plt.subplot(330 + i + 1)\n    plt.imshow(img[i])\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:38:22.094883Z","iopub.execute_input":"2022-07-27T11:38:22.095374Z","iopub.status.idle":"2022-07-27T11:38:22.992382Z","shell.execute_reply.started":"2022-07-27T11:38:22.095269Z","shell.execute_reply":"2022-07-27T11:38:22.991293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def blocks(in_layer, filters, n_blocks): \n    \n    for _ in range(n_blocks):\n        in_layer = Conv2D(filters, (3,3), activation='relu', padding='same', strides=(1,1))(in_layer)   \n    in_layer = MaxPooling2D((2,2))(in_layer)\n\n    return in_layer\n\n\ndef classifier(in_layer):\n    flat = Flatten()(in_layer)\n    dense = Dense(128, activation='relu')(flat)\n    dense2 = Dense(64, activation='relu')(dense)\n    last_layer =  Dense(10, activation='softmax')(dense2)\n    return last_layer\n\n\nin_ = Input(shape=(28,28,1))\nconv1 = blocks(in_, 32, 2)\nconv2 = blocks(conv1, 64, 2)\n\n\nout_ = classifier(conv2)\n\nmodel = Model(inputs=in_, outputs=out_)\nmodel.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics='accuracy')\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:38:22.994045Z","iopub.execute_input":"2022-07-27T11:38:22.994391Z","iopub.status.idle":"2022-07-27T11:38:23.164613Z","shell.execute_reply.started":"2022-07-27T11:38:22.994352Z","shell.execute_reply":"2022-07-27T11:38:23.163797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(img, labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:38:23.165911Z","iopub.execute_input":"2022-07-27T11:38:23.166475Z","iopub.status.idle":"2022-07-27T11:39:20.589489Z","shell.execute_reply.started":"2022-07-27T11:38:23.166433Z","shell.execute_reply":"2022-07-27T11:39:20.588535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('digit_recognizer_model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:39:20.591392Z","iopub.execute_input":"2022-07-27T11:39:20.592095Z","iopub.status.idle":"2022-07-27T11:39:20.633363Z","shell.execute_reply.started":"2022-07-27T11:39:20.592050Z","shell.execute_reply":"2022-07-27T11:39:20.632473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ = preprocess(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:39:20.636048Z","iopub.execute_input":"2022-07-27T11:39:20.636595Z","iopub.status.idle":"2022-07-27T11:39:23.209886Z","shell.execute_reply.started":"2022-07-27T11:39:20.636560Z","shell.execute_reply":"2022-07-27T11:39:23.208731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(test_)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:39:23.211343Z","iopub.execute_input":"2022-07-27T11:39:23.211711Z","iopub.status.idle":"2022-07-27T11:39:34.088627Z","shell.execute_reply.started":"2022-07-27T11:39:23.211680Z","shell.execute_reply":"2022-07-27T11:39:34.087712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(9):\n    plt.subplot(330 + 1 + i)\n    plt.imshow(test_[i])\n    plt.ylabel('prediction: '+ str(results[i].argmax()))\n    \n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:39:34.091269Z","iopub.execute_input":"2022-07-27T11:39:34.091688Z","iopub.status.idle":"2022-07-27T11:39:34.839612Z","shell.execute_reply.started":"2022-07-27T11:39:34.091652Z","shell.execute_reply":"2022-07-27T11:39:34.838717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = [a.argmax() for a in results]\ncolumn = {'ImageId': [a for a in range(1, len(label) + 1)],'Label': label}\n\nto_csv = pd.DataFrame(column)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:49:59.893575Z","iopub.execute_input":"2022-07-27T11:49:59.894726Z","iopub.status.idle":"2022-07-27T11:49:59.935437Z","shell.execute_reply.started":"2022-07-27T11:49:59.894683Z","shell.execute_reply":"2022-07-27T11:49:59.934258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to_csv.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T11:50:02.011449Z","iopub.execute_input":"2022-07-27T11:50:02.011864Z","iopub.status.idle":"2022-07-27T11:50:02.059166Z","shell.execute_reply.started":"2022-07-27T11:50:02.011828Z","shell.execute_reply":"2022-07-27T11:50:02.058105Z"},"trusted":true},"execution_count":null,"outputs":[]}]}