資料集文件

建立影像資料集

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

建立圖像資料集

建立並共享圖像資料集有兩種方法。本指南將向您展示如何:

  • 使用 Python 中的 Dataset.push_to_hub() 從本機檔案建立圖像資料集。這是一個簡單的方法,在 Python 中只需幾個步驟即可完成。

  • 使用 ImageFolder 和一些後設資料(metadata)建立圖像資料集。這是一種無需程式碼的解決方案,可用於快速建立包含數千張圖像的圖像資料集。

您可以要求使用者先分享其聯絡資訊,藉此控管對資料集的存取權限。請查看 Gated datasets (受限資料集) 指南,了解如何在 Hub 上啟用此功能。

ImageFolder

ImageFolder 是一個資料集建構工具,專為快速載入包含數千張圖像的圖像資料集而設計,無需您編寫任何程式碼。

💡 請查看分割模式層級 (Split pattern hierarchy),以進一步了解 ImageFolder 如何根據您的資料集儲存庫結構建立資料集分割。

ImageFolder 會根據目錄名稱自動推斷資料集的類別標籤。請將您的資料集儲存為以下目錄結構:

folder/train/dog/golden_retriever.png
folder/train/dog/german_shepherd.png
folder/train/dog/chihuahua.png

folder/train/cat/maine_coon.png
folder/train/cat/bengal.png
folder/train/cat/birman.png

如果資料集遵循 ImageFolder 結構,則可以直接使用 load_dataset() 載入它:

>>> from datasets import load_dataset

>>> dataset = load_dataset("path/to/folder")

這等同於在 load_dataset() 中手動傳入 imagefolder,並在 data_dir 中指定目錄:

>>> dataset = load_dataset("imagefolder", data_dir="/path/to/folder")

您也可以使用 imagefolder 載入包含多個分割(splits)的資料集。為此,您的資料集目錄應具有以下結構:

folder/train/dog/golden_retriever.png
folder/train/cat/maine_coon.png
folder/test/dog/german_shepherd.png
folder/test/cat/bengal.png

如果所有圖像檔案都包含在單一目錄中,或者它們不在相同的目錄層級結構中,則不會自動新增 label 欄位。如果您需要它,請明確設定 drop_labels=False

如果您想加入有關資料集的額外資訊(例如文字註解或邊界框),請在資料夾中新增一個 metadata.csv 檔案。這讓您可以快速為不同的電腦視覺任務建立資料集,例如文字描述 (captioning) 或物件偵測。您也可以使用 metadata.jsonl (JSONL 檔案) 或 metadata.parquet (Parquet 檔案)。

folder/train/metadata.csv
folder/train/0001.png
folder/train/0002.png
folder/train/0003.png

您也可以壓縮您的圖像;在這種情況下,每個 zip 檔案應同時包含圖像和後設資料。

folder/train.zip
folder/test.zip
folder/validation.zip

您的 metadata.csv 檔案必須包含一個 file_name*_file_name 欄位,用於將圖像檔案與其後設資料連結起來。

file_name,additional_feature
0001.png,This is a first value of a text feature you added to your images
0002.png,This is a second value of a text feature you added to your images
0003.png,This is a third value of a text feature you added to your images

或者使用 metadata.jsonl

{"file_name": "0001.png", "additional_feature": "This is a first value of a text feature you added to your images"}
{"file_name": "0002.png", "additional_feature": "This is a second value of a text feature you added to your images"}
{"file_name": "0003.png", "additional_feature": "This is a third value of a text feature you added to your images"}

此處的 file_name 必須是緊鄰後設資料檔案的圖像檔案名稱。更廣義地說,它必須是從包含後設資料的目錄到圖像檔案的相對路徑。

在資料集的每一行中指向多張圖像也是可能的,例如您的輸入和輸出皆為圖像時。

{"input_file_name": "0001.png", "output_file_name": "0001_output.png"}
{"input_file_name": "0002.png", "output_file_name": "0002_output.png"}
{"input_file_name": "0003.png", "output_file_name": "0003_output.png"}

您也可以定義圖像列表。在這種情況下,您需要將欄位命名為 file_names*_file_names。以下是一個範例:

{"frames_file_names": ["0001_t0.png", "0001_t1.png"], label: "moving_up"}
{"frames_file_names": ["0002_t0.png", "0002_t1.png"], label: "moving_down"}
{"frames_file_names": ["0003_t0.png", "0003_t1.png"], label: "moving_right"}

圖像說明 (Image captioning)

圖像標註資料集包含描述圖像的文字。一個 metadata.csv 的範例可能如下所示:

file_name,text
0001.png,This is a golden retriever playing with a ball
0002.png,A german shepherd
0003.png,One chihuahua

使用 ImageFolder 載入資料集,它將為圖像標註建立一個 text 欄位。

>>> dataset = load_dataset("imagefolder", data_dir="/path/to/folder", split="train")
>>> dataset[0]["text"]
"This is a golden retriever playing with a ball"

物件偵測

物件偵測資料集具有識別圖像中物件的邊界框(bounding boxes)和類別。一個 metadata.jsonl 的範例可能如下所示:

{"file_name": "0001.png", "objects": {"bbox": [[302.0, 109.0, 73.0, 52.0]], "categories": [0]}}
{"file_name": "0002.png", "objects": {"bbox": [[810.0, 100.0, 57.0, 28.0]], "categories": [1]}}
{"file_name": "0003.png", "objects": {"bbox": [[160.0, 31.0, 248.0, 616.0], [741.0, 68.0, 202.0, 401.0]], "categories": [2, 2]}}

使用 ImageFolder 載入資料集,它將建立一個包含邊界框和類別的 objects 欄位。

>>> dataset = load_dataset("imagefolder", data_dir="/path/to/folder", split="train")
>>> dataset[0]["objects"]
{"bbox": [[302.0, 109.0, 73.0, 52.0]], "categories": [0]}

將資料集上傳至 Hub

一旦您建立了資料集,就可以使用 push_to_hub() 方法將其共享到 Hub。請確保您已安裝 huggingface_hub 函式庫,並且已登入您的 Hugging Face 帳戶(詳情請參閱使用 Python 上傳教學)。

使用 push_to_hub() 上傳您的資料集:

>>> from datasets import load_dataset

>>> dataset = load_dataset("imagefolder", data_dir="/path/to/folder", split="train")
>>> dataset.push_to_hub("stevhliu/my-image-captioning-dataset")

WebDataset

WebDataset 格式基於 TAR 存檔,適用於大型圖像資料集。實際上,您可以將圖像分組存入 TAR 存檔(例如每個 TAR 存檔 1GB 的圖像),並擁有數千個 TAR 存檔。

folder/train/00000.tar
folder/train/00001.tar
folder/train/00002.tar
...

在壓縮檔中,每個範例都由共用相同前綴 (prefix) 的檔案組成。

e39871fd9fd74f55.jpg
e39871fd9fd74f55.json
f18b91585c4d3f3e.jpg
f18b91585c4d3f3e.json
ede6e66b2fb59aab.jpg
ede6e66b2fb59aab.json
ed600d57fcee4f94.jpg
ed600d57fcee4f94.json
...

例如,您可以使用 JSON 或文字檔案來放置圖像的標籤/標註/邊界框。

載入您的 WebDataset,它將為每個檔案字尾(此處為“jpg”和“json”)建立一個欄位。

>>> from datasets import load_dataset

>>> dataset = load_dataset("webdataset", data_dir="/path/to/folder", split="train")
>>> dataset[0]["json"]
{"bbox": [[302.0, 109.0, 73.0, 52.0]], "categories": [0]}

每個樣本包含多張圖像也是可以的,如下所示:

e39871fd9fd74f55.input.jpg
e39871fd9fd74f55.output.jpg
e39871fd9fd74f55.json
f18b91585c4d3f3e.input.jpg
f18b91585c4d3f3e.output.jpg
f18b91585c4d3f3e.json
...

有關 WebDataset 格式與 Python 函式庫的更多詳細資訊,請查看 WebDataset 文件

Lance

Lance 是一種開放的多模態 Lakehouse 表格格式。Lance 表格不僅可以原生儲存文字和純量值,還可以在表格數據旁邊儲存大型二進位物件(blobs),例如圖像、音訊和影片。

從磁碟上的圖像檔案加上相關的後設資料(例如標註和尺寸)開始,您可以將一個自包含的 Lance 資料集寫入本機的 *.lance 目錄。產生的表格可以在包含編碼後圖像位元組的 image 欄位旁邊,儲存您的後設資料欄位。

例如,您可以從這樣的後設資料開始:

{'caption': 'Cordelia and Dudley on their wedding  day last year', 'height': 315, 'width': 233}
{'caption': 'Statistics on challenges for automation in 2021', 'height': 299, 'width': 701}

您可以為您的後設資料和圖像位元組定義一個 pyarrow 結構描述(schema),建立表格,並將其寫入為 Lance 資料集:

import lance
import pyarrow as pa

schema = pa.schema(
    [
        pa.field("caption", pa.utf8()),
        pa.field("height", pa.int32()),
        pa.field("width", pa.int32()),
        # ... add any additional metadata columns you want here ...
        pa.field("image", pa.binary()),
    ]
)

# Provide image files alongside metadata
rows = [
    {
        "image_path": "/path/to/images/0001.jpg",
        "caption": "Cordelia and Dudley on their wedding  day last year",
        "height": 315,
        "width": 233,
    },
    {
        "image_path": "/path/to/images/0002.jpg",
        "caption": "Statistics on challenges for automation in 2021",
        "height": 299,
        "width": 701,
    },
]

image_bytes = []
for r in rows:
    with open(r["image_path"], "rb") as f:
        image_bytes.append(f.read())

table = pa.table(
    {
        "caption": [r["caption"] for r in rows],
        "height": [r["height"] for r in rows],
        "width": [r["width"] for r in rows],
        "image": image_bytes,
    },
    schema=schema,
)

ds = lance.write_dataset(
    table,
    "./images.lance",
    schema=schema,
    mode="create",
)

這是一個儲存圖像的 Lance 表格的代表性視圖(image 欄位包含編碼後的位元組):

+-----------------------------------------------+--------+-------+-----+------------------------------+
| caption                                       | height | width | ... | image                        |
+-----------------------------------------------+--------+-------+-----+------------------------------+
| "Cordelia and Dudley on their wedding ..."    | 315    | 233   | ... | b"\\xff\\xd8\\xff...\\xd9"   |
| "Statistics on challenges for automation ..." | 299    | 701   | ... | b"\\xff\\xd8\\xff...\\xd9"   |
+-----------------------------------------------+--------+-------+-----+------------------------------+

使用這種方法,您可以在 Lance 中儲存任意大的圖像資料集。產生的 images.lance/ 目錄及其 *.lance 檔案可以像上述其他範例一樣上傳到 Hugging Face Hub。請參考 Hub 上 lance-format/laion-1m 資料集以獲取 Lance 圖像資料集的範例。

有關使用 Lance 資料集的更多詳細資訊,請參閱 Lance 文件 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.