{"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":"# Recreating the Learner","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n\nimages_path = Path('/kaggle/input/benetech-making-graphs-accessible/train/images')\nextra_data_path = Path('/kaggle/input/b-m-g-a-extra-data')","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:32:08.683930Z","iopub.execute_input":"2023-06-08T08:32:08.684629Z","iopub.status.idle":"2023-06-08T08:32:08.715803Z","shell.execute_reply.started":"2023-06-08T08:32:08.684589Z","shell.execute_reply":"2023-06-08T08:32:08.714833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv(extra_data_path/'rotated_vs_non_rotated_x_tick_labels.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:32:12.962919Z","iopub.execute_input":"2023-06-08T08:32:12.963614Z","iopub.status.idle":"2023-06-08T08:32:13.056168Z","shell.execute_reply.started":"2023-06-08T08:32:12.963574Z","shell.execute_reply":"2023-06-08T08:32:13.055053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n\nrotation_dls = torch.load(extra_data_path/'rotation_dls.pkl')\ntype(rotation_dls)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:32:22.988372Z","iopub.execute_input":"2023-06-08T08:32:22.988925Z","iopub.status.idle":"2023-06-08T08:32:28.441477Z","shell.execute_reply.started":"2023-06-08T08:32:22.988874Z","shell.execute_reply":"2023-06-08T08:32:28.433865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_dls.vocab","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:32:33.703455Z","iopub.execute_input":"2023-06-08T08:32:33.703994Z","iopub.status.idle":"2023-06-08T08:32:33.717483Z","shell.execute_reply.started":"2023-06-08T08:32:33.703959Z","shell.execute_reply":"2023-06-08T08:32:33.716224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_learn = vision_learner(rotation_dls, resnet34, metrics=accuracy, pretrained=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:32:37.899391Z","iopub.execute_input":"2023-06-08T08:32:37.899907Z","iopub.status.idle":"2023-06-08T08:32:38.689220Z","shell.execute_reply.started":"2023-06-08T08:32:37.899854Z","shell.execute_reply":"2023-06-08T08:32:38.688084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models_path = Path('/kaggle/working/models')\nif not models_path.exists():\n    models_path.mkdir()","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:32:46.387362Z","iopub.execute_input":"2023-06-08T08:32:46.387954Z","iopub.status.idle":"2023-06-08T08:32:46.395149Z","shell.execute_reply.started":"2023-06-08T08:32:46.387910Z","shell.execute_reply":"2023-06-08T08:32:46.394026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\norigin = str(extra_data_path) + '/'\ntarget = str(models_path) + '/'\nshutil.copy(origin + 'rotation_model.pth', target + 'rotation_model.pth')","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:32:50.082090Z","iopub.execute_input":"2023-06-08T08:32:50.083075Z","iopub.status.idle":"2023-06-08T08:32:51.227151Z","shell.execute_reply.started":"2023-06-08T08:32:50.082991Z","shell.execute_reply":"2023-06-08T08:32:51.225987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_learn.path = Path('/kaggle/working')\nrotation_learn.model_dir = 'models'","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:32:53.712848Z","iopub.execute_input":"2023-06-08T08:32:53.713905Z","iopub.status.idle":"2023-06-08T08:32:53.727638Z","shell.execute_reply.started":"2023-06-08T08:32:53.713806Z","shell.execute_reply":"2023-06-08T08:32:53.726324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_learn.load('rotation_model', with_opt=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:32:57.296438Z","iopub.execute_input":"2023-06-08T08:32:57.297094Z","iopub.status.idle":"2023-06-08T08:33:00.048128Z","shell.execute_reply.started":"2023-06-08T08:32:57.297046Z","shell.execute_reply":"2023-06-08T08:33:00.046977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's see how much time it takes to perform inference on the 880 validation set images.","metadata":{}},{"cell_type":"code","source":"len(rotation_dls.valid.dataset)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:33:50.363636Z","iopub.execute_input":"2023-06-08T08:33:50.364047Z","iopub.status.idle":"2023-06-08T08:33:50.372710Z","shell.execute_reply.started":"2023-06-08T08:33:50.363990Z","shell.execute_reply":"2023-06-08T08:33:50.371364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nrotation_preds_val, rotation_targs_val = rotation_learn.get_preds(ds_idx=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:33:52.109907Z","iopub.execute_input":"2023-06-08T08:33:52.110302Z","iopub.status.idle":"2023-06-08T08:34:00.290560Z","shell.execute_reply.started":"2023-06-08T08:33:52.110265Z","shell.execute_reply":"2023-06-08T08:34:00.289291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Set Inference","metadata":{}},{"cell_type":"code","source":"test_images_path = Path('/kaggle/input/benetech-making-graphs-accessible/test/images')","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:05.564380Z","iopub.execute_input":"2023-06-08T08:34:05.565306Z","iopub.status.idle":"2023-06-08T08:34:05.570817Z","shell.execute_reply.started":"2023-06-08T08:34:05.565266Z","shell.execute_reply":"2023-06-08T08:34:05.569619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Path.BASE_PATH = test_images_path\ntest_images_path","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:06.505350Z","iopub.execute_input":"2023-06-08T08:34:06.505844Z","iopub.status.idle":"2023-06-08T08:34:06.516332Z","shell.execute_reply.started":"2023-06-08T08:34:06.505798Z","shell.execute_reply":"2023-06-08T08:34:06.515061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = get_image_files(test_images_path)\nlen(test_images)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:07.554769Z","iopub.execute_input":"2023-06-08T08:34:07.555184Z","iopub.status.idle":"2023-06-08T08:34:07.567278Z","shell.execute_reply.started":"2023-06-08T08:34:07.555147Z","shell.execute_reply":"2023-06-08T08:34:07.566178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images[:5]","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:09.651512Z","iopub.execute_input":"2023-06-08T08:34:09.652636Z","iopub.status.idle":"2023-06-08T08:34:09.662455Z","shell.execute_reply.started":"2023-06-08T08:34:09.652584Z","shell.execute_reply":"2023-06-08T08:34:09.660962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = L(sorted(test_images))\ntest_images[:5]","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:10.919117Z","iopub.execute_input":"2023-06-08T08:34:10.919494Z","iopub.status.idle":"2023-06-08T08:34:10.926725Z","shell.execute_reply.started":"2023-06-08T08:34:10.919461Z","shell.execute_reply":"2023-06-08T08:34:10.925521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_test_dl = rotation_learn.dls.test_dl(test_images)\ntype(rotation_test_dl)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:20.698975Z","iopub.execute_input":"2023-06-08T08:34:20.699692Z","iopub.status.idle":"2023-06-08T08:34:20.717340Z","shell.execute_reply.started":"2023-06-08T08:34:20.699653Z","shell.execute_reply":"2023-06-08T08:34:20.716202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_preds_test, _ = rotation_learn.get_preds(dl=rotation_test_dl)\ntype(rotation_preds_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:26.954234Z","iopub.execute_input":"2023-06-08T08:34:26.954779Z","iopub.status.idle":"2023-06-08T08:34:27.275423Z","shell.execute_reply.started":"2023-06-08T08:34:26.954727Z","shell.execute_reply":"2023-06-08T08:34:27.274218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_preds_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:29.575162Z","iopub.execute_input":"2023-06-08T08:34:29.575632Z","iopub.status.idle":"2023-06-08T08:34:29.584353Z","shell.execute_reply.started":"2023-06-08T08:34:29.575589Z","shell.execute_reply":"2023-06-08T08:34:29.583149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_pred_idxs_test = rotation_preds_test.argmax(dim=1)\nrotation_pred_idxs_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:33.917792Z","iopub.execute_input":"2023-06-08T08:34:33.918537Z","iopub.status.idle":"2023-06-08T08:34:33.929890Z","shell.execute_reply.started":"2023-06-08T08:34:33.918493Z","shell.execute_reply":"2023-06-08T08:34:33.928694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_pred_idxs_test[:5]","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:35.343796Z","iopub.execute_input":"2023-06-08T08:34:35.344296Z","iopub.status.idle":"2023-06-08T08:34:35.354580Z","shell.execute_reply.started":"2023-06-08T08:34:35.344251Z","shell.execute_reply":"2023-06-08T08:34:35.353341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_pred_labels_test = []\nfor i in range(len(rotation_pred_idxs_test)):\n    rotation_pred_labels_test.append(rotation_dls.vocab[rotation_pred_idxs_test[i].item()])\nlen(rotation_pred_labels_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:39.832291Z","iopub.execute_input":"2023-06-08T08:34:39.832747Z","iopub.status.idle":"2023-06-08T08:34:39.846293Z","shell.execute_reply.started":"2023-06-08T08:34:39.832705Z","shell.execute_reply":"2023-06-08T08:34:39.844858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rotation_pred_labels_test[:5]","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:34:42.716450Z","iopub.execute_input":"2023-06-08T08:34:42.716934Z","iopub.status.idle":"2023-06-08T08:34:42.726134Z","shell.execute_reply.started":"2023-06-08T08:34:42.716883Z","shell.execute_reply":"2023-06-08T08:34:42.724921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"# Cleanup:\n!rm -r /kaggle/working/models","metadata":{"execution":{"iopub.status.busy":"2023-06-08T08:35:13.094707Z","iopub.execute_input":"2023-06-08T08:35:13.095216Z","iopub.status.idle":"2023-06-08T08:35:14.303163Z","shell.execute_reply.started":"2023-06-08T08:35:13.095169Z","shell.execute_reply":"2023-06-08T08:35:14.301657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}