{"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":"%config Completer.use_jedi = False","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-23T17:33:42.931325Z","iopub.execute_input":"2021-11-23T17:33:42.931583Z","iopub.status.idle":"2021-11-23T17:33:42.946296Z","shell.execute_reply.started":"2021-11-23T17:33:42.931555Z","shell.execute_reply":"2021-11-23T17:33:42.945494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:33:42.947967Z","iopub.execute_input":"2021-11-23T17:33:42.948565Z","iopub.status.idle":"2021-11-23T17:33:45.797759Z","shell.execute_reply.started":"2021-11-23T17:33:42.948529Z","shell.execute_reply":"2021-11-23T17:33:45.797025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/digit-recognizer/train.csv')\ntrain_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:33:45.799122Z","iopub.execute_input":"2021-11-23T17:33:45.799355Z","iopub.status.idle":"2021-11-23T17:33:49.27811Z","shell.execute_reply.started":"2021-11-23T17:33:45.799322Z","shell.execute_reply":"2021-11-23T17:33:49.277428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:33:49.279248Z","iopub.execute_input":"2021-11-23T17:33:49.279502Z","iopub.status.idle":"2021-11-23T17:33:49.346609Z","shell.execute_reply.started":"2021-11-23T17:33:49.279468Z","shell.execute_reply":"2021-11-23T17:33:49.345932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataloader","metadata":{}},{"cell_type":"code","source":"def get_img(line): return line.drop('label').values.reshape(28, 28)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:33:49.349145Z","iopub.execute_input":"2021-11-23T17:33:49.349699Z","iopub.status.idle":"2021-11-23T17:33:49.353277Z","shell.execute_reply.started":"2021-11-23T17:33:49.349668Z","shell.execute_reply":"2021-11-23T17:33:49.352576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(get_img(train_df.loc[100]), cmap = 'gray');","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:33:49.354548Z","iopub.execute_input":"2021-11-23T17:33:49.355436Z","iopub.status.idle":"2021-11-23T17:33:49.592086Z","shell.execute_reply.started":"2021-11-23T17:33:49.355377Z","shell.execute_reply":"2021-11-23T17:33:49.591447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(\n    blocks = [ImageBlock(cls = PILImageBW), CategoryBlock],\n    splitter = RandomSplitter(valid_pct = 0.2, seed = 42),\n    get_x = get_img,\n    get_y = ColReader('label')\n)\ndls = dblock.dataloaders(train_df, num_workers = 4, bs = 256)\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:33:49.593232Z","iopub.execute_input":"2021-11-23T17:33:49.593496Z","iopub.status.idle":"2021-11-23T17:33:57.119141Z","shell.execute_reply.started":"2021-11-23T17:33:49.59346Z","shell.execute_reply":"2021-11-23T17:33:57.118455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Learner","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, metrics = accuracy)\nlearn.fine_tune(10)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:37:01.59761Z","iopub.execute_input":"2021-11-23T17:37:01.59822Z","iopub.status.idle":"2021-11-23T17:46:53.667259Z","shell.execute_reply.started":"2021-11-23T17:37:01.598183Z","shell.execute_reply":"2021-11-23T17:46:53.66645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Verification","metadata":{}},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:15:19.653395Z","iopub.execute_input":"2021-11-23T18:15:19.653997Z","iopub.status.idle":"2021-11-23T18:15:30.717217Z","shell.execute_reply.started":"2021-11-23T18:15:19.653956Z","shell.execute_reply":"2021-11-23T18:15:30.716457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv('../input/digit-recognizer/test.csv')\ntest_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:50:21.371941Z","iopub.execute_input":"2021-11-23T17:50:21.372539Z","iopub.status.idle":"2021-11-23T17:50:22.885122Z","shell.execute_reply.started":"2021-11-23T17:50:21.372492Z","shell.execute_reply":"2021-11-23T17:50:22.884447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'] = 99\ntest_df = test_df.astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:56:58.922917Z","iopub.execute_input":"2021-11-23T17:56:58.923952Z","iopub.status.idle":"2021-11-23T17:56:58.981496Z","shell.execute_reply.started":"2021-11-23T17:56:58.923912Z","shell.execute_reply":"2021-11-23T17:56:58.980793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dl_test = dls.test_dl(test_df)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:56:59.202506Z","iopub.execute_input":"2021-11-23T17:56:59.203133Z","iopub.status.idle":"2021-11-23T17:56:59.222429Z","shell.execute_reply.started":"2021-11-23T17:56:59.203099Z","shell.execute_reply":"2021-11-23T17:56:59.221647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds, _ = learn.get_preds(dl = dl_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T17:57:00.502556Z","iopub.execute_input":"2021-11-23T17:57:00.503109Z","iopub.status.idle":"2021-11-23T17:58:19.305337Z","shell.execute_reply.started":"2021-11-23T17:57:00.503076Z","shell.execute_reply":"2021-11-23T17:58:19.304261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_labels = preds.argmax(dim = 1)\npredicted_labels","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:13:46.578537Z","iopub.execute_input":"2021-11-23T18:13:46.579046Z","iopub.status.idle":"2021-11-23T18:13:46.600193Z","shell.execute_reply.started":"2021-11-23T18:13:46.579005Z","shell.execute_reply":"2021-11-23T18:13:46.599544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': range(1, len(predicted_labels) + 1),\n    'Label': predicted_labels\n})\nsubmission.to_csv('submission.csv', index = False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T18:14:41.779933Z","iopub.execute_input":"2021-11-23T18:14:41.780195Z","iopub.status.idle":"2021-11-23T18:14:41.837559Z","shell.execute_reply.started":"2021-11-23T18:14:41.780168Z","shell.execute_reply":"2021-11-23T18:14:41.836773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}