{ "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