Package: webdataset-py36
%matplotlibinlineimportmatplotlib.pyplotaspltimporttorch.utils.dataimporttorch.nnfromrandomimportrandrangeimportosos.environ["WDS_VERBOSE_CACHE"]="1"The WebDataset FormatWebDataset reads dataset that are stored as tar files, with the simple convention that files that belong together and make up a training sample share the same basename. WebDataset can read files from local disk or from any pipe, which allows it to access files using common cloud object stores. WebDataset can also read concatenated MsgPack and CBORs sources.The WebDataset representation allows writing purely sequential I/O pipelines for large scale deep learning. This is important for achieving high I/O rates from local storage (3x-10x for local drives compared to random access) and for using object stores and cloud storage for training.The WebDataset format represents images, movies, audio, etc. in their native file formats, making the creation of WebDataset format data as easy as just creating a tar archive. Because of the way data is aligned, WebDataset works well with block deduplication as well and aligns data on predictable boundaries.Standard tools can be used for accessing and processing WebDataset-format files.%%bash
curl-shttp://storage.googleapis.com/nvdata-openimages/openimages-train-000000.tar|tartf-|sed10qe39871fd9fd74f55.jpg
e39871fd9fd74f55.json
f18b91585c4d3f3e.jpg
f18b91585c4d3f3e.json
ede6e66b2fb59aab.jpg
ede6e66b2fb59aab.json
ed600d57fcee4f94.jpg
ed600d57fcee4f94.json
ff47e649b23f446d.jpg
ff47e649b23f446d.jsonRelated Projectsthe newtorchdatalibrary in PyTorch will add native (built-in) support for WebDatasetthe AIStore server provides high-speed storage, caching, and data transformation for WebDataset dataWebDataset training can be carried out directly against S3, GCS, and other cloud storage bucketsNVIDIA's DALI library supports reading WebDataset format data directlythere is a companion project to read WebDataset data in Juliathetarpcommand line program can be used for quick and easy dataset transformations of WebDataset dataWebDatasetWebDataset makes it easy to write I/O pipelines for large datasets. Datasets can be stored locally or in the cloud.When your data is stored in the cloud, by default, local copies are downloaded and cached when you open the remote dataset.importwebdatasetaswdsurl="http://storage.googleapis.com/nvdata-publaynet/publaynet-train-{000000..000009}.tar"dataset=wds.WebDataset(url).shuffle(1000).decode("rgb").to_tuple("png","json")The resulting datasets are standard PyTorchIterableDatasetinstances.isinstance(dataset,torch.utils.data.IterableDataset)Trueforimage,jsonindataset:breakplt.imshow(image)<matplotlib.image.AxesImage at 0x7f40849aa5b0>Let's add some code to transform the input data.defpreprocess(sample):image,json=sampletry:label=json["annotations"][0]["category_id"]except:label=0return1-image,labeldataset=dataset.map(preprocess)forimage,labelindataset:breakplt.imshow(image)print(label)1Note that this uses the fluid interface to WebDataset, a convenient shorthand for a lot of training loops. We'll see later what this expands to.Expanding SamplesLet's add another processing pipeline stage; this one expands a single large input sample into multiple smaller samples. We shuffle the newly generated sub-samples further to mix up sub-samples from different images in the stream.This uses the.composemethod, which takes a function that maps an interator over samples into another iterator over samples.defget_patches(src):forsampleinsrc:image,label=sampleh,w=image.shape[:2]foriinrange(16):y,x=randrange(h-256),randrange(w-265)patch=image[y:y+256,x:x+256]yield(patch,label)dataset=dataset.compose(get_patches).shuffle(10000)forimage,jsonindataset:breakplt.imshow(image)<matplotlib.image.AxesImage at 0x7f40848c18e0>DataLoaderWebDatasetis just an instance of a standardIterableDataset. It's a single-threaded way of iterating over a dataset.Since image decompression and data augmentation can be compute intensive, PyTorch usually uses theDataLoaderclass to parallelize data loading and preprocessing.WebDatasetis fully compatible with the standardDataLoader.loader=torch.utils.data.DataLoader(dataset,num_workers=4,batch_size=8)batch=next(iter(loader))batch[0].shape,batch[1].shape(torch.Size([8, 256, 256, 3]), torch.Size([8]))Thewebdatasetlibrary contains a small wrapper that adds a fluid interface to theDataLoader(and is otherwise identical).This comes in handy if you want to shuffle across dataset instances and allows you to change batch size dynamically.loader=wds.WebLoader(dataset,num_workers=4,batch_size=8)loader=loader.unbatched().shuffle(1000).batched(12)batch=next(iter(loader))batch[0].shape,batch[1].shape(torch.Size([12, 256, 256, 3]), torch.Size([12]))It is generally most efficient to avoid batching in theDataLoaderaltogether; instead, batch in the dataset and then rebatch after the loader.A complete pipeline then looks like this.url="http://storage.googleapis.com/nvdata-publaynet/publaynet-train-{000000..000009}.tar"dataset=wds.WebDataset(url).shuffle(1000).decode("rgb").to_tuple("png","json").map(preprocess)dataset=dataset.compose(get_patches)dataset=dataset.batched(16)loader=wds.WebLoader(dataset,num_workers=4,batch_size=None)loader=loader.unbatched().shuffle(1000).batched(12)batch=next(iter(loader))batch[0].shape,batch[1].shape(torch.Size([12, 256, 256, 3]), torch.Size([12]))Pipeline InterfaceThewds.WebDatasetfluid interface is just a convenient shorthand for writing down pipelines. The underlying pipeline is an instance of thewds.DataPipelineclass, and you can construct data pipelines explicitly, similar to the way you usenn.Sequentialinside models.dataset=wds.DataPipeline(wds.SimpleShardList(url),# at this point we have an iterator over all the shardswds.shuffle(100),wds.split_by_worker,# at this point, we have an iterator over the shards assigned to each workerwds.tarfile_to_samples(),wds.shuffle(1000),wds.decode("torchrgb"),# at this point, we have an list of decompressed training samples from each shard in this worker in sequenceget_patches,# note that can put iterator->iterator functions into the pipeline directlywds.shuffle(10000),wds.to_tuple("big.jpg","json"),wds.batched(16))batch=next(iter(loader))batch[0].shape,batch[1].shape(torch.Size([12, 256, 256, 3]), torch.Size([12]))Multinode TrainingMultinode training in PyTorch and other frameworks is complex. It depends on how exactly you distribute training across nodes, whether you want to keep "exact epochs" (exactly and only one sample from the dataset per epoch), and whether your training framework can deal with unequal number of samples per node.The simplest solution for multinode training is to use a resampling strategy for the shards, generating an infinite stream of samples. You then set the epoch length explicitly with the.with_epochmethod.dataset=wds.WebDataset(url,resampled=True).shuffle(1000).decode("rgb").to_tuple("png","json").map(preprocess).with_epoch(10000)sample=next(iter(dataset))Inside a pipeline, you can do the same thing using theResampledShardsgenerator. Shuffling and splitting by worker are then not needed.dataset=wds.DataPipeline(wds.ResampledShards(url),# at this point we have an iterator over all the shardswds.tarfile_to_samples(),wds.shuffle(1000),wds.decode("torchrgb"),# at this point, we have an list of decompressed training samples from each shard in this worker in sequenceget_patches,# note that can put iterator->iterator functions into the pipeline directlywds.shuffle(10000),wds.to_tuple("big.jpg","json"),wds.batched(16)).with_epoch(10000)batch=next(iter(loader))batch[0].shape,batch[1].shape(torch.Size([12, 256, 256, 3]), torch.Size([12]))Installation and Documentation$ pip install webdatasetFor the Github version:$ pip install git+https://github.com/tmbdev/webdataset.gitHere are some videos talking about WebDataset and large scale deep learning:Introduction to Large Scale Deep LearningLoading Training Data with WebDatasetCreating Datasets in WebDataset FormatTools for Working with Large DatasetsExamples: (NB: some of these are for older versions of WebDataset, but the differences should be small)loading videossplitting raw videos into clips for trainingconverting the Falling Things datasetDependenciesThe WebDataset library only requires PyTorch, NumPy, and a small library calledbraceexpand.WebDataset loads a few additional libraries dynamically only when they are actually needed and only in the decoder:PIL/Pillow for image decodingtorchvision,torchvideo,torchaudiofor image/video/audio decodingmsgpackfor MessagePack decodingthecurlcommand line tool for accessing HTTP serversthe Google/Amazon/Azure command line tools for accessing cloud storage bucketsLoading of one of these libraries is triggered by configuring a decoder that attempts to decode content in the given format and encountering a file in that format during decoding. (Eventually, the torch... dependencies will be refactored into those libraries.)Data DecodingData decoding is a special kind of transformations of samples. You could simply write a decoding function like this:defmy_sample_decoder(sample):result=dict(__key__=sample["__key__"])forkey,valueinsample.items():ifkey=="png"orkey.endswith(".png"):result[key]=mageio.imread(io.BytesIO(value))elif...:...returnresultdataset=wds.Processor(wds.map,my_sample_decoder)(dataset)This gets tedious, though, and it also unnecessarily hardcodes the sample's keys into the processing pipeline. To help with this, there is a helper class that simplifies this kind of code. The primary use ofDecoderis for decoding compressed image, video, and audio formats, as well as unzipping.gzfiles.Here is an example of automatically decoding.pngimages withimreadand using the defaulttorch_videoandtorch_audiodecoders for video and audio:defmy_png_decoder(key,value):ifnotkey.endswith(".png"):returnNoneassertisinstance(value,bytes)returnimageio.imread(io.BytesIO(value))dataset=wds.Decoder(my_png_decoder,wds.torch_video,wds.torch_audio)(dataset)You can use whatever criteria you like for deciding how to decode values in samples. When used with standardWebDatasetformat files, the keys are the full extensions of the file names inside a.tarfile. For consistency, it's recommended that you primarily rely on the extensions (e.g.,.png,.mp4) to decide which decoders to use. There is a special helper function that simplifies this:defmy_decoder(value):returnimageio.imread(io.BytesIO(value))dataset=wds.Decoder(wds.handle_extension(".png",my_decoder))(dataset)Alternative Representation: CBORAn alternative representation of collections of samples is based on the IETF CBOR standard, an efficient, binary representation of data structures. CBOR files are particularly useful for large collections of very small samples (data tuples, short strings, etc.)importcborimportnumpyasnpWriting CBOR files is very easy:withopen("test.cbors","wb")asstream:foriinnp.linspace(-5.0,5.0,1000):cbor.dump((i,str(i)[-5:]),stream)Of course, you can these files directly:withopen("test.cbors","rb")asstream:foriinrange(3):print(cbor.load(stream))[-5.0, '-5.0']
[-4.98998998998999, '98999']
[-4.97997997997998, '97998']And CBOR files/shards integrate fully intoDataPipelinewith thecbors_to_samplesfunction.dataset=wds.DataPipeline(wds.SimpleShardList("test.cbors"),wds.cbors_to_samples(),)data=list(iter(dataset))len(data),data[0](1000, [-5.0, '-5.0'])"Smaller" Datasets and Desktop ComputingWebDataset is an ideal solution for training on petascale datasets kept on high performance distributed data stores like AIStore, AWS/S3, and Google Cloud. Compared to data center GPU servers, desktop machines have much slower network connections, but training jobs on desktop machines often also use much smaller datasets. WebDataset also is very useful for such smaller datasets, and it can easily be used for developing and testing on small datasets and then scaling up to large datasets by simply using more shards.Here are different usage scenarios:desktop deep learning, smaller datasetscopy all shards to local disk manuallyuse automatic shard cachingprototyping, development, testing of jobs for large scale trainingcopy a small subset of shards to local diskuse automatic shard caching with a small subrange of shardscloud training against cloud bucketsuse WebDataset directly with remote URLson premises training with high performance store (e.g., AIStore) and fast networksuse WebDataset directly with remote URLson premises training with slower object stores and/or slower networksuse automatic shard cachingDirect Copying of ShardsLet's take the OpenImages dataset as an example; it's half a terabyte large. For development and testing, you may not want to download the entire dataset, but you may also not want to use the dataset remotely. With WebDataset, you can just download a small number of shards and use them during development.!curl-L-shttp://storage.googleapis.com/nvdata-openimages/openimages-train-000000.tar>/tmp/openimages-train-000000.tardataset=wds.DataPipeline(wds.SimpleShardList("/tmp/openimages-train-000000.tar"),wds.tarfile_to_samples(),)repr(next(iter(dataset)))[:200]"{'__key__': 'e39871fd9fd74f55', '__url__': '/tmp/openimages-train-000000.tar', 'jpg': b'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x01\\x01:\\x01:\\x00\\x00\\xff\\xdb\\x00C\\x00\\x06\\x04\\x05\\x06\\x05\\x04\\x06\\x06\\"Note that the WebDataset class works the same way on local files as it does on remote files. Furthermore, unlike other kinds of dataset formats and archive formats, downloaded datasets are immediately useful and don't need to be unpacked.Creating a WebDatasetUsingtarSince WebDatasets are just regular tar files, you can usually create them by just using thetarcommand. All you have to do is to arrange for any files that should be in the same sample to share the same basename. Many datasets already come that way. For those, you can simply create a WebDataset with$ tar --sort=name -cf dataset.tar dataset/If your dataset has some other directory layout, you may need a different file name in the archive from the name on disk. You can use the--transformargument to GNU tar to transform file names. You can also use the-Targument to read the files from a text file and embed other options in that text file.Thetarp createCommandThetarpcommand is a little utility for manipulatingtararchives. Itscreatesubcommand makes it particularly simple to construct tar archives from files. Thetarp createcommand takes a recipe for building
a tar archive that contains lines of the form:archive-name-1 source-name-1
archive-name-2 source-name-2
...The source name can either be a file, "text:something", or "pipe:something".Programmatically in PythonYou can also create a WebDataset with library functions in this library:webdataset.TarWritertakes dictionaries containing key value pairs and writes them to diskwebdataset.ShardWritertakes dictionaries containing key value pairs and writes them to disk as a series of shardsHere is a quick way of converting an existing dataset into a WebDataset; this will store all tensors as Python pickles:sink=wds.TarWriter("dest.tar")dataset=open_my_dataset()forindex,(input,output)indataset:sink.write({"__key__":"sample%06d"%index,"input.pyd":input,"output.pyd":output,})sink.close()Storing data as Python pickles allows most common Python datatypes to be stored, it is lossless, and the format is fast to decode.
However, it is uncompressed and cannot be read by non-Python programs. It's often better to choose other storage formats, e.g.,
taking advantage of common image compression formats.If you know that the input is an image and the output is an integer class, you can also write something like this:sink=wds.TarWriter("dest.tar")dataset=open_my_dataset()forindex,(input,output)indataset:assertinput.ndim==3andinput.shape[2]==3assertinput.dtype=np.float32andnp.amin(input)>=0andnp.amax(input)<=1asserttype(output)==intsink.write({"__key__":"sample%06d"%index,"input.jpg":input,"output.cls":output,})sink.close()Theassertstatements in that loop are not necessary, but they document and illustrate the expectations for this
particular dataset. Generally, the ".jpg" encoder can actually encode a wide variety of array types as images. The
".cls" encoder always requires an integer for encoding.Here is how you can useTarWriterfor writing a dataset without using an encoder:sink=wds.TarWriter("dest.tar",encoder=False)forbasenameinbasenames:withopen(f"{basename}.png","rb")asstream):image=stream.read()cls=lookup_cls(basename)sample={"__key__":basename,"input.png":image,"target.cls":cls}sink.write(sample)sink.close()Since no encoder is used, if you want to be able to read this data with the default decoder,imagemust contain a byte string corresponding to a PNG image (as indicated by the ".png" extension on its dictionary key), andclsmust contain an integer encoded in ASCII (as indicated by the ".cls" extension on its dictionary key).Writing Filters and Offline AugmentationWebdataset can be used for filters and offline augmentation of datasets. Here is a complete example that pre-augments a shard and extracts class labels.fromtorchvisionimporttransformsfromitertoolsimportislicedefextract_class(data):# mock implementationreturn0defpreproc(image):image=transforms.ToTensor()(image)# more preprocessing herereturnimagedefaugment_wds(input,output,maxcount=999999999):src=wds.DataPipeline(wds.SimpleShardList(input),wds.tarfile_to_samples(),wds.decode("pil"),wds.to_tuple("__key__","jpg;png","json"),wds.map_tuple(None,preproc,None),)withwds.TarWriter(output)asdst:forkey,image,datainislice(src,0,maxcount):print(key)image=image.numpy().transpose(1,2,0)image-=np.amin(image)image/=np.amax(image)sample={"__key__":key,"png":image,"cls":extract_class(data)}dst.write(sample)Now run the augmentation pipeline:url="http://storage.googleapis.com/nvdata-openimages/openimages-train-000000.tar"url=f"pipe:curl -L -s{url}|| true"augment_wds(url,"_temp.tar",maxcount=5)e39871fd9fd74f55
f18b91585c4d3f3e
ede6e66b2fb59aab
ed600d57fcee4f94
ff47e649b23f446dTo verify that things worked correctly, let's look at the output file:%%bash
tartf_temp.tare39871fd9fd74f55.cls
e39871fd9fd74f55.png
f18b91585c4d3f3e.cls
f18b91585c4d3f3e.png
ede6e66b2fb59aab.cls
ede6e66b2fb59aab.png
ed600d57fcee4f94.cls
ed600d57fcee4f94.png
ff47e649b23f446d.cls
ff47e649b23f446d.pngIf you want to preprocess the entire OpenImages dataset with a process like this, you can use your favorite job queueing or worflow system.For example, using Dask, you could process all 554 shards in parallel using code like this:shards=braceexpand.braceexpand("{000000..000554}")inputs=[f"gs://bucket/openimages-{shard}.tar"forshardinshards]outputs=[f"gs://bucket2/openimages-augmented-{shard}.tar"forshardinshards]results=[dask.delayed(augment_wds)(args)forargsinzip(inputs,outputs)]dask.compute(*results)Note that the data is streaming from and to Google Cloud Storage buckets, so very little local storage is required on each worker.For very large scale processing, it's easiest to submit separate jobs to a Kubernetes cluster using the KubernetesJobtemplate, or using a workflow engine like Argo.Whether you preferWebDatasetorDatasetis a matter of style.Syntax for URL SourcesTheSimpleShardListandResampledShardstake either a string or a list of URLs as an argument. If it is given a string, the string is expanded using thebraceexpandlibrary. So, the following are equivalent:ShardList("dataset-{000..001}.tar")ShardList(["dataset-000.tar","dataset-001.tar"])The url strings in a shard list are handled by default by thewebdataset.url_openerfilter. It recognizes three simple kinds of strings: "-", "/path/to/file", and "pipe:command":the string "-", referring to stdina UNIX path, opened as a regular filea URL-like string with the schema "pipe:"; such URLs are opened withsubprocess.Popen. For example:pipe:curl -s -L http://server/fileaccesses a file via HTTPpipe:gsutil cat gs://bucket/fileaccesses a file on GCSpipe:az cp --container bucket --name file --file /dev/stdoutaccesses a file on Azurepipe:ssh host cat fileaccesses a file viasshIt might seem at first glance to be "more efficient" to use built-in Python libraries for accessing object stores rather than subprocesses, but efficient object store access from Python really requires spawning a separate process anyway, so this approach to accessing object stores is not only convenient, it also is as efficient as we can make it in Python.Related Libraries and SoftwareTheAIStoreserver provides an efficient backend for WebDataset; it functions like a combination of web server, content distribution network, P2P network, and distributed file system. Together, AIStore and WebDataset can serve input data from rotational drives distributed across many servers at the speed of local SSDs to many GPUs, at a fraction of the cost. We can easily achieve hundreds of MBytes/s of I/O per GPU even in large, distributed training jobs.Thetarprocutilities provide command line manipulation and processing of webdatasets and other tar files, including splitting, concatenation, andxargs-like functionality.Thetensorcomlibrary provides fast three-tiered I/O; it can be inserted betweenAIStoreandWebDatasetto permit distributed data augmentation and I/O. It is particularly useful when data augmentation requires more CPU than the GPU server has available.You can find the full PyTorch ImageNet sample code converted to WebDataset attmbdev/pytorch-imagenet-wds