TFDS TensorFlow, Jax और अन्य मशीन लर्निंग फ्रेमवर्क के साथ उपयोग के लिए रेडी-टू-यूज़ डेटासेट का संग्रह प्रदान करता है।
यह डेटा को नियत रूप से डाउनलोड करने और तैयार करने औरtf.data.Dataset
(या np.array
) के निर्माण का कामtf.data.Dataset
।
![]() | ![]() | ![]() | ![]() |
स्थापना
TFDS दो पैकेज में मौजूद है:
-
pip install tensorflow-datasets
: स्थिर संस्करण, हर कुछ महीनों में जारी किया जाता है। -
pip install tfds-nightly
: हर दिन जारी, डेटासेट के अंतिम संस्करण शामिल हैं।
यह कोलाब tfds-nightly
tfds का उपयोग करता है:
pip install -q tfds-nightly tensorflow matplotlib
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
उपलब्ध डेटासेट खोजें
सभी डेटासेट बिल्डर tfds.core.DatasetBuilder
उपवर्ग हैं। उपलब्ध बिल्डरों की सूची प्राप्त करने के लिए, tfds.list_builders()
उपयोग करें या हमारी सूची देखें ।
tfds.list_builders()
['abstract_reasoning', 'accentdb', 'aeslc', 'aflw2k3d', 'ag_news_subset', 'ai2_arc', 'ai2_arc_with_ir', 'amazon_us_reviews', 'anli', 'arc', 'bair_robot_pushing_small', 'bccd', 'beans', 'big_patent', 'bigearthnet', 'billsum', 'binarized_mnist', 'binary_alpha_digits', 'blimp', 'bool_q', 'c4', 'caltech101', 'caltech_birds2010', 'caltech_birds2011', 'cars196', 'cassava', 'cats_vs_dogs', 'celeb_a', 'celeb_a_hq', 'cfq', 'cherry_blossoms', 'chexpert', 'cifar10', 'cifar100', 'cifar10_1', 'cifar10_corrupted', 'citrus_leaves', 'cityscapes', 'civil_comments', 'clevr', 'clic', 'clinc_oos', 'cmaterdb', 'cnn_dailymail', 'coco', 'coco_captions', 'coil100', 'colorectal_histology', 'colorectal_histology_large', 'common_voice', 'coqa', 'cos_e', 'cosmos_qa', 'covid19sum', 'crema_d', 'curated_breast_imaging_ddsm', 'cycle_gan', 'dart', 'davis', 'deep_weeds', 'definite_pronoun_resolution', 'dementiabank', 'diabetic_retinopathy_detection', 'div2k', 'dmlab', 'downsampled_imagenet', 'drop', 'dsprites', 'dtd', 'duke_ultrasound', 'e2e_cleaned', 'efron_morris75', 'emnist', 'eraser_multi_rc', 'esnli', 'eurosat', 'fashion_mnist', 'flic', 'flores', 'food101', 'forest_fires', 'fuss', 'gap', 'geirhos_conflict_stimuli', 'genomics_ood', 'german_credit_numeric', 'gigaword', 'glue', 'goemotions', 'gpt3', 'gref', 'groove', 'gtzan', 'gtzan_music_speech', 'hellaswag', 'higgs', 'horses_or_humans', 'howell', 'i_naturalist2017', 'imagenet2012', 'imagenet2012_corrupted', 'imagenet2012_real', 'imagenet2012_subset', 'imagenet_a', 'imagenet_r', 'imagenet_resized', 'imagenet_v2', 'imagenette', 'imagewang', 'imdb_reviews', 'irc_disentanglement', 'iris', 'kitti', 'kmnist', 'lambada', 'lfw', 'librispeech', 'librispeech_lm', 'libritts', 'ljspeech', 'lm1b', 'lost_and_found', 'lsun', 'lvis', 'malaria', 'math_dataset', 'mctaco', 'mlqa', 'mnist', 'mnist_corrupted', 'movie_lens', 'movie_rationales', 'movielens', 'moving_mnist', 'multi_news', 'multi_nli', 'multi_nli_mismatch', 'natural_questions', 'natural_questions_open', 'newsroom', 'nsynth', 'nyu_depth_v2', 'omniglot', 'open_images_challenge2019_detection', 'open_images_v4', 'openbookqa', 'opinion_abstracts', 'opinosis', 'opus', 'oxford_flowers102', 'oxford_iiit_pet', 'para_crawl', 'patch_camelyon', 'paws_wiki', 'paws_x_wiki', 'pet_finder', 'pg19', 'piqa', 'places365_small', 'plant_leaves', 'plant_village', 'plantae_k', 'qa4mre', 'qasc', 'quac', 'quickdraw_bitmap', 'race', 'radon', 'reddit', 'reddit_disentanglement', 'reddit_tifu', 'resisc45', 'robonet', 'rock_paper_scissors', 'rock_you', 's3o4d', 'salient_span_wikipedia', 'samsum', 'savee', 'scan', 'scene_parse150', 'scicite', 'scientific_papers', 'sentiment140', 'shapes3d', 'siscore', 'smallnorb', 'snli', 'so2sat', 'speech_commands', 'spoken_digit', 'squad', 'stanford_dogs', 'stanford_online_products', 'star_cfq', 'starcraft_video', 'stl10', 'story_cloze', 'sun397', 'super_glue', 'svhn_cropped', 'tao', 'ted_hrlr_translate', 'ted_multi_translate', 'tedlium', 'tf_flowers', 'the300w_lp', 'tiny_shakespeare', 'titanic', 'trec', 'trivia_qa', 'tydi_qa', 'uc_merced', 'ucf101', 'vctk', 'vgg_face2', 'visual_domain_decathlon', 'voc', 'voxceleb', 'voxforge', 'waymo_open_dataset', 'web_nlg', 'web_questions', 'wider_face', 'wiki40b', 'wiki_bio', 'wiki_table_questions', 'wiki_table_text', 'wikihow', 'wikipedia', 'wikipedia_toxicity_subtypes', 'wine_quality', 'winogrande', 'wmt13_translate', 'wmt14_translate', 'wmt15_translate', 'wmt16_translate', 'wmt17_translate', 'wmt18_translate', 'wmt19_translate', 'wmt_t2t_translate', 'wmt_translate', 'wordnet', 'wsc273', 'xnli', 'xquad', 'xsum', 'xtreme_pawsx', 'xtreme_xnli', 'yelp_polarity_reviews', 'yes_no', 'youtube_vis']
एक डेटा लोड करें
tfds.load
डेटासेट लोड करने का सबसे आसान तरीका tfds.load
। यह:
- डेटा डाउनलोड करें और इसे
tfrecord
फ़ाइलों के रूप मेंtfrecord
। - लोड
tfrecord
और बनानेtf.data.Dataset
।
ds = tfds.load('mnist', split='train', shuffle_files=True)
assert isinstance(ds, tf.data.Dataset)
print(ds)
<_OptionsDataset shapes: {image: (28, 28, 1), label: ()}, types: {image: tf.uint8, label: tf.int64}>
कुछ सामान्य तर्क:
-
split=
: कौन सा विभाजन पढ़ने के लिए (उदाहरण के लिए'train'
,['train', 'test']
,'train[80%:]'
, ...)। हमारे विभाजन एपीआई गाइड देखें। -
shuffle_files=
: नियंत्रित करें कि क्या प्रत्येक युग के बीच फ़ाइलों को फेरबदल करना है (TFDS कई बड़े फ़ाइलों में बड़े डेटासेट को संग्रहीत करता है)। -
data_dir=
: वह स्थान जहां डेटासेट सहेजा गया है (~/tensorflow_datasets/
लिए डिफ़ॉल्ट) -
with_info=True
: डेटासेट मेटाडेटा युक्तtfds.core.DatasetInfo
लौटाता है -
download=False
: डाउनलोड को अक्षम करें
tfds.builder
tfds.load
चारों ओर एक पतली आवरण है tfds.core.DatasetBuilder
। आप tfds.core.DatasetBuilder
API का उपयोग करके समान आउटपुट प्राप्त कर सकते हैं:
builder = tfds.builder('mnist')
# 1. Create the tfrecord files (no-op if already exists)
builder.download_and_prepare()
# 2. Load the `tf.data.Dataset`
ds = builder.as_dataset(split='train', shuffle_files=True)
print(ds)
<_OptionsDataset shapes: {image: (28, 28, 1), label: ()}, types: {image: tf.uint8, label: tf.int64}>
tfds build
सीएलआई का tfds build
यदि आप एक विशिष्ट डेटासेट उत्पन्न करना चाहते हैं, तो आप tfds
कमांड लाइन का उपयोग कर सकते हैं। उदाहरण के लिए:
tfds build mnist
उपलब्ध झंडे के लिए डॉक्टर देखें।
डेटासेट पर Iterate करें
जैसा कि तानाशाही
डिफ़ॉल्ट रूप से,tf.data.Dataset
वस्तु एक शामिल dict
की tf.Tensor
रों:
ds = tfds.load('mnist', split='train')
ds = ds.take(1) # Only take a single example
for example in ds: # example is `{'image': tf.Tensor, 'label': tf.Tensor}`
print(list(example.keys()))
image = example["image"]
label = example["label"]
print(image.shape, label)
['image', 'label'] (28, 28, 1) tf.Tensor(4, shape=(), dtype=int64)
पता लगाने के लिए dict
में डाटासेट प्रलेखन पर मुख्य नामों और संरचना, देखो हमारे कैटलॉग । उदाहरण के लिए: mnist प्रलेखन ।
टपल के रूप में ( as_supervised=True
)
as_supervised=True
का उपयोग करके, आप पर्यवेक्षित डेटासेट के बदले एक tuple (features, label)
प्राप्त कर सकते हैं।
ds = tfds.load('mnist', split='train', as_supervised=True)
ds = ds.take(1)
for image, label in ds: # example is (image, label)
print(image.shape, label)
(28, 28, 1) tf.Tensor(4, shape=(), dtype=int64)
सुन्न के रूप में ( tfds.as_numpy
)
कनवर्ट करने के लिए tfds.as_numpy
का उपयोग करता है:
-
tf.Tensor
->np.array
tf.data.Dataset
->Iterator[Tree[np.array]]
(Tree
मनमाना नेस्टेड हो सकता हैDict
,Tuple
)
ds = tfds.load('mnist', split='train', as_supervised=True)
ds = ds.take(1)
for image, label in tfds.as_numpy(ds):
print(type(image), type(label), label)
<class 'numpy.ndarray'> <class 'numpy.int64'> 4
के रूप में बैच tf.Tensor (बैच_साइज batch_size=-1
)
batch_size=-1
का उपयोग करके, आप एक ही बैच में पूर्ण डेटासेट लोड कर सकते हैं।
इसे डेटा (np.array, np.array)
रूप में प्राप्त करने के लिए as_supervised=True
और tfds.as_numpy
के साथ जोड़ा जा सकता है:
image, label = tfds.as_numpy(tfds.load(
'mnist',
split='test',
batch_size=-1,
as_supervised=True,
))
print(type(image), image.shape)
<class 'numpy.ndarray'> (10000, 28, 28, 1)
सावधान रहें कि आपका डेटासेट मेमोरी में फिट हो सकता है, और सभी उदाहरणों का आकार समान है।
अपने डेटासेट को बेंचमार्क करें
डेटासेट को बेंचमार्क करना किसी भीtf.data.Dataset
(जैसेtf.data.Dataset
, tfds.as_numpy
, ...) पर एक साधारण tfds.benchmark
कॉल है।
ds = tfds.load('mnist', split='train')
ds = ds.batch(32).prefetch(1)
tfds.benchmark(ds, batch_size=32)
tfds.benchmark(ds, batch_size=32) # Second epoch much faster due to auto-caching
************ Summary ************ Examples/sec (First included) 47889.92 ex/sec (total: 60000 ex, 1.25 sec) Examples/sec (First only) 110.24 ex/sec (total: 32 ex, 0.29 sec) Examples/sec (First excluded) 62298.08 ex/sec (total: 59968 ex, 0.96 sec) ************ Summary ************ Examples/sec (First included) 290380.50 ex/sec (total: 60000 ex, 0.21 sec) Examples/sec (First only) 2506.57 ex/sec (total: 32 ex, 0.01 sec) Examples/sec (First excluded) 309338.21 ex/sec (total: 59968 ex, 0.19 sec)
- बैच के साथ प्रति बैच आकार के परिणाम को सामान्य करने के लिए मत भूलना_कार
batch_size=
kwarg। - सारांश में, पहले वार्मअप बैच को
tf.data.Dataset
अतिरिक्त सेटअप समय (जैसे बफ़र्स इनिशियलाइज़ेशन, ...) को कैप्चर करने के लिए अन्य लोगों से अलग किया जाता है। - नोटिस करें कि टीएफडीएस ऑटो-कैशिंग के कारण दूसरा पुनरावृत्ति कितना तेज है।
-
tfds.benchmark
एक रिटर्नtfds.core.BenchmarkResult
जो आगे के विश्लेषण के लिए निरीक्षण किया जा सकता है।
एंड-टू-एंड पाइपलाइन का निर्माण करें
आगे जाने के लिए, आप देख सकते हैं:
- एक पूर्ण प्रशिक्षण पाइपलाइन (बैचिंग, फेरबदल के साथ, ...) को देखने के लिए हमारा एंड-टू-एंड केरस उदाहरण है।
- आपकी पाइपलाइनों की गति में सुधार करने के लिए हमारा प्रदर्शन गाइड (टिप: अपने डेटासेट को बेंचमार्क करने के लिए
tfds.benchmark(ds)
का उपयोग करें)।
दृश्य
tfds.as_dataframe
tf.data.Dataset
वस्तुओं के लिए परिवर्तित किया जा सकता pandas.DataFrame
साथ tfds.as_dataframe
पर देखे जा करने के लिए Colab ।
- जोड़े
tfds.core.DatasetInfo
के रूप में की दूसरा तर्कtfds.as_dataframe
चित्र, ऑडियो, टेक्स्ट, वीडियो कल्पना करने के लिए, ... - केवल पहले
x
उदाहरण प्रदर्शित करने के लिएds.take(x)
का उपयोग करें।pandas.DataFrame
पूरे डेटासेट-इन-मेमोरी को लोड करेगा, और प्रदर्शित करने के लिए बहुत महंगा हो सकता है।
ds, info = tfds.load('mnist', split='train', with_info=True)
tfds.as_dataframe(ds.take(4), info)
tfds.show_examples
tfds.show_examples
एक matplotlib.figure.Figure
(अब केवल समर्थित छवि डेटासेट) लौटाता है:
ds, info = tfds.load('mnist', split='train', with_info=True)
fig = tfds.show_examples(ds, info)
डेटासेट मेटाडेटा तक पहुँचें
सभी बिल्डरों में डेटासेट मेटाडेटा युक्त tfds.core.DatasetInfo
ऑब्जेक्ट शामिल है।
इसके माध्यम से पहुँचा जा सकता है:
-
tfds.load
API:
ds, info = tfds.load('mnist', with_info=True)
builder = tfds.builder('mnist')
info = builder.info
डेटासेट जानकारी में डेटासेट (संस्करण, उद्धरण, मुखपृष्ठ, विवरण, ...) के बारे में अतिरिक्त जानकारी होती है।
print(info)
tfds.core.DatasetInfo( name='mnist', full_name='mnist/3.0.1', description=""" The MNIST database of handwritten digits. """, homepage='http://yann.lecun.com/exdb/mnist/', data_path='gs://tensorflow-datasets/datasets/mnist/3.0.1', download_size=11.06 MiB, dataset_size=21.00 MiB, features=FeaturesDict({ 'image': Image(shape=(28, 28, 1), dtype=tf.uint8), 'label': ClassLabel(shape=(), dtype=tf.int64, num_classes=10), }), supervised_keys=('image', 'label'), splits={ 'test': <SplitInfo num_examples=10000, num_shards=1>, 'train': <SplitInfo num_examples=60000, num_shards=1>, }, citation="""@article{lecun2010mnist, title={MNIST handwritten digit database}, author={LeCun, Yann and Cortes, Corinna and Burges, CJ}, journal={ATT Labs [Online]. Available: http://yann.lecun.com/exdb/mnist}, volume={2}, year={2010} }""", )
मेटाडेटा की विशेषताएं (लेबल नाम, छवि आकार, ...)
tfds.features.FeatureDict
पर tfds.features.FeatureDict
:
info.features
FeaturesDict({ 'image': Image(shape=(28, 28, 1), dtype=tf.uint8), 'label': ClassLabel(shape=(), dtype=tf.int64, num_classes=10), })
कक्षाओं की संख्या, लेबल नाम:
print(info.features["label"].num_classes)
print(info.features["label"].names)
print(info.features["label"].int2str(7)) # Human readable version (8 -> 'cat')
print(info.features["label"].str2int('7'))
10 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 7 7
आकृतियाँ, dtypes:
print(info.features.shape)
print(info.features.dtype)
print(info.features['image'].shape)
print(info.features['image'].dtype)
{'image': (28, 28, 1), 'label': ()} {'image': tf.uint8, 'label': tf.int64} (28, 28, 1) <dtype: 'uint8'>
विभाजन मेटाडेटा (उदाहरण के विभाजन के नाम, उदाहरणों की संख्या, ...)
tfds.core.SplitDict
पर tfds.core.SplitDict
:
print(info.splits)
{'test': <SplitInfo num_examples=10000, num_shards=1>, 'train': <SplitInfo num_examples=60000, num_shards=1>}
उपलब्ध विभाजन:
print(list(info.splits.keys()))
['test', 'train']
अलग-अलग विभाजन के बारे में जानकारी प्राप्त करें:
print(info.splits['train'].num_examples)
print(info.splits['train'].filenames)
print(info.splits['train'].num_shards)
60000 ['mnist-train.tfrecord-00000-of-00001'] 1
यह सब्स्क्रिप्ट API के साथ भी काम करता है:
print(info.splits['train[15%:75%]'].num_examples)
print(info.splits['train[15%:75%]'].file_instructions)
36000 [FileInstruction(filename='mnist-train.tfrecord-00000-of-00001', skip=9000, take=36000, num_examples=36000)]
समस्या निवारण
मैनुअल डाउनलोड (यदि डाउनलोड विफल रहता है)
यदि डाउनलोड किसी कारण से विफल रहता है (जैसे ऑफ़लाइन, ...)। आप हमेशा डेटा को स्वयं डाउनलोड कर सकते हैं और इसे manual_dir
( manual_dir
टू ~/tensorflow_datasets/download/manual/
।
यह जानने के लिए कि कौन सा url डाउनलोड करना है, इस पर गौर करें:
नए डेटासेट के लिए (फ़ोल्डर के रूप में कार्यान्वित):
tensorflow_datasets/
<type>/<dataset_name>/checksums.tsv
। उदाहरण के लिए:tensorflow_datasets/text/bool_q/checksums.tsv
।आप हमारी सूची में डेटासेट स्रोत स्थान पा सकते हैं।
पुराने डेटासेट के लिए:
tensorflow_datasets/url_checksums/<dataset_name>.txt
NonMatchingChecksumError
ठीक NonMatchingChecksumError
TFDS डाउनलोड किए गए url के चेकसम को मान्य करके नियतत्ववाद सुनिश्चित करता है। यदि NonMatchingChecksumError
उठाया जाता है, तो संकेत हो सकता है:
- वेबसाइट नीचे हो सकती है (उदाहरण के लिए
503 status code
)। कृपया url की जाँच करें। - Google डिस्क URL के लिए, बाद में पुनः प्रयास करें क्योंकि ड्राइव कभी-कभी डाउनलोड को अस्वीकार कर देता है जब बहुत से लोग एक ही URL का उपयोग करते हैं। बग देखें
- मूल डेटासेट फ़ाइलों को अद्यतन किया जा सकता है। इस मामले में TFDS डेटासेट बिल्डर को अद्यतन किया जाना चाहिए। कृपया नया Github अंक या PR खोलें:
- नए चेकसमों को
tfds build --register_checksums
साथ पंजीकृत करें - अंततः डेटासेट जनरेशन कोड अपडेट करें।
- डेटासेट
VERSION
अद्यतन करें - डेटासेट अपडेट करें
RELEASE_NOTES
: चेकसम बदलने के कारण क्या हुआ? क्या कुछ उदाहरण बदल गए? - सुनिश्चित करें कि डेटासेट अभी भी बनाया जा सकता है।
- हमें एक पीआर भेजें
- नए चेकसमों को
प्रशस्ति पत्र
यदि आप किसी पेपर के लिए tensorflow-datasets
का उपयोग कर रहे हैं, तो कृपया उपयोग किए गए डेटासेट्स (जो डेटासेट कैटलॉग में पाया जा सकता है) के लिए किसी भी उद्धरण के अलावा निम्नलिखित उद्धरण भी शामिल करें।
@misc{TFDS,
title = { {TensorFlow Datasets}, A collection of ready-to-use datasets},
howpublished = {\url{https://www.tensorflow.org/datasets} },
}