{"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":"markdown","source":"# Getting started","metadata":{"papermill":{"duration":0.007433,"end_time":"2022-10-14T18:41:11.326357","exception":false,"start_time":"2022-10-14T18:41:11.318924","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":3.207709,"end_time":"2022-10-14T18:41:14.540101","exception":false,"start_time":"2022-10-14T18:41:11.332392","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-15T01:57:41.668953Z","iopub.execute_input":"2022-10-15T01:57:41.669363Z","iopub.status.idle":"2022-10-15T01:57:44.978227Z","shell.execute_reply.started":"2022-10-15T01:57:41.669283Z","shell.execute_reply":"2022-10-15T01:57:44.977078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/digit-recognizer')\npath.ls()","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:41:14.550366Z","iopub.status.busy":"2022-10-14T18:41:14.549963Z","iopub.status.idle":"2022-10-14T18:41:14.558625Z","shell.execute_reply":"2022-10-14T18:41:14.557535Z"},"papermill":{"duration":0.016935,"end_time":"2022-10-14T18:41:14.561527","exception":false,"start_time":"2022-10-14T18:41:14.544592","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = Path(\"../images\")\nimages_path.mkdir(exist_ok=True)","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:41:14.571591Z","iopub.status.busy":"2022-10-14T18:41:14.571319Z","iopub.status.idle":"2022-10-14T18:41:14.575932Z","shell.execute_reply":"2022-10-14T18:41:14.574911Z"},"papermill":{"duration":0.012075,"end_time":"2022-10-14T18:41:14.578055","exception":false,"start_time":"2022-10-14T18:41:14.565980","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(path/'train.csv')\ntest_df = pd.read_csv(path/'test.csv')","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:41:14.587536Z","iopub.status.busy":"2022-10-14T18:41:14.587280Z","iopub.status.idle":"2022-10-14T18:41:19.227855Z","shell.execute_reply":"2022-10-14T18:41:19.226862Z"},"papermill":{"duration":4.648077,"end_time":"2022-10-14T18:41:19.230390","exception":false,"start_time":"2022-10-14T18:41:14.582313","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:41:19.241513Z","iopub.status.busy":"2022-10-14T18:41:19.240630Z","iopub.status.idle":"2022-10-14T18:41:19.262648Z","shell.execute_reply":"2022-10-14T18:41:19.261646Z"},"papermill":{"duration":0.029797,"end_time":"2022-10-14T18:41:19.264849","exception":false,"start_time":"2022-10-14T18:41:19.235052","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Export train data as jpg files\nfor index, row in train_df.iterrows():\n    \n    label,digit = row[0], row[1:]\n    \n    filepath = images_path / \"train\" / str(label)\n    filepath.mkdir(parents=True, exist_ok=True)\n\n    filename = f\"{index}.jpg\"\n    \n    digit = digit.values\n    digit = digit.reshape(28,28)\n    digit = digit.astype(np.uint8)\n    \n    img = Image.fromarray(digit)\n    img.save(filepath/filename)","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:41:19.275097Z","iopub.status.busy":"2022-10-14T18:41:19.274833Z","iopub.status.idle":"2022-10-14T18:41:37.365358Z","shell.execute_reply":"2022-10-14T18:41:37.364366Z"},"papermill":{"duration":18.098243,"end_time":"2022-10-14T18:41:37.367709","exception":false,"start_time":"2022-10-14T18:41:19.269466","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Export test data as jpg files\nfor index, row in test_df.iterrows():   \n    \n    filepath = images_path / \"test\"\n    filepath.mkdir(parents=True, exist_ok=True)\n\n    filename = f\"{index+1}.jpg\"\n    \n    digit = row.values\n    digit = digit.reshape(28,28)\n    digit = digit.astype(np.uint8)\n    \n    img = Image.fromarray(digit)\n    img.save(filepath/filename)","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:41:37.378537Z","iopub.status.busy":"2022-10-14T18:41:37.378211Z","iopub.status.idle":"2022-10-14T18:41:46.773676Z","shell.execute_reply":"2022-10-14T18:41:46.772715Z"},"papermill":{"duration":9.403326,"end_time":"2022-10-14T18:41:46.776036","exception":false,"start_time":"2022-10-14T18:41:37.372710","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dls(bs, size):\n    dls = ImageDataLoaders.from_folder(path=images_path / \"train\",valid_pct=0.2,\n                                       seed=25, item_tfms=Resize(460),\n                                       batch_tfms=[*aug_transforms(size=size, min_scale=0.75, mult=1, do_flip=False),\n                                                   Normalize.from_stats(*imagenet_stats)],bs=bs)\n    return dls","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:41:46.787744Z","iopub.status.busy":"2022-10-14T18:41:46.786653Z","iopub.status.idle":"2022-10-14T18:41:46.792715Z","shell.execute_reply":"2022-10-14T18:41:46.791717Z"},"papermill":{"duration":0.013881,"end_time":"2022-10-14T18:41:46.794785","exception":false,"start_time":"2022-10-14T18:41:46.780904","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = get_dls(32, 224)\n\ndls.show_batch(max_n=6)","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:41:46.804817Z","iopub.status.busy":"2022-10-14T18:41:46.804560Z","iopub.status.idle":"2022-10-14T18:41:58.125139Z","shell.execute_reply":"2022-10-14T18:41:58.124243Z"},"papermill":{"duration":11.328781,"end_time":"2022-10-14T18:41:58.127959","exception":false,"start_time":"2022-10-14T18:41:46.799178","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training SOTA models","metadata":{"papermill":{"duration":0.020004,"end_time":"2022-10-14T18:41:58.167859","exception":false,"start_time":"2022-10-14T18:41:58.147855","status":"completed"},"tags":[]}},{"cell_type":"code","source":"dls = get_dls(128, 128)\nmodel=xresnet50(n_out=dls.c)\nlearn=Learner(dls, model, loss_func=CrossEntropyLossFlat(), metrics=accuracy)\nlearn.lr_find()","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:41:58.202554Z","iopub.status.busy":"2022-10-14T18:41:58.202150Z","iopub.status.idle":"2022-10-14T18:43:12.664249Z","shell.execute_reply":"2022-10-14T18:43:12.663159Z"},"papermill":{"duration":74.486102,"end_time":"2022-10-14T18:43:12.666473","exception":false,"start_time":"2022-10-14T18:41:58.180371","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(4, 1e-3)","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:43:12.681491Z","iopub.status.busy":"2022-10-14T18:43:12.680034Z","iopub.status.idle":"2022-10-14T18:57:08.736762Z","shell.execute_reply":"2022-10-14T18:57:08.735726Z"},"papermill":{"duration":836.066131,"end_time":"2022-10-14T18:57:08.738916","exception":false,"start_time":"2022-10-14T18:43:12.672785","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.dls = get_dls(64, 224)\nlearn.lr_find()","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:57:08.754034Z","iopub.status.busy":"2022-10-14T18:57:08.753084Z","iopub.status.idle":"2022-10-14T18:57:45.590767Z","shell.execute_reply":"2022-10-14T18:57:45.588926Z"},"papermill":{"duration":36.847391,"end_time":"2022-10-14T18:57:45.593043","exception":false,"start_time":"2022-10-14T18:57:08.745652","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(10, lr_max=slice(1e-5, 1e-4))","metadata":{"execution":{"iopub.execute_input":"2022-10-14T18:57:45.608858Z","iopub.status.busy":"2022-10-14T18:57:45.608087Z","iopub.status.idle":"2022-10-14T19:42:08.845670Z","shell.execute_reply":"2022-10-14T19:42:08.844588Z"},"papermill":{"duration":2663.247784,"end_time":"2022-10-14T19:42:08.847912","exception":false,"start_time":"2022-10-14T18:57:45.600128","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.execute_input":"2022-10-14T19:42:08.864691Z","iopub.status.busy":"2022-10-14T19:42:08.863884Z","iopub.status.idle":"2022-10-14T19:43:30.638071Z","shell.execute_reply":"2022-10-14T19:43:30.636975Z"},"papermill":{"duration":81.784905,"end_time":"2022-10-14T19:43:30.640261","exception":false,"start_time":"2022-10-14T19:42:08.855356","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.most_confused(min_val=3)","metadata":{"execution":{"iopub.execute_input":"2022-10-14T19:43:30.657669Z","iopub.status.busy":"2022-10-14T19:43:30.657320Z","iopub.status.idle":"2022-10-14T19:44:11.225057Z","shell.execute_reply":"2022-10-14T19:44:11.224004Z"},"papermill":{"duration":40.578853,"end_time":"2022-10-14T19:44:11.227194","exception":false,"start_time":"2022-10-14T19:43:30.648341","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submitting to kaggle","metadata":{"papermill":{"duration":0.007888,"end_time":"2022-10-14T19:44:11.243673","exception":false,"start_time":"2022-10-14T19:44:11.235785","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_files = list((images_path / \"test\").glob(\"*.jpg\"))\ntest_dl = learn.dls.test_dl(test_files)\n\npredictions, _ = learn.get_preds(dl=test_dl)\npredictions = np.argmax(predictions,axis=1)","metadata":{"execution":{"iopub.execute_input":"2022-10-14T19:44:11.262429Z","iopub.status.busy":"2022-10-14T19:44:11.260813Z","iopub.status.idle":"2022-10-14T19:46:15.334273Z","shell.execute_reply":"2022-10-14T19:46:15.333147Z"},"papermill":{"duration":124.085142,"end_time":"2022-10-14T19:46:15.336688","exception":false,"start_time":"2022-10-14T19:44:11.251546","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = [int(path.stem) for path in test_files]\nsubmission = pd.DataFrame({\"ImageId\": image_ids, \"Label\": predictions})\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.execute_input":"2022-10-14T19:46:15.355106Z","iopub.status.busy":"2022-10-14T19:46:15.354773Z","iopub.status.idle":"2022-10-14T19:46:15.427658Z","shell.execute_reply":"2022-10-14T19:46:15.426732Z"},"papermill":{"duration":0.084342,"end_time":"2022-10-14T19:46:15.429654","exception":false,"start_time":"2022-10-14T19:46:15.345312","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.008044,"end_time":"2022-10-14T19:46:15.446353","exception":false,"start_time":"2022-10-14T19:46:15.438309","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}