{"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 os\nos.environ['OPENCV_IO_MAX_IMAGE_PIXELS'] = str(pow(2, 40))\n\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-08T11:57:47.744065Z","iopub.execute_input":"2023-11-08T11:57:47.744356Z","iopub.status.idle":"2023-11-08T11:57:49.393754Z","shell.execute_reply.started":"2023-11-08T11:57:47.74433Z","shell.execute_reply":"2023-11-08T11:57:49.392905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/UBC-OCEAN/train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:57:49.39557Z","iopub.execute_input":"2023-11-08T11:57:49.396021Z","iopub.status.idle":"2023-11-08T11:57:49.425236Z","shell.execute_reply.started":"2023-11-08T11:57:49.395989Z","shell.execute_reply":"2023-11-08T11:57:49.424196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.value_counts(\"is_tma\"))\nprint(\"-\" * 30)\nprint(df.value_counts(\"label\"))","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:57:49.426351Z","iopub.execute_input":"2023-11-08T11:57:49.426675Z","iopub.status.idle":"2023-11-08T11:57:49.4407Z","shell.execute_reply.started":"2023-11-08T11:57:49.426643Z","shell.execute_reply":"2023-11-08T11:57:49.439705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:57:49.443173Z","iopub.execute_input":"2023-11-08T11:57:49.443419Z","iopub.status.idle":"2023-11-08T11:57:49.461374Z","shell.execute_reply.started":"2023-11-08T11:57:49.443397Z","shell.execute_reply":"2023-11-08T11:57:49.460536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(4, 3))\nsns.histplot(data=df, x=\"label\", kde=True)\nplt.title(\"label\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:57:49.462425Z","iopub.execute_input":"2023-11-08T11:57:49.462738Z","iopub.status.idle":"2023-11-08T11:57:49.736925Z","shell.execute_reply.started":"2023-11-08T11:57:49.462707Z","shell.execute_reply":"2023-11-08T11:57:49.735917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(4, 3))\nsns.histplot(data=df, x=\"is_tma\")\nplt.title(\"is tma\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:57:49.738489Z","iopub.execute_input":"2023-11-08T11:57:49.73886Z","iopub.status.idle":"2023-11-08T11:57:50.046447Z","shell.execute_reply.started":"2023-11-08T11:57:49.738829Z","shell.execute_reply":"2023-11-08T11:57:50.04537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(4, 3))\nwh_ratios = df[\"image_width\"] / df[\"image_height\"]\nsns.histplot(wh_ratios, bins=50, kde=True)\nplt.title(\"width / height\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T13:02:42.014595Z","iopub.execute_input":"2023-11-08T13:02:42.015274Z","iopub.status.idle":"2023-11-08T13:02:42.349339Z","shell.execute_reply.started":"2023-11-08T13:02:42.01524Z","shell.execute_reply":"2023-11-08T13:02:42.348268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 3, figsize=(10, 3))\nsns.scatterplot(x=\"image_width\", y=\"image_height\",hue=\"is_tma\", style=\"is_tma\", data=df, ax=ax[0])\nsns.scatterplot(x=\"image_width\", y=\"image_height\", data=df.query(\"is_tma == False\"), ax=ax[1])\nsns.scatterplot(x=\"image_width\", y=\"image_height\", data=df.query(\"is_tma == True\"), ax=ax[2], color=\"orange\", marker=\"x\")\nplt.suptitle(\"image size\")\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:57:50.047912Z","iopub.execute_input":"2023-11-08T11:57:50.048286Z","iopub.status.idle":"2023-11-08T11:57:50.791262Z","shell.execute_reply.started":"2023-11-08T11:57:50.048252Z","shell.execute_reply":"2023-11-08T11:57:50.790358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\n\nwhile True:\n    random_image_df = df.sample(n=1)\n    image_id = random_image_df[\"image_id\"].iloc[0]\n    random_image_path = os.path.join(folder, f\"{image_id}_thumbnail.png\")\n    if os.path.isfile(random_image_path):\n        break\n        \nprint(random_image_path)\nlabel = random_image_df[\"label\"].iloc[0]\nimg = cv2.imread(random_image_path)\nprint(\"image shape:\", img.shape)\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nplt.imshow(img)\nplt.title(f\"random train thumbnail image (label: {label})\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:57:50.792428Z","iopub.execute_input":"2023-11-08T11:57:50.792702Z","iopub.status.idle":"2023-11-08T11:57:51.703817Z","shell.execute_reply.started":"2023-11-08T11:57:50.792678Z","shell.execute_reply":"2023-11-08T11:57:51.702927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!yes | sudo dpkg -i /kaggle/input/libvips-pyvips-installation-and-getting-started/libvips/*.deb\n!pip install /kaggle/input/libvips-pyvips-installation-and-getting-started/pyvips/pyvips-2.2.1-py2.py3-none-any.whl --no-index --find-links /kaggle/input/libvips-pyvips-installation-and-getting-started/pyvips","metadata":{"scrolled":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-11-08T11:57:51.705266Z","iopub.execute_input":"2023-11-08T11:57:51.705832Z","iopub.status.idle":"2023-11-08T11:58:29.455648Z","shell.execute_reply.started":"2023-11-08T11:57:51.705803Z","shell.execute_reply":"2023-11-08T11:58:29.45469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyvips","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:58:29.458757Z","iopub.execute_input":"2023-11-08T11:58:29.459088Z","iopub.status.idle":"2023-11-08T11:58:29.884094Z","shell.execute_reply.started":"2023-11-08T11:58:29.459059Z","shell.execute_reply":"2023-11-08T11:58:29.883104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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    )\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\n    if path is None:\n        plt.show()\n    else:\n        plt.savefig(path)\n        plt.close(fig)\n        ","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:58:29.885189Z","iopub.execute_input":"2023-11-08T11:58:29.885447Z","iopub.status.idle":"2023-11-08T11:58:29.895304Z","shell.execute_reply.started":"2023-11-08T11:58:29.885424Z","shell.execute_reply":"2023-11-08T11:58:29.894367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder = \"/kaggle/input/UBC-OCEAN/train_images\"\nrandom_image_df = df.sample(n=1)\nimage_id = random_image_df[\"image_id\"].iloc[0]\nrandom_image_path = os.path.join(folder, f\"{image_id}.png\")\n\nprint(random_image_path)\nlabel = random_image_df[\"label\"].iloc[0]\nimage = vips_read_image(random_image_path, longest_edge=5000)\nprint(image.shape)\nvisualize_image(\n    image=image,\n    title=f\"image_id: {image_id}, label: {label}\"\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T11:58:29.896498Z","iopub.execute_input":"2023-11-08T11:58:29.896781Z","iopub.status.idle":"2023-11-08T11:58:34.033855Z","shell.execute_reply.started":"2023-11-08T11:58:29.896757Z","shell.execute_reply":"2023-11-08T11:58:34.03302Z"},"trusted":true},"execution_count":null,"outputs":[]}]}