File size: 9,022 Bytes
b0c0df0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Contribute Your Models and Datasets\n",
"\n",
"[](https://colab.research.google.com/github/EvolvingLMMs-Lab/lmms-eval/blob/main/tools/make_image_hf_dataset.ipynb)\n",
"\n",
"This notebook will guide you to make correct format of Huggingface dataset, in proper parquet format and visualizable in Huggingface dataset hub.\n",
"\n",
"We will take the example of the dataset [`lmms-lab/VQAv2_TOY`](https://huggingface.co/datasets/lmms-lab/VQAv2_TOY) and convert it to the proper format."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preparation\n",
"\n",
"We need to install `datasets` library to create the dataset and `Pillow` to handle images."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"!pip install datasets Pillow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And we need to login into Hugging Face to upload the dataset. You should goto the [Hugging Face website](https://huggingface.co/settings/tokens) to get your API token."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"!huggingface-cli login --token hf_YOUR_HF_TOKEN # replace hf_YOUR_HF_TOKEN to your own Hugging Face token."
]
},
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"source": [
"## Download Dataset\n",
"\n",
"We have uploaded the zip file of the dataset to [Hugging Face](https://huggingface.co/datasets/pufanyi/VQAv2_TOY/tree/main/source_data) for download. This dataset is a subset of the [VQAv2](https://visualqa.org/) dataset, with $20$ entries each from the `val`, `test`, and `test-dev` splits, for easier downloading."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"!wget https://huggingface.co/datasets/lmms-lab/VQAv2_TOY/resolve/main/source_data/sample_data.zip -P data\n",
"!unzip data/sample_data.zip -d data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can open `data/questions` to take a view of the dataset organization. We found that the toy-`VQAv2` dataset is organized as follows:\n",
"\n",
"```json\n",
"{\n",
" \"info\": { /* some infomation */ },\n",
" \"task_type\": \"TASK_TYPE\", \"data_type\": \"mscoco\",\n",
" \"license\": { /* some license */ },\n",
" \"questions\": [\n",
" {\n",
" \"image_id\": 262144, // integer id of the image\n",
" \"question\": \"Is the ball flying towards the batter?\",\n",
" \"question_id\": 262144000\n",
" },\n",
" /* ... */\n",
" ]\n",
"}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define Dataset Features _(Optional<sup>*</sup>)_\n",
"\n",
"You can define the features of the dataset. For more details, please refer to the [official documentation](https://huggingface.co/docs/datasets/en/about_dataset_features).\n",
"\n",
"<sup>*</sup> _Note that if the dataset features are consistent and all entries in your dataset table are non-null **for all splits of data**, you can skip this step._"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import datasets\n",
"\n",
"features = datasets.Features(\n",
" {\n",
" \"question_id\": datasets.Value(\"int64\"),\n",
" \"question\": datasets.Value(\"string\"),\n",
" \"image_id\": datasets.Value(\"string\"),\n",
" \"image\": datasets.Image(),\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define Data Generator\n",
"\n",
"We use [`datasets.Dataset.from_generator`](https://huggingface.co/docs/datasets/v2.20.0/en/package_reference/main_classes#datasets.Dataset.from_generator) to create the dataset.\n",
"\n",
"The generator function should `yield` dictionaries with the keys corresponding to the dataset features. This can save memory when loading large datasets.\n",
"\n",
"For the image data, we can convert the image to [`PIL.Image`](https://pillow.readthedocs.io/en/stable/reference/Image.html) object.\n",
"\n",
"Note that if some columns are missing in some splits of the dataset (for example, the `answer` column is usually missing in the `test` split), we need to set these columns to null to ensure that all splits have the same features."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"from PIL import Image\n",
"\n",
"\n",
"def generator(qa_file, image_folder, image_prefix):\n",
" with open(qa_file, \"r\") as f:\n",
" data = json.load(f)\n",
" qa = data[\"questions\"]\n",
"\n",
" for q in qa:\n",
" image_id = q[\"image_id\"]\n",
" image_path = os.path.join(image_folder, f\"{image_prefix}_{image_id:012}.jpg\")\n",
" q[\"image\"] = Image.open(image_path)\n",
" yield q"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate Dataset\n",
"\n",
"We generate the dataset using the generator function.\n",
"\n",
"Note that if you skip the step of defining dataset features, there is no need to pass the `features` argument. The dataset infer the features from the dataset automatically."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"NUM_PROC = 32 # number of processes to use for multiprocessing, set to 1 for no multiprocessing\n",
"\n",
"data_val = datasets.Dataset.from_generator(\n",
" generator,\n",
" gen_kwargs={\n",
" \"qa_file\": \"data/questions/vqav2_toy_questions_val2014.json\",\n",
" \"image_folder\": \"data/images\",\n",
" \"image_prefix\": \"COCO_val2014\",\n",
" },\n",
" # For this dataset, there is no need to specify the features, as all cells are non-null and all splits have the same schema\n",
" # features=features,\n",
" num_proc=NUM_PROC,\n",
")\n",
"\n",
"data_test = datasets.Dataset.from_generator(\n",
" generator,\n",
" gen_kwargs={\n",
" \"qa_file\": \"data/questions/vqav2_toy_questions_test2015.json\",\n",
" \"image_folder\": \"data/images\",\n",
" \"image_prefix\": \"COCO_test2015\",\n",
" },\n",
" # features=features,\n",
" num_proc=NUM_PROC,\n",
")\n",
"\n",
"data_test_dev = datasets.Dataset.from_generator(\n",
" generator,\n",
" gen_kwargs={\n",
" \"qa_file\": \"data/questions/vqav2_toy_questions_test-dev2015.json\",\n",
" \"image_folder\": \"data/images\",\n",
" \"image_prefix\": \"COCO_test2015\",\n",
" },\n",
" # features=features,\n",
" num_proc=NUM_PROC,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dataset Upload\n",
"\n",
"Finally, we group the dataset with different splits and upload it to the Huggingface dataset hub."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = datasets.DatasetDict({\"val\": data_val, \"test\": data_test, \"test_dev\": data_test_dev})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data.push_to_hub(\"lmms-lab/VQAv2_TOY\") # replace lmms-lab to your username"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, you can check the dataset on the [Hugging Face dataset hub](https://huggingface.co/datasets/lmms-lab/VQAv2_TOY)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "lmms-eval",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|