新增資料集集合

按照本指南建立新的資料集集合 (在 TFDS 或您自己的存放庫中)。

總覽

若要將新的資料集集合 my_collection 新增至 TFDS,使用者需要產生一個包含下列檔案的 my_collection 資料夾

my_collection/
  __init__.py
  my_collection.py # Dataset collection definition
  my_collection_test.py # (Optional) test
  description.md # (Optional) collection description (if not included in my_collection.py)
  citations.md # (Optional) collection citations (if not included in my_collection.py)

依照慣例,新的資料集集合應新增至 TFDS 存放庫中的 tensorflow_datasets/dataset_collections/ 資料夾。

撰寫您的資料集集合

所有資料集集合皆為 tfds.core.dataset_collection_builder.DatasetCollection 的實作子類別。

以下是在檔案 my_collection.py 中定義的資料集集合建構工具的最小範例

import collections
from typing import Mapping
from tensorflow_datasets.core import dataset_collection_builder
from tensorflow_datasets.core import naming

class MyCollection(dataset_collection_builder.DatasetCollection):
  """Dataset collection builder my_dataset_collection."""

  @property
  def info(self) -> dataset_collection_builder.DatasetCollectionInfo:
    return dataset_collection_builder.DatasetCollectionInfo.from_cls(
        dataset_collection_class=self.__class__,
        description="my_dataset_collection description.",
        release_notes={
            "1.0.0": "Initial release",
        },
    )

  @property
  def datasets(
      self,
  ) -> Mapping[str, Mapping[str, naming.DatasetReference]]:
    return collections.OrderedDict({
        "1.0.0":
            naming.references_for({
                "dataset_1": "natural_questions/default:0.0.2",
                "dataset_2": "media_sum:1.0.0",
            }),
        "1.1.0":
            naming.references_for({
                "dataset_1": "natural_questions/longt5:0.1.0",
                "dataset_2": "media_sum:1.0.0",
                "dataset_3": "squad:3.0.0"
            })
    })

下節說明要覆寫的 2 個抽象方法。

info:資料集集合中繼資料

info 方法會傳回包含集合中繼資料的 dataset_collection_builder.DatasetCollectionInfo

資料集集合資訊包含四個欄位

  • name:資料集集合的名稱。
  • description:資料集集合的 Markdown 格式說明。定義資料集集合說明的方式有兩種:(1) 直接在集合的 my_collection.py 檔案中以 (多行) 字串形式定義,與 TFDS 資料集的現有做法類似;(2) 在 description.md 檔案中定義,該檔案必須放置在資料集集合資料夾中。
  • release_notes:從資料集集合版本到對應版本資訊的對應。
  • citation:資料集集合的選用 BibTeX 引用 (或引用清單)。定義資料集集合引用的方式有兩種:(1) 直接在集合的 my_collection.py 檔案中以 (多行) 字串形式定義,與 TFDS 資料集的現有做法類似;(2) 在 citations.bib 檔案中定義,該檔案必須放置在資料集集合資料夾中。

datasets:定義集合中的資料集

datasets 方法會傳回集合中的 TFDS 資料集。

其定義為版本的字典,其中說明資料集集合的演進。

針對每個版本,內含的 TFDS 資料集會儲存為從資料集名稱到 naming.DatasetReference 的字典。例如

class MyCollection(dataset_collection_builder.DatasetCollection):
  ...
  @property
  def datasets(self):
    return {
        "1.0.0": {
            "yes_no":
                naming.DatasetReference(
                    dataset_name="yes_no", version="1.0.0"),
            "sst2":
                naming.DatasetReference(
                    dataset_name="glue", config="sst2", version="2.0.0"),
            "assin2":
                naming.DatasetReference(
                    dataset_name="assin2", version="1.0.0"),
        },
        ...
    }

naming.references_for 方法提供更精簡的方式來表達與上述相同的內容

class MyCollection(dataset_collection_builder.DatasetCollection):
  ...
  @property
  def datasets(self):
    return {
        "1.0.0":
            naming.references_for({
                "yes_no": "yes_no:1.0.0",
                "sst2": "glue/sst:2.0.0",
                "assin2": "assin2:1.0.0",
            }),
        ...
    }

單元測試您的資料集集合

DatasetCollectionTestBase 是資料集集合的基本測試類別。其提供許多簡單的檢查,以確保資料集集合已正確註冊,且其資料集存在於 TFDS 中。

唯一要設定的類別屬性是 DATASET_COLLECTION_CLASS,其指定要測試的資料集集合的類別物件。

此外,使用者可以設定下列類別屬性

  • VERSION:用於執行測試的資料集集合版本 (預設為最新版本)。
  • DATASETS_TO_TEST:包含要在 TFDS 中測試其是否存在的資料集的清單 (預設為集合中的所有資料集)。
  • CHECK_DATASETS_VERSION:是否檢查資料集集合中版本化資料集的存在性,或檢查其預設版本 (預設為 true)。

資料集集合最簡單的有效測試會是

from tensorflow_datasets.testing.dataset_collection_builder_testing import DatasetCollectionTestBase
from . import my_collection

class TestMyCollection(DatasetCollectionTestBase):
  DATASET_COLLECTION_CLASS = my_collection.MyCollection

執行下列指令以測試資料集集合。

python my_dataset_test.py

意見回饋

我們不斷嘗試改善資料集建立工作流程,但唯有在我們瞭解問題時才能做到。您在建立資料集集合時遇到哪些問題或錯誤?是否有任何部分令人困惑,或第一次使用時無法運作?

請在 GitHub 上分享您的意見回饋。