{"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":"import numpy as np\nimport pandas as pd\nimport scipy as sp\nfrom sklearn.metrics import roc_auc_score, accuracy_score, f1_score, log_loss\nimport matplotlib.pyplot as plt\nimport sys\nimport os\nimport gc\nimport sys\nimport pickle\nimport warnings\nimport math\nimport time\nimport random\nimport argparse\nimport importlib\nfrom tqdm.auto import tqdm\nfrom functools import partial\nimport matplotlib.pylab as plt\nimport cv2\nfrom PIL import Image\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-25T19:30:09.461367Z","iopub.execute_input":"2023-05-25T19:30:09.462062Z","iopub.status.idle":"2023-05-25T19:30:11.316485Z","shell.execute_reply.started":"2023-05-25T19:30:09.462015Z","shell.execute_reply":"2023-05-25T19:30:11.314306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_path='/kaggle/input/vesuvius-challenge-ink-detection/train'\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T19:30:11.319335Z","iopub.execute_input":"2023-05-25T19:30:11.319753Z","iopub.status.idle":"2023-05-25T19:30:11.325251Z","shell.execute_reply.started":"2023-05-25T19:30:11.319717Z","shell.execute_reply":"2023-05-25T19:30:11.324137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(cv2.imread(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png\").astype(np.float32)/255)\nimage_path = \"/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png\"\nfig = plt.figure(figsize=(15, 15))\n\nimage = cv2.imread(image_path).astype(np.float32) / 255\n\n# Display the image\nplt.imshow(image)\n\n# Draw the grid lines\ngrid_color = 'red'\ngap = 224\nplt.grid(True, color=grid_color, linewidth=1, linestyle='--')\n\n# Customize the grid lines\nax = plt.gca()\nax.set_xticks(np.arange(0, image.shape[1], gap))\nax.set_yticks(np.arange(0, image.shape[0], gap))\nax.xaxis.set_tick_params(width=0)\nax.yaxis.set_tick_params(width=0)\nax.tick_params(axis='both', which='both', length=0)\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-25T19:30:11.337162Z","iopub.execute_input":"2023-05-25T19:30:11.337686Z","iopub.status.idle":"2023-05-25T19:30:24.284378Z","shell.execute_reply.started":"2023-05-25T19:30:11.337647Z","shell.execute_reply":"2023-05-25T19:30:24.283129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(cv2.imread(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png\").astype(np.float32)/255)\nimage_path = \"/kaggle/input/vesuvius-challenge-ink-detection/train/2/inklabels.png\"\nfig = plt.figure(figsize=(15, 15))\n\nimage = cv2.imread(image_path).astype(np.float32) / 255\n\n# Display the image\nplt.imshow(image)\n\n# Draw the grid lines\ngrid_color = 'red'\ngap = 224\nplt.grid(True, color=grid_color, linewidth=1, linestyle='--')\n\n# Customize the grid lines\nax = plt.gca()\nax.set_xticks(np.arange(0, image.shape[1], gap))\nax.set_yticks(np.arange(0, image.shape[0], gap))\nax.xaxis.set_tick_params(width=0)\nax.yaxis.set_tick_params(width=0)\nax.tick_params(axis='both', which='both', length=0)\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-25T19:30:24.286019Z","iopub.execute_input":"2023-05-25T19:30:24.287543Z","iopub.status.idle":"2023-05-25T19:30:55.645151Z","shell.execute_reply.started":"2023-05-25T19:30:24.287487Z","shell.execute_reply":"2023-05-25T19:30:55.643711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(cv2.imread(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png\").astype(np.float32)/255)\nfig = plt.figure(figsize=(15, 15))\n\nimage_path = \"/kaggle/input/vesuvius-challenge-ink-detection/train/3/inklabels.png\"\nimage = cv2.imread(image_path).astype(np.float32) / 255\n\n# Display the image\nplt.imshow(image)\n\n# Draw the grid lines\ngrid_color = 'red'\ngap = 224\nplt.grid(True, color=grid_color, linewidth=1, linestyle='--')\n\n# Customize the grid lines\nax = plt.gca()\nax.set_xticks(np.arange(0, image.shape[1], gap))\nax.set_yticks(np.arange(0, image.shape[0], gap))\nax.xaxis.set_tick_params(width=0)\nax.yaxis.set_tick_params(width=0)\nax.tick_params(axis='both', which='both', length=0)\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:34:31.464796Z","iopub.execute_input":"2023-05-25T18:34:31.465141Z","iopub.status.idle":"2023-05-25T18:34:40.485467Z","shell.execute_reply.started":"2023-05-25T18:34:31.465111Z","shell.execute_reply":"2023-05-25T18:34:40.484307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in [1,2,3]:\n    img1=train_img_path+f'/{i}/'+'mask.png'\n    img1=cv2.imread(img1)\n    h,w, _=img1.shape\n    print(img1.shape)\n    print(h*w/1000000)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T19:30:55.647828Z","iopub.execute_input":"2023-05-25T19:30:55.648221Z","iopub.status.idle":"2023-05-25T19:30:57.360683Z","shell.execute_reply.started":"2023-05-25T19:30:55.648175Z","shell.execute_reply":"2023-05-25T19:30:57.359423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"/kaggle/working/train/1/surface_volume\")\nos.makedirs(\"/kaggle/working/train/2/surface_volume\")\nos.makedirs(\"/kaggle/working/train/3/surface_volume\")\nos.makedirs(\"/kaggle/working/train/4/surface_volume\")\nos.makedirs(\"/kaggle/working/train/5/surface_volume\")\nos.makedirs(\"/kaggle/working/train/6/surface_volume\")\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T19:30:57.361900Z","iopub.execute_input":"2023-05-25T19:30:57.362246Z","iopub.status.idle":"2023-05-25T19:30:57.369690Z","shell.execute_reply.started":"2023-05-25T19:30:57.362209Z","shell.execute_reply":"2023-05-25T19:30:57.368426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\ndef divide_image(image_path, idx, data='sfv'):\n    # Open the image\n    image = Image.open(image_path)\n\n    # Get the dimensions of the image\n    image_width, image_height = image.size\n\n    # Calculate the dimensions of each segment\n    segment_width = image_width // 2\n    segment_height = image_height // 2\n    overlap = segment_width // 20\n\n    segments = []\n\n    cut_coordinates = [\n        [(0, segment_width + overlap), (0, segment_height + overlap)],\n        [(segment_width - overlap, image_width), (0, segment_height + overlap)],\n        [(0, segment_width + overlap), (segment_height - overlap, image_height)],\n        [(segment_width - overlap, image_width), (segment_height - overlap, image_height)]\n    ]\n\n    for i in range(4):\n        left, right = cut_coordinates[i][0]\n        upper, lower = cut_coordinates[i][1]\n\n        # Crop the image to extract the current segment\n        segment = image.crop((left, upper, right, lower))\n\n        # Save the segment as a separate image\n        if data == 'sfv':\n            output_path = f'/kaggle/working/train/{i+1}/surface_volume/{idx:02}.tif'\n        elif data == 'ink_lbls':\n            output_path = f'/kaggle/working/train/{i+1}/inklabels.png'\n        elif data == 'mask':\n            output_path = f'/kaggle/working/train/{i+1}/mask.png'\n\n        segment.save(output_path)\n\n    return None\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T19:30:57.371411Z","iopub.execute_input":"2023-05-25T19:30:57.371871Z","iopub.status.idle":"2023-05-25T19:30:57.385748Z","shell.execute_reply.started":"2023-05-25T19:30:57.371831Z","shell.execute_reply":"2023-05-25T19:30:57.384531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(22, 39)):\n    folder_path = '/kaggle/input/vesuvius-challenge-ink-detection/train/2/surface_volume'\n    img_path=folder_path+'/'+f\"{i:02}.tif\"\n    idx=i\n\n    divide_image(img_path, idx)\ndivide_image('/kaggle/input/vesuvius-challenge-ink-detection/train/2/inklabels.png', 0, data='ink_lbls')\ndivide_image('/kaggle/input/vesuvius-challenge-ink-detection/train/2/mask.png', 0, data='mask')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:47:20.699173Z","iopub.execute_input":"2023-05-25T18:47:20.699599Z","iopub.status.idle":"2023-05-25T18:49:36.014186Z","shell.execute_reply.started":"2023-05-25T18:47:20.699568Z","shell.execute_reply":"2023-05-25T18:49:36.012378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# divide_image_horizontal('/kaggle/input/vesuvius-challenge-ink-detection/train/2/inklabels.png', 0, data='ink_lbls')\n# divide_image_horizontal('/kaggle/input/vesuvius-challenge-ink-detection/train/2/mask.png', 0, data='mask')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:33:34.719359Z","iopub.status.idle":"2023-05-25T18:33:34.719973Z","shell.execute_reply.started":"2023-05-25T18:33:34.719709Z","shell.execute_reply":"2023-05-25T18:33:34.719741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor i in tqdm(range(22, 39)):\n    folder_path = '/kaggle/input/vesuvius-challenge-ink-detection/train/1/surface_volume'\n    img_path=folder_path+'/'+f\"{i:02}.tif\"\n    idx=i\n    output_path=f'/kaggle/working/train/5/surface_volume'+'/'+f\"{idx:02}.tif\"\n    image = Image.open(img_path)\n    image.save(output_path)\n\nimg_path='/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png'\noutput_path='/kaggle/working/train/5/inklabels.png'\nimage = Image.open(img_path)\nimage.save(output_path)\n\nimg_path='/kaggle/input/vesuvius-challenge-ink-detection/train/1/mask.png'\noutput_path='/kaggle/working/train/5/mask.png'\nimage = Image.open(img_path)\nimage.save(output_path)\n\n\n\nfor i in tqdm(range(22, 39)):\n    folder_path = '/kaggle/input/vesuvius-challenge-ink-detection/train/3/surface_volume'\n    img_path=folder_path+'/'+f\"{i:02}.tif\"\n    idx=i\n    output_path=f'/kaggle/working/train/6/surface_volume'+'/'+f\"{idx:02}.tif\"\n    image = Image.open(img_path)\n    image.save(output_path)\n\n    \nimg_path='/kaggle/input/vesuvius-challenge-ink-detection/train/3/inklabels.png'\noutput_path='/kaggle/working/train/6/inklabels.png'\nimage = Image.open(img_path)\nimage.save(output_path)\n\n\nimg_path='/kaggle/input/vesuvius-challenge-ink-detection/train/3/mask.png'\noutput_path='/kaggle/working/train/6/mask.png'\nimage = Image.open(img_path)\nimage.save(output_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-25T19:30:57.387281Z","iopub.execute_input":"2023-05-25T19:30:57.387675Z","iopub.status.idle":"2023-05-25T19:31:51.566683Z","shell.execute_reply.started":"2023-05-25T19:30:57.387641Z","shell.execute_reply":"2023-05-25T19:31:51.565493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_img(idx):\n    # plt.imshow(cv2.imread(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png\").astype(np.float32)/255)\n    fig = plt.figure(figsize=(8, 8))\n\n    image_path = f\"//kaggle/working/train/{idx}/inklabels.png\"\n    image = cv2.imread(image_path).astype(np.float32) / 255\n\n    # Display the image\n    plt.imshow(image)\n\n    # Draw the grid lines\n    grid_color = 'red'\n    gap = 224\n    plt.grid(True, color=grid_color, linewidth=1, linestyle='--')\n\n    # Customize the grid lines\n    ax = plt.gca()\n    ax.set_xticks(np.arange(0, image.shape[1], gap))\n    ax.set_yticks(np.arange(0, image.shape[0], gap))\n    ax.xaxis.set_tick_params(width=0)\n    ax.yaxis.set_tick_params(width=0)\n    ax.tick_params(axis='both', which='both', length=0)\n\n    # Show the plot\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:57:40.427244Z","iopub.execute_input":"2023-05-25T18:57:40.427748Z","iopub.status.idle":"2023-05-25T18:57:40.439739Z","shell.execute_reply.started":"2023-05-25T18:57:40.427710Z","shell.execute_reply":"2023-05-25T18:57:40.438326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_img(1)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:57:41.346720Z","iopub.execute_input":"2023-05-25T18:57:41.347134Z","iopub.status.idle":"2023-05-25T18:57:49.831702Z","shell.execute_reply.started":"2023-05-25T18:57:41.347101Z","shell.execute_reply":"2023-05-25T18:57:49.830409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_img(2)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:57:49.834314Z","iopub.execute_input":"2023-05-25T18:57:49.835169Z","iopub.status.idle":"2023-05-25T18:57:58.161972Z","shell.execute_reply.started":"2023-05-25T18:57:49.835133Z","shell.execute_reply":"2023-05-25T18:57:58.160581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_img(3)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:57:58.163762Z","iopub.execute_input":"2023-05-25T18:57:58.164154Z","iopub.status.idle":"2023-05-25T18:58:06.616693Z","shell.execute_reply.started":"2023-05-25T18:57:58.164120Z","shell.execute_reply":"2023-05-25T18:58:06.615755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_img(4)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:58:06.618792Z","iopub.execute_input":"2023-05-25T18:58:06.619452Z","iopub.status.idle":"2023-05-25T18:58:17.955276Z","shell.execute_reply.started":"2023-05-25T18:58:06.619403Z","shell.execute_reply":"2023-05-25T18:58:17.954040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_img(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:58:17.957040Z","iopub.execute_input":"2023-05-25T18:58:17.957729Z","iopub.status.idle":"2023-05-25T18:58:26.545118Z","shell.execute_reply.started":"2023-05-25T18:58:17.957688Z","shell.execute_reply":"2023-05-25T18:58:26.543767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_img(6)","metadata":{"execution":{"iopub.status.busy":"2023-05-25T18:58:26.546727Z","iopub.execute_input":"2023-05-25T18:58:26.547090Z","iopub.status.idle":"2023-05-25T18:58:26.613201Z","shell.execute_reply.started":"2023-05-25T18:58:26.547060Z","shell.execute_reply":"2023-05-25T18:58:26.611630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}