{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from datasets import load_dataset\n", "\n", "dataset = load_dataset(\"dnth/pets-enriched\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'filename': Value(dtype='string', id=None),\n", " 'caption': Value(dtype='string', id=None),\n", " 'image_labels': Sequence(feature=Value(dtype='string', id=None), length=-1, id=None),\n", " 'objects': [{'bbox': Sequence(feature=Value(dtype='float64', id=None), length=-1, id=None),\n", " 'confidence': Value(dtype='float64', id=None),\n", " 'label': Value(dtype='string', id=None)}]}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset['train'].features" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'filename': 'oxford-iiit-pet/images/Abyssinian_144.jpg',\n", " 'caption': 'a cat standing on a wooden floor next to a glass',\n", " 'image_labels': ['cat'],\n", " 'objects': [{'bbox': [91.0, 13.0, 408.0, 345.0],\n", " 'confidence': 0.9800000190734863,\n", " 'label': 'cat'}]}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset['train'][0]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'filename': 'oxford-iiit-pet/images/Abyssinian_100.jpg',\n", " 'caption': 'a cat is sitting in a bag',\n", " 'image_labels': ['cat'],\n", " 'objects': [{'bbox': [48.0, 72.0, 288.0, 371.0],\n", " 'confidence': 0.9539999961853027,\n", " 'label': 'cat'},\n", " {'bbox': [0.0, 31.0, 148.0, 92.0],\n", " 'confidence': 0.4880000054836273,\n", " 'label': 'strap'},\n", " {'bbox': [241.0, 341.0, 153.0, 160.0],\n", " 'confidence': 0.4309999942779541,\n", " 'label': 'strap'},\n", " {'bbox': [193.0, 1.0, 202.0, 179.0],\n", " 'confidence': 0.3700000047683716,\n", " 'label': 'pillow'}]}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset['train'][10]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import os\n", "from PIL import Image\n", "import io\n", "import datasets\n", "\n", "def load_image(example):\n", " image_path = example['filename'] # Assuming 'filename' contains the path\n", " if os.path.exists(image_path):\n", " with Image.open(image_path) as img:\n", " buf = io.BytesIO()\n", " img.save(buf, format='PNG')\n", " example['image'] = buf.getvalue()\n", " else:\n", " example['image'] = None\n", " return example\n", "\n", "# Assuming your dataset is called 'dataset'\n", "dataset = dataset.map(load_image)\n", "\n", "# Update the features of the dataset\n", "dataset = dataset.cast_column(\"image\", datasets.Image())" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dataset({\n", " features: ['filename', 'caption', 'image_labels', 'objects', 'image'],\n", " num_rows: 7275\n", "})" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset['train']" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f35fb0f0588e477b985cc75dfdc00569", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Uploading the dataset shards: 0%| | 0/5 [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ca299917858a4d9e8b2264da5c609be1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/1455 [00:00, ? examples/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "21e2c7755e1c4890b8710a0fc3fb2e25", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Creating parquet from Arrow format: 0%| | 0/15 [00:00, ?ba/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "05c256e8a8b54c4a8a9c905f7fcf78ab", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/1455 [00:00, ? examples/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0aa862e525f8403fa1489c11a08d9626", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Creating parquet from Arrow format: 0%| | 0/15 [00:00, ?ba/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1ae4a025e0a54f92a7932a2bfe104789", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/1455 [00:00, ? examples/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "da530e1109d6437dbdd39fe40e091724", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Creating parquet from Arrow format: 0%| | 0/15 [00:00, ?ba/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9b71da5cc1cb4f12af4b04fc5597e4d1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/1455 [00:00, ? examples/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a3f3ec986b044612a653fc7e27faa3b0", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Creating parquet from Arrow format: 0%| | 0/15 [00:00, ?ba/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8761c776867044e0a39c5be67b5e4536", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/1455 [00:00, ? examples/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2d64775cae0e46c2b96db080977f7caf", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Creating parquet from Arrow format: 0%| | 0/15 [00:00, ?ba/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "81348e1aa35f41b9b68e178c9f7fd203", "version_major": 2, "version_minor": 0 }, "text/plain": [ "README.md: 0%| | 0.00/478 [00:00, ?B/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "CommitInfo(commit_url='https://huggingface.co/datasets/dnth/pets-enriched/commit/1e0711eae3c228b4bdd45096d28ae167f1ae49e7', commit_message='Upload dataset', commit_description='', oid='1e0711eae3c228b4bdd45096d28ae167f1ae49e7', pr_url=None, pr_revision=None, pr_num=None)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset.push_to_hub('dnth/pets-enriched')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "421ffbe1dea442bc956d20ccedc539f8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value='