Datasets 文件

建立資料集

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

建立資料集

有時,當您使用自己的資料時,可能需要建立資料集。使用 🤗 Datasets 建立資料集可以讓您的資料集擁有該庫的所有優勢:快速載入和處理、流式處理超大資料集記憶體對映等。您可以使用 🤗 Datasets 的低程式碼方法輕鬆快速地建立資料集,從而減少開始訓練模型所需的時間。在許多情況下,這就像將您的資料檔案拖放到 Hub 上的資料集儲存庫一樣簡單。

在本教程中,您將學習如何使用 🤗 Datasets 的低程式碼方法建立所有型別的資料集:

  • 基於資料夾的構建器,用於快速建立影像或音訊資料集
  • from_ 方法,用於從本地檔案建立資料集

基於檔案的構建器

🤗 Datasets 支援多種常見格式,如 csvjson/jsonlparquettxt

例如,它可以讀取一個由一個或多個 CSV 檔案組成的資料集(在這種情況下,將您的 CSV 檔案作為列表傳遞)

>>> from datasets import load_dataset
>>> dataset = load_dataset("csv", data_files="my_file.csv")

要獲取支援的格式列表和程式碼示例,請參閱此處的指南

基於資料夾的構建器

有兩個基於資料夾的構建器:ImageFolderAudioFolder。這些是低程式碼方法,用於快速建立包含數千個樣本的影像或語音和音訊資料集。它們非常適合在擴充套件到更大資料集之前快速原型化計算機視覺和語音模型。基於資料夾的構建器會獲取您的資料並自動生成資料集的特徵、拆分和標籤。

資料集的拆分是根據倉庫結構生成的,而標籤名稱則從目錄名稱中自動推斷出來。

例如,如果您的影像資料集(音訊資料集同理)是這樣儲存的:

pokemon/train/grass/bulbasaur.png
pokemon/train/fire/charmander.png
pokemon/train/water/squirtle.png

pokemon/test/grass/ivysaur.png
pokemon/test/fire/charmeleon.png
pokemon/test/water/wartortle.png

那麼,基於資料夾的構建器會這樣生成一個樣本:

透過在 load_dataset() 中指定 imagefolder 來建立影像資料集:

>>> from datasets import load_dataset

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

建立音訊資料集的方式相同,只是您需要在 load_dataset() 中指定 audiofolder

>>> from datasets import load_dataset

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

有關資料集的任何附加資訊,例如文字說明或轉錄,都可以透過在包含資料集的資料夾中新增一個 metadata.csv 檔案來包含。該元資料檔案需要有一個 file_name 列,用於將影像或音訊檔案與其相應的元資料關聯起來:

file_name, text
bulbasaur.png, There is a plant seed on its back right from the day this Pokémon is born.
charmander.png, It has a preference for hot things.
squirtle.png, When it retracts its long neck into its shell, it squirts out water with vigorous force.

要了解更多關於這些基於資料夾的構建器的資訊,請檢視 ImageFolderAudioFolder 指南。

從 Python 字典建立

您還可以從 Python 字典中的資料建立資料集。有兩種方式可以使用 from_ 方法建立資料集:

  • from_generator() 方法是利用 生成器 建立資料集時記憶體效率最高的方式,這得益於生成器的迭代行為。當您處理一個可能無法完全載入到記憶體中的非常大的資料集時,這尤其有用,因為資料集是在磁碟上逐步生成然後進行記憶體對映的。

    >>> from datasets import Dataset
    >>> def gen():
    ...     yield {"pokemon": "bulbasaur", "type": "grass"}
    ...     yield {"pokemon": "squirtle", "type": "water"}
    >>> ds = Dataset.from_generator(gen)
    >>> ds[0]
    {"pokemon": "bulbasaur", "type": "grass"}

    基於生成器的 IterableDataset 需要透過 for 迴圈等方式進行迭代:

    >>> from datasets import IterableDataset
    >>> ds = IterableDataset.from_generator(gen)
    >>> for example in ds:
    ...     print(example)
    {"pokemon": "bulbasaur", "type": "grass"}
    {"pokemon": "squirtle", "type": "water"}
  • from_dict() 方法是從字典建立資料集的一種直接方式:

    >>> from datasets import Dataset
    >>> ds = Dataset.from_dict({"pokemon": ["bulbasaur", "squirtle"], "type": ["grass", "water"]})
    >>> ds[0]
    {"pokemon": "bulbasaur", "type": "grass"}

    要建立影像或音訊資料集,請將 cast_column() 方法與 from_dict() 鏈式呼叫,並指定列和特徵型別。例如,要建立一個音訊資料集:

    >>> audio_dataset = Dataset.from_dict({"audio": ["path/to/audio_1", ..., "path/to/audio_n"]}).cast_column("audio", Audio())

現在您已經知道如何建立資料集了,考慮將其分享到 Hub 上,讓社群也能從您的工作中受益!請繼續下一節,學習如何分享您的資料集。

< > 在 GitHub 上更新

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