# load package "tibble"
library(tibble)
# load packages "sits" and "sitsdata"
library(sits)
library(sitsdata)
# set tempdir if it does not exist
tempdir_r <- "~/sitsbook/tempdir/R/annex_hf"
dir.create(tempdir_r, showWarnings = FALSE, recursive = TRUE)
tempdir_r_cube <- "~/sitsbook/tempdir/R/annex_hf/cube"
dir.create(tempdir_r_cube, showWarnings = FALSE, recursive = TRUE)
tempdir_r_share <- "~/sitsbook/tempdir/R/annex_hf/share"
dir.create(tempdir_r_share, showWarnings = FALSE, recursive = TRUE)31 Sharing and retrieving data with Hugging Face
Configurations to run this chapter
# load "pysits" library
from pysits import *
from pathlib import Path
# set tempdir if it does not exist
tempdir_py = Path.home() / "sitsbook/tempdir/Python/annex_hf"
tempdir_py_cube = Path.home() / "sitsbook/tempdir/Python/annex_hf/cube"
tempdir_py_share = Path.home() / "sitsbook/tempdir/Python/annex_hf/share"
# Create directories
tempdir_py_cube.mkdir(parents=True, exist_ok=True)
tempdir_py_share.mkdir(parents=True, exist_ok=True)31.1 Introduction
Hugging Face is an open platform for sharing machine learning resources. Its Hub hosts git-based repositories of three kinds: models, datasets and spaces (applications). Each repository is identified by a name in the form "<user>/<repository>", where <user> is either a personal account or an organisation. Public repositories can be read by anyone without an account; private repositories require an access token. Large files, such as satellite images, are stored efficiently, and each repository has a version history.
These features make Hugging Face a convenient place to share the data used in Earth observation studies. Training samples, trained models and data cubes are often too big for code repositories such as GitHub, and too specialised for general-purpose data archives. By publishing them on Hugging Face, authors allow others to reproduce their results without repeating expensive steps, such as extracting time series from large image collections, regularising data cubes or training deep learning models.
This book uses Hugging Face extensively. Most chapters retrieve their training samples from the e-sensing/samples_book dataset, and the chapters on embeddings retrieve data cubes, encoders and embedding cubes from Hugging Face, since producing them requires many hours of processing. The main repositories used in the book are listed below.
| Repository | Contents |
|---|---|
e-sensing/samples_book |
Training samples used in this book (in Parquet) |
e-sensing/samples_cerrado |
Data used in “Embeddings” chapters |
e-sensing/rondonia20LMR |
Regular Sentinel-2 data cube for tile 20LMR (2022), used in “How to use SITS with real examples” |
e-sensing/cerrado_cube_reg_2018 |
Regular Landsat-8 data cube for the Cerrado (2018) used in “Building regional embeddings: a full example” |
e-sensing/cerrado_emb_ssl_lejepa_tcnn_2018 |
Embedding cube produced by a LeJEPA encoder (2018) used in “Building regional embeddings: a full example” |
e-sensing/encoders_cerrado |
Encoders trained with different representation learning methods (.rds files). Used in embeddings chapters |
The sits package provides two functions to work with Hugging Face. Function sits_from_hf() reads samples, models and data cubes from a repository. Function sits_to_hf() writes the files that describe a data cube, so that it can be uploaded to a dataset repository and later read by sits_from_hf(). Samples and models do not need a description: they are shared as single files, saved with sits_to_parquet() or saveRDS(). The rest of this chapter describes how to retrieve and share each type of data.
31.2 Retrieving data with sits_from_hf()
Function sits_from_hf() reads data from a Hugging Face repository. What it returns depends on the contents of the repository and on the file parameter:
-
Samples: when
filepoints to a.parquetfile (written bysits_to_parquet()) or to an.rdsfile containing time series, the function returns asitstibble. -
Models: when
filepoints to an.rdsfile with a model trained bysits_train()or an encoder produced bysits_pre_train(), the function returns the model. -
Data cubes: when
fileis not informed and the repository has asits/sits.ymlfile (written bysits_to_hf()), the function downloads the images tooutput_dirand returns a local data cube.
The main parameters of sits_from_hf() are:
| Parameter | Meaning |
|---|---|
repo |
Hugging Face repository, in the form "<user>/<repository>". |
file |
File to be read (optional). Required when the repository has more than one .rds or .parquet file. |
type |
Type of the repository: "dataset" (default) or "model". Data cubes are always shared as datasets. |
output_dir |
Directory where the images of a data cube are saved. Required for data cubes; the directory must exist. |
bands, tiles, roi, crs, start_date, end_date
|
Optional selection of the part of the data cube to be downloaded. |
labels, version
|
Optional selection of results produced by sits (e.g., probability cubes or classified maps). |
n_tries |
Number of attempts to download each image (default = 3). |
multicores |
Number of cores for parallel download (default = 2). |
progress |
Show a progress bar? (default = TRUE). |
31.2.1 Retrieving samples
Training samples are the most common type of data retrieved from Hugging Face in this book. In the example below, we retrieve the samples of deforestation in Rondonia used in the chapters “Classification of raster data cubes” and “Building regional embeddings: a simple example”. Since the e-sensing/samples_book repository contains many files, we use file to choose one of them. The result is a sits tibble with 6,007 time series, each containing the Sentinel-2 bands B02, B03, B04, B05, B06, B07, B08, B8A, B11 and B12. The function downloads the file to a temporary location and reads it; no output_dir is needed.
# Get the samples from Hugging Face
samples_deforestation_rondonia <- sits_from_hf(
repo = "e-sensing/samples_book",
file = "samples_deforestation_rondonia.parquet",
type = "dataset"
)
# show the bands and the number of samples per class
sits_bands(samples_deforestation_rondonia)
summary(samples_deforestation_rondonia)# Get the samples from Hugging Face
samples_deforestation_rondonia = sits_from_hf(
repo = "e-sensing/samples_book",
file = "samples_deforestation_rondonia.parquet",
type = "dataset"
)
# show the bands and the number of samples per class
sits_bands(samples_deforestation_rondonia)
summary(samples_deforestation_rondonia)The samples can then be used as any other sits tibble, for example to visualise temporal patterns with sits_patterns() or to train a model with sits_train(), as shown in the chapter “Basic operations on image time series”.
31.2.2 Retrieving models and encoders
Trained models and encoders are shared as .rds files in model repositories. To read them, set type = "model". The example below retrieves the LeJEPA encoder used in the chapter “Building regional embeddings: a full example”. The e-sensing/encoders_cerrado repository has several encoders, each trained with a different representation learning method (see “Embedding Methods in SITS”); we choose one with file. The result is a sits_encoder object, which can be used directly in sits_encode(). Models trained with sits_train() are retrieved in the same way and can be used directly in sits_classify().
# Retrieve a LeJEPA encoder from a model repository
lejepa_encoder <- sits_from_hf(
repo = "e-sensing/encoders_cerrado",
file = "ssl_lejepa_tcnn_model_2017_2024.rds",
type = "model"
)# Retrieve a LeJEPA encoder from a model repository
lejepa_encoder = sits_from_hf(
repo = "e-sensing/encoders_cerrado",
file = "ssl_lejepa_tcnn_model_2017_2024.rds",
type = "model"
)31.2.3 Retrieving data cubes
A data cube shared on Hugging Face is a dataset repository that contains the image files and a sits directory with a description of the cube (see the next section). When file is not informed, sits_from_hf() reads this description, downloads the images to output_dir and returns a local data cube. After the download, the cube is the same as one created by sits_cube() from local files (see “Data cubes from local files”), and can be used in all sits functions.
Data cubes for large areas can have tens or hundreds of gigabytes. To avoid downloading the whole dataset, users can choose part of the cube using bands, tiles, roi, start_date and end_date. Only the images that match the selection are downloaded. In the example below, we select three bands and the dry season of 2022 from the rondonia20LMR cube used in the chapter “How to use SITS with real examples”, which reduces the download from about 26 GB to a small fraction of that size. The result is a data cube with bands B02, B8A and B11 for the dates between June and September 2022.
# Download part of the rondonia20LMR cube
cube_20LMR_part <- sits_from_hf(
repo = "e-sensing/rondonia20LMR",
type = "dataset",
bands = c("B02", "B8A", "B11"),
start_date = "2022-06-01",
end_date = "2022-09-30",
output_dir = tempdir_r_cube,
multicores = 4
)
plot(cube_20LMR_part, date = "2022-07-16", blue = "B02", green = "B8A", red = "B11")# Download part of the rondonia20LMR cube
cube_20LMR_part = sits_from_hf(
repo = "e-sensing/rondonia20LMR",
type = "dataset",
bands = ("B02", "B8A", "B11"),
start_date = "2022-06-01",
end_date = "2022-09-30",
output_dir = tempdir_py_cube,
multicores = 4
)
plot(cube_20LMR_part, date = "2022-07-16", blue = "B02", green = "B8A", red = "B11")The same function retrieves embedding cubes and results produced by sits. For example, the embedding cube used in “Building regional embeddings: a full example” is read with sits_from_hf(repo = "e-sensing/cerrado_emb_ssl_lejepa_tcnn_2018", output_dir = ...), and the result is an embedding cube ready to be classified. When a dataset holds results from a classification, such as probability cubes and classified maps, the parameters labels and version select which result is read.
Hugging Face limits the number of files a user can request in each five-minute window. To use these limits well, sits_from_hf() downloads each image only once, in batches that fit the requests available; when no request is available, it waits for the next window. Images already in output_dir are not requested again. Therefore, if a download is interrupted, calling sits_from_hf() again with the same parameters resumes it.
31.2.4 Accessing private repositories
To read private repositories, users need a Hugging Face access token, which can be created in the account settings. The token must be stored in the HF_TOKEN environment variable. In R, this is best done by adding a line HF_TOKEN=<your token> to the .Renviron file (for example, using usethis::edit_r_environ()), so that the token is not written in scripts. For a single session, one can use Sys.setenv().
# set the access token for the current session
Sys.setenv(HF_TOKEN = "<your token>")import os
# set the access token for the current session
os.environ["HF_TOKEN"] = "<your token>"31.4 Summary
This chapter described how to use Hugging Face to share and retrieve the data used in sits workflows. Function sits_from_hf() reads samples, models and data cubes from Hugging Face repositories; for data cubes, users can select bands, tiles, regions and dates so that only the images needed are downloaded, and interrupted downloads are resumed automatically. To share data, samples are saved with sits_to_parquet() and models with saveRDS(), while data cubes are described with sits_to_hf(), which writes the sits/sits.yml and sits/cache.rds files expected by sits_from_hf(). The files are then uploaded with the tools provided by Hugging Face. Sharing data in this way makes studies easier to reproduce, and allows others to reuse samples, encoders and data cubes that are expensive to produce.