{"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":"# Install libs","metadata":{}},{"cell_type":"code","source":"!dpkg -i --force-depends /kaggle/input/pyvips-offline-installer/archives/*.deb >/dev/null 2>&1\n!pip3 install /kaggle/input/pyvips-offline-installer/cffi-1.15.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip3 install /kaggle/input/pyvips-offline-installer/pycparser-2.21-py2.py3-none-any.whl\n!pip3 install /kaggle/input/pyvips-offline-installer/pyvips-2.2.1.tar.gz","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-03T21:48:20.711431Z","iopub.execute_input":"2022-09-03T21:48:20.712448Z","iopub.status.idle":"2022-09-03T21:55:28.911698Z","shell.execute_reply.started":"2022-09-03T21:48:20.712345Z","shell.execute_reply":"2022-09-03T21:55:28.909918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import dependencies","metadata":{}},{"cell_type":"code","source":"import cv2\nimport gc\nimport numpy as np\nimport os\nimport pandas as pd\nimport pyvips\nimport shutil\n\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-09-03T22:00:10.652066Z","iopub.execute_input":"2022-09-03T22:00:10.653463Z","iopub.status.idle":"2022-09-03T22:00:11.358612Z","shell.execute_reply.started":"2022-09-03T22:00:10.653419Z","shell.execute_reply":"2022-09-03T22:00:11.357487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare dataset","metadata":{}},{"cell_type":"code","source":"def make_tiles(image_path, tile_size=1024, max_tiles=64, avg_thr=230, std_thr=15, clip_edge=0.05):\n\n    image = pyvips.Image.new_from_file(image_path, access='sequential')\n\n    # cropping\n    offset_x = int(image.width * clip_edge)\n    offset_y = int(image.height * clip_edge)\n    w = int(image.width * (1-clip_edge*2))\n    h = int(image.height * (1-clip_edge*2))\n    image = image.crop(offset_x, offset_y, w, h)\n\n    # padding\n    pad_w = (tile_size - image.width%tile_size)%tile_size\n    pad_h = (tile_size - image.height%tile_size)%tile_size\n    image = image.embed(\n        pad_w//2, pad_h//2,\n        image.width+pad_w, image.height+pad_h,\n        extend=\"mirror\")\n\n    # Get the scanning position of the image\n    x_pos_list = []\n    y_pos_list = []\n    for y in range(0, image.height, tile_size):\n        for x in range(0, image.width, tile_size):\n            x_pos_list.append(x)\n            y_pos_list.append(y)\n\n    # Get the cropping position of the image\n    selected_x_pos_list = []\n    selected_y_pos_list = []\n    avg_list = []\n    for x, y in zip(x_pos_list, y_pos_list):\n        tile = image.crop(x, y, tile_size, tile_size)\n        avg = tile.avg()\n        std = tile.deviate()\n        if avg < avg_thr and std > std_thr:\n            selected_x_pos_list.append(x)\n            selected_y_pos_list.append(y)\n            avg_list.append(avg)\n\n    # Sort by ascending order of average brightness\n    sorted_idx = np.argsort(np.array(avg_list))\n    selected_x_pos_array = np.array(selected_x_pos_list)[sorted_idx][:max_tiles]\n    selected_y_pos_array = np.array(selected_y_pos_list)[sorted_idx][:max_tiles]\n\n    # crop\n    images = []\n    for x, y in zip(selected_x_pos_array, selected_y_pos_array):\n        tile = image.crop(x, y, tile_size, tile_size)\n        img = tile.numpy()\n        images.append(img)\n\n    if len(images) > 0:\n        images = np.stack(images)\n\n    del image\n    gc.collect()\n\n    return images","metadata":{"execution":{"iopub.status.busy":"2022-09-03T21:55:33.814295Z","iopub.execute_input":"2022-09-03T21:55:33.815168Z","iopub.status.idle":"2022-09-03T21:55:33.830247Z","shell.execute_reply.started":"2022-09-03T21:55:33.815124Z","shell.execute_reply":"2022-09-03T21:55:33.82827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tile_size = 1024\ndata_type = \"train\" # \"train\" or \"other\"\npos = 0 # 0-75 if data_type == \"train\" else 0-39\n\ninput_dir = \"../input/mayo-clinic-strip-ai\"\noutput_dir = os.path.join(\"/kaggle/working\", data_type)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T22:00:15.597103Z","iopub.execute_input":"2022-09-03T22:00:15.598086Z","iopub.status.idle":"2022-09-03T22:00:15.604176Z","shell.execute_reply.started":"2022-09-03T22:00:15.598008Z","shell.execute_reply":"2022-09-03T22:00:15.60247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(os.path.join(input_dir, f\"{data_type}.csv\"))\nimage_ids = df[\"image_id\"].unique()\n\nif data_type == \"train\":\n    if pos == 75: # 0-75\n        image_ids = image_ids[pos*10:]\n    else:\n        image_ids = image_ids[pos*10:(pos+1)*10]\nelif data_type == \"other\":\n    if pos == 39: # 0-39\n        image_ids = image_ids[pos*10:]\n    else:\n        image_ids = image_ids[pos*10:(pos+1)*10]\n\nfor i, image_id in tqdm(enumerate(image_ids), total=len(image_ids), dynamic_ncols=True):\n\n    images = make_tiles(\n        os.path.join(input_dir, data_type, f\"{image_id}.tif\"),\n        tile_size=tile_size,\n    )\n\n    if len(images) == 0:\n        continue\n\n    # output\n    output_image_dir = os.path.join(output_dir, image_id)\n    if os.path.exists(output_image_dir):\n        shutil.rmtree(output_image_dir)\n    os.makedirs(output_image_dir)\n\n    for i in range(len(images)):\n        cv2.imwrite(\n            os.path.join(output_image_dir, f\"tile{i}.jpg\"),\n            cv2.cvtColor(images[i], cv2.COLOR_RGB2BGR))","metadata":{"execution":{"iopub.status.busy":"2022-09-03T22:00:17.846291Z","iopub.execute_input":"2022-09-03T22:00:17.846722Z","iopub.status.idle":"2022-09-03T22:06:28.485577Z","shell.execute_reply.started":"2022-09-03T22:00:17.846687Z","shell.execute_reply":"2022-09-03T22:06:28.484389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}