Loading data
In this section, we download the Food-101 dataset [1] that we will later use to train and fine-tune models.
Context
Step one of a classic deep learning workflow: getting the data. There are several options. In this example, we use Datasets from Hugging Face.
Load packages
Packages necessary for this section:
# to get information about a dataset before downloading it
from datasets import load_dataset_builder
# to load dataset from Hugging Face Hub
from datasets import load_dataset
# to display a few samples
import matplotlib.pyplot as pltChoosing a library
Data can be downloaded and processed manually, but many datasets are available via Hugging Face datasets, torchvision, and TensorFlow Datasets. Remember that JAX does not implement domain-specific utilities and is not a deep learning library. Flax is a deep learning library, but, because there are already so many good options to load and process data, they did not implement a method of their own.
Choose the library you are the most familiar with, or the one for which you found code somewhere, or the one that seems the easiest to you, or provides the exact functionality that you want for your project.
The Food-101 dataset for instance can be accessed with torchvision.datasets.Food101 since it is one of TorchVision datasets or with tfds.image_classification.Food101 since it is also one of TFDS datasets.
It is also in the Hugging Face Hub and that’s the method that we will use here.
Hugging Face datasets
The Datasets library from Hugging Face is a lightweight, framework-agnostic, and easy to use API to download datasets from the Hugging Face Hub. It uses Apache Arrow’s efficient caching system, allowing large datasets to be used on machines with small memory [2].
Search dataset
Go to the Hugging Face Hub and search through thousands of open source datasets provided by the community.
Inspect dataset
You can get information on a dataset before downloading it.
Load the dataset builder for the dataset you are interested in:
ds_builder = load_dataset_builder("food101")Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
--------------------------------------------------------------------------- HfUriError Traceback (most recent call last) Cell In[2], line 1 ----> 1 ds_builder = load_dataset_builder("food101") File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/load.py:1325, in load_dataset_builder(path, name, data_dir, data_files, cache_dir, features, download_config, download_mode, revision, token, storage_options, **config_kwargs) 1323 if features is not None: 1324 features = _fix_for_backward_compatible_features(features) -> 1325 dataset_module = dataset_module_factory( 1326 path, 1327 revision=revision, 1328 download_config=download_config, 1329 download_mode=download_mode, 1330 data_dir=data_dir, 1331 data_files=data_files, 1332 cache_dir=cache_dir, 1333 ) 1334 # Get dataset builder class 1335 builder_kwargs = dataset_module.builder_kwargs File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/load.py:1217, in dataset_module_factory(path, revision, download_config, download_mode, data_dir, data_files, cache_dir, **download_kwargs) 1212 if isinstance(e1, FileNotFoundError): 1213 raise FileNotFoundError( 1214 f"Couldn't find any data file at {relative_to_absolute_path(path)}. " 1215 f"Couldn't find '{path}' on the Hugging Face Hub either: {type(e1).__name__}: {e1}" 1216 ) from None -> 1217 raise e1 from None 1218 else: 1219 raise FileNotFoundError(f"Couldn't find any data file at {relative_to_absolute_path(path)}.") File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/load.py:1192, in dataset_module_factory(path, revision, download_config, download_mode, data_dir, data_files, cache_dir, **download_kwargs) 1182 else: 1183 use_exported_dataset_infos = True 1184 return HubDatasetModuleFactory( 1185 path, 1186 commit_hash=commit_hash, 1187 data_dir=data_dir, 1188 data_files=data_files, 1189 download_config=download_config, 1190 download_mode=download_mode, 1191 use_exported_dataset_infos=use_exported_dataset_infos, -> 1192 ).get_module() 1193 except GatedRepoError as e: 1194 message = f"Dataset '{path}' is a gated dataset on the Hub." File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/load.py:608, in HubDatasetModuleFactory.get_module(self) 606 download_config.download_desc = "Downloading standalone yaml" 607 try: --> 608 standalone_yaml_path = cached_path( 609 hf_dataset_url(self.name, config.REPOYAML_FILENAME, revision=self.commit_hash), 610 download_config=download_config, 611 ) 612 with open(standalone_yaml_path, encoding="utf-8") as f: 613 standalone_yaml_data = yaml.safe_load(f.read()) File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/utils/file_utils.py:180, in cached_path(url_or_filename, download_config, **download_kwargs) 174 # Download files from Hugging Face. 175 # Note: no need to check for https://huggingface.co file URLs since _prepare_path_and_storage_options 176 # prepares Hugging Face HTTP URLs as hf:// paths already 177 if url_or_filename.startswith("hf://") and not url_or_filename.startswith("hf://buckets/"): 178 resolved_path = huggingface_hub.HfFileSystem( 179 endpoint=config.HF_ENDPOINT, token=download_config.token --> 180 ).resolve_path(url_or_filename) 181 try: 182 output_path = huggingface_hub.HfApi( 183 endpoint=config.HF_ENDPOINT, 184 token=download_config.token, (...) 194 proxies=download_config.proxies, 195 ) File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/huggingface_hub/hf_file_system.py:305, in HfFileSystem.resolve_path(self, path, revision) 300 if path.count("/") == 0: 301 raise ValueError( 302 f"Repository id must be 'namespace/name', got '{path}'. Single-segment ids (e.g. 'gpt2') are no longer supported." 303 ) --> 305 parsed = parse_hf_uri(f"{constants.HF_PROTOCOL}{path}") 307 # --- Buckets --- 308 if parsed.is_bucket: File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/huggingface_hub/utils/_hf_uris.py:313, in parse_hf_uri(uri) 311 if type_ == "bucket": 312 return _parse_bucket_body(location, type_, raw=raw) --> 313 return _parse_repo_body(location, type_, raw=raw) File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/huggingface_hub/utils/_hf_uris.py:582, in _parse_repo_body(location, type_, raw) 580 raise HfUriError(uri=raw, msg="Missing repository id before '@'.") 581 if repo_id.count("/") != 1: --> 582 raise HfUriError(uri=raw, msg=f"Repository id must be 'namespace/name', got '{repo_id}'.") 583 # Special refs like 'refs/pr/10' contain '/' and must be matched eagerly, 584 # otherwise we would split them at the first '/' and treat the rest as a path. 585 match = _SPECIAL_REFS_REVISION_REGEX.match(rev_and_path) HfUriError: Invalid HF URI 'hf://datasets/food101@83488de741c1bd1ce27aa6a2b33e19c7bdf92ca9/.huggingface.yaml'. Repository id must be 'namespace/name', got 'food101'.
Get a description of the dataset (if it exists—here it doesn’t):
ds_builder.info.description--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 1 ----> 1 ds_builder.info.description NameError: name 'ds_builder' is not defined
Get information on the features:
ds_builder.info.features--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[4], line 1 ----> 1 ds_builder.info.features NameError: name 'ds_builder' is not defined
Download dataset
We will only use the first 5 classes of food (instead of 101) to test our code. To prevent us from all downloading the data (by default in ~/.cache/huggingface), we will use a joint cache directory at /project/60055/data.
train_size = 5 * 750
val_size = 5 * 250
train_dataset = load_dataset("food101",
split=f"train[:{train_size}]",
cache_dir="/project/60055/data")
val_dataset = load_dataset("food101",
split=f"validation[:{val_size}]",
cache_dir="/project/60055/data")--------------------------------------------------------------------------- HfUriError Traceback (most recent call last) Cell In[5], line 4 1 train_size = 5 * 750 2 val_size = 5 * 250 3 ----> 4 train_dataset = load_dataset("food101", 5 split=f"train[:{train_size}]") 6 7 val_dataset = load_dataset("food101", File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/load.py:1698, in load_dataset(path, name, data_dir, data_files, split, cache_dir, features, download_config, download_mode, verification_mode, keep_in_memory, save_infos, revision, token, streaming, num_proc, storage_options, **config_kwargs) 1693 verification_mode = VerificationMode( 1694 (verification_mode or VerificationMode.BASIC_CHECKS) if not save_infos else VerificationMode.ALL_CHECKS 1695 ) 1697 # Create a dataset builder -> 1698 builder_instance = load_dataset_builder( 1699 path=path, 1700 name=name, 1701 data_dir=data_dir, 1702 data_files=data_files, 1703 cache_dir=cache_dir, 1704 features=features, 1705 download_config=download_config, 1706 download_mode=download_mode, 1707 revision=revision, 1708 token=token, 1709 storage_options=storage_options, 1710 **config_kwargs, 1711 ) 1713 # Return iterable dataset in case of streaming 1714 if streaming: File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/load.py:1325, in load_dataset_builder(path, name, data_dir, data_files, cache_dir, features, download_config, download_mode, revision, token, storage_options, **config_kwargs) 1323 if features is not None: 1324 features = _fix_for_backward_compatible_features(features) -> 1325 dataset_module = dataset_module_factory( 1326 path, 1327 revision=revision, 1328 download_config=download_config, 1329 download_mode=download_mode, 1330 data_dir=data_dir, 1331 data_files=data_files, 1332 cache_dir=cache_dir, 1333 ) 1334 # Get dataset builder class 1335 builder_kwargs = dataset_module.builder_kwargs File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/load.py:1217, in dataset_module_factory(path, revision, download_config, download_mode, data_dir, data_files, cache_dir, **download_kwargs) 1212 if isinstance(e1, FileNotFoundError): 1213 raise FileNotFoundError( 1214 f"Couldn't find any data file at {relative_to_absolute_path(path)}. " 1215 f"Couldn't find '{path}' on the Hugging Face Hub either: {type(e1).__name__}: {e1}" 1216 ) from None -> 1217 raise e1 from None 1218 else: 1219 raise FileNotFoundError(f"Couldn't find any data file at {relative_to_absolute_path(path)}.") File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/load.py:1192, in dataset_module_factory(path, revision, download_config, download_mode, data_dir, data_files, cache_dir, **download_kwargs) 1182 else: 1183 use_exported_dataset_infos = True 1184 return HubDatasetModuleFactory( 1185 path, 1186 commit_hash=commit_hash, 1187 data_dir=data_dir, 1188 data_files=data_files, 1189 download_config=download_config, 1190 download_mode=download_mode, 1191 use_exported_dataset_infos=use_exported_dataset_infos, -> 1192 ).get_module() 1193 except GatedRepoError as e: 1194 message = f"Dataset '{path}' is a gated dataset on the Hub." File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/load.py:608, in HubDatasetModuleFactory.get_module(self) 606 download_config.download_desc = "Downloading standalone yaml" 607 try: --> 608 standalone_yaml_path = cached_path( 609 hf_dataset_url(self.name, config.REPOYAML_FILENAME, revision=self.commit_hash), 610 download_config=download_config, 611 ) 612 with open(standalone_yaml_path, encoding="utf-8") as f: 613 standalone_yaml_data = yaml.safe_load(f.read()) File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/datasets/utils/file_utils.py:180, in cached_path(url_or_filename, download_config, **download_kwargs) 174 # Download files from Hugging Face. 175 # Note: no need to check for https://huggingface.co file URLs since _prepare_path_and_storage_options 176 # prepares Hugging Face HTTP URLs as hf:// paths already 177 if url_or_filename.startswith("hf://") and not url_or_filename.startswith("hf://buckets/"): 178 resolved_path = huggingface_hub.HfFileSystem( 179 endpoint=config.HF_ENDPOINT, token=download_config.token --> 180 ).resolve_path(url_or_filename) 181 try: 182 output_path = huggingface_hub.HfApi( 183 endpoint=config.HF_ENDPOINT, 184 token=download_config.token, (...) 194 proxies=download_config.proxies, 195 ) File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/huggingface_hub/hf_file_system.py:305, in HfFileSystem.resolve_path(self, path, revision) 300 if path.count("/") == 0: 301 raise ValueError( 302 f"Repository id must be 'namespace/name', got '{path}'. Single-segment ids (e.g. 'gpt2') are no longer supported." 303 ) --> 305 parsed = parse_hf_uri(f"{constants.HF_PROTOCOL}{path}") 307 # --- Buckets --- 308 if parsed.is_bucket: File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/huggingface_hub/utils/_hf_uris.py:313, in parse_hf_uri(uri) 311 if type_ == "bucket": 312 return _parse_bucket_body(location, type_, raw=raw) --> 313 return _parse_repo_body(location, type_, raw=raw) File ~/parvus/prog/mint/ai/fl/.venv/lib/python3.13/site-packages/huggingface_hub/utils/_hf_uris.py:582, in _parse_repo_body(location, type_, raw) 580 raise HfUriError(uri=raw, msg="Missing repository id before '@'.") 581 if repo_id.count("/") != 1: --> 582 raise HfUriError(uri=raw, msg=f"Repository id must be 'namespace/name', got '{repo_id}'.") 583 # Special refs like 'refs/pr/10' contain '/' and must be matched eagerly, 584 # otherwise we would split them at the first '/' and treat the rest as a path. 585 match = _SPECIAL_REFS_REVISION_REGEX.match(rev_and_path) HfUriError: Invalid HF URI 'hf://datasets/food101@83488de741c1bd1ce27aa6a2b33e19c7bdf92ca9/.huggingface.yaml'. Repository id must be 'namespace/name', got 'food101'.
Explore data
Let’s inspect our data:
print("Training set size:", len(train_dataset))
print("Validation set size:", len(val_dataset))
print("Training set shape:", train_dataset.shape)
print("Validation set shape:", val_dataset.shape)
print("First item of training set:", train_dataset[0])
print("Firt image of training set:", train_dataset[0]["image"])
print("First label of training set:", train_dataset[0]["label"])--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[6], line 1 ----> 1 print("Training set size:", len(train_dataset)) 2 print("Validation set size:", len(val_dataset)) 3 print("Training set shape:", train_dataset.shape) 4 print("Validation set shape:", val_dataset.shape) NameError: name 'train_dataset' is not defined
Here is the beginning of the list of foods:
print(train_dataset.features["label"].names[:5])--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[7], line 1 ----> 1 print(train_dataset.features["label"].names[:5]) NameError: name 'train_dataset' is not defined
And here is the food of the first item in the training set (label 6):
print("First food of training set:", train_dataset.features["label"].names[6])--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[8], line 1 ----> 1 print("First food of training set:", train_dataset.features["label"].names[6]) NameError: name 'train_dataset' is not defined
To make this simpler, we can create a mapping of the labels matching their order:
labels_mapping = {}
index = 0
for i in range(0, len(val_dataset), 250):
label = val_dataset[i]["label"]
if label not in labels_mapping:
labels_mapping[label] = index
index += 1
inv_labels_mapping = {v: k for k, v in labels_mapping.items()}
print(inv_labels_mapping)--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[9], line 3 1 labels_mapping = {} 2 index = 0 ----> 3 for i in range(0, len(val_dataset), 250): 4 label = val_dataset[i]["label"] 5 if label not in labels_mapping: 6 labels_mapping[label] = index NameError: name 'val_dataset' is not defined
And a mapping of the names:
names_map={k: train_dataset.features["label"].names[v] for k, v in inv_labels_mapping.items()}
print(names_map)--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[10], line 1 ----> 1 names_map={k: train_dataset.features["label"].names[v] for k, v in inv_labels_mapping.items()} 2 print(names_map) NameError: name 'inv_labels_mapping' is not defined
Now, to get the food of the first item, we just have to do:
print(names_map[0])--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[11], line 1 ----> 1 print(names_map[0]) NameError: name 'names_map' is not defined
Here is a function to display some samples (their images, label, and food type):
def display_datapoints(*datapoints, tag="", names_map=None):
num_samples = len(datapoints)
fig, axs = plt.subplots(1, num_samples, figsize=(8.5, 9))
for i, datapoint in enumerate(datapoints):
if isinstance(datapoint, dict):
img, label = datapoint["image"], datapoint["label"]
else:
img, label = datapoint
if hasattr(img, "dtype") and img.dtype in (np.float32, ):
img = (
(img - img.min()) / (img.max() - img.min()) * 255.0
).astype(np.uint8)
label_str = f" ({names_map[label]})" if names_map is not None else ""
axs[i].set_title(
f"{tag}\nLabel: {label}{label_str}",
fontsize=9,
linespacing=1.5
)
axs[i].axis('off')
axs[i].imshow(img)Let’s display the first 3 items (images and labels) of both the training and validation sets:
display_datapoints(
train_dataset[0],
train_dataset[1],
train_dataset[2],
tag="(Training)",
names_map=train_dataset.features["label"].names,
)
display_datapoints(
val_dataset[0],
val_dataset[1],
val_dataset[2],
tag="(Validation)",
names_map=val_dataset.features["label"].names,
)--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[13], line 2 1 display_datapoints( ----> 2 train_dataset[0], 3 train_dataset[1], 4 train_dataset[2], 5 tag="(Training)", NameError: name 'train_dataset' is not defined