{"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":"## Problem Statement\nThe problem statement for this challenge is to develop a solution that can accurately identify and recover where ink is present from 3D x-ray scans of detached fragments of ancient papyrus scrolls. This task is a critical subproblem in the overall goal of solving the Vesuvius Challenge, which aims to digitally reconstruct and decipher the contents of carbonized papyrus scrolls that were buried by the eruption of Mount Vesuvius in AD 79.\n\n## Dataset Description:\n\n### Files\n\n  * **[train/test]/[fragment_id]/surface_volume/[image_id].**tif slices from the 3d x-ray surface volume. Each file contains a greyscale slice in the z-direction. Each fragment contains 65 slices. Combined this image stack gives us width * height * 65 number of voxels per fragment. You can expect two fragments in the hidden test set, which together are roughly the same size as a single training fragment. The sample slices available to download in the test folders are simply copied from training fragment one, but when you submit your notebook they will be substituted with the real test data.\n\n  * **rain/test]/[fragment_id]/mask.png** — a binary mask of which pixels contain data.\n\n  * **train/[fragment_id]/inklabels.png**— a binary mask of the ink vs no-ink labels.\n\n  * **in/[fragment_id]/inklabels_rle.csv** — a run-length-encoded version of the labels, generated using this script. This is the same format as you should make your submission in.\n\n  * **ain/[fragment_id]/ir.png** — the infrared photo on which the binary mask is based.\n\n  * **mple_submission.csv**, an example of a submission file in the correct format. You need to output the following file in the home directory: submission.csv. See the evaluation page for information.\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"### Import All required libraries ","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport numpy as np\nimport glob\nimport PIL.Image as Image\nfrom torch.utils.data import Dataset\nfrom torchvision import datasets\nfrom torchvision.transforms import ToTensor\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom tqdm import tqdm\nfrom ipywidgets import interact, fixed\nimport cv2 \nimport random","metadata":{"execution":{"iopub.status.busy":"2023-04-14T07:22:47.576934Z","iopub.execute_input":"2023-04-14T07:22:47.577459Z","iopub.status.idle":"2023-04-14T07:22:47.875899Z","shell.execute_reply.started":"2023-04-14T07:22:47.577417Z","shell.execute_reply":"2023-04-14T07:22:47.874360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image =Image.open(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/ir.png\")\n\n# load original images\nplt.imshow(image,cmap='gray')\n","metadata":{"execution":{"iopub.status.busy":"2023-04-14T06:04:28.181864Z","iopub.execute_input":"2023-04-14T06:04:28.182324Z","iopub.status.idle":"2023-04-14T06:04:31.578476Z","shell.execute_reply.started":"2023-04-14T06:04:28.182287Z","shell.execute_reply":"2023-04-14T06:04:31.577284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load original, mask and inklabels image:\n\n*  **ir.png:** Original image\n*  **mask.png:** A image is mask if pixel contain data and other pixel will be ignored\n* **inklabels.png :** label data- whether a pixel contains ink or no ink.","metadata":{}},{"cell_type":"code","source":"image =Image.open(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/ir.png\")\nmask_img =Image.open(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/mask.png\")\ninklabels_img= Image.open(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png\")","metadata":{"execution":{"iopub.status.busy":"2023-04-14T06:35:01.730668Z","iopub.execute_input":"2023-04-14T06:35:01.731108Z","iopub.status.idle":"2023-04-14T06:35:01.741115Z","shell.execute_reply.started":"2023-04-14T06:35:01.731072Z","shell.execute_reply":"2023-04-14T06:35:01.740267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images(images,title_img):\n    n= len(images)\n    f = plt.figure(figsize=(14,8))\n    for i in range(n):\n        # Debug, plot figure\n        f.add_subplot(1, n, i + 1)\n        plt.imshow(images[i],cmap=\"gray\")\n        plt.title(title_img[i])\n\n    plt.show(block=True)\n    \nshow_images([image,mask_img,inklabels_img],['Original_image','mask_img','inklabels_img'])    ","metadata":{"execution":{"iopub.status.busy":"2023-04-14T06:37:29.626100Z","iopub.execute_input":"2023-04-14T06:37:29.626576Z","iopub.status.idle":"2023-04-14T06:37:42.042872Z","shell.execute_reply.started":"2023-04-14T06:37:29.626541Z","shell.execute_reply":"2023-04-14T06:37:42.041471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load 3D x-ray fragement. These are the .tif image stack. Image is an array of 16-bit grayscale image ( pow(2,16)-65536 Pixel Range -( 0,65535)). So each 16-bit image is represented in 3D shape so axis will be x,y and z direction. To scale the image we will convert it into [0,1] range dividing by 65535.","metadata":{}},{"cell_type":"code","source":"path = \"/kaggle/input/vesuvius-challenge-ink-detection/train/1/surface_volume/*.tif\"\nimages=[]\nfor filename in tqdm(sorted(glob.glob(path))[20:30]):\n    # read the imahe\n    image1= Image.open(filename)\n    # convert it into array opf float32 and divide by 65535.0 \n    image1 = np.array(image1,dtype=np.float32)/65535.0 \n    \n    images.append(image1)\n                     \n                     ","metadata":{"execution":{"iopub.status.busy":"2023-04-14T07:16:03.791585Z","iopub.execute_input":"2023-04-14T07:16:03.792152Z","iopub.status.idle":"2023-04-14T07:16:05.908290Z","shell.execute_reply.started":"2023-04-14T07:16:03.792066Z","shell.execute_reply":"2023-04-14T07:16:05.906973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we will randomly select 10 3d fragment\n# no_of_fragment =10\n# image_list = random.sample(images,no_of_fragment)\n\nfig, axes = plt.subplots(1, len(images), figsize=(15, 3))\nfor image, ax in zip(images, axes):\n    img =Image.fromarray(image).resize((image.shape[1]//20, image.shape[0]//20))\n    # convert it into array\n    img = np.array(img,dtype=np.float32)\n    ax.imshow(img,cmap='gray')\n    ax.set_xticks([]); ax.set_yticks([])\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-14T07:19:09.868786Z","iopub.execute_input":"2023-04-14T07:19:09.869602Z","iopub.status.idle":"2023-04-14T07:19:14.565248Z","shell.execute_reply.started":"2023-04-14T07:19:09.869558Z","shell.execute_reply":"2023-04-14T07:19:14.561872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-04-14T07:02:58.078875Z","iopub.execute_input":"2023-04-14T07:02:58.079797Z","iopub.status.idle":"2023-04-14T07:02:58.088172Z","shell.execute_reply.started":"2023-04-14T07:02:58.079750Z","shell.execute_reply":"2023-04-14T07:02:58.086702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}