Weights & Biases Dataloader
A set of utilities for easily accessing datasets for various machine learning tasks using Weights & Biases artifacts.
WandbDatasetBuilder
Bases: GeneratorBasedBuilder
An abstract class for Dataset builder that enables building a dataset and upload it as a Weights & Biases Artifact. It expects subclasses to override the following functions:
-
_split_generators
to return a dict of splits, generators. -
_generate_examples
to return a generator or an iterator corresponding to the split.
Note
Note that this process is alternative to the dataset preparation process using tfds module
described here. The dataset registered and uploaded using both
approaches is easily consumable using the fuction
load_dataset
.
Example Artifacts
Usage:
import os
from glob import glob
from typing import Any, Mapping, Optional, Union
from etils import epath
import tensorflow_datasets as tfds
import wandb
from wandb_addons.dataset import WandbDatasetBuilder
class MonkeyDatasetBuilder(WandbDatasetBuilder):
def __init__(
self,
*,
name: str,
dataset_path: str,
features: tfds.features.FeatureConnector,
upload_raw_dataset: bool = True,
config: Union[None, str, tfds.core.BuilderConfig] = None,
data_dir: Optional[epath.PathLike] = None,
description: Optional[str] = None,
release_notes: Optional[Mapping[str, str]] = None,
homepage: Optional[str] = None,
file_format: Optional[Union[str, tfds.core.FileFormat]] = None,
disable_shuffling: Optional[bool] = False,
**kwargs: Any,
):
super().__init__(
name=name,
dataset_path=dataset_path,
features=features,
upload_raw_dataset=upload_raw_dataset,
config=config,
description=description,
data_dir=data_dir,
release_notes=release_notes,
homepage=homepage,
file_format=file_format,
disable_shuffling=disable_shuffling,
)
def _split_generators(self, dl_manager: tfds.download.DownloadManager):
return {
"train": self._generate_examples(
os.path.join(self.dataset_path, "training", "training")
),
"val": self._generate_examples(
os.path.join(self.dataset_path, "validation", "validation")
),
}
def _generate_examples(self, path):
image_paths = glob(os.path.join(path, "*", "*.jpg"))
for image_path in image_paths:
label = _CLASS_LABELS[int(image_path.split("/")[-2][-1])]
yield image_path, {
"image": image_path,
"label": label,
}
if __name__ == "__main__":
wandb.init(project="artifact-accessor", entity="geekyrakshit")
builder = MonkeyDatasetBuilder(
name="monkey_dataset",
dataset_path="path/to/my/datase",
features=tfds.features.FeaturesDict(
{
"image": tfds.features.Image(shape=(None, None, 3)),
"label": tfds.features.ClassLabel(names=_CLASS_LABELS),
}
),
data_dir="build_dir/",
description=_DESCRIPTION,
)
builder.build_and_upload(create_visualizations=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
A human-readable name for this artifact, which is how you can identify this
artifact in the UI or reference it in
|
required |
dataset_path |
str
|
Path to the dataset. |
required |
features |
FeatureConnector
|
The dataset feature types. Refer to the
|
required |
upload_raw_dataset |
Optional[bool]
|
Whether to upload the raw dataset to Weights & Biases
artifacts as well or not. If set to |
False
|
config |
Union[None, str, BuilderConfig]
|
Dataset configuration. |
None
|
data_dir |
Optional[PathLike]
|
The directory where the dataset will be built. |
None
|
description |
Optional[str]
|
Description of the dataset as a valid markdown string. |
None
|
release_notes |
Optional[Mapping[str, str]]
|
Release notes. |
None
|
homepage |
Optional[str]
|
Homepage of the dataset. |
None
|
file_format |
Optional[Union[str, FileFormat]]
|
EXPERIMENTAL, may change at any
time; Format of the record files in which dataset will be read/written to. If |
None
|
disable_shuffling |
Optional[bool]
|
Disable shuffling of the dataset order. |
True
|
Source code in wandb_addons/dataset/dataset_builder.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
build_and_upload(create_visualizations=False, max_visualizations_per_split=None)
Build and prepare the dataset for loading and uploads as a Weights & Biases Artifact. This function also creates a Weights & Biases reports that contains the dataset description, visualizations and all additional metadata logged to Weights & Biases.
Sample Auto-generated Report
Args: create_visualizations (bool): Automatically parse the dataset and visualize using a Weights & Biase Table. max_visualizations_per_split (Optional[int]): Maximum number of visualizations per split to be visualized in WandB Table. By default, the whole dataset is visualized.
Source code in wandb_addons/dataset/dataset_builder.py
upload_dataset(dataset_name, dataset_path, aliases=None, upload_tfrecords=True, quiet=False)
Upload and register a dataset with a TFDS module or a TFDS builder script as a Weights & Biases artifact. This function would verify if a TFDS build/registration is possible with the current specified dataset path and upload it as a Weights & Biases artifact.
Check this guide for preparing a dataset for registering in on Weights & Biases
Usage:
import wandb
from wandb_addons.dataset import upload_dataset
# Initialize a W&B Run
wandb.init(project="my-awesome-project", job_type="upload_dataset")
# Note that we should set our dataset name as the name of the artifact
upload_dataset(dataset_name="my_awesome_dataset", dataset_path="./my/dataset/path")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
Name of the dataset. This name should follow the PEP8 package and module name convenmtions. |
required |
dataset_path |
str
|
Path to the dataset. |
required |
aliases |
Optional[List[str]]
|
Aliases to apply to this artifact. If the parameter |
None
|
upload_tfrecords |
bool
|
Upload dataset as TFRecords or not. If set to |
True
|
quiet |
bool
|
Whether to suppress the output of dataset build process or not. |
False
|
Source code in wandb_addons/dataset/dataset_upload.py
load_dataset(artifact_address, artifact_type='dataset', remove_redundant_data_files=True, quiet=False)
Load a dataset from a wandb artifact.
Using this function you can load a dataset hosted as a wandb artifact in a single line of code, and use our powerful data processing methods to quickly get your dataset ready for training in a deep learning model.
Usage:
from wandb_addons.dataset import load_dataset
datasets, dataset_builder_info = load_dataset("geekyrakshit/artifact-accessor/monkey_species:v0")
Example notebooks:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_address |
str
|
A human-readable name for the artifact, which is how you can
identify the artifact in the UI or reference it in
|
required |
artifact_type |
str
|
The type of the artifact, which is used to organize and differentiate artifacts. Common typesCinclude dataset or model, but you can use any string containing letters, numbers, underscores, hyphens, and dots. |
'dataset'
|
remove_redundant_data_files |
bool
|
Whether to remove the redundant data files from the artifacts directory after building the tfrecord dataset. |
True
|
quiet |
bool
|
Whether to suppress the output of dataset build process or not. |
False
|
Returns:
Type | Description |
---|---|
Tuple[Dict[str, Dataset], DatasetInfo]
|
A tuple of dictionary Dictionary mapping
split aliases to the respective
TensorFlow Prefetched dataset
objects and the
|