{ "cells": [ { "cell_type": "markdown", "id": "f175028c", "metadata": {}, "source": [ "## The \"Wild Khmer\" Dataset Creation Script" ] }, { "cell_type": "code", "execution_count": 8, "id": "1ed4f71e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded 10000 images from JSON\n", "Processing images and labels... This may take a while.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "42c314cacca648d6a0e8b4df6093a9d9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Generating train split: 0 examples [00:00, ? examples/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 10000/10000 [02:28<00:00, 67.53it/s]\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2b0fef7ef288405c908d8e5ca92348fe", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading dataset shards: 0%| | 0/17 [00:00 Normalized Bounding Boxes)\n", " regions_list = []\n", " for region in data['regions']:\n", " try:\n", " # Extract coordinates\n", " xs = region['shape_attributes']['all_points_x']\n", " ys = region['shape_attributes']['all_points_y']\n", " label = region['region_attributes']['label']\n", "\n", " # Convert Polygon to Bounding Box (ymin, xmin, ymax, xmax)\n", " xmin, xmax = min(xs), max(xs)\n", " ymin, ymax = min(ys), max(ys)\n", "\n", " # Normalize to 0-1000 scale (Qwen3-VL Standard)\n", " n_xmin = int((xmin / width) * 1000)\n", " n_xmax = int((xmax / width) * 1000)\n", " n_ymin = int((ymin / height) * 1000)\n", " n_ymax = int((ymax / height) * 1000)\n", "\n", " # Format as a dictionary for the grounding task\n", " regions_list.append({\n", " \"bbox_2d\": [n_ymin, n_xmin, n_ymax, n_xmax],\n", " \"label\": label\n", " })\n", " except KeyError:\n", " continue # Skip regions without labels or points\n", "\n", " # 3. Create the final text label (JSON string)\n", " # This will be processed by your convert_to_conversation function\n", " grounding_json = json.dumps({\"regions\": regions_list}, ensure_ascii=False)\n", "\n", " yield {\n", " \"image\": img_bytes,\n", " \"text\": grounding_json\n", " }\n", "\n", "# -------------------------\n", "# DATASET FEATURES\n", "# -------------------------\n", "features = Features({\n", " \"image\": datasets_Image(), # embedded image bytes\n", " \"text\": Value(\"string\"), # JSON string of boxes and text\n", "})\n", "\n", "# -------------------------\n", "# CREATE & SAVE\n", "# -------------------------\n", "print(\"Processing images and labels... This may take a while.\")\n", "ds = Dataset.from_generator(generate_examples, features=features)\n", "\n", "# Shuffle and split\n", "ds = ds.train_test_split(test_size=TEST_SPLIT)\n", "\n", "print(\"Saving Parquet files...\")\n", "ds[\"train\"].to_parquet(TRAIN_OUT)\n", "ds[\"test\"].to_parquet(TEST_OUT)\n", "\n", "print(\"DONE ✅\")\n", "print(f\"Wild Train file: {TRAIN_OUT}\")" ] }, { "cell_type": "markdown", "id": "b1f80720", "metadata": {}, "source": [ "## Open The Image Dataset For Checking" ] }, { "cell_type": "code", "execution_count": 19, "id": "57395385", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d2da1e3e954a45fdb05b47c8d38e66b1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading dataset shards: 0%| | 0/17 [00:00\n", "\n" ] } ], "source": [ "from datasets import Dataset\n", "ds = Dataset.from_parquet(\"wild_khmer_train.parquet\")\n", "sample = ds[1][\"image\"]\n", "print(type(sample))\n", "print(sample)\n", "sample.show()\n" ] }, { "cell_type": "markdown", "id": "2307a106", "metadata": {}, "source": [ "## DUMMY DATASET ROW" ] }, { "cell_type": "markdown", "id": "aa809986", "metadata": {}, "source": [ "```python\n", "{\n", " 'image': ,\n", " 'text': '{\"regions\": [{\"bbox_2d\": [150, 200, 300, 800], \"label\": \"អាហារដ្ឋានមិត្តភាព\"}, {\"bbox_2d\": [850, 400, 920, 600], \"label\": \"012 345 678\"}]}'\n", "}\n", "```" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 5 }