{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":6688004,"sourceType":"competition"},{"sourceId":146802337,"sourceType":"kernelVersion"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Install pyvips for handling large images \nTo achive this you need to \"Add data\" from following notebook \n\n[pyvips_offline](http://www.kaggle.com/code/aliabbasi/pyvips-offline)\n\nThen running followings, it should get installed in offline mode!","metadata":{}},{"cell_type":"code","source":"# install pyvips and its dependency\n!dpkg -i --force-depends /kaggle/input/pyvips-offline/archives/*.deb >/dev/null 2>&1\n!pip install --no-index --find-links /kaggle/input/pyvips-offline cffi==1.15.1 \n!pip install --no-index --find-links /kaggle/input/pyvips-offline pycparser==2.21\n!pip install --no-index --find-links /kaggle/input/pyvips-offline pyvips==2.2.1","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyvips\nimport pandas as pd\nimport matplotlib.pyplot as plt \nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image dimensions Scatter Plot","metadata":{}},{"cell_type":"code","source":"# train data csv\ntrain_df = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\nprint(\"train data csv shape:\", train_df.shape)\nprint(train_df.head(10))\n \n# plot image dimensions\nplt.figure(figsize=(10, 5)) \nplt.scatter(train_df['image_width'], train_df['image_height'], c=train_df['is_tma'], cmap='viridis')\nplt.colorbar(label='is_tma')\nplt.xlabel('Image Width')\nplt.ylabel('Image Height')\nplt.title('Train Image Dimensions')\nplt.grid(True)\nplt.show()\n ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Label Distribution","metadata":{}},{"cell_type":"code","source":"labels_count = train_df.label.value_counts().to_dict() \ncategories = labels_count.keys()\nvalues = labels_count.values() \nplt.bar(categories, values) \nplt.title('Label distribution')\nplt.xlabel('Labels')\nplt.ylabel('Count') \nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"code","source":"def to_numpy(pyvips_img): \n    return np.ndarray(\n        buffer=pyvips_img.write_to_memory(),\n        dtype=np.uint8,\n        shape=[pyvips_img.height, pyvips_img.width, pyvips_img.bands]\n    )\n\n\ndef tiling(image): \n    tile_size = image.shape[0] // 5 \n    tiles = list()\n    for i in range(5):\n        for j in range(5):\n            left = j * tile_size\n            top = i * tile_size\n            right = left + tile_size\n            bottom = top + tile_size \n            tiles.append(image[top:bottom, left:right, :]) \n    return tiles\n\n\ndef display_tiles(tiles): \n    fig, axes = plt.subplots(5, 5, figsize=(10, 10)) \n    for i, tile in enumerate(tiles):\n        axes[i // 5, i % 5].imshow(tile)\n        axes[i // 5, i % 5].axis('off')\n    plt.tight_layout()\n    plt.show()\n\n    \ndef process_image(image_path):\n    pyvips_img = pyvips.Image.new_from_file(image_path, access='sequential') \n    img_np = to_numpy(pyvips_img)  \n    print (img_np.shape)\n    tiles = tiling(img_np) \n    display_tiles(tiles)\n    \n\nprocess_image(\"/kaggle/input/UBC-OCEAN/train_images/13568.png\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"process_image(\"/kaggle/input/UBC-OCEAN/train_images/13364.png\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"process_image(\"/kaggle/input/UBC-OCEAN/train_images/15221.png\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# UPDATE\n\nRefer to this wondeful notebook from \"Gunes\" for offline installation and usage of pyvips\n\nhttps://www.kaggle.com/code/gunesevitan/libvips-pyvips-installation-and-getting-started","metadata":{}}]}