# How do I make a dataset for vision models?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%3E%5C
**Category:** 🤗Datasets\
**Created:** [April 15, 2024, 3:28pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877 "2024-04-15T15:28:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![marksuccsmfewercoc](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@marksuccsmfewercoc](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarksuccsmfewercoc)%5C
**Post date:** [April 15, 2024, 3:28pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F1 "2024-04-15T15:28:26Z")

</div>

So I was reading this blog [Vision Language Models Explained](https://huggingface.co/blog/vlms) on fine-tuning LLaVA from a dataset from HF, and I was wondering - How can I upload my custom dataset on HF to do that same with my data? Can anyone help me out with that?

Currently, I have an images folder that contains all the images and have a file named data.json in my root folder. How will I process would look like? I already read some tutorials on docs.

data.json:

```auto
[
  {
    "role": "user",
    "content": [
      { "type": "text", "text": "What’s in this image?" },
      { "type": "image" }
    ]
  },
  { "role": "assistant", "content": [{ "type": "text", "text": "DOG" }] },
  {
    "role": "user",
    "content": [
      { "type": "text", "text": "What’s in this image?" },
      { "type": "image" }
    ]
  },
  { "role": "assistant", "content": [{ "type": "text", "text": "CAT" }] },
  {
    "role": "user",
    "content": [
      { "type": "text", "text": "What’s in this image?" },
      { "type": "image" }
    ]
  },
  { "role": "assistant", "content": [{ "type": "text", "text": "DUCK" }] }
]

```

---

<div class="post-metadata">

**Author:** ![marksuccsmfewercoc](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@marksuccsmfewercoc](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarksuccsmfewercoc)%5C
**Post date:** [April 16, 2024, 5:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F2 "2024-04-16T05:41:45Z")

</div>

Any idea? @lhoestq @mariosasko

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flhoestq)%5C
**Post date:** [April 16, 2024, 10:06am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F3 "2024-04-16T10:06:55Z")

</div>

In the blog they use the dataset at [HuggingFaceH4/llava-instruct-mix-vsft · Datasets at Hugging Face](https://huggingface.co/datasets/HuggingFaceH4/llava-instruct-mix-vsft), so you should use the same format. I notice your already have the JSON part in the right format, you just need to associate the JSON part with the images.

You can do so programmatically:

```python
from datasets import Dataset, Image, Sequence

ds = Dataset.from_dict({"messages": your_data_json_messages_list})
ds = ds.add_column("images", [[img_path] for img_path in img_paths]) # contains lists of 1 image
ds = ds.cast_column("images", Sequence(Image())) # from string type to image type

# Optionally push to the HF Hub
ds.push_to_hub("username/datasetname")

```

---

<div class="post-metadata">

**Author:** ![marksuccsmfewercoc](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@marksuccsmfewercoc](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarksuccsmfewercoc)%5C
**Post date:** [April 16, 2024, 10:29am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F4 "2024-04-16T10:29:27Z")

</div>

Getting this error “ValueError: Failed to concatenate on axis=1 because tables don’t have the same number of rows”

```auto
import os
import json
from datasets import Dataset, Image, Sequence

with open("data.json", "r") as f:
    your_data_json = json.load(f)

img_folder = "images/"
img_paths = [os.path.join(img_folder, img_name) for img_name in os.listdir(img_folder) if img_name.endswith(('.jpg', '.jpeg', '.png'))]

ds = Dataset.from_dict({"messages": your_data_json})

ds = ds.add_column("images", [[img_path] for img_path in img_paths])  

ds = ds.cast_column("images", Sequence(Image()))

ds.push_to_hub("marksuccsmfewercoc/test")

```

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flhoestq)%5C
**Post date:** [April 16, 2024, 10:52am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F5 "2024-04-16T10:52:43Z")

</div>

You should have as many images as items in your messages list for it to work. In particular you can make sure each item in the messages list is a list of one user message and one assistant message.

---

<div class="post-metadata">

**Author:** ![marksuccsmfewercoc](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@marksuccsmfewercoc](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarksuccsmfewercoc)%5C
**Post date:** [April 16, 2024, 11:14am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F6 "2024-04-16T11:14:20Z")

</div>

But the LLava dataset and mine dataset look very different how to fix it?

here is what the correct dataset looks like [HuggingFaceH4/llava-instruct-mix-vsft · Datasets at Hugging Face](https://huggingface.co/datasets/HuggingFaceH4/llava-instruct-mix-vsft?row=0) and here is what my dataset looks like [marksuccsmfewercoc/test · Datasets at Hugging Face](https://huggingface.co/datasets/marksuccsmfewercoc/test)

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flhoestq)%5C
**Post date:** [April 16, 2024, 11:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F7 "2024-04-16T11:41:23Z")

</div>

The `messages` column should contain lists (one list per image), for example the first list can look like

```auto
[
  {
    "content": [{ "index": null, "text": "What’s in this image?\n", "type": "text" }, { "index": 0, "text": null, "type": "image" }],
    "role": "user"
  },
  {
    "content": [{ "index": null, "text": "DOG", "type": "text" }],
    "role": "assistant" 
  }
]

```

---

<div class="post-metadata">

**Author:** ![marksuccsmfewercoc](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@marksuccsmfewercoc](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarksuccsmfewercoc)%5C
**Post date:** [April 16, 2024, 11:58am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F8 "2024-04-16T11:58:40Z")

</div>

So what does my JSON file look like? before uploading it to the hub

---

<div class="post-metadata">

**Author:** ![marksuccsmfewercoc](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@marksuccsmfewercoc](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarksuccsmfewercoc)%5C
**Post date:** [April 16, 2024, 3:39pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F10 "2024-04-16T15:39:31Z")

</div>

Can you please tell me what’s the correct format to make it work with fine-tuning LLaVA according to the blog by HF?

I just have three images in my images folder.

---

<div class="post-metadata">

**Author:** ![marksuccsmfewercoc](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@marksuccsmfewercoc](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarksuccsmfewercoc)%5C
**Post date:** [April 17, 2024, 11:51am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F11 "2024-04-17T11:51:56Z")

</div>

Hey @lhoestq I’m still getting “raise ValueError(“Failed to concatenate on axis=1 because tables don’t have the same number of rows”) ValueError: Failed to concatenate on axis=1 because tables don’t have the same number of rows”.

But when I print the length of images and JSON it prints 3 and 3 which is correct. Any idea about what can be the issue?

```auto

import os
import json
from datasets import Dataset, Image, Sequence

with open("data.json", "r") as f:
    your_data_json = json.load(f)

print(len(your_data_json))
print(len(os.listdir('images/')))

ds = Dataset.from_dict({"messages": your_data_json})
ds = ds.add_column("images", [[img_path] for img_path in 'images/']) 
ds = ds.cast_column("images", Sequence(Image()))  

ds.push_to_hub("marksuccsmfewercoc/test")

```

I don’t understand if both len(your\_data\_json) value and (len(os.listdir(‘images/’)) are 3 then what’s the issue?

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flhoestq)%5C
**Post date:** [April 19, 2024, 8:48am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F12 "2024-04-19T08:48:03Z")

</div>

Hi ! I think there is a small bug in your code:

```diff
- ds = ds.add_column("images", [[img_path] for img_path in 'images/']) 
+ ds = ds.add_column("images", [[img_path] for img_path in os.listdir('images/')]) 

```

---

<div class="post-metadata">

**Author:** ![marksuccsmfewercoc](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@marksuccsmfewercoc](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarksuccsmfewercoc)%5C
**Post date:** [April 20, 2024, 8:20am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F13 "2024-04-20T08:20:39Z")

</div>

Very confusing, Now I’m getting FileNotFoundError: [Errno 2] No such file or directory: ‘dog.png’ error, But I do have a dog.png file in my images folder

---

<div class="post-metadata">

**Author:** ![marksuccsmfewercoc](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@marksuccsmfewercoc](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmarksuccsmfewercoc)%5C
**Post date:** [April 20, 2024, 2:54pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-do-i-make-a-dataset-for-vision-models%2F81877%2F14 "2024-04-20T14:54:09Z")

</div>

Hey @lhoestq I fixed that problem but the images and dataset are not in proper sequence, how to fix this? How to link images with each conversations in the JSON data?
