{"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":"This provides the most simple submission possible, we use the mask on the test images as prediction.","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom tqdm.auto import tqdm, trange\nimport matplotlib.pyplot as plt\n%matplotlib inline\n","metadata":{"execution":{"iopub.status.busy":"2023-04-26T12:25:02.658185Z","iopub.execute_input":"2023-04-26T12:25:02.658983Z","iopub.status.idle":"2023-04-26T12:25:05.267003Z","shell.execute_reply.started":"2023-04-26T12:25:02.658941Z","shell.execute_reply":"2023-04-26T12:25:05.265865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_path = Path('/kaggle/input/vesuvius-challenge-ink-detection')\nPath.BASE_PATH = base_path\ntrain_path = base_path / 'train'\ntest_path = base_path / 'test'","metadata":{"execution":{"iopub.status.busy":"2023-04-26T12:25:05.269770Z","iopub.execute_input":"2023-04-26T12:25:05.270659Z","iopub.status.idle":"2023-04-26T12:25:05.276829Z","shell.execute_reply.started":"2023-04-26T12:25:05.270607Z","shell.execute_reply":"2023-04-26T12:25:05.275788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_files = get_files(test_path, extensions='.png')\nimage_files.sort()\nimage_files","metadata":{"execution":{"iopub.status.busy":"2023-04-26T12:25:05.278776Z","iopub.execute_input":"2023-04-26T12:25:05.279662Z","iopub.status.idle":"2023-04-26T12:25:05.302406Z","shell.execute_reply.started":"2023-04-26T12:25:05.279608Z","shell.execute_reply":"2023-04-26T12:25:05.301523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(1,len(image_files), figsize=(10,5))\n\nfor i, image_file in enumerate(image_files):\n    im = np.array(Image.open(image_file))\n    axs[i].imshow(im)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T12:25:05.304655Z","iopub.execute_input":"2023-04-26T12:25:05.305356Z","iopub.status.idle":"2023-04-26T12:25:09.477630Z","shell.execute_reply.started":"2023-04-26T12:25:05.305313Z","shell.execute_reply":"2023-04-26T12:25:09.476018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle(output):\n    flat_img = np.where(output.flatten() > 0.4, 1, 0).astype(np.uint8)\n    starts = np.array((flat_img[:-1] == 0) & (flat_img[1:] == 1))\n    ends = np.array((flat_img[:-1] == 1) & (flat_img[1:] == 0))\n    starts_ix = np.where(starts)[0] + 2\n    ends_ix = np.where(ends)[0] + 2\n    lengths = ends_ix - starts_ix\n    return \" \".join(map(str, sum(zip(starts_ix, lengths), ())))","metadata":{"execution":{"iopub.status.busy":"2023-04-26T12:25:09.479183Z","iopub.execute_input":"2023-04-26T12:25:09.479605Z","iopub.status.idle":"2023-04-26T12:25:09.489082Z","shell.execute_reply.started":"2023-04-26T12:25:09.479560Z","shell.execute_reply":"2023-04-26T12:25:09.487669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = defaultdict(list)\n\nfor i, fragment_name in enumerate(image_files):\n    submission[\"Id\"].append(fragment_name.parent.name)\n    submission[\"Predicted\"].append(rle(np.array(Image.open(fragment_name))))","metadata":{"execution":{"iopub.status.busy":"2023-04-26T12:25:09.490848Z","iopub.execute_input":"2023-04-26T12:25:09.491418Z","iopub.status.idle":"2023-04-26T12:25:10.677747Z","shell.execute_reply.started":"2023-04-26T12:25:09.491366Z","shell.execute_reply":"2023-04-26T12:25:10.676260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame.from_dict(submission)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-04-26T12:25:10.679646Z","iopub.execute_input":"2023-04-26T12:25:10.680014Z","iopub.status.idle":"2023-04-26T12:25:10.700498Z","shell.execute_reply.started":"2023-04-26T12:25:10.679980Z","shell.execute_reply":"2023-04-26T12:25:10.699491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-26T12:25:10.702806Z","iopub.execute_input":"2023-04-26T12:25:10.703688Z","iopub.status.idle":"2023-04-26T12:25:10.720083Z","shell.execute_reply.started":"2023-04-26T12:25:10.703645Z","shell.execute_reply":"2023-04-26T12:25:10.718277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}