{"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":"My data processing including 2 steps:\n- Step 1: Pad the original .tif images to make sure their widths and heights are multiples of 224 with torch.nn.functional.pad. My padding will apply for right and bottom side of images.\n- Step 2: Patchify the images into smaller resolution with unfold in pytorch. ","metadata":{}},{"cell_type":"code","source":"import os\nfrom PIL import Image\nimport numpy as np\nimport torch\nimport torch.nn.functional as F\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport glob","metadata":{"execution":{"iopub.status.busy":"2023-03-17T08:23:24.636657Z","iopub.execute_input":"2023-03-17T08:23:24.637763Z","iopub.status.idle":"2023-03-17T08:23:27.344508Z","shell.execute_reply.started":"2023-03-17T08:23:24.637716Z","shell.execute_reply":"2023-03-17T08:23:27.342940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = os.path.join('/kaggle/input/vesuvius-challenge-ink-detection/')\nPROCESSED_DIR = os.path.join('/kaggle/working/processed_data')\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nIMG_SIZE = 224","metadata":{"execution":{"iopub.status.busy":"2023-03-17T08:23:27.346360Z","iopub.execute_input":"2023-03-17T08:23:27.346879Z","iopub.status.idle":"2023-03-17T08:23:27.354304Z","shell.execute_reply.started":"2023-03-17T08:23:27.346842Z","shell.execute_reply":"2023-03-17T08:23:27.352480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This is my implementation for these 2 steps.","metadata":{}},{"cell_type":"code","source":"def get_patches(image):\n    patches = image.unfold(0, IMG_SIZE, IMG_SIZE).unfold(1, IMG_SIZE, IMG_SIZE)\n    patches = patches.contiguous().view(-1, IMG_SIZE, IMG_SIZE)\n    return patches\n\ndef process_tiff(prefix, fragment_id):\n    # process patches\n    for filename in sorted(os.listdir(os.path.join(prefix, 'surface_volume/'))):\n        filename_id = filename.split('.')[0]\n        filepath = os.path.join(prefix, 'surface_volume', filename)\n        image = torch.from_numpy(np.array(Image.open(filepath), dtype=np.float32)/65535.0)\n        height = image.shape[0]\n        width = image.shape[1]\n        print(filepath, filename_id, 'image shape', image.shape)\n        pad_height = IMG_SIZE - int(np.ceil(height % IMG_SIZE))\n        pad_width = IMG_SIZE - int(np.ceil(width % IMG_SIZE))\n        image = F.pad(image, (0, pad_width, 0, pad_height), mode='constant',  value=0)\n        patches = get_patches(image)\n        SAVE_TIFF_FOLDER = os.path.join(PROCESSED_DIR, f'train/{fragment_id}', filename_id)\n        os.makedirs(SAVE_TIFF_FOLDER, exist_ok=True)\n        for patch_idx in range(patches.shape[0]):\n            patch = np.array(patches[patch_idx], dtype=np.float32)\n            np.save(os.path.join(SAVE_TIFF_FOLDER, f'{filename_id}_{patch_idx}.npy'), patch)\n            \n    # process ink-mask patches\n    ink_image = torch.from_numpy(\n        np.array(Image.open(os.path.join(prefix, 'inklabels.png')))/255.0\n    )\n    ink_image = F.pad(ink_image, (0, pad_width, 0, pad_height), mode='constant',  value=0)\n    ink_patches = get_patches(ink_image)\n    SAVE_INK_FOLDER = os.path.join(PROCESSED_DIR, f'train/{fragment_id}', 'ink')\n    os.makedirs(SAVE_INK_FOLDER, exist_ok=True)\n    print('ink shape', ink_image.shape, ink_patches.shape)\n    for patch_idx in range(ink_patches.shape[0]):\n        patch = np.array(ink_patches[patch_idx], dtype=np.float32)\n        np.save(os.path.join(SAVE_INK_FOLDER, f'ink_{patch_idx}.npy'), patch)\n        \n    # process mask patches\n    mask_image = torch.from_numpy(\n        np.array(Image.open(os.path.join(prefix, 'mask.png')))/255.0\n    )\n    mask_image = F.pad(mask_image, (0, pad_width, 0, pad_height), mode='constant',  value=0)\n    mask_patches = get_patches(mask_image)\n    SAVE_MASK_FOLDER = os.path.join(PROCESSED_DIR, f'train/{fragment_id}', 'mask')\n    print('ink shape', mask_image.shape, mask_patches.shape)\n    os.makedirs(SAVE_MASK_FOLDER, exist_ok=True)\n    for patch_idx in range(mask_patches.shape[0]):\n        patch = np.array(mask_patches[patch_idx], dtype=np.float32)\n        np.save(os.path.join(SAVE_MASK_FOLDER, f'mask_{patch_idx}.npy'), patch)\n    return pad_height, pad_width","metadata":{"execution":{"iopub.status.busy":"2023-03-17T08:30:46.487619Z","iopub.execute_input":"2023-03-17T08:30:46.488248Z","iopub.status.idle":"2023-03-17T08:30:46.510237Z","shell.execute_reply.started":"2023-03-17T08:30:46.488188Z","shell.execute_reply":"2023-03-17T08:30:46.508475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for fragment_id in [1, 2, 3]:\nfor fragment_id in [1]: \n    PREFIX = os.path.join(DATA_DIR, f'train/{fragment_id}/')\n    process_tiff(PREFIX, fragment_id)","metadata":{"execution":{"iopub.status.busy":"2023-03-17T08:30:47.163903Z","iopub.execute_input":"2023-03-17T08:30:47.165087Z","iopub.status.idle":"2023-03-17T08:33:32.482849Z","shell.execute_reply.started":"2023-03-17T08:30:47.165023Z","shell.execute_reply":"2023-03-17T08:33:32.481517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FRAGMENT_ID = '1'\nIMAGE_ID = '00'\nPATCH_IDX = 70","metadata":{"execution":{"iopub.status.busy":"2023-03-17T08:40:48.769063Z","iopub.execute_input":"2023-03-17T08:40:48.769517Z","iopub.status.idle":"2023-03-17T08:40:48.775328Z","shell.execute_reply.started":"2023-03-17T08:40:48.769478Z","shell.execute_reply":"2023-03-17T08:40:48.774078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PREFIX = os.path.join(PROCESSED_DIR, f'train/{FRAGMENT_ID}/')\npatch_image = np.load(os.path.join(PROCESSED_DIR, f'train/{FRAGMENT_ID}', IMAGE_ID, f'{IMAGE_ID}_{PATCH_IDX}.npy'))\nink_image = np.load(os.path.join(PROCESSED_DIR, f'train/{FRAGMENT_ID}', 'ink', f'ink_{PATCH_IDX}.npy'))\nmask_image = np.load(os.path.join(PROCESSED_DIR, f'train/{FRAGMENT_ID}', 'mask', f'mask_{PATCH_IDX}.npy'))","metadata":{"execution":{"iopub.status.busy":"2023-03-17T08:40:48.908118Z","iopub.execute_input":"2023-03-17T08:40:48.908534Z","iopub.status.idle":"2023-03-17T08:40:48.919260Z","shell.execute_reply.started":"2023-03-17T08:40:48.908499Z","shell.execute_reply":"2023-03-17T08:40:48.918347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(patch_image)","metadata":{"execution":{"iopub.status.busy":"2023-03-17T08:40:49.052319Z","iopub.execute_input":"2023-03-17T08:40:49.053143Z","iopub.status.idle":"2023-03-17T08:40:49.361348Z","shell.execute_reply.started":"2023-03-17T08:40:49.053101Z","shell.execute_reply":"2023-03-17T08:40:49.359926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask_image)","metadata":{"execution":{"iopub.status.busy":"2023-03-17T08:40:49.363741Z","iopub.execute_input":"2023-03-17T08:40:49.364332Z","iopub.status.idle":"2023-03-17T08:40:49.630778Z","shell.execute_reply.started":"2023-03-17T08:40:49.364288Z","shell.execute_reply":"2023-03-17T08:40:49.629437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(ink_image)","metadata":{"execution":{"iopub.status.busy":"2023-03-17T08:40:49.632988Z","iopub.execute_input":"2023-03-17T08:40:49.633375Z","iopub.status.idle":"2023-03-17T08:40:49.894045Z","shell.execute_reply.started":"2023-03-17T08:40:49.633339Z","shell.execute_reply":"2023-03-17T08:40:49.892697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I will upload the full processing data and write a Dataloader for this dataset.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}