{"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":"# 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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\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\n\nimport fastai\nimport pathlib\nfrom pathlib import Path\nfrom fastai.vision.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-05T06:54:02.832747Z","iopub.execute_input":"2022-01-05T06:54:02.833096Z","iopub.status.idle":"2022-01-05T06:54:02.844596Z","shell.execute_reply.started":"2022-01-05T06:54:02.833057Z","shell.execute_reply":"2022-01-05T06:54:02.843552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fastai.__version__","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:02.846967Z","iopub.execute_input":"2022-01-05T06:54:02.847254Z","iopub.status.idle":"2022-01-05T06:54:02.859422Z","shell.execute_reply.started":"2022-01-05T06:54:02.847220Z","shell.execute_reply":"2022-01-05T06:54:02.858701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:02.860484Z","iopub.execute_input":"2022-01-05T06:54:02.861424Z","iopub.status.idle":"2022-01-05T06:54:02.866759Z","shell.execute_reply.started":"2022-01-05T06:54:02.861383Z","shell.execute_reply":"2022-01-05T06:54:02.865925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_train = \"/kaggle/input/digit-recognizer/train.csv\"\npath_test = \"/kaggle/input/digit-recognizer/test.csv\"\npath_submit = \"/kaggle/input/digit-recognizer/sample_submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:02.868270Z","iopub.execute_input":"2022-01-05T06:54:02.868834Z","iopub.status.idle":"2022-01-05T06:54:02.876027Z","shell.execute_reply.started":"2022-01-05T06:54:02.868795Z","shell.execute_reply":"2022-01-05T06:54:02.875240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/digit-recognizer')","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:02.879036Z","iopub.execute_input":"2022-01-05T06:54:02.879547Z","iopub.status.idle":"2022-01-05T06:54:02.884443Z","shell.execute_reply.started":"2022-01-05T06:54:02.879507Z","shell.execute_reply":"2022-01-05T06:54:02.883796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample_submission = pd.read_csv(path/'sample_submission.csv'); df_sample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:02.886296Z","iopub.execute_input":"2022-01-05T06:54:02.887291Z","iopub.status.idle":"2022-01-05T06:54:02.921979Z","shell.execute_reply.started":"2022-01-05T06:54:02.887253Z","shell.execute_reply":"2022-01-05T06:54:02.921166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(path_train)\ntest = pd.read_csv(path_test)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:02.923317Z","iopub.execute_input":"2022-01-05T06:54:02.923740Z","iopub.status.idle":"2022-01-05T06:54:07.809241Z","shell.execute_reply.started":"2022-01-05T06:54:02.923704Z","shell.execute_reply":"2022-01-05T06:54:07.808556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('/kaggle/working')\ntrain_path = path/'train'\ntest_path = path/'test'\ntrain_path.mkdir()\ntest_path.mkdir()","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:07.813021Z","iopub.execute_input":"2022-01-05T06:54:07.814834Z","iopub.status.idle":"2022-01-05T06:54:07.821041Z","shell.execute_reply.started":"2022-01-05T06:54:07.814795Z","shell.execute_reply":"2022-01-05T06:54:07.820203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, row in enumerate(train.values):\n    dir = train_path/str(row[0])\n    dir.mkdir(exist_ok=True)\n    img_arr = row[1:].reshape(28, 28).astype(np.uint8)\n    Image.fromarray(img_arr, \"L\").save(dir/f\"{i}.png\")\n    ","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:07.825224Z","iopub.execute_input":"2022-01-05T06:54:07.827422Z","iopub.status.idle":"2022-01-05T06:54:18.772638Z","shell.execute_reply.started":"2022-01-05T06:54:07.825613Z","shell.execute_reply":"2022-01-05T06:54:18.771751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, row in enumerate(test.values):\n    img_arr = row.reshape(28, 28).astype(np.uint8)\n    Image.fromarray(img_arr, \"L\").save(test_path/f\"{i}.png\")","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:18.774071Z","iopub.execute_input":"2022-01-05T06:54:18.774330Z","iopub.status.idle":"2022-01-05T06:54:25.191395Z","shell.execute_reply.started":"2022-01-05T06:54:18.774297Z","shell.execute_reply":"2022-01-05T06:54:25.190654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist = DataBlock(\n    blocks = (ImageBlock, CategoryBlock),\n    get_items = get_image_files,\n    splitter = RandomSplitter(),\n    get_y = parent_label,\n    batch_tfms= Normalize(imagenet_stats)\n)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:25.192642Z","iopub.execute_input":"2022-01-05T06:54:25.192918Z","iopub.status.idle":"2022-01-05T06:54:25.200309Z","shell.execute_reply.started":"2022-01-05T06:54:25.192864Z","shell.execute_reply":"2022-01-05T06:54:25.199567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = mnist.dataloaders(train_path, batch_size = 1024)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:25.201712Z","iopub.execute_input":"2022-01-05T06:54:25.201985Z","iopub.status.idle":"2022-01-05T06:54:32.094664Z","shell.execute_reply.started":"2022-01-05T06:54:25.201949Z","shell.execute_reply":"2022-01-05T06:54:32.093924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.train.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:32.097050Z","iopub.execute_input":"2022-01-05T06:54:32.097245Z","iopub.status.idle":"2022-01-05T06:54:33.459353Z","shell.execute_reply.started":"2022-01-05T06:54:32.097221Z","shell.execute_reply":"2022-01-05T06:54:33.458635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet101, metrics = accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:33.460631Z","iopub.execute_input":"2022-01-05T06:54:33.461057Z","iopub.status.idle":"2022-01-05T06:54:40.919622Z","shell.execute_reply.started":"2022-01-05T06:54:33.461018Z","shell.execute_reply":"2022-01-05T06:54:40.918694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2022-01-05T06:54:40.921810Z","iopub.execute_input":"2022-01-05T06:54:40.922095Z","iopub.status.idle":"2022-01-05T06:56:13.118663Z","shell.execute_reply.started":"2022-01-05T06:54:40.922056Z","shell.execute_reply":"2022-01-05T06:56:13.117916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet101, metrics = accuracy)\nlearn.fine_tune(6, base_lr = 1e-2)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T07:03:35.513597Z","iopub.execute_input":"2022-01-05T07:03:35.513918Z","iopub.status.idle":"2022-01-05T07:08:00.319829Z","shell.execute_reply.started":"2022-01-05T07:03:35.513857Z","shell.execute_reply":"2022-01-05T07:08:00.318912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_len = len(test_path.ls())\ntest_fns = L()\n# sort test file names (0.png, 1.png, 2.png, ...)\nfor i in range(test_len):\n    fn = test_path/f\"{i}.png\"\n    test_fns.append(fn)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T07:08:18.407900Z","iopub.execute_input":"2022-01-05T07:08:18.408192Z","iopub.status.idle":"2022-01-05T07:08:18.950772Z","shell.execute_reply.started":"2022-01-05T07:08:18.408152Z","shell.execute_reply":"2022-01-05T07:08:18.949806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = dls.test_dl(test_fns)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T07:08:30.267295Z","iopub.execute_input":"2022-01-05T07:08:30.267551Z","iopub.status.idle":"2022-01-05T07:08:30.277753Z","shell.execute_reply.started":"2022-01-05T07:08:30.267523Z","shell.execute_reply":"2022-01-05T07:08:30.277082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = learn.get_preds(dl=test_dl)[0].argmax(dim=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T07:08:35.459917Z","iopub.execute_input":"2022-01-05T07:08:35.460402Z","iopub.status.idle":"2022-01-05T07:08:51.303499Z","shell.execute_reply.started":"2022-01-05T07:08:35.460367Z","shell.execute_reply":"2022-01-05T07:08:51.302677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_result = pd.DataFrame(\n    {\n        \"Label\": preds.tolist()\n    }\n)\ndf_result.index += 1\ndf_result.to_csv(path/\"attempt1.csv\", index_label=\"ImageId\")","metadata":{"execution":{"iopub.status.busy":"2022-01-05T07:09:06.518991Z","iopub.execute_input":"2022-01-05T07:09:06.519269Z","iopub.status.idle":"2022-01-05T07:09:06.599825Z","shell.execute_reply.started":"2022-01-05T07:09:06.519236Z","shell.execute_reply":"2022-01-05T07:09:06.599050Z"},"trusted":true},"execution_count":null,"outputs":[]}]}