{"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":{"execution":{"iopub.status.busy":"2021-11-16T01:53:54.020724Z","iopub.execute_input":"2021-11-16T01:53:54.021079Z","iopub.status.idle":"2021-11-16T01:53:54.049504Z","shell.execute_reply.started":"2021-11-16T01:53:54.020987Z","shell.execute_reply":"2021-11-16T01:53:54.048839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-16T01:53:54.052322Z","iopub.execute_input":"2021-11-16T01:53:54.052510Z","iopub.status.idle":"2021-11-16T01:53:56.676667Z","shell.execute_reply.started":"2021-11-16T01:53:54.052487Z","shell.execute_reply":"2021-11-16T01:53:56.675966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:53:56.678080Z","iopub.execute_input":"2021-11-16T01:53:56.678370Z","iopub.status.idle":"2021-11-16T01:53:59.838573Z","shell.execute_reply.started":"2021-11-16T01:53:56.678334Z","shell.execute_reply":"2021-11-16T01:53:59.837840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.dtypes","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:53:59.839843Z","iopub.execute_input":"2021-11-16T01:53:59.840097Z","iopub.status.idle":"2021-11-16T01:53:59.855139Z","shell.execute_reply.started":"2021-11-16T01:53:59.840070Z","shell.execute_reply":"2021-11-16T01:53:59.854216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:53:59.857803Z","iopub.execute_input":"2021-11-16T01:53:59.858299Z","iopub.status.idle":"2021-11-16T01:53:59.925038Z","shell.execute_reply.started":"2021-11-16T01:53:59.858264Z","shell.execute_reply":"2021-11-16T01:53:59.924364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_x(x): return x.drop('label').values.reshape(28, 28)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:53:59.926302Z","iopub.execute_input":"2021-11-16T01:53:59.926791Z","iopub.status.idle":"2021-11-16T01:53:59.930877Z","shell.execute_reply.started":"2021-11-16T01:53:59.926755Z","shell.execute_reply":"2021-11-16T01:53:59.930175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_x(train_df.loc[3]).dtype","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:53:59.931890Z","iopub.execute_input":"2021-11-16T01:53:59.932720Z","iopub.status.idle":"2021-11-16T01:53:59.986533Z","shell.execute_reply.started":"2021-11-16T01:53:59.932661Z","shell.execute_reply":"2021-11-16T01:53:59.982448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(get_x(train_df.loc[3]), cmap = 'gray');","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:53:59.987604Z","iopub.execute_input":"2021-11-16T01:53:59.988450Z","iopub.status.idle":"2021-11-16T01:54:00.210441Z","shell.execute_reply.started":"2021-11-16T01:53:59.988410Z","shell.execute_reply":"2021-11-16T01:54:00.209774Z"},"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_y = ColReader('label'),\n    get_x = get_x,\n)\ndls = dblock.dataloaders(train_df, num_workers = 4, bs = 256)\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:54:00.211622Z","iopub.execute_input":"2021-11-16T01:54:00.211863Z","iopub.status.idle":"2021-11-16T01:54:07.991165Z","shell.execute_reply.started":"2021-11-16T01:54:00.211828Z","shell.execute_reply":"2021-11-16T01:54:07.990440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet50, metrics = accuracy)\nlearn.fine_tune(10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:56:16.682529Z","iopub.status.idle":"2021-11-16T01:56:16.683131Z","shell.execute_reply.started":"2021-11-16T01:56:16.682871Z","shell.execute_reply":"2021-11-16T01:56:16.682897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_df['label'] = 0\ntest_df = test_df.astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:59:32.520294Z","iopub.execute_input":"2021-11-16T01:59:32.520567Z","iopub.status.idle":"2021-11-16T01:59:34.201368Z","shell.execute_reply.started":"2021-11-16T01:59:32.520537Z","shell.execute_reply":"2021-11-16T01:59:34.200171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = dls.test_dl(test_df)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:59:36.439822Z","iopub.execute_input":"2021-11-16T01:59:36.440391Z","iopub.status.idle":"2021-11-16T01:59:36.458027Z","shell.execute_reply.started":"2021-11-16T01:59:36.440354Z","shell.execute_reply":"2021-11-16T01:59:36.456979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds,_ = learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T01:59:59.739996Z","iopub.execute_input":"2021-11-16T01:59:59.740837Z","iopub.status.idle":"2021-11-16T02:01:20.580074Z","shell.execute_reply.started":"2021-11-16T01:59:59.740785Z","shell.execute_reply":"2021-11-16T02:01:20.579256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-16T02:01:32.860972Z","iopub.execute_input":"2021-11-16T02:01:32.861850Z","iopub.status.idle":"2021-11-16T02:01:32.867830Z","shell.execute_reply.started":"2021-11-16T02:01:32.861811Z","shell.execute_reply":"2021-11-16T02:01:32.866979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-16T02:01:45.559608Z","iopub.execute_input":"2021-11-16T02:01:45.559871Z","iopub.status.idle":"2021-11-16T02:01:45.567503Z","shell.execute_reply.started":"2021-11-16T02:01:45.559840Z","shell.execute_reply":"2021-11-16T02:01:45.566718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': tensor(range(len(preds))) + 1, 'Label': preds.argmax(dim=1)})\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T02:02:00.790875Z","iopub.execute_input":"2021-11-16T02:02:00.791486Z","iopub.status.idle":"2021-11-16T02:02:00.864539Z","shell.execute_reply.started":"2021-11-16T02:02:00.791446Z","shell.execute_reply":"2021-11-16T02:02:00.863744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}