Datasets

Datasets

HuggingFace Datasets is a library for easily accessing and sharing datasets for Audio, Computer Vision, and Natural Language Processing (NLP) tasks. Load a Dataset with just one line of code and utilize our robust data processing methods to prepare it swiftly for deep learning model training.

Thanks to the Apache Arrow format, you can handle large datasets with zero-copy reads, ensuring optimal speed and efficiency without memory constraints. The deep integration with the Hugging Face Hub also allows you to seamlessly load and share datasets with the machine learning community.

Discover your dataset on the Hugging Face Hub today and explore it in detail with the live viewer.

Quickstart

This quickstart guide is designed for developers who are eager to jump into the code and see an example of integrating Hugging Face Datasets into their model training workflow. Beginners should start with our tutorials for a more comprehensive introduction.

Each dataset is unique, and some may require additional steps for preparation depending on the task. However, Datasets tools make it easy to load and process datasets. To get started quickly and efficiently, load an existing dataset from the Hugging Face Hub. With thousands of datasets available for various tasks, you can select the type that fits your needs and get going!

Start by installing Datasets:

pip install datasets

Datasets also support audio and image data formats:

  • To work with audio datasets, install the Audio feature:Copiedpip install datasets
  • To work with image datasets, install the Image feature:Copiedpip install datasets[vision]

Besides Hugging Face Datasets, make sure your preferred machine learning framework is installed:

Pytorch

pip install torch

TensorFlow

pip install tensorflow

Read detailed installation guide here.

What is Arrow?

Apache Arrow is a high-performance, columnar memory format designed to accelerate data processing and movement. It offers several key benefits:

  • Zero-Copy Reads: Arrow’s standard format enables zero-copy reads, eliminating nearly all serialization overhead and enhancing speed.
  • Language-Agnostic: It supports multiple programming languages, facilitating interoperability across different tools and environments.
  • Column-Oriented: Arrow’s columnar layout speeds up querying and processing of data slices or columns.
  • Copy-Free Integration: It allows seamless integration with popular machine learning libraries like NumPy, Pandas, PyTorch, and TensorFlow without additional data copying.
  • Flexible Data Types: Arrow supports many complex and nested column types.

Memory-Mapping and Performance

🤗 Datasets leverages Arrow for efficient local caching. It enables datasets to be memory-mapped from disk, which means large datasets can be accessed without consuming extensive RAM. For instance, loading the English Wikipedia dataset requires only a few MB of RAM due to Arrow’s memory-mapped design.

Example:

pythonCopy codeimport os
import psutil
import timeit
from datasets import load_dataset

# Measure RAM before and after loading dataset
mem_before = psutil.Process(os.getpid()).memory_info().rss / (1024 * 1024)
wiki = load_dataset("wikipedia", "20220301.en", split="train")
mem_after = psutil.Process(os.getpid()).memory_info().rss / (1024 * 1024)

print(f"RAM memory used: {(mem_after - mem_before)} MB")

Performance Benchmark

Iterating over a memory-mapped Arrow dataset like Wikipedia can be extremely fast. On a laptop, iteration speeds can reach 1-3 Gbit/s.

pythonCopy codes = """batch_size = 1000
for batch in wiki.iter(batch_size):
...
"""

elapsed_time = timeit.timeit(stmt=s, number=1, globals=globals())
print(f"Time to iterate over the {wiki.dataset_size >> 30} GB dataset: {elapsed_time:.1f} sec, "
f"ie. {float(wiki.dataset_size >> 27)/elapsed_time:.1f} Gb/s")