{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:53:32.094119Z","iopub.execute_input":"2024-10-22T03:53:32.094554Z","iopub.status.idle":"2024-10-22T03:53:33.638172Z","shell.execute_reply.started":"2024-10-22T03:53:32.09451Z","shell.execute_reply":"2024-10-22T03:53:33.636751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!sudo apt-get update\n!sudo apt-get install libvips-dev -y --no-install-recommends --download-only -o dir::cache='./'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:53:57.45132Z","iopub.execute_input":"2024-10-22T03:53:57.451878Z","iopub.status.idle":"2024-10-22T03:54:21.898984Z","shell.execute_reply.started":"2024-10-22T03:53:57.451837Z","shell.execute_reply":"2024-10-22T03:54:21.89733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mkdir ./libvips\n!mv ./archives/* ./libvips\n!rm -rf ./archives\n!ls ./libvips","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:54:45.068011Z","iopub.execute_input":"2024-10-22T03:54:45.068516Z","iopub.status.idle":"2024-10-22T03:54:50.644258Z","shell.execute_reply.started":"2024-10-22T03:54:45.068463Z","shell.execute_reply":"2024-10-22T03:54:50.642789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!yes | sudo dpkg -i ./libvips/*.deb","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:54:59.096314Z","iopub.execute_input":"2024-10-22T03:54:59.096824Z","iopub.status.idle":"2024-10-22T03:55:42.042412Z","shell.execute_reply.started":"2024-10-22T03:54:59.096771Z","shell.execute_reply":"2024-10-22T03:55:42.041088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install pyvips\n!pip wheel pyvips\n!mkdir pyvips\n!mv *.whl ./pyvips","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:56:44.704275Z","iopub.execute_input":"2024-10-22T03:56:44.704782Z","iopub.status.idle":"2024-10-22T03:57:11.511379Z","shell.execute_reply.started":"2024-10-22T03:56:44.704733Z","shell.execute_reply":"2024-10-22T03:57:11.509637Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.environ['OPENCV_IO_MAX_IMAGE_PIXELS'] = str(pow(2, 40))\n\nfrom pathlib import Path\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport pyvips\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:57:24.342511Z","iopub.execute_input":"2024-10-22T03:57:24.343959Z","iopub.status.idle":"2024-10-22T03:57:25.131086Z","shell.execute_reply.started":"2024-10-22T03:57:24.343896Z","shell.execute_reply":"2024-10-22T03:57:25.129792Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"competition_dataset_directory = Path('/kaggle/input/UBC-OCEAN')\n\ndf_train = pd.read_csv(competition_dataset_directory / 'train.csv')\nlargest_image_row = df_train.loc[np.argmax(df_train['image_height'] * df_train['image_width'])]\nlargest_image_row","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:57:35.564133Z","iopub.execute_input":"2024-10-22T03:57:35.564597Z","iopub.status.idle":"2024-10-22T03:57:35.605893Z","shell.execute_reply.started":"2024-10-22T03:57:35.564552Z","shell.execute_reply":"2024-10-22T03:57:35.604717Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_path = str(competition_dataset_directory / 'train_images' / f'{largest_image_row[\"image_id\"]}.png')\nimage_path","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:57:47.143608Z","iopub.execute_input":"2024-10-22T03:57:47.144068Z","iopub.status.idle":"2024-10-22T03:57:47.151651Z","shell.execute_reply.started":"2024-10-22T03:57:47.144023Z","shell.execute_reply":"2024-10-22T03:57:47.1505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def resize_with_aspect_ratio(image, longest_edge):\n\n    \"\"\"\n    Resize image while preserving its aspect ratio\n\n    Parameters\n    ----------\n    image: numpy.ndarray of shape (height, width, 3)\n        Image array\n\n    longest_edge: int\n        Desired number of pixels on the longest edge\n\n    Returns\n    -------\n    image: numpy.ndarray of shape (resized_height, resized_width, 3)\n        Resized image array\n    \"\"\"\n\n    height, width = image.shape[:2]\n    scale = longest_edge / max(height, width)\n    image = cv2.resize(image, dsize=(int(np.ceil(width * scale)), int(np.ceil(height * scale))), interpolation=cv2.INTER_LANCZOS4)\n\n    return image\ndef vips_read_image(image_path, longest_edge):\n    \n    \"\"\"\n    Read image using libvips\n\n    Parameters\n    ----------\n    image_path: str\n        Path of the image\n\n    Returns\n    -------\n    image: numpy.ndarray of shape (height, width, 3)\n        Image array\n    \"\"\"\n    \n    image_thumbnail = pyvips.Image.thumbnail(image_path, longest_edge)\n\n    return np.ndarray(\n        buffer=image_thumbnail.write_to_memory(),\n        dtype=np.uint8,\n        shape=[image_thumbnail.height, image_thumbnail.width, image_thumbnail.bands]\n    )\ndef visualize_image(image, title, path=None):\n\n    \"\"\"\n    Visualize the given image\n\n    Parameters\n    ----------\n    image: numpy.ndarray of shape (height, width, channel)\n        Image array\n        \n    title: str\n        Title of the plot\n\n    path: str or None\n        Path of the output file or None (if path is None, plot is displayed with selected backend)\n    \"\"\"\n\n    fig, ax = plt.subplots(figsize=(8, 8))\n    ax.imshow(image)\n    ax.set_xlabel('')\n    ax.set_ylabel('')\n    ax.tick_params(axis='x', labelsize=15, pad=10)\n    ax.tick_params(axis='y', labelsize=15, pad=10)\n    ax.set_title(title, size=15, pad=12.5, loc='center', wrap=True)\n    if path is None:\n        plt.show()\n    else:\n        plt.savefig(path)\n        plt.close(fig)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:59:34.866335Z","iopub.execute_input":"2024-10-22T03:59:34.866783Z","iopub.status.idle":"2024-10-22T03:59:34.879776Z","shell.execute_reply.started":"2024-10-22T03:59:34.866742Z","shell.execute_reply":"2024-10-22T03:59:34.878209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\n\nis_gpu = True\n\nif is_gpu is False:\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = resize_with_aspect_ratio(image=image, longest_edge=5000)\n\n    visualize_image(\n        image=image,\n        title=f'Image: {largest_image_row[\"image_id\"]} Label: {largest_image_row[\"label\"]}\\nHeight: {image.shape[0]} Width: {image.shape[1]}\\nMean: {np.mean(image):.2f} Std: {np.std(image):.2f}\\nMin: {np.min(image):.2f} Max: {np.max(image):.2f}'\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T03:59:55.135909Z","iopub.execute_input":"2024-10-22T03:59:55.13637Z","iopub.status.idle":"2024-10-22T03:59:55.145123Z","shell.execute_reply.started":"2024-10-22T03:59:55.136328Z","shell.execute_reply":"2024-10-22T03:59:55.14369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\n\nimage = vips_read_image(image_path=image_path, longest_edge=5000)\n\nvisualize_image(\n    image=image,\n    title=f'Image: {largest_image_row[\"image_id\"]} Label: {largest_image_row[\"label\"]}\\nHeight: {image.shape[0]} Width: {image.shape[1]}\\nMean: {np.mean(image):.2f} Std: {np.std(image):.2f}\\nMin: {np.min(image):.2f} Max: {np.max(image):.2f}'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T04:00:12.646836Z","iopub.execute_input":"2024-10-22T04:00:12.648062Z","iopub.status.idle":"2024-10-22T04:03:21.072173Z","shell.execute_reply.started":"2024-10-22T04:00:12.647995Z","shell.execute_reply":"2024-10-22T04:03:21.070826Z"}},"outputs":[],"execution_count":null}]}