partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
train
FeaturesDict.save_metadata
See base class for details.
tensorflow_datasets/core/features/feature.py
def save_metadata(self, data_dir, feature_name=None): """See base class for details.""" # Recursively save all child features for feature_key, feature in six.iteritems(self._feature_dict): if feature_name: feature_key = '-'.join((feature_name, feature_key)) feature.save_metadata(data_dir, feature_name=feature_key)
def save_metadata(self, data_dir, feature_name=None): """See base class for details.""" # Recursively save all child features for feature_key, feature in six.iteritems(self._feature_dict): if feature_name: feature_key = '-'.join((feature_name, feature_key)) feature.save_metadata(data_dir, feature_name=feature_key)
[ "See", "base", "class", "for", "details", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/feature.py#L508-L514
[ "def", "save_metadata", "(", "self", ",", "data_dir", ",", "feature_name", "=", "None", ")", ":", "# Recursively save all child features", "for", "feature_key", ",", "feature", "in", "six", ".", "iteritems", "(", "self", ".", "_feature_dict", ")", ":", "if", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Tensor.encode_example
See base class for details.
tensorflow_datasets/core/features/feature.py
def encode_example(self, example_data): """See base class for details.""" np_dtype = np.dtype(self._dtype.as_numpy_dtype) # Convert to numpy if possible if not isinstance(example_data, np.ndarray): example_data = np.array(example_data, dtype=np_dtype) # Ensure the shape and dtype match if example_data.dtype != np_dtype: raise ValueError('Dtype {} do not match {}'.format( example_data.dtype, np_dtype)) utils.assert_shape_match(example_data.shape, self._shape) # For booleans, convert to integer (tf.train.Example does not support bool) if example_data.dtype == np.bool_: example_data = example_data.astype(int) return example_data
def encode_example(self, example_data): """See base class for details.""" np_dtype = np.dtype(self._dtype.as_numpy_dtype) # Convert to numpy if possible if not isinstance(example_data, np.ndarray): example_data = np.array(example_data, dtype=np_dtype) # Ensure the shape and dtype match if example_data.dtype != np_dtype: raise ValueError('Dtype {} do not match {}'.format( example_data.dtype, np_dtype)) utils.assert_shape_match(example_data.shape, self._shape) # For booleans, convert to integer (tf.train.Example does not support bool) if example_data.dtype == np.bool_: example_data = example_data.astype(int) return example_data
[ "See", "base", "class", "for", "details", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/feature.py#L548-L562
[ "def", "encode_example", "(", "self", ",", "example_data", ")", ":", "np_dtype", "=", "np", ".", "dtype", "(", "self", ".", "_dtype", ".", "as_numpy_dtype", ")", "# Convert to numpy if possible", "if", "not", "isinstance", "(", "example_data", ",", "np", ".", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Tensor.decode_example
See base class for details.
tensorflow_datasets/core/features/feature.py
def decode_example(self, tfexample_data): """See base class for details.""" # TODO(epot): Support dynamic shape if self.shape.count(None) < 2: # Restore the shape if possible. TF Example flattened it. shape = [-1 if i is None else i for i in self.shape] tfexample_data = tf.reshape(tfexample_data, shape) if tfexample_data.dtype != self.dtype: tfexample_data = tf.dtypes.cast(tfexample_data, self.dtype) return tfexample_data
def decode_example(self, tfexample_data): """See base class for details.""" # TODO(epot): Support dynamic shape if self.shape.count(None) < 2: # Restore the shape if possible. TF Example flattened it. shape = [-1 if i is None else i for i in self.shape] tfexample_data = tf.reshape(tfexample_data, shape) if tfexample_data.dtype != self.dtype: tfexample_data = tf.dtypes.cast(tfexample_data, self.dtype) return tfexample_data
[ "See", "base", "class", "for", "details", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/feature.py#L564-L573
[ "def", "decode_example", "(", "self", ",", "tfexample_data", ")", ":", "# TODO(epot): Support dynamic shape", "if", "self", ".", "shape", ".", "count", "(", "None", ")", "<", "2", ":", "# Restore the shape if possible. TF Example flattened it.", "shape", "=", "[", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
CelebA._process_celeba_config_file
Unpack the celeba config file. The file starts with the number of lines, and a header. Afterwards, there is a configuration for each file: one per line. Args: file_path: Path to the file with the configuration. Returns: keys: names of the attributes values: map from the file name to the list of attribute values for this file.
tensorflow_datasets/image/celeba.py
def _process_celeba_config_file(self, file_path): """Unpack the celeba config file. The file starts with the number of lines, and a header. Afterwards, there is a configuration for each file: one per line. Args: file_path: Path to the file with the configuration. Returns: keys: names of the attributes values: map from the file name to the list of attribute values for this file. """ with tf.io.gfile.GFile(file_path) as f: data_raw = f.read() lines = data_raw.split("\n") keys = lines[1].strip().split() values = {} # Go over each line (skip the last one, as it is empty). for line in lines[2:-1]: row_values = line.strip().split() # Each row start with the 'file_name' and then space-separated values. values[row_values[0]] = [int(v) for v in row_values[1:]] return keys, values
def _process_celeba_config_file(self, file_path): """Unpack the celeba config file. The file starts with the number of lines, and a header. Afterwards, there is a configuration for each file: one per line. Args: file_path: Path to the file with the configuration. Returns: keys: names of the attributes values: map from the file name to the list of attribute values for this file. """ with tf.io.gfile.GFile(file_path) as f: data_raw = f.read() lines = data_raw.split("\n") keys = lines[1].strip().split() values = {} # Go over each line (skip the last one, as it is empty). for line in lines[2:-1]: row_values = line.strip().split() # Each row start with the 'file_name' and then space-separated values. values[row_values[0]] = [int(v) for v in row_values[1:]] return keys, values
[ "Unpack", "the", "celeba", "config", "file", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/celeba.py#L150-L175
[ "def", "_process_celeba_config_file", "(", "self", ",", "file_path", ")", ":", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "file_path", ")", "as", "f", ":", "data_raw", "=", "f", ".", "read", "(", ")", "lines", "=", "data_raw", ".", "sp...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
CelebA._generate_examples
Yields examples.
tensorflow_datasets/image/celeba.py
def _generate_examples(self, file_id, extracted_dirs): """Yields examples.""" filedir = os.path.join(extracted_dirs["img_align_celeba"], "img_align_celeba") img_list_path = extracted_dirs["list_eval_partition"] landmarks_path = extracted_dirs["landmarks_celeba"] attr_path = extracted_dirs["list_attr_celeba"] with tf.io.gfile.GFile(img_list_path) as f: files = [ line.split()[0] for line in f.readlines() if int(line.split()[1]) == file_id ] attributes = self._process_celeba_config_file(attr_path) landmarks = self._process_celeba_config_file(landmarks_path) for file_name in sorted(files): path = os.path.join(filedir, file_name) yield { "image": path, "landmarks": { k: v for k, v in zip(landmarks[0], landmarks[1][file_name]) }, "attributes": { # atributes value are either 1 or -1, so convert to bool k: v > 0 for k, v in zip(attributes[0], attributes[1][file_name]) }, }
def _generate_examples(self, file_id, extracted_dirs): """Yields examples.""" filedir = os.path.join(extracted_dirs["img_align_celeba"], "img_align_celeba") img_list_path = extracted_dirs["list_eval_partition"] landmarks_path = extracted_dirs["landmarks_celeba"] attr_path = extracted_dirs["list_attr_celeba"] with tf.io.gfile.GFile(img_list_path) as f: files = [ line.split()[0] for line in f.readlines() if int(line.split()[1]) == file_id ] attributes = self._process_celeba_config_file(attr_path) landmarks = self._process_celeba_config_file(landmarks_path) for file_name in sorted(files): path = os.path.join(filedir, file_name) yield { "image": path, "landmarks": { k: v for k, v in zip(landmarks[0], landmarks[1][file_name]) }, "attributes": { # atributes value are either 1 or -1, so convert to bool k: v > 0 for k, v in zip(attributes[0], attributes[1][file_name]) }, }
[ "Yields", "examples", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/celeba.py#L177-L207
[ "def", "_generate_examples", "(", "self", ",", "file_id", ",", "extracted_dirs", ")", ":", "filedir", "=", "os", ".", "path", ".", "join", "(", "extracted_dirs", "[", "\"img_align_celeba\"", "]", ",", "\"img_align_celeba\"", ")", "img_list_path", "=", "extracted...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
QuickdrawBitmap._generate_examples
Generate QuickDraw bitmap examples. Given a list of file paths with data for each class label, generate examples in a random order. Args: file_paths: (dict of {str: str}) the paths to files containing the data, indexed by label. Yields: The QuickDraw examples, as defined in the dataset info features.
tensorflow_datasets/image/quickdraw.py
def _generate_examples(self, file_paths): """Generate QuickDraw bitmap examples. Given a list of file paths with data for each class label, generate examples in a random order. Args: file_paths: (dict of {str: str}) the paths to files containing the data, indexed by label. Yields: The QuickDraw examples, as defined in the dataset info features. """ for label, path in sorted(file_paths.items(), key=lambda x: x[0]): with tf.io.gfile.GFile(path, "rb") as f: class_images = np.load(f) for np_image in class_images: yield { "image": np_image.reshape(_QUICKDRAW_IMAGE_SHAPE), "label": label, }
def _generate_examples(self, file_paths): """Generate QuickDraw bitmap examples. Given a list of file paths with data for each class label, generate examples in a random order. Args: file_paths: (dict of {str: str}) the paths to files containing the data, indexed by label. Yields: The QuickDraw examples, as defined in the dataset info features. """ for label, path in sorted(file_paths.items(), key=lambda x: x[0]): with tf.io.gfile.GFile(path, "rb") as f: class_images = np.load(f) for np_image in class_images: yield { "image": np_image.reshape(_QUICKDRAW_IMAGE_SHAPE), "label": label, }
[ "Generate", "QuickDraw", "bitmap", "examples", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/quickdraw.py#L97-L117
[ "def", "_generate_examples", "(", "self", ",", "file_paths", ")", ":", "for", "label", ",", "path", "in", "sorted", "(", "file_paths", ".", "items", "(", ")", ",", "key", "=", "lambda", "x", ":", "x", "[", "0", "]", ")", ":", "with", "tf", ".", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
ensure_tf_install
Attempt to import tensorflow, and ensure its version is sufficient. Raises: ImportError: if either tensorflow is not importable or its version is inadequate.
tensorflow_datasets/core/tf_compat.py
def ensure_tf_install(): # pylint: disable=g-statement-before-imports """Attempt to import tensorflow, and ensure its version is sufficient. Raises: ImportError: if either tensorflow is not importable or its version is inadequate. """ try: import tensorflow as tf except ImportError: # Print more informative error message, then reraise. print("\n\nFailed to import TensorFlow. Please note that TensorFlow is not " "installed by default when you install TensorFlow Datasets. This is " "so that users can decide whether to install the GPU-enabled " "TensorFlow package. To use TensorFlow Datasets, please install the " "most recent version of TensorFlow, by following instructions at " "https://tensorflow.org/install.\n\n") raise tf_version = distutils.version.LooseVersion(tf.__version__) v_1_12 = distutils.version.LooseVersion("1.12.0") if tf_version < v_1_12: raise ImportError( "This version of TensorFlow Datasets requires TensorFlow " "version >= {required}; Detected an installation of version {present}. " "Please upgrade TensorFlow to proceed.".format( required="1.12.0", present=tf.__version__)) _patch_tf(tf)
def ensure_tf_install(): # pylint: disable=g-statement-before-imports """Attempt to import tensorflow, and ensure its version is sufficient. Raises: ImportError: if either tensorflow is not importable or its version is inadequate. """ try: import tensorflow as tf except ImportError: # Print more informative error message, then reraise. print("\n\nFailed to import TensorFlow. Please note that TensorFlow is not " "installed by default when you install TensorFlow Datasets. This is " "so that users can decide whether to install the GPU-enabled " "TensorFlow package. To use TensorFlow Datasets, please install the " "most recent version of TensorFlow, by following instructions at " "https://tensorflow.org/install.\n\n") raise tf_version = distutils.version.LooseVersion(tf.__version__) v_1_12 = distutils.version.LooseVersion("1.12.0") if tf_version < v_1_12: raise ImportError( "This version of TensorFlow Datasets requires TensorFlow " "version >= {required}; Detected an installation of version {present}. " "Please upgrade TensorFlow to proceed.".format( required="1.12.0", present=tf.__version__)) _patch_tf(tf)
[ "Attempt", "to", "import", "tensorflow", "and", "ensure", "its", "version", "is", "sufficient", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/tf_compat.py#L39-L67
[ "def", "ensure_tf_install", "(", ")", ":", "# pylint: disable=g-statement-before-imports", "try", ":", "import", "tensorflow", "as", "tf", "except", "ImportError", ":", "# Print more informative error message, then reraise.", "print", "(", "\"\\n\\nFailed to import TensorFlow. Pl...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_patch_tf
Patch TF to maintain compatibility across versions.
tensorflow_datasets/core/tf_compat.py
def _patch_tf(tf): """Patch TF to maintain compatibility across versions.""" global TF_PATCH if TF_PATCH: return v_1_12 = distutils.version.LooseVersion("1.12.0") v_1_13 = distutils.version.LooseVersion("1.13.0") v_2 = distutils.version.LooseVersion("2.0.0") tf_version = distutils.version.LooseVersion(tf.__version__) if v_1_12 <= tf_version < v_1_13: # TODO(b/123930850): remove when 1.13 is stable. TF_PATCH = "tf1_12" _patch_for_tf1_12(tf) elif v_1_13 <= tf_version < v_2: TF_PATCH = "tf1_13" _patch_for_tf1_13(tf) else: TF_PATCH = "tf2" _patch_for_tf2(tf)
def _patch_tf(tf): """Patch TF to maintain compatibility across versions.""" global TF_PATCH if TF_PATCH: return v_1_12 = distutils.version.LooseVersion("1.12.0") v_1_13 = distutils.version.LooseVersion("1.13.0") v_2 = distutils.version.LooseVersion("2.0.0") tf_version = distutils.version.LooseVersion(tf.__version__) if v_1_12 <= tf_version < v_1_13: # TODO(b/123930850): remove when 1.13 is stable. TF_PATCH = "tf1_12" _patch_for_tf1_12(tf) elif v_1_13 <= tf_version < v_2: TF_PATCH = "tf1_13" _patch_for_tf1_13(tf) else: TF_PATCH = "tf2" _patch_for_tf2(tf)
[ "Patch", "TF", "to", "maintain", "compatibility", "across", "versions", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/tf_compat.py#L70-L89
[ "def", "_patch_tf", "(", "tf", ")", ":", "global", "TF_PATCH", "if", "TF_PATCH", ":", "return", "v_1_12", "=", "distutils", ".", "version", ".", "LooseVersion", "(", "\"1.12.0\"", ")", "v_1_13", "=", "distutils", ".", "version", ".", "LooseVersion", "(", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_patch_for_tf1_12
Monkey patch tf 1.12 so tfds can use it.
tensorflow_datasets/core/tf_compat.py
def _patch_for_tf1_12(tf): """Monkey patch tf 1.12 so tfds can use it.""" tf.io.gfile = tf.gfile tf.io.gfile.copy = tf.gfile.Copy tf.io.gfile.exists = tf.gfile.Exists tf.io.gfile.glob = tf.gfile.Glob tf.io.gfile.isdir = tf.gfile.IsDirectory tf.io.gfile.listdir = tf.gfile.ListDirectory tf.io.gfile.makedirs = tf.gfile.MakeDirs tf.io.gfile.mkdir = tf.gfile.MkDir tf.io.gfile.remove = tf.gfile.Remove tf.io.gfile.rename = tf.gfile.Rename tf.io.gfile.rmtree = tf.gfile.DeleteRecursively tf.io.gfile.stat = tf.gfile.Stat tf.io.gfile.walk = tf.gfile.Walk tf.io.gfile.GFile = tf.gfile.GFile tf.data.experimental = tf.contrib.data tf.compat.v1 = types.ModuleType("tf.compat.v1") tf.compat.v1.assert_greater = tf.assert_greater tf.compat.v1.placeholder = tf.placeholder tf.compat.v1.ConfigProto = tf.ConfigProto tf.compat.v1.Session = tf.Session tf.compat.v1.enable_eager_execution = tf.enable_eager_execution tf.compat.v1.io = tf.io tf.compat.v1.data = tf.data tf.compat.v1.data.Dataset = tf.data.Dataset tf.compat.v1.data.make_one_shot_iterator = ( lambda ds: ds.make_one_shot_iterator()) tf.compat.v1.train = tf.train tf.compat.v1.global_variables_initializer = tf.global_variables_initializer tf.compat.v1.test = tf.test tf.compat.v1.test.get_temp_dir = tf.test.get_temp_dir tf.nest = tf.contrib.framework.nest
def _patch_for_tf1_12(tf): """Monkey patch tf 1.12 so tfds can use it.""" tf.io.gfile = tf.gfile tf.io.gfile.copy = tf.gfile.Copy tf.io.gfile.exists = tf.gfile.Exists tf.io.gfile.glob = tf.gfile.Glob tf.io.gfile.isdir = tf.gfile.IsDirectory tf.io.gfile.listdir = tf.gfile.ListDirectory tf.io.gfile.makedirs = tf.gfile.MakeDirs tf.io.gfile.mkdir = tf.gfile.MkDir tf.io.gfile.remove = tf.gfile.Remove tf.io.gfile.rename = tf.gfile.Rename tf.io.gfile.rmtree = tf.gfile.DeleteRecursively tf.io.gfile.stat = tf.gfile.Stat tf.io.gfile.walk = tf.gfile.Walk tf.io.gfile.GFile = tf.gfile.GFile tf.data.experimental = tf.contrib.data tf.compat.v1 = types.ModuleType("tf.compat.v1") tf.compat.v1.assert_greater = tf.assert_greater tf.compat.v1.placeholder = tf.placeholder tf.compat.v1.ConfigProto = tf.ConfigProto tf.compat.v1.Session = tf.Session tf.compat.v1.enable_eager_execution = tf.enable_eager_execution tf.compat.v1.io = tf.io tf.compat.v1.data = tf.data tf.compat.v1.data.Dataset = tf.data.Dataset tf.compat.v1.data.make_one_shot_iterator = ( lambda ds: ds.make_one_shot_iterator()) tf.compat.v1.train = tf.train tf.compat.v1.global_variables_initializer = tf.global_variables_initializer tf.compat.v1.test = tf.test tf.compat.v1.test.get_temp_dir = tf.test.get_temp_dir tf.nest = tf.contrib.framework.nest
[ "Monkey", "patch", "tf", "1", ".", "12", "so", "tfds", "can", "use", "it", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/tf_compat.py#L100-L132
[ "def", "_patch_for_tf1_12", "(", "tf", ")", ":", "tf", ".", "io", ".", "gfile", "=", "tf", ".", "gfile", "tf", ".", "io", ".", "gfile", ".", "copy", "=", "tf", ".", "gfile", ".", "Copy", "tf", ".", "io", ".", "gfile", ".", "exists", "=", "tf", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_patch_for_tf1_13
Monkey patch tf 1.13 so tfds can use it.
tensorflow_datasets/core/tf_compat.py
def _patch_for_tf1_13(tf): """Monkey patch tf 1.13 so tfds can use it.""" if not hasattr(tf.io.gfile, "GFile"): tf.io.gfile.GFile = tf.gfile.GFile if not hasattr(tf, "nest"): tf.nest = tf.contrib.framework.nest if not hasattr(tf.compat, "v2"): tf.compat.v2 = types.ModuleType("tf.compat.v2") tf.compat.v2.data = types.ModuleType("tf.compat.v2.data") from tensorflow.python.data.ops import dataset_ops tf.compat.v2.data.Dataset = dataset_ops.DatasetV2 if not hasattr(tf.compat.v2.data.Dataset, "output_shapes"): from tensorflow.python.data.ops import dataset_ops if hasattr(dataset_ops, "get_legacy_output_shapes"): tf.compat.v2.data.Dataset.output_shapes = property( dataset_ops.get_legacy_output_shapes) tf.compat.v2.data.Dataset.output_types = property( dataset_ops.get_legacy_output_types)
def _patch_for_tf1_13(tf): """Monkey patch tf 1.13 so tfds can use it.""" if not hasattr(tf.io.gfile, "GFile"): tf.io.gfile.GFile = tf.gfile.GFile if not hasattr(tf, "nest"): tf.nest = tf.contrib.framework.nest if not hasattr(tf.compat, "v2"): tf.compat.v2 = types.ModuleType("tf.compat.v2") tf.compat.v2.data = types.ModuleType("tf.compat.v2.data") from tensorflow.python.data.ops import dataset_ops tf.compat.v2.data.Dataset = dataset_ops.DatasetV2 if not hasattr(tf.compat.v2.data.Dataset, "output_shapes"): from tensorflow.python.data.ops import dataset_ops if hasattr(dataset_ops, "get_legacy_output_shapes"): tf.compat.v2.data.Dataset.output_shapes = property( dataset_ops.get_legacy_output_shapes) tf.compat.v2.data.Dataset.output_types = property( dataset_ops.get_legacy_output_types)
[ "Monkey", "patch", "tf", "1", ".", "13", "so", "tfds", "can", "use", "it", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/tf_compat.py#L135-L152
[ "def", "_patch_for_tf1_13", "(", "tf", ")", ":", "if", "not", "hasattr", "(", "tf", ".", "io", ".", "gfile", ",", "\"GFile\"", ")", ":", "tf", ".", "io", ".", "gfile", ".", "GFile", "=", "tf", ".", "gfile", ".", "GFile", "if", "not", "hasattr", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
is_dataset
Whether ds is a Dataset. Compatible across TF versions.
tensorflow_datasets/core/tf_compat.py
def is_dataset(ds): """Whether ds is a Dataset. Compatible across TF versions.""" import tensorflow as tf from tensorflow_datasets.core.utils import py_utils dataset_types = [tf.data.Dataset] v1_ds = py_utils.rgetattr(tf, "compat.v1.data.Dataset", None) v2_ds = py_utils.rgetattr(tf, "compat.v2.data.Dataset", None) if v1_ds is not None: dataset_types.append(v1_ds) if v2_ds is not None: dataset_types.append(v2_ds) return isinstance(ds, tuple(dataset_types))
def is_dataset(ds): """Whether ds is a Dataset. Compatible across TF versions.""" import tensorflow as tf from tensorflow_datasets.core.utils import py_utils dataset_types = [tf.data.Dataset] v1_ds = py_utils.rgetattr(tf, "compat.v1.data.Dataset", None) v2_ds = py_utils.rgetattr(tf, "compat.v2.data.Dataset", None) if v1_ds is not None: dataset_types.append(v1_ds) if v2_ds is not None: dataset_types.append(v2_ds) return isinstance(ds, tuple(dataset_types))
[ "Whether", "ds", "is", "a", "Dataset", ".", "Compatible", "across", "TF", "versions", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/tf_compat.py#L155-L166
[ "def", "is_dataset", "(", "ds", ")", ":", "import", "tensorflow", "as", "tf", "from", "tensorflow_datasets", ".", "core", ".", "utils", "import", "py_utils", "dataset_types", "=", "[", "tf", ".", "data", ".", "Dataset", "]", "v1_ds", "=", "py_utils", ".", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
TedMultiTranslate._generate_examples
This function returns the examples in the raw (text) form.
tensorflow_datasets/translate/ted_multi.py
def _generate_examples(self, data_file): """This function returns the examples in the raw (text) form.""" with tf.io.gfile.GFile(data_file) as f: reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE) for row in reader: # Everything in the row except for 'talk_name' will be a translation. # Missing/incomplete translations will contain the string "__NULL__" or # "_ _ NULL _ _". yield { 'translations': { lang: text for lang, text in six.iteritems(row) if lang != 'talk_name' and _is_translation_complete(text) }, 'talk_name': row['talk_name'] }
def _generate_examples(self, data_file): """This function returns the examples in the raw (text) form.""" with tf.io.gfile.GFile(data_file) as f: reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE) for row in reader: # Everything in the row except for 'talk_name' will be a translation. # Missing/incomplete translations will contain the string "__NULL__" or # "_ _ NULL _ _". yield { 'translations': { lang: text for lang, text in six.iteritems(row) if lang != 'talk_name' and _is_translation_complete(text) }, 'talk_name': row['talk_name'] }
[ "This", "function", "returns", "the", "examples", "in", "the", "raw", "(", "text", ")", "form", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/ted_multi.py#L108-L123
[ "def", "_generate_examples", "(", "self", ",", "data_file", ")", ":", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "data_file", ")", "as", "f", ":", "reader", "=", "csv", ".", "DictReader", "(", "f", ",", "delimiter", "=", "'\\t'", ",", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
MultiNLI._generate_examples
Generate mnli examples. Args: filepath: a string Yields: dictionaries containing "premise", "hypothesis" and "label" strings
tensorflow_datasets/text/multi_nli.py
def _generate_examples(self, filepath): """Generate mnli examples. Args: filepath: a string Yields: dictionaries containing "premise", "hypothesis" and "label" strings """ for idx, line in enumerate(tf.io.gfile.GFile(filepath, "rb")): if idx == 0: continue # skip header line = tf.compat.as_text(line.strip()) split_line = line.split("\t") # Examples not marked with a three out of five consensus are marked with # "-" and should not be used in standard evaluations. if split_line[0] == "-": continue # Works for both splits even though dev has some extra human labels. yield { "premise": split_line[5], "hypothesis": split_line[6], "label": split_line[0] }
def _generate_examples(self, filepath): """Generate mnli examples. Args: filepath: a string Yields: dictionaries containing "premise", "hypothesis" and "label" strings """ for idx, line in enumerate(tf.io.gfile.GFile(filepath, "rb")): if idx == 0: continue # skip header line = tf.compat.as_text(line.strip()) split_line = line.split("\t") # Examples not marked with a three out of five consensus are marked with # "-" and should not be used in standard evaluations. if split_line[0] == "-": continue # Works for both splits even though dev has some extra human labels. yield { "premise": split_line[5], "hypothesis": split_line[6], "label": split_line[0] }
[ "Generate", "mnli", "examples", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/text/multi_nli.py#L148-L171
[ "def", "_generate_examples", "(", "self", ",", "filepath", ")", ":", "for", "idx", ",", "line", "in", "enumerate", "(", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "filepath", ",", "\"rb\"", ")", ")", ":", "if", "idx", "==", "0", ":", "contin...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
ImageLabelFolder._split_generators
Returns SplitGenerators from the folder names.
tensorflow_datasets/image/image_folder.py
def _split_generators(self, dl_manager): """Returns SplitGenerators from the folder names.""" # At data creation time, parse the folder to deduce number of splits, # labels, image size, # The splits correspond to the high level folders split_names = list_folders(dl_manager.manual_dir) # Extract all label names and associated images split_label_images = {} # dict[split_name][label_name] = list(img_paths) for split_name in split_names: split_dir = os.path.join(dl_manager.manual_dir, split_name) split_label_images[split_name] = { label_name: list_imgs(os.path.join(split_dir, label_name)) for label_name in list_folders(split_dir) } # Merge all label names from all splits to get the final list of labels # Sorted list for determinism labels = [split.keys() for split in split_label_images.values()] labels = list(sorted(set(itertools.chain(*labels)))) # Could improve the automated encoding format detection # Extract the list of all image paths image_paths = [ image_paths for label_images in split_label_images.values() for image_paths in label_images.values() ] if any(f.lower().endswith(".png") for f in itertools.chain(*image_paths)): encoding_format = "png" else: encoding_format = "jpeg" # Update the info.features. Those info will be automatically resored when # the dataset is re-created self.info.features["image"].set_encoding_format(encoding_format) self.info.features["label"].names = labels def num_examples(label_images): return sum(len(imgs) for imgs in label_images.values()) # Define the splits return [ tfds.core.SplitGenerator( name=split_name, # The number of shards is a dynamic function of the total # number of images (between 0-10) num_shards=min(10, max(num_examples(label_images) // 1000, 1)), gen_kwargs=dict(label_images=label_images,), ) for split_name, label_images in split_label_images.items() ]
def _split_generators(self, dl_manager): """Returns SplitGenerators from the folder names.""" # At data creation time, parse the folder to deduce number of splits, # labels, image size, # The splits correspond to the high level folders split_names = list_folders(dl_manager.manual_dir) # Extract all label names and associated images split_label_images = {} # dict[split_name][label_name] = list(img_paths) for split_name in split_names: split_dir = os.path.join(dl_manager.manual_dir, split_name) split_label_images[split_name] = { label_name: list_imgs(os.path.join(split_dir, label_name)) for label_name in list_folders(split_dir) } # Merge all label names from all splits to get the final list of labels # Sorted list for determinism labels = [split.keys() for split in split_label_images.values()] labels = list(sorted(set(itertools.chain(*labels)))) # Could improve the automated encoding format detection # Extract the list of all image paths image_paths = [ image_paths for label_images in split_label_images.values() for image_paths in label_images.values() ] if any(f.lower().endswith(".png") for f in itertools.chain(*image_paths)): encoding_format = "png" else: encoding_format = "jpeg" # Update the info.features. Those info will be automatically resored when # the dataset is re-created self.info.features["image"].set_encoding_format(encoding_format) self.info.features["label"].names = labels def num_examples(label_images): return sum(len(imgs) for imgs in label_images.values()) # Define the splits return [ tfds.core.SplitGenerator( name=split_name, # The number of shards is a dynamic function of the total # number of images (between 0-10) num_shards=min(10, max(num_examples(label_images) // 1000, 1)), gen_kwargs=dict(label_images=label_images,), ) for split_name, label_images in split_label_images.items() ]
[ "Returns", "SplitGenerators", "from", "the", "folder", "names", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/image_folder.py#L103-L154
[ "def", "_split_generators", "(", "self", ",", "dl_manager", ")", ":", "# At data creation time, parse the folder to deduce number of splits,", "# labels, image size,", "# The splits correspond to the high level folders", "split_names", "=", "list_folders", "(", "dl_manager", ".", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
ImageLabelFolder._generate_examples
Generate example for each image in the dict.
tensorflow_datasets/image/image_folder.py
def _generate_examples(self, label_images): """Generate example for each image in the dict.""" for label, image_paths in label_images.items(): for image_path in image_paths: yield { "image": image_path, "label": label, }
def _generate_examples(self, label_images): """Generate example for each image in the dict.""" for label, image_paths in label_images.items(): for image_path in image_paths: yield { "image": image_path, "label": label, }
[ "Generate", "example", "for", "each", "image", "in", "the", "dict", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/image_folder.py#L156-L164
[ "def", "_generate_examples", "(", "self", ",", "label_images", ")", ":", "for", "label", ",", "image_paths", "in", "label_images", ".", "items", "(", ")", ":", "for", "image_path", "in", "image_paths", ":", "yield", "{", "\"image\"", ":", "image_path", ",", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
create_dataset_file
Create a new dataset from a template.
tensorflow_datasets/scripts/create_new_dataset.py
def create_dataset_file(root_dir, data): """Create a new dataset from a template.""" file_path = os.path.join(root_dir, '{dataset_type}', '{dataset_name}.py') context = ( _HEADER + _DATASET_DEFAULT_IMPORTS + _CITATION + _DESCRIPTION + _DATASET_DEFAULTS ) with gfile.GFile(file_path.format(**data), 'w') as f: f.write(context.format(**data))
def create_dataset_file(root_dir, data): """Create a new dataset from a template.""" file_path = os.path.join(root_dir, '{dataset_type}', '{dataset_name}.py') context = ( _HEADER + _DATASET_DEFAULT_IMPORTS + _CITATION + _DESCRIPTION + _DATASET_DEFAULTS ) with gfile.GFile(file_path.format(**data), 'w') as f: f.write(context.format(**data))
[ "Create", "a", "new", "dataset", "from", "a", "template", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/scripts/create_new_dataset.py#L155-L164
[ "def", "create_dataset_file", "(", "root_dir", ",", "data", ")", ":", "file_path", "=", "os", ".", "path", ".", "join", "(", "root_dir", ",", "'{dataset_type}'", ",", "'{dataset_name}.py'", ")", "context", "=", "(", "_HEADER", "+", "_DATASET_DEFAULT_IMPORTS", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
add_the_init
Append the new dataset file to the __init__.py.
tensorflow_datasets/scripts/create_new_dataset.py
def add_the_init(root_dir, data): """Append the new dataset file to the __init__.py.""" init_file = os.path.join(root_dir, '{dataset_type}', '__init__.py') context = ( 'from tensorflow_datasets.{dataset_type}.{dataset_name} import ' '{dataset_cls} # {TODO} Sort alphabetically\n' ) with gfile.GFile(init_file.format(**data), 'a') as f: f.write(context.format(**data))
def add_the_init(root_dir, data): """Append the new dataset file to the __init__.py.""" init_file = os.path.join(root_dir, '{dataset_type}', '__init__.py') context = ( 'from tensorflow_datasets.{dataset_type}.{dataset_name} import ' '{dataset_cls} # {TODO} Sort alphabetically\n' ) with gfile.GFile(init_file.format(**data), 'a') as f: f.write(context.format(**data))
[ "Append", "the", "new", "dataset", "file", "to", "the", "__init__", ".", "py", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/scripts/create_new_dataset.py#L167-L175
[ "def", "add_the_init", "(", "root_dir", ",", "data", ")", ":", "init_file", "=", "os", ".", "path", ".", "join", "(", "root_dir", ",", "'{dataset_type}'", ",", "'__init__.py'", ")", "context", "=", "(", "'from tensorflow_datasets.{dataset_type}.{dataset_name} import...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SvhnCropped._generate_examples
Generate examples as dicts. Args: filepath: `str` path of the file to process. Yields: Generator yielding the next samples
tensorflow_datasets/image/svhn.py
def _generate_examples(self, filepath): """Generate examples as dicts. Args: filepath: `str` path of the file to process. Yields: Generator yielding the next samples """ with tf.io.gfile.GFile(filepath, "rb") as f: data = tfds.core.lazy_imports.scipy.io.loadmat(f) # Maybe should shuffle ? assert np.max(data["y"]) <= 10 # Sanity check assert np.min(data["y"]) > 0 for image, label in zip(np.rollaxis(data["X"], -1), data["y"]): yield { "image": image, "label": label % 10, # digit 0 is saved as 0 (instead of 10) }
def _generate_examples(self, filepath): """Generate examples as dicts. Args: filepath: `str` path of the file to process. Yields: Generator yielding the next samples """ with tf.io.gfile.GFile(filepath, "rb") as f: data = tfds.core.lazy_imports.scipy.io.loadmat(f) # Maybe should shuffle ? assert np.max(data["y"]) <= 10 # Sanity check assert np.min(data["y"]) > 0 for image, label in zip(np.rollaxis(data["X"], -1), data["y"]): yield { "image": image, "label": label % 10, # digit 0 is saved as 0 (instead of 10) }
[ "Generate", "examples", "as", "dicts", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/svhn.py#L92-L113
[ "def", "_generate_examples", "(", "self", ",", "filepath", ")", ":", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "filepath", ",", "\"rb\"", ")", "as", "f", ":", "data", "=", "tfds", ".", "core", ".", "lazy_imports", ".", "scipy", ".", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Chexpert._split_generators
Returns SplitGenerators.
tensorflow_datasets/image/chexpert.py
def _split_generators(self, dl_manager): """Returns SplitGenerators.""" path = dl_manager.manual_dir train_path = os.path.join(path, _TRAIN_DIR) val_path = os.path.join(path, _VALIDATION_DIR) if not tf.io.gfile.exists(train_path) or not tf.io.gfile.exists(val_path): msg = ("You must download the dataset folder from CheXpert" "website manually and place it into %s." % path) raise AssertionError(msg) return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=100, gen_kwargs={ "imgs_path": path, # Relative img path is provided in csv "csv_path": os.path.join(path, _TRAIN_LABELS_FNAME) }, ), tfds.core.SplitGenerator( name=tfds.Split.VALIDATION, num_shards=10, gen_kwargs={ "imgs_path": path, "csv_path": os.path.join(path, _VALIDATION_LABELS_FNAME) }, ), ]
def _split_generators(self, dl_manager): """Returns SplitGenerators.""" path = dl_manager.manual_dir train_path = os.path.join(path, _TRAIN_DIR) val_path = os.path.join(path, _VALIDATION_DIR) if not tf.io.gfile.exists(train_path) or not tf.io.gfile.exists(val_path): msg = ("You must download the dataset folder from CheXpert" "website manually and place it into %s." % path) raise AssertionError(msg) return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=100, gen_kwargs={ "imgs_path": path, # Relative img path is provided in csv "csv_path": os.path.join(path, _TRAIN_LABELS_FNAME) }, ), tfds.core.SplitGenerator( name=tfds.Split.VALIDATION, num_shards=10, gen_kwargs={ "imgs_path": path, "csv_path": os.path.join(path, _VALIDATION_LABELS_FNAME) }, ), ]
[ "Returns", "SplitGenerators", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/chexpert.py#L93-L121
[ "def", "_split_generators", "(", "self", ",", "dl_manager", ")", ":", "path", "=", "dl_manager", ".", "manual_dir", "train_path", "=", "os", ".", "path", ".", "join", "(", "path", ",", "_TRAIN_DIR", ")", "val_path", "=", "os", ".", "path", ".", "join", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Chexpert._generate_examples
Yields examples.
tensorflow_datasets/image/chexpert.py
def _generate_examples(self, imgs_path, csv_path): """Yields examples.""" with tf.io.gfile.GFile(csv_path) as csv_f: reader = csv.DictReader(csv_f) # Get keys for each label from csv label_keys = reader.fieldnames[5:] data = [] for row in reader: # Get image based on indicated path in csv name = row["Path"] labels = [_LABELS[row[key]] for key in label_keys] data.append((name, labels)) for name, labels in data: yield { "name": name, "image": os.path.join(imgs_path, name), "label": labels }
def _generate_examples(self, imgs_path, csv_path): """Yields examples.""" with tf.io.gfile.GFile(csv_path) as csv_f: reader = csv.DictReader(csv_f) # Get keys for each label from csv label_keys = reader.fieldnames[5:] data = [] for row in reader: # Get image based on indicated path in csv name = row["Path"] labels = [_LABELS[row[key]] for key in label_keys] data.append((name, labels)) for name, labels in data: yield { "name": name, "image": os.path.join(imgs_path, name), "label": labels }
[ "Yields", "examples", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/chexpert.py#L123-L141
[ "def", "_generate_examples", "(", "self", ",", "imgs_path", ",", "csv_path", ")", ":", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "csv_path", ")", "as", "csv_f", ":", "reader", "=", "csv", ".", "DictReader", "(", "csv_f", ")", "# Get key...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_make_builder_configs
Construct a list of BuilderConfigs. Construct a list of 60 Imagenet2012CorruptedConfig objects, corresponding to the 12 corruption types, with each type having 5 severities. Returns: A list of 60 Imagenet2012CorruptedConfig objects.
tensorflow_datasets/image/imagenet2012_corrupted.py
def _make_builder_configs(): """Construct a list of BuilderConfigs. Construct a list of 60 Imagenet2012CorruptedConfig objects, corresponding to the 12 corruption types, with each type having 5 severities. Returns: A list of 60 Imagenet2012CorruptedConfig objects. """ config_list = [] for each_corruption in TYPE_LIST: for each_severity in range(1, 6): name_str = each_corruption + '_' + str(each_severity) version_str = '0.0.1' description_str = 'corruption type = ' + each_corruption + ', severity = ' description_str += str(each_severity) config_list.append( Imagenet2012CorruptedConfig( name=name_str, version=version_str, description=description_str, corruption_type=each_corruption, severity=each_severity, )) return config_list
def _make_builder_configs(): """Construct a list of BuilderConfigs. Construct a list of 60 Imagenet2012CorruptedConfig objects, corresponding to the 12 corruption types, with each type having 5 severities. Returns: A list of 60 Imagenet2012CorruptedConfig objects. """ config_list = [] for each_corruption in TYPE_LIST: for each_severity in range(1, 6): name_str = each_corruption + '_' + str(each_severity) version_str = '0.0.1' description_str = 'corruption type = ' + each_corruption + ', severity = ' description_str += str(each_severity) config_list.append( Imagenet2012CorruptedConfig( name=name_str, version=version_str, description=description_str, corruption_type=each_corruption, severity=each_severity, )) return config_list
[ "Construct", "a", "list", "of", "BuilderConfigs", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/imagenet2012_corrupted.py#L83-L107
[ "def", "_make_builder_configs", "(", ")", ":", "config_list", "=", "[", "]", "for", "each_corruption", "in", "TYPE_LIST", ":", "for", "each_severity", "in", "range", "(", "1", ",", "6", ")", ":", "name_str", "=", "each_corruption", "+", "'_'", "+", "str", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Imagenet2012Corrupted._split_generators
Return the validation split of ImageNet2012. Args: dl_manager: download manager object. Returns: validation split.
tensorflow_datasets/image/imagenet2012_corrupted.py
def _split_generators(self, dl_manager): """Return the validation split of ImageNet2012. Args: dl_manager: download manager object. Returns: validation split. """ splits = super(Imagenet2012Corrupted, self)._split_generators(dl_manager) validation = splits[1] return [validation]
def _split_generators(self, dl_manager): """Return the validation split of ImageNet2012. Args: dl_manager: download manager object. Returns: validation split. """ splits = super(Imagenet2012Corrupted, self)._split_generators(dl_manager) validation = splits[1] return [validation]
[ "Return", "the", "validation", "split", "of", "ImageNet2012", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/imagenet2012_corrupted.py#L134-L145
[ "def", "_split_generators", "(", "self", ",", "dl_manager", ")", ":", "splits", "=", "super", "(", "Imagenet2012Corrupted", ",", "self", ")", ".", "_split_generators", "(", "dl_manager", ")", "validation", "=", "splits", "[", "1", "]", "return", "[", "valida...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Imagenet2012Corrupted._generate_examples_validation
Generate corrupted imagenet validation data. Apply corruptions to the raw images according to self.corruption_type. Args: archive: an iterator for the raw dataset. labels: a dictionary that maps the file names to imagenet labels. Yields: dictionary with the file name, an image file objective, and label of each imagenet validation data.
tensorflow_datasets/image/imagenet2012_corrupted.py
def _generate_examples_validation(self, archive, labels): """Generate corrupted imagenet validation data. Apply corruptions to the raw images according to self.corruption_type. Args: archive: an iterator for the raw dataset. labels: a dictionary that maps the file names to imagenet labels. Yields: dictionary with the file name, an image file objective, and label of each imagenet validation data. """ # Get the current random seeds. numpy_st0 = np.random.get_state() # Set new random seeds. np.random.seed(135) logging.warning('Overwriting cv2 RNG seed.') tfds.core.lazy_imports.cv2.setRNGSeed(357) for example in super(Imagenet2012Corrupted, self)._generate_examples_validation(archive, labels): with tf.Graph().as_default(): tf_img = tf.image.decode_jpeg(example['image'].read(), channels=3) image_np = tfds.as_numpy(tf_img) example['image'] = self._get_corrupted_example(image_np) yield example # Reset the seeds back to their original values. np.random.set_state(numpy_st0)
def _generate_examples_validation(self, archive, labels): """Generate corrupted imagenet validation data. Apply corruptions to the raw images according to self.corruption_type. Args: archive: an iterator for the raw dataset. labels: a dictionary that maps the file names to imagenet labels. Yields: dictionary with the file name, an image file objective, and label of each imagenet validation data. """ # Get the current random seeds. numpy_st0 = np.random.get_state() # Set new random seeds. np.random.seed(135) logging.warning('Overwriting cv2 RNG seed.') tfds.core.lazy_imports.cv2.setRNGSeed(357) for example in super(Imagenet2012Corrupted, self)._generate_examples_validation(archive, labels): with tf.Graph().as_default(): tf_img = tf.image.decode_jpeg(example['image'].read(), channels=3) image_np = tfds.as_numpy(tf_img) example['image'] = self._get_corrupted_example(image_np) yield example # Reset the seeds back to their original values. np.random.set_state(numpy_st0)
[ "Generate", "corrupted", "imagenet", "validation", "data", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/imagenet2012_corrupted.py#L147-L175
[ "def", "_generate_examples_validation", "(", "self", ",", "archive", ",", "labels", ")", ":", "# Get the current random seeds.", "numpy_st0", "=", "np", ".", "random", ".", "get_state", "(", ")", "# Set new random seeds.", "np", ".", "random", ".", "seed", "(", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Imagenet2012Corrupted._get_corrupted_example
Return corrupted images. Args: x: numpy array, uncorrupted image. Returns: numpy array, corrupted images.
tensorflow_datasets/image/imagenet2012_corrupted.py
def _get_corrupted_example(self, x): """Return corrupted images. Args: x: numpy array, uncorrupted image. Returns: numpy array, corrupted images. """ corruption_type = self.builder_config.corruption_type severity = self.builder_config.severity return { 'gaussian_noise': corruptions.gaussian_noise, 'shot_noise': corruptions.shot_noise, 'impulse_noise': corruptions.impulse_noise, 'defocus_blur': corruptions.defocus_blur, 'frosted_glass_blur': corruptions.frosted_glass_blur, 'zoom_blur': corruptions.zoom_blur, 'fog': corruptions.fog, 'brightness': corruptions.brightness, 'contrast': corruptions.contrast, 'elastic': corruptions.elastic, 'pixelate': corruptions.pixelate, 'jpeg_compression': corruptions.jpeg_compression, }[corruption_type](x, severity)
def _get_corrupted_example(self, x): """Return corrupted images. Args: x: numpy array, uncorrupted image. Returns: numpy array, corrupted images. """ corruption_type = self.builder_config.corruption_type severity = self.builder_config.severity return { 'gaussian_noise': corruptions.gaussian_noise, 'shot_noise': corruptions.shot_noise, 'impulse_noise': corruptions.impulse_noise, 'defocus_blur': corruptions.defocus_blur, 'frosted_glass_blur': corruptions.frosted_glass_blur, 'zoom_blur': corruptions.zoom_blur, 'fog': corruptions.fog, 'brightness': corruptions.brightness, 'contrast': corruptions.contrast, 'elastic': corruptions.elastic, 'pixelate': corruptions.pixelate, 'jpeg_compression': corruptions.jpeg_compression, }[corruption_type](x, severity)
[ "Return", "corrupted", "images", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/imagenet2012_corrupted.py#L177-L202
[ "def", "_get_corrupted_example", "(", "self", ",", "x", ")", ":", "corruption_type", "=", "self", ".", "builder_config", ".", "corruption_type", "severity", "=", "self", ".", "builder_config", ".", "severity", "return", "{", "'gaussian_noise'", ":", "corruptions",...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
assert_shape_match
Ensure the shape1 match the pattern given by shape2. Ex: assert_shape_match((64, 64, 3), (None, None, 3)) Args: shape1 (tuple): Static shape shape2 (tuple): Dynamic shape (can contain None)
tensorflow_datasets/core/utils/tf_utils.py
def assert_shape_match(shape1, shape2): """Ensure the shape1 match the pattern given by shape2. Ex: assert_shape_match((64, 64, 3), (None, None, 3)) Args: shape1 (tuple): Static shape shape2 (tuple): Dynamic shape (can contain None) """ shape1 = tf.TensorShape(shape1) shape2 = tf.TensorShape(shape2) if shape1.ndims is None or shape2.ndims is None: raise ValueError('Shapes must have known rank. Got %s and %s.' % (shape1.ndims, shape2.ndims)) shape1.assert_same_rank(shape2) shape1.assert_is_compatible_with(shape2)
def assert_shape_match(shape1, shape2): """Ensure the shape1 match the pattern given by shape2. Ex: assert_shape_match((64, 64, 3), (None, None, 3)) Args: shape1 (tuple): Static shape shape2 (tuple): Dynamic shape (can contain None) """ shape1 = tf.TensorShape(shape1) shape2 = tf.TensorShape(shape2) if shape1.ndims is None or shape2.ndims is None: raise ValueError('Shapes must have known rank. Got %s and %s.' % (shape1.ndims, shape2.ndims)) shape1.assert_same_rank(shape2) shape1.assert_is_compatible_with(shape2)
[ "Ensure", "the", "shape1", "match", "the", "pattern", "given", "by", "shape2", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/tf_utils.py#L132-L148
[ "def", "assert_shape_match", "(", "shape1", ",", "shape2", ")", ":", "shape1", "=", "tf", ".", "TensorShape", "(", "shape1", ")", "shape2", "=", "tf", ".", "TensorShape", "(", "shape2", ")", "if", "shape1", ".", "ndims", "is", "None", "or", "shape2", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
raw_nogpu_session
tf.Session, hiding GPUs.
tensorflow_datasets/core/utils/tf_utils.py
def raw_nogpu_session(graph=None): """tf.Session, hiding GPUs.""" config = tf.compat.v1.ConfigProto(device_count={'GPU': 0}) return tf.compat.v1.Session(config=config, graph=graph)
def raw_nogpu_session(graph=None): """tf.Session, hiding GPUs.""" config = tf.compat.v1.ConfigProto(device_count={'GPU': 0}) return tf.compat.v1.Session(config=config, graph=graph)
[ "tf", ".", "Session", "hiding", "GPUs", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/tf_utils.py#L161-L164
[ "def", "raw_nogpu_session", "(", "graph", "=", "None", ")", ":", "config", "=", "tf", ".", "compat", ".", "v1", ".", "ConfigProto", "(", "device_count", "=", "{", "'GPU'", ":", "0", "}", ")", "return", "tf", ".", "compat", ".", "v1", ".", "Session", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
maybe_with_graph
Eager-compatible Graph().as_default() yielding the graph.
tensorflow_datasets/core/utils/tf_utils.py
def maybe_with_graph(graph=None, create_if_none=True): """Eager-compatible Graph().as_default() yielding the graph.""" if tf.executing_eagerly(): yield None else: if graph is None and create_if_none: graph = tf.Graph() if graph is None: yield None else: with graph.as_default(): yield graph
def maybe_with_graph(graph=None, create_if_none=True): """Eager-compatible Graph().as_default() yielding the graph.""" if tf.executing_eagerly(): yield None else: if graph is None and create_if_none: graph = tf.Graph() if graph is None: yield None else: with graph.as_default(): yield graph
[ "Eager", "-", "compatible", "Graph", "()", ".", "as_default", "()", "yielding", "the", "graph", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/tf_utils.py#L168-L180
[ "def", "maybe_with_graph", "(", "graph", "=", "None", ",", "create_if_none", "=", "True", ")", ":", "if", "tf", ".", "executing_eagerly", "(", ")", ":", "yield", "None", "else", ":", "if", "graph", "is", "None", "and", "create_if_none", ":", "graph", "="...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
TFGraphRunner.run
Execute the given TensorFlow function.
tensorflow_datasets/core/utils/tf_utils.py
def run(self, fct, input_): """Execute the given TensorFlow function.""" # TF 2.0 if tf.executing_eagerly(): return fct(input_).numpy() # TF 1.0 else: # Should compile the function if this is the first time encountered if not isinstance(input_, np.ndarray): input_ = np.array(input_) run_args = RunArgs(fct=fct, input=input_) signature = self._build_signature(run_args) if signature not in self._graph_run_cache: graph_run = self._build_graph_run(run_args) self._graph_run_cache[signature] = graph_run else: graph_run = self._graph_run_cache[signature] # Then execute the cached graph return graph_run.session.run( graph_run.output, feed_dict={graph_run.placeholder: input_}, )
def run(self, fct, input_): """Execute the given TensorFlow function.""" # TF 2.0 if tf.executing_eagerly(): return fct(input_).numpy() # TF 1.0 else: # Should compile the function if this is the first time encountered if not isinstance(input_, np.ndarray): input_ = np.array(input_) run_args = RunArgs(fct=fct, input=input_) signature = self._build_signature(run_args) if signature not in self._graph_run_cache: graph_run = self._build_graph_run(run_args) self._graph_run_cache[signature] = graph_run else: graph_run = self._graph_run_cache[signature] # Then execute the cached graph return graph_run.session.run( graph_run.output, feed_dict={graph_run.placeholder: input_}, )
[ "Execute", "the", "given", "TensorFlow", "function", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/tf_utils.py#L70-L92
[ "def", "run", "(", "self", ",", "fct", ",", "input_", ")", ":", "# TF 2.0", "if", "tf", ".", "executing_eagerly", "(", ")", ":", "return", "fct", "(", "input_", ")", ".", "numpy", "(", ")", "# TF 1.0", "else", ":", "# Should compile the function if this is...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
TFGraphRunner._build_graph_run
Create a new graph for the given args.
tensorflow_datasets/core/utils/tf_utils.py
def _build_graph_run(self, run_args): """Create a new graph for the given args.""" # Could try to use tfe.py_func(fct) but this would require knowing # information about the signature of the function. # Create a new graph: with tf.Graph().as_default() as g: # Create placeholder input_ = run_args.input placeholder = tf.compat.v1.placeholder( dtype=input_.dtype, shape=input_.shape) output = run_args.fct(placeholder) return GraphRun( session=raw_nogpu_session(g), graph=g, placeholder=placeholder, output=output, )
def _build_graph_run(self, run_args): """Create a new graph for the given args.""" # Could try to use tfe.py_func(fct) but this would require knowing # information about the signature of the function. # Create a new graph: with tf.Graph().as_default() as g: # Create placeholder input_ = run_args.input placeholder = tf.compat.v1.placeholder( dtype=input_.dtype, shape=input_.shape) output = run_args.fct(placeholder) return GraphRun( session=raw_nogpu_session(g), graph=g, placeholder=placeholder, output=output, )
[ "Create", "a", "new", "graph", "for", "the", "given", "args", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/tf_utils.py#L94-L111
[ "def", "_build_graph_run", "(", "self", ",", "run_args", ")", ":", "# Could try to use tfe.py_func(fct) but this would require knowing", "# information about the signature of the function.", "# Create a new graph:", "with", "tf", ".", "Graph", "(", ")", ".", "as_default", "(", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
TFGraphRunner._build_signature
Create a unique signature for each fct/inputs.
tensorflow_datasets/core/utils/tf_utils.py
def _build_signature(self, run_args): """Create a unique signature for each fct/inputs.""" return (id(run_args.fct), run_args.input.dtype, run_args.input.shape)
def _build_signature(self, run_args): """Create a unique signature for each fct/inputs.""" return (id(run_args.fct), run_args.input.dtype, run_args.input.shape)
[ "Create", "a", "unique", "signature", "for", "each", "fct", "/", "inputs", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/tf_utils.py#L113-L115
[ "def", "_build_signature", "(", "self", ",", "run_args", ")", ":", "return", "(", "id", "(", "run_args", ".", "fct", ")", ",", "run_args", ".", "input", ".", "dtype", ",", "run_args", ".", "input", ".", "shape", ")" ]
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Video.encode_example
Converts the given image into a dict convertible to tf example.
tensorflow_datasets/core/features/video_feature.py
def encode_example(self, video_or_path_or_fobj): """Converts the given image into a dict convertible to tf example.""" if isinstance(video_or_path_or_fobj, six.string_types): if not os.path.isfile(video_or_path_or_fobj): _, video_temp_path = tempfile.mkstemp() try: tf.gfile.Copy(video_or_path_or_fobj, video_temp_path, overwrite=True) encoded_video = self._ffmpeg_decode(video_temp_path) finally: os.unlink(video_temp_path) else: encoded_video = self._ffmpeg_decode(video_or_path_or_fobj) elif hasattr(video_or_path_or_fobj, 'read'): encoded_video = self._ffmpeg_decode(video_or_path_or_fobj) else: encoded_video = video_or_path_or_fobj return super(Video, self).encode_example(encoded_video)
def encode_example(self, video_or_path_or_fobj): """Converts the given image into a dict convertible to tf example.""" if isinstance(video_or_path_or_fobj, six.string_types): if not os.path.isfile(video_or_path_or_fobj): _, video_temp_path = tempfile.mkstemp() try: tf.gfile.Copy(video_or_path_or_fobj, video_temp_path, overwrite=True) encoded_video = self._ffmpeg_decode(video_temp_path) finally: os.unlink(video_temp_path) else: encoded_video = self._ffmpeg_decode(video_or_path_or_fobj) elif hasattr(video_or_path_or_fobj, 'read'): encoded_video = self._ffmpeg_decode(video_or_path_or_fobj) else: encoded_video = video_or_path_or_fobj return super(Video, self).encode_example(encoded_video)
[ "Converts", "the", "given", "image", "into", "a", "dict", "convertible", "to", "tf", "example", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/video_feature.py#L148-L164
[ "def", "encode_example", "(", "self", ",", "video_or_path_or_fobj", ")", ":", "if", "isinstance", "(", "video_or_path_or_fobj", ",", "six", ".", "string_types", ")", ":", "if", "not", "os", ".", "path", ".", "isfile", "(", "video_or_path_or_fobj", ")", ":", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
RockPaperScissors._generate_examples
Generate rock, paper or scissors images and labels given the directory path. Args: archive: object that iterates over the zip. Yields: The image path and its corresponding label.
tensorflow_datasets/image/rock_paper_scissors.py
def _generate_examples(self, archive): """Generate rock, paper or scissors images and labels given the directory path. Args: archive: object that iterates over the zip. Yields: The image path and its corresponding label. """ for fname, fobj in archive: res = _NAME_RE.match(fname) if not res: # if anything other than .png; skip continue label = res.group(2).lower() yield { "image": fobj, "label": label, }
def _generate_examples(self, archive): """Generate rock, paper or scissors images and labels given the directory path. Args: archive: object that iterates over the zip. Yields: The image path and its corresponding label. """ for fname, fobj in archive: res = _NAME_RE.match(fname) if not res: # if anything other than .png; skip continue label = res.group(2).lower() yield { "image": fobj, "label": label, }
[ "Generate", "rock", "paper", "or", "scissors", "images", "and", "labels", "given", "the", "directory", "path", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/rock_paper_scissors.py#L82-L100
[ "def", "_generate_examples", "(", "self", ",", "archive", ")", ":", "for", "fname", ",", "fobj", "in", "archive", ":", "res", "=", "_NAME_RE", ".", "match", "(", "fname", ")", "if", "not", "res", ":", "# if anything other than .png; skip", "continue", "label...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Titanic._generate_examples
Generate features and target given the directory path. Args: file_path: path where the csv file is stored Yields: The features and the target
tensorflow_datasets/structured/titanic.py
def _generate_examples(self, file_path): """Generate features and target given the directory path. Args: file_path: path where the csv file is stored Yields: The features and the target """ with tf.io.gfile.GFile(file_path) as f: raw_data = csv.DictReader(f) for row in raw_data: survive_val = row.pop("survived") yield { "survived": convert_to_label(survive_val, _SURVIVED_DICT), "features": { name: FEATURE_DICT[name][1](value) for name, value in row.items() } }
def _generate_examples(self, file_path): """Generate features and target given the directory path. Args: file_path: path where the csv file is stored Yields: The features and the target """ with tf.io.gfile.GFile(file_path) as f: raw_data = csv.DictReader(f) for row in raw_data: survive_val = row.pop("survived") yield { "survived": convert_to_label(survive_val, _SURVIVED_DICT), "features": { name: FEATURE_DICT[name][1](value) for name, value in row.items() } }
[ "Generate", "features", "and", "target", "given", "the", "directory", "path", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/structured/titanic.py#L130-L150
[ "def", "_generate_examples", "(", "self", ",", "file_path", ")", ":", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "file_path", ")", "as", "f", ":", "raw_data", "=", "csv", ".", "DictReader", "(", "f", ")", "for", "row", "in", "raw_data"...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
pad_decr
Strip ID 0 and decrement ids by 1.
tensorflow_datasets/core/features/text/text_encoder.py
def pad_decr(ids): """Strip ID 0 and decrement ids by 1.""" if len(ids) < 1: return list(ids) if not any(ids): return [] # all padding. idx = -1 while not ids[idx]: idx -= 1 if idx == -1: ids = ids else: ids = ids[:idx + 1] return [i - 1 for i in ids]
def pad_decr(ids): """Strip ID 0 and decrement ids by 1.""" if len(ids) < 1: return list(ids) if not any(ids): return [] # all padding. idx = -1 while not ids[idx]: idx -= 1 if idx == -1: ids = ids else: ids = ids[:idx + 1] return [i - 1 for i in ids]
[ "Strip", "ID", "0", "and", "decrement", "ids", "by", "1", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/text_encoder.py#L426-L439
[ "def", "pad_decr", "(", "ids", ")", ":", "if", "len", "(", "ids", ")", "<", "1", ":", "return", "list", "(", "ids", ")", "if", "not", "any", "(", "ids", ")", ":", "return", "[", "]", "# all padding.", "idx", "=", "-", "1", "while", "not", "ids"...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_prepare_reserved_tokens
Prepare reserved tokens and a regex for splitting them out of strings.
tensorflow_datasets/core/features/text/text_encoder.py
def _prepare_reserved_tokens(reserved_tokens): """Prepare reserved tokens and a regex for splitting them out of strings.""" reserved_tokens = [tf.compat.as_text(tok) for tok in reserved_tokens or []] dups = _find_duplicates(reserved_tokens) if dups: raise ValueError("Duplicates found in tokens: %s" % dups) reserved_tokens_re = _make_reserved_tokens_re(reserved_tokens) return reserved_tokens, reserved_tokens_re
def _prepare_reserved_tokens(reserved_tokens): """Prepare reserved tokens and a regex for splitting them out of strings.""" reserved_tokens = [tf.compat.as_text(tok) for tok in reserved_tokens or []] dups = _find_duplicates(reserved_tokens) if dups: raise ValueError("Duplicates found in tokens: %s" % dups) reserved_tokens_re = _make_reserved_tokens_re(reserved_tokens) return reserved_tokens, reserved_tokens_re
[ "Prepare", "reserved", "tokens", "and", "a", "regex", "for", "splitting", "them", "out", "of", "strings", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/text_encoder.py#L447-L454
[ "def", "_prepare_reserved_tokens", "(", "reserved_tokens", ")", ":", "reserved_tokens", "=", "[", "tf", ".", "compat", ".", "as_text", "(", "tok", ")", "for", "tok", "in", "reserved_tokens", "or", "[", "]", "]", "dups", "=", "_find_duplicates", "(", "reserve...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_make_reserved_tokens_re
Constructs compiled regex to parse out reserved tokens.
tensorflow_datasets/core/features/text/text_encoder.py
def _make_reserved_tokens_re(reserved_tokens): """Constructs compiled regex to parse out reserved tokens.""" if not reserved_tokens: return None escaped_tokens = [_re_escape(rt) for rt in reserved_tokens] pattern = "(%s)" % "|".join(escaped_tokens) reserved_tokens_re = _re_compile(pattern) return reserved_tokens_re
def _make_reserved_tokens_re(reserved_tokens): """Constructs compiled regex to parse out reserved tokens.""" if not reserved_tokens: return None escaped_tokens = [_re_escape(rt) for rt in reserved_tokens] pattern = "(%s)" % "|".join(escaped_tokens) reserved_tokens_re = _re_compile(pattern) return reserved_tokens_re
[ "Constructs", "compiled", "regex", "to", "parse", "out", "reserved", "tokens", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/text_encoder.py#L463-L470
[ "def", "_make_reserved_tokens_re", "(", "reserved_tokens", ")", ":", "if", "not", "reserved_tokens", ":", "return", "None", "escaped_tokens", "=", "[", "_re_escape", "(", "rt", ")", "for", "rt", "in", "reserved_tokens", "]", "pattern", "=", "\"(%s)\"", "%", "\...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
write_lines_to_file
Writes lines to file prepended by header and metadata.
tensorflow_datasets/core/features/text/text_encoder.py
def write_lines_to_file(cls_name, filename, lines, metadata_dict): """Writes lines to file prepended by header and metadata.""" metadata_dict = metadata_dict or {} header_line = "%s%s" % (_HEADER_PREFIX, cls_name) metadata_line = "%s%s" % (_METADATA_PREFIX, json.dumps(metadata_dict, sort_keys=True)) with tf.io.gfile.GFile(filename, "wb") as f: for line in [header_line, metadata_line]: f.write(tf.compat.as_bytes(line)) f.write(tf.compat.as_bytes("\n")) if lines: f.write(tf.compat.as_bytes("\n".join(lines))) f.write(tf.compat.as_bytes("\n"))
def write_lines_to_file(cls_name, filename, lines, metadata_dict): """Writes lines to file prepended by header and metadata.""" metadata_dict = metadata_dict or {} header_line = "%s%s" % (_HEADER_PREFIX, cls_name) metadata_line = "%s%s" % (_METADATA_PREFIX, json.dumps(metadata_dict, sort_keys=True)) with tf.io.gfile.GFile(filename, "wb") as f: for line in [header_line, metadata_line]: f.write(tf.compat.as_bytes(line)) f.write(tf.compat.as_bytes("\n")) if lines: f.write(tf.compat.as_bytes("\n".join(lines))) f.write(tf.compat.as_bytes("\n"))
[ "Writes", "lines", "to", "file", "prepended", "by", "header", "and", "metadata", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/text_encoder.py#L492-L504
[ "def", "write_lines_to_file", "(", "cls_name", ",", "filename", ",", "lines", ",", "metadata_dict", ")", ":", "metadata_dict", "=", "metadata_dict", "or", "{", "}", "header_line", "=", "\"%s%s\"", "%", "(", "_HEADER_PREFIX", ",", "cls_name", ")", "metadata_line"...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
read_lines_from_file
Read lines from file, parsing out header and metadata.
tensorflow_datasets/core/features/text/text_encoder.py
def read_lines_from_file(cls_name, filename): """Read lines from file, parsing out header and metadata.""" with tf.io.gfile.GFile(filename, "rb") as f: lines = [tf.compat.as_text(line)[:-1] for line in f] header_line = "%s%s" % (_HEADER_PREFIX, cls_name) if lines[0] != header_line: raise ValueError("File {fname} does not seem to have been created from " "{name}.save_to_file.".format( fname=filename, name=cls_name)) metadata_dict = json.loads(lines[1][len(_METADATA_PREFIX):]) return lines[2:], metadata_dict
def read_lines_from_file(cls_name, filename): """Read lines from file, parsing out header and metadata.""" with tf.io.gfile.GFile(filename, "rb") as f: lines = [tf.compat.as_text(line)[:-1] for line in f] header_line = "%s%s" % (_HEADER_PREFIX, cls_name) if lines[0] != header_line: raise ValueError("File {fname} does not seem to have been created from " "{name}.save_to_file.".format( fname=filename, name=cls_name)) metadata_dict = json.loads(lines[1][len(_METADATA_PREFIX):]) return lines[2:], metadata_dict
[ "Read", "lines", "from", "file", "parsing", "out", "header", "and", "metadata", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/text_encoder.py#L507-L517
[ "def", "read_lines_from_file", "(", "cls_name", ",", "filename", ")", ":", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "filename", ",", "\"rb\"", ")", "as", "f", ":", "lines", "=", "[", "tf", ".", "compat", ".", "as_text", "(", "line", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Tokenizer.tokenize
Splits a string into tokens.
tensorflow_datasets/core/features/text/text_encoder.py
def tokenize(self, s): """Splits a string into tokens.""" s = tf.compat.as_text(s) if self.reserved_tokens: # First split out the reserved tokens substrs = self._reserved_tokens_re.split(s) else: substrs = [s] toks = [] for substr in substrs: if substr in self.reserved_tokens: toks.append(substr) else: toks.extend(self._alphanum_re.split(substr)) # Filter out empty strings toks = [t for t in toks if t] return toks
def tokenize(self, s): """Splits a string into tokens.""" s = tf.compat.as_text(s) if self.reserved_tokens: # First split out the reserved tokens substrs = self._reserved_tokens_re.split(s) else: substrs = [s] toks = [] for substr in substrs: if substr in self.reserved_tokens: toks.append(substr) else: toks.extend(self._alphanum_re.split(substr)) # Filter out empty strings toks = [t for t in toks if t] return toks
[ "Splits", "a", "string", "into", "tokens", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/text_encoder.py#L378-L397
[ "def", "tokenize", "(", "self", ",", "s", ")", ":", "s", "=", "tf", ".", "compat", ".", "as_text", "(", "s", ")", "if", "self", ".", "reserved_tokens", ":", "# First split out the reserved tokens", "substrs", "=", "self", ".", "_reserved_tokens_re", ".", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
slice_to_percent_mask
Convert a python slice [15:50] into a list[bool] mask of 100 elements.
tensorflow_datasets/core/splits.py
def slice_to_percent_mask(slice_value): """Convert a python slice [15:50] into a list[bool] mask of 100 elements.""" if slice_value is None: slice_value = slice(None) # Select only the elements of the slice selected = set(list(range(100))[slice_value]) # Create the binary mask return [i in selected for i in range(100)]
def slice_to_percent_mask(slice_value): """Convert a python slice [15:50] into a list[bool] mask of 100 elements.""" if slice_value is None: slice_value = slice(None) # Select only the elements of the slice selected = set(list(range(100))[slice_value]) # Create the binary mask return [i in selected for i in range(100)]
[ "Convert", "a", "python", "slice", "[", "15", ":", "50", "]", "into", "a", "list", "[", "bool", "]", "mask", "of", "100", "elements", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/splits.py#L479-L486
[ "def", "slice_to_percent_mask", "(", "slice_value", ")", ":", "if", "slice_value", "is", "None", ":", "slice_value", "=", "slice", "(", "None", ")", "# Select only the elements of the slice", "selected", "=", "set", "(", "list", "(", "range", "(", "100", ")", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
get_shard_id2num_examples
Return the mapping shard_id=>num_examples, assuming round-robin.
tensorflow_datasets/core/splits.py
def get_shard_id2num_examples(num_shards, total_num_examples): """Return the mapping shard_id=>num_examples, assuming round-robin.""" # TODO(b/130353071): This has the strong assumption that the shards have # been written in a round-robin fashion. This assumption does not hold, for # instance, with Beam generation. The mapping shard_id=>num_examples # should be computed during generation. # Minimum number of example per shards num_example_in_shard = total_num_examples // num_shards shard_id2num_examples = [num_example_in_shard for _ in range(num_shards)] # If there are remaining examples, we add them to the first shards for shard_id in range(total_num_examples % num_shards): shard_id2num_examples[shard_id] += 1 return shard_id2num_examples
def get_shard_id2num_examples(num_shards, total_num_examples): """Return the mapping shard_id=>num_examples, assuming round-robin.""" # TODO(b/130353071): This has the strong assumption that the shards have # been written in a round-robin fashion. This assumption does not hold, for # instance, with Beam generation. The mapping shard_id=>num_examples # should be computed during generation. # Minimum number of example per shards num_example_in_shard = total_num_examples // num_shards shard_id2num_examples = [num_example_in_shard for _ in range(num_shards)] # If there are remaining examples, we add them to the first shards for shard_id in range(total_num_examples % num_shards): shard_id2num_examples[shard_id] += 1 return shard_id2num_examples
[ "Return", "the", "mapping", "shard_id", "=", ">", "num_examples", "assuming", "round", "-", "robin", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/splits.py#L489-L502
[ "def", "get_shard_id2num_examples", "(", "num_shards", ",", "total_num_examples", ")", ":", "# TODO(b/130353071): This has the strong assumption that the shards have", "# been written in a round-robin fashion. This assumption does not hold, for", "# instance, with Beam generation. The mapping sh...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
compute_mask_offsets
Return the list of offsets associated with each shards. Args: shard_id2num_examples: `list[int]`, mapping shard_id=>num_examples Returns: mask_offsets: `list[int]`, offset to skip for each of the shard
tensorflow_datasets/core/splits.py
def compute_mask_offsets(shard_id2num_examples): """Return the list of offsets associated with each shards. Args: shard_id2num_examples: `list[int]`, mapping shard_id=>num_examples Returns: mask_offsets: `list[int]`, offset to skip for each of the shard """ total_num_examples = sum(shard_id2num_examples) mask_offsets = [] total_num_examples = 0 for num_examples_in_shard in shard_id2num_examples: # The offset (nb of examples to skip in the next shard) correspond to the # number of examples remaining in the current shard mask_offsets.append(total_num_examples % 100) total_num_examples += num_examples_in_shard return mask_offsets
def compute_mask_offsets(shard_id2num_examples): """Return the list of offsets associated with each shards. Args: shard_id2num_examples: `list[int]`, mapping shard_id=>num_examples Returns: mask_offsets: `list[int]`, offset to skip for each of the shard """ total_num_examples = sum(shard_id2num_examples) mask_offsets = [] total_num_examples = 0 for num_examples_in_shard in shard_id2num_examples: # The offset (nb of examples to skip in the next shard) correspond to the # number of examples remaining in the current shard mask_offsets.append(total_num_examples % 100) total_num_examples += num_examples_in_shard return mask_offsets
[ "Return", "the", "list", "of", "offsets", "associated", "with", "each", "shards", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/splits.py#L505-L524
[ "def", "compute_mask_offsets", "(", "shard_id2num_examples", ")", ":", "total_num_examples", "=", "sum", "(", "shard_id2num_examples", ")", "mask_offsets", "=", "[", "]", "total_num_examples", "=", "0", "for", "num_examples_in_shard", "in", "shard_id2num_examples", ":",...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
check_splits_equals
Check that the two split dicts have the same names and num_shards.
tensorflow_datasets/core/splits.py
def check_splits_equals(splits1, splits2): """Check that the two split dicts have the same names and num_shards.""" if set(splits1) ^ set(splits2): # Name intersection should be null return False for _, (split1, split2) in utils.zip_dict(splits1, splits2): if split1.num_shards != split2.num_shards: return False return True
def check_splits_equals(splits1, splits2): """Check that the two split dicts have the same names and num_shards.""" if set(splits1) ^ set(splits2): # Name intersection should be null return False for _, (split1, split2) in utils.zip_dict(splits1, splits2): if split1.num_shards != split2.num_shards: return False return True
[ "Check", "that", "the", "two", "split", "dicts", "have", "the", "same", "names", "and", "num_shards", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/splits.py#L573-L580
[ "def", "check_splits_equals", "(", "splits1", ",", "splits2", ")", ":", "if", "set", "(", "splits1", ")", "^", "set", "(", "splits2", ")", ":", "# Name intersection should be null", "return", "False", "for", "_", ",", "(", "split1", ",", "split2", ")", "in...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SplitDict.add
Add the split info.
tensorflow_datasets/core/splits.py
def add(self, split_info): """Add the split info.""" if split_info.name in self: raise ValueError("Split {} already present".format(split_info.name)) # TODO(epot): Make sure this works with Named splits correctly. super(SplitDict, self).__setitem__(split_info.name, split_info)
def add(self, split_info): """Add the split info.""" if split_info.name in self: raise ValueError("Split {} already present".format(split_info.name)) # TODO(epot): Make sure this works with Named splits correctly. super(SplitDict, self).__setitem__(split_info.name, split_info)
[ "Add", "the", "split", "info", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/splits.py#L542-L547
[ "def", "add", "(", "self", ",", "split_info", ")", ":", "if", "split_info", ".", "name", "in", "self", ":", "raise", "ValueError", "(", "\"Split {} already present\"", ".", "format", "(", "split_info", ".", "name", ")", ")", "# TODO(epot): Make sure this works w...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SplitDict.from_proto
Returns a new SplitDict initialized from the `repeated_split_infos`.
tensorflow_datasets/core/splits.py
def from_proto(cls, repeated_split_infos): """Returns a new SplitDict initialized from the `repeated_split_infos`.""" split_dict = cls() for split_info_proto in repeated_split_infos: split_info = SplitInfo() split_info.CopyFrom(split_info_proto) split_dict.add(split_info) return split_dict
def from_proto(cls, repeated_split_infos): """Returns a new SplitDict initialized from the `repeated_split_infos`.""" split_dict = cls() for split_info_proto in repeated_split_infos: split_info = SplitInfo() split_info.CopyFrom(split_info_proto) split_dict.add(split_info) return split_dict
[ "Returns", "a", "new", "SplitDict", "initialized", "from", "the", "repeated_split_infos", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/splits.py#L550-L557
[ "def", "from_proto", "(", "cls", ",", "repeated_split_infos", ")", ":", "split_dict", "=", "cls", "(", ")", "for", "split_info_proto", "in", "repeated_split_infos", ":", "split_info", "=", "SplitInfo", "(", ")", "split_info", ".", "CopyFrom", "(", "split_info_pr...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SplitDict.to_proto
Returns a list of SplitInfo protos that we have.
tensorflow_datasets/core/splits.py
def to_proto(self): """Returns a list of SplitInfo protos that we have.""" # Return the proto.SplitInfo, sorted by name return sorted((s.get_proto() for s in self.values()), key=lambda s: s.name)
def to_proto(self): """Returns a list of SplitInfo protos that we have.""" # Return the proto.SplitInfo, sorted by name return sorted((s.get_proto() for s in self.values()), key=lambda s: s.name)
[ "Returns", "a", "list", "of", "SplitInfo", "protos", "that", "we", "have", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/splits.py#L559-L562
[ "def", "to_proto", "(", "self", ")", ":", "# Return the proto.SplitInfo, sorted by name", "return", "sorted", "(", "(", "s", ".", "get_proto", "(", ")", "for", "s", "in", "self", ".", "values", "(", ")", ")", ",", "key", "=", "lambda", "s", ":", "s", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Squad._generate_examples
This function returns the examples in the raw (text) form.
tensorflow_datasets/text/squad.py
def _generate_examples(self, filepath): """This function returns the examples in the raw (text) form.""" logging.info("generating examples from = %s", filepath) with tf.io.gfile.GFile(filepath) as f: squad = json.load(f) for article in squad["data"]: if "title" in article: title = article["title"].strip() else: title = "" for paragraph in article["paragraphs"]: context = paragraph["context"].strip() for qa in paragraph["qas"]: question = qa["question"].strip() id_ = qa["id"] answer_starts = [answer["answer_start"] for answer in qa["answers"]] answers = [answer["text"].strip() for answer in qa["answers"]] # Features currently used are "context", "question", and "answers". # Others are extracted here for the ease of future expansions. example = { "title": title, "context": context, "question": question, "id": id_, "answer_starts": answer_starts, "answers": answers, } yield { "question": example["question"], # TODO(b/121176753): return all the answers. "first_answer": example["answers"][0], "context": example["context"] }
def _generate_examples(self, filepath): """This function returns the examples in the raw (text) form.""" logging.info("generating examples from = %s", filepath) with tf.io.gfile.GFile(filepath) as f: squad = json.load(f) for article in squad["data"]: if "title" in article: title = article["title"].strip() else: title = "" for paragraph in article["paragraphs"]: context = paragraph["context"].strip() for qa in paragraph["qas"]: question = qa["question"].strip() id_ = qa["id"] answer_starts = [answer["answer_start"] for answer in qa["answers"]] answers = [answer["text"].strip() for answer in qa["answers"]] # Features currently used are "context", "question", and "answers". # Others are extracted here for the ease of future expansions. example = { "title": title, "context": context, "question": question, "id": id_, "answer_starts": answer_starts, "answers": answers, } yield { "question": example["question"], # TODO(b/121176753): return all the answers. "first_answer": example["answers"][0], "context": example["context"] }
[ "This", "function", "returns", "the", "examples", "in", "the", "raw", "(", "text", ")", "form", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/text/squad.py#L164-L198
[ "def", "_generate_examples", "(", "self", ",", "filepath", ")", ":", "logging", ".", "info", "(", "\"generating examples from = %s\"", ",", "filepath", ")", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "filepath", ")", "as", "f", ":", "squad",...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
ParaCrawl._generate_examples
This function returns the examples in the raw (text) form.
tensorflow_datasets/translate/para_crawl.py
def _generate_examples(self, data_file): """This function returns the examples in the raw (text) form.""" target_language = self.builder_config.target_language with tf.io.gfile.GFile(data_file) as f: for i, line in enumerate(f): line_parts = line.strip().split("\t") if len(line_parts) != 2: raise ValueError(("Wrong data format in line {}. The line '{}' does " "not have exactly one delimiter.").format(i, line)) source, target = line_parts[0].strip(), line_parts[1].strip() yield {"en": source, target_language: target}
def _generate_examples(self, data_file): """This function returns the examples in the raw (text) form.""" target_language = self.builder_config.target_language with tf.io.gfile.GFile(data_file) as f: for i, line in enumerate(f): line_parts = line.strip().split("\t") if len(line_parts) != 2: raise ValueError(("Wrong data format in line {}. The line '{}' does " "not have exactly one delimiter.").format(i, line)) source, target = line_parts[0].strip(), line_parts[1].strip() yield {"en": source, target_language: target}
[ "This", "function", "returns", "the", "examples", "in", "the", "raw", "(", "text", ")", "form", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/para_crawl.py#L148-L160
[ "def", "_generate_examples", "(", "self", ",", "data_file", ")", ":", "target_language", "=", "self", ".", "builder_config", ".", "target_language", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "data_file", ")", "as", "f", ":", "for", "i", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
build_synchronize_decorator
Returns a decorator which prevents concurrent calls to functions. Usage: synchronized = build_synchronize_decorator() @synchronized def read_value(): ... @synchronized def write_value(x): ... Returns: make_threadsafe (fct): The decorator which lock all functions to which it is applied under a same lock
tensorflow_datasets/core/download/util.py
def build_synchronize_decorator(): """Returns a decorator which prevents concurrent calls to functions. Usage: synchronized = build_synchronize_decorator() @synchronized def read_value(): ... @synchronized def write_value(x): ... Returns: make_threadsafe (fct): The decorator which lock all functions to which it is applied under a same lock """ lock = threading.Lock() def lock_decorator(fn): @functools.wraps(fn) def lock_decorated(*args, **kwargs): with lock: return fn(*args, **kwargs) return lock_decorated return lock_decorator
def build_synchronize_decorator(): """Returns a decorator which prevents concurrent calls to functions. Usage: synchronized = build_synchronize_decorator() @synchronized def read_value(): ... @synchronized def write_value(x): ... Returns: make_threadsafe (fct): The decorator which lock all functions to which it is applied under a same lock """ lock = threading.Lock() def lock_decorator(fn): @functools.wraps(fn) def lock_decorated(*args, **kwargs): with lock: return fn(*args, **kwargs) return lock_decorated return lock_decorator
[ "Returns", "a", "decorator", "which", "prevents", "concurrent", "calls", "to", "functions", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/download/util.py#L72-L101
[ "def", "build_synchronize_decorator", "(", ")", ":", "lock", "=", "threading", ".", "Lock", "(", ")", "def", "lock_decorator", "(", "fn", ")", ":", "@", "functools", ".", "wraps", "(", "fn", ")", "def", "lock_decorated", "(", "*", "args", ",", "*", "*"...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
get_file_name
Returns file name of file at given url.
tensorflow_datasets/core/download/util.py
def get_file_name(url): """Returns file name of file at given url.""" return os.path.basename(urllib.parse.urlparse(url).path) or 'unknown_name'
def get_file_name(url): """Returns file name of file at given url.""" return os.path.basename(urllib.parse.urlparse(url).path) or 'unknown_name'
[ "Returns", "file", "name", "of", "file", "at", "given", "url", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/download/util.py#L104-L106
[ "def", "get_file_name", "(", "url", ")", ":", "return", "os", ".", "path", ".", "basename", "(", "urllib", ".", "parse", ".", "urlparse", "(", "url", ")", ".", "path", ")", "or", "'unknown_name'" ]
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_make_builder_configs
Make built-in Librispeech BuilderConfigs. Uses 4 text encodings (plain text, bytes, subwords with 8k vocab, subwords with 32k vocab) crossed with the data subsets (clean100, clean360, all). Returns: `list<tfds.audio.LibrispeechConfig>`
tensorflow_datasets/audio/librispeech.py
def _make_builder_configs(): """Make built-in Librispeech BuilderConfigs. Uses 4 text encodings (plain text, bytes, subwords with 8k vocab, subwords with 32k vocab) crossed with the data subsets (clean100, clean360, all). Returns: `list<tfds.audio.LibrispeechConfig>` """ text_encoder_configs = [ None, tfds.features.text.TextEncoderConfig( name="bytes", encoder=tfds.features.text.ByteTextEncoder()), tfds.features.text.TextEncoderConfig( name="subwords8k", encoder_cls=tfds.features.text.SubwordTextEncoder, vocab_size=2**13), tfds.features.text.TextEncoderConfig( name="subwords32k", encoder_cls=tfds.features.text.SubwordTextEncoder, vocab_size=2**15), ] version = "0.1.0" configs = [] for text_encoder_config in text_encoder_configs: for data in _DATA_OPTIONS: config = LibrispeechConfig( version=version, text_encoder_config=text_encoder_config, data=data) configs.append(config) return configs
def _make_builder_configs(): """Make built-in Librispeech BuilderConfigs. Uses 4 text encodings (plain text, bytes, subwords with 8k vocab, subwords with 32k vocab) crossed with the data subsets (clean100, clean360, all). Returns: `list<tfds.audio.LibrispeechConfig>` """ text_encoder_configs = [ None, tfds.features.text.TextEncoderConfig( name="bytes", encoder=tfds.features.text.ByteTextEncoder()), tfds.features.text.TextEncoderConfig( name="subwords8k", encoder_cls=tfds.features.text.SubwordTextEncoder, vocab_size=2**13), tfds.features.text.TextEncoderConfig( name="subwords32k", encoder_cls=tfds.features.text.SubwordTextEncoder, vocab_size=2**15), ] version = "0.1.0" configs = [] for text_encoder_config in text_encoder_configs: for data in _DATA_OPTIONS: config = LibrispeechConfig( version=version, text_encoder_config=text_encoder_config, data=data) configs.append(config) return configs
[ "Make", "built", "-", "in", "Librispeech", "BuilderConfigs", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/audio/librispeech.py#L130-L159
[ "def", "_make_builder_configs", "(", ")", ":", "text_encoder_configs", "=", "[", "None", ",", "tfds", ".", "features", ".", "text", ".", "TextEncoderConfig", "(", "name", "=", "\"bytes\"", ",", "encoder", "=", "tfds", ".", "features", ".", "text", ".", "By...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_walk_librispeech_dir
Walk a Librispeech directory and yield examples.
tensorflow_datasets/audio/librispeech.py
def _walk_librispeech_dir(directory): """Walk a Librispeech directory and yield examples.""" directory = os.path.join(directory, "LibriSpeech") for path, _, files in tf.io.gfile.walk(directory): if not files: continue transcript_file = [f for f in files if f.endswith(".txt")] if not transcript_file: continue assert len(transcript_file) == 1 transcript_file, = transcript_file transcripts = {} with tf.io.gfile.GFile(os.path.join(path, transcript_file)) as f: for line in f: line = line.strip() key, transcript = line.split(" ", 1) transcripts[key] = transcript audio_files = [f for f in files if not f.endswith(".txt")] for audio_file in audio_files: assert audio_file.endswith(".flac") key = audio_file[:-len(".flac")] transcript = transcripts[key] speaker_id, chapter_id = [int(el) for el in key.split("-")[:2]] yield LibrispeechExample( speaker_id=speaker_id, chapter_id=chapter_id, audio_file=os.path.join(path, audio_file), transcript=transcript)
def _walk_librispeech_dir(directory): """Walk a Librispeech directory and yield examples.""" directory = os.path.join(directory, "LibriSpeech") for path, _, files in tf.io.gfile.walk(directory): if not files: continue transcript_file = [f for f in files if f.endswith(".txt")] if not transcript_file: continue assert len(transcript_file) == 1 transcript_file, = transcript_file transcripts = {} with tf.io.gfile.GFile(os.path.join(path, transcript_file)) as f: for line in f: line = line.strip() key, transcript = line.split(" ", 1) transcripts[key] = transcript audio_files = [f for f in files if not f.endswith(".txt")] for audio_file in audio_files: assert audio_file.endswith(".flac") key = audio_file[:-len(".flac")] transcript = transcripts[key] speaker_id, chapter_id = [int(el) for el in key.split("-")[:2]] yield LibrispeechExample( speaker_id=speaker_id, chapter_id=chapter_id, audio_file=os.path.join(path, audio_file), transcript=transcript)
[ "Walk", "a", "Librispeech", "directory", "and", "yield", "examples", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/audio/librispeech.py#L237-L265
[ "def", "_walk_librispeech_dir", "(", "directory", ")", ":", "directory", "=", "os", ".", "path", ".", "join", "(", "directory", ",", "\"LibriSpeech\"", ")", "for", "path", ",", "_", ",", "files", "in", "tf", ".", "io", ".", "gfile", ".", "walk", "(", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
LibrispeechConfig.download_urls
Returns download urls for this config.
tensorflow_datasets/audio/librispeech.py
def download_urls(self): """Returns download urls for this config.""" urls = { tfds.Split.TRAIN: ["train_clean100"], tfds.Split.VALIDATION: ["dev_clean"], tfds.Split.TEST: ["test_clean"], } if self.data in ["all", "clean360"]: urls[tfds.Split.TRAIN].append("train_clean360") if self.data == "all": urls[tfds.Split.TRAIN].extend(["train_clean360", "train_other500"]) urls[tfds.Split.VALIDATION].append("dev_other") urls[tfds.Split.TEST].append("test_other") urls = { split: [_DL_URLS[name] for name in names ] for split, names in urls.items() } return urls
def download_urls(self): """Returns download urls for this config.""" urls = { tfds.Split.TRAIN: ["train_clean100"], tfds.Split.VALIDATION: ["dev_clean"], tfds.Split.TEST: ["test_clean"], } if self.data in ["all", "clean360"]: urls[tfds.Split.TRAIN].append("train_clean360") if self.data == "all": urls[tfds.Split.TRAIN].extend(["train_clean360", "train_other500"]) urls[tfds.Split.VALIDATION].append("dev_other") urls[tfds.Split.TEST].append("test_other") urls = { split: [_DL_URLS[name] for name in names ] for split, names in urls.items() } return urls
[ "Returns", "download", "urls", "for", "this", "config", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/audio/librispeech.py#L109-L127
[ "def", "download_urls", "(", "self", ")", ":", "urls", "=", "{", "tfds", ".", "Split", ".", "TRAIN", ":", "[", "\"train_clean100\"", "]", ",", "tfds", ".", "Split", ".", "VALIDATION", ":", "[", "\"dev_clean\"", "]", ",", "tfds", ".", "Split", ".", "T...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
ClassLabel.str2int
Conversion class name string => integer.
tensorflow_datasets/core/features/class_label_feature.py
def str2int(self, str_value): """Conversion class name string => integer.""" str_value = tf.compat.as_text(str_value) if self._str2int: return self._str2int[str_value] # No names provided, try to integerize failed_parse = False try: int_value = int(str_value) except ValueError: failed_parse = True if failed_parse or not 0 <= int_value < self._num_classes: raise ValueError("Invalid string class label %s" % str_value) return int_value
def str2int(self, str_value): """Conversion class name string => integer.""" str_value = tf.compat.as_text(str_value) if self._str2int: return self._str2int[str_value] # No names provided, try to integerize failed_parse = False try: int_value = int(str_value) except ValueError: failed_parse = True if failed_parse or not 0 <= int_value < self._num_classes: raise ValueError("Invalid string class label %s" % str_value) return int_value
[ "Conversion", "class", "name", "string", "=", ">", "integer", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/class_label_feature.py#L99-L113
[ "def", "str2int", "(", "self", ",", "str_value", ")", ":", "str_value", "=", "tf", ".", "compat", ".", "as_text", "(", "str_value", ")", "if", "self", ".", "_str2int", ":", "return", "self", ".", "_str2int", "[", "str_value", "]", "# No names provided, try...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
ClassLabel.int2str
Conversion integer => class name string.
tensorflow_datasets/core/features/class_label_feature.py
def int2str(self, int_value): """Conversion integer => class name string.""" if self._int2str: # Maybe should support batched np array/eager tensors, to allow things # like # out_ids = model(inputs) # labels = cifar10.info.features['label'].int2str(out_ids) return self._int2str[int_value] # No names provided, return str(int) if not 0 <= int_value < self._num_classes: raise ValueError("Invalid integer class label %d" % int_value) return tf.compat.as_text(str(int_value))
def int2str(self, int_value): """Conversion integer => class name string.""" if self._int2str: # Maybe should support batched np array/eager tensors, to allow things # like # out_ids = model(inputs) # labels = cifar10.info.features['label'].int2str(out_ids) return self._int2str[int_value] # No names provided, return str(int) if not 0 <= int_value < self._num_classes: raise ValueError("Invalid integer class label %d" % int_value) return tf.compat.as_text(str(int_value))
[ "Conversion", "integer", "=", ">", "class", "name", "string", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/class_label_feature.py#L115-L127
[ "def", "int2str", "(", "self", ",", "int_value", ")", ":", "if", "self", ".", "_int2str", ":", "# Maybe should support batched np array/eager tensors, to allow things", "# like", "# out_ids = model(inputs)", "# labels = cifar10.info.features['label'].int2str(out_ids)", "return", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
ClassLabel.save_metadata
See base class for details.
tensorflow_datasets/core/features/class_label_feature.py
def save_metadata(self, data_dir, feature_name=None): """See base class for details.""" # Save names if defined if self._str2int is not None: names_filepath = _get_names_filepath(data_dir, feature_name) _write_names_to_file(names_filepath, self.names)
def save_metadata(self, data_dir, feature_name=None): """See base class for details.""" # Save names if defined if self._str2int is not None: names_filepath = _get_names_filepath(data_dir, feature_name) _write_names_to_file(names_filepath, self.names)
[ "See", "base", "class", "for", "details", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/class_label_feature.py#L152-L157
[ "def", "save_metadata", "(", "self", ",", "data_dir", ",", "feature_name", "=", "None", ")", ":", "# Save names if defined", "if", "self", ".", "_str2int", "is", "not", "None", ":", "names_filepath", "=", "_get_names_filepath", "(", "data_dir", ",", "feature_nam...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
ClassLabel.load_metadata
See base class for details.
tensorflow_datasets/core/features/class_label_feature.py
def load_metadata(self, data_dir, feature_name=None): """See base class for details.""" # Restore names if defined names_filepath = _get_names_filepath(data_dir, feature_name) if tf.io.gfile.exists(names_filepath): self.names = _load_names_from_file(names_filepath)
def load_metadata(self, data_dir, feature_name=None): """See base class for details.""" # Restore names if defined names_filepath = _get_names_filepath(data_dir, feature_name) if tf.io.gfile.exists(names_filepath): self.names = _load_names_from_file(names_filepath)
[ "See", "base", "class", "for", "details", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/class_label_feature.py#L159-L164
[ "def", "load_metadata", "(", "self", ",", "data_dir", ",", "feature_name", "=", "None", ")", ":", "# Restore names if defined", "names_filepath", "=", "_get_names_filepath", "(", "data_dir", ",", "feature_name", ")", "if", "tf", ".", "io", ".", "gfile", ".", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_token_counts_from_generator
Builds token counts from generator.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def _token_counts_from_generator(generator, max_chars, reserved_tokens): """Builds token counts from generator.""" reserved_tokens = list(reserved_tokens) + [_UNDERSCORE_REPLACEMENT] tokenizer = text_encoder.Tokenizer( alphanum_only=False, reserved_tokens=reserved_tokens) num_chars = 0 token_counts = collections.defaultdict(int) for s in generator: s = tf.compat.as_text(s) if max_chars and (num_chars + len(s)) >= max_chars: s = s[:(max_chars - num_chars)] tokens = tokenizer.tokenize(s) tokens = _prepare_tokens_for_encode(tokens) for t in tokens: token_counts[t] += 1 if max_chars: num_chars += len(s) if num_chars > max_chars: break return token_counts
def _token_counts_from_generator(generator, max_chars, reserved_tokens): """Builds token counts from generator.""" reserved_tokens = list(reserved_tokens) + [_UNDERSCORE_REPLACEMENT] tokenizer = text_encoder.Tokenizer( alphanum_only=False, reserved_tokens=reserved_tokens) num_chars = 0 token_counts = collections.defaultdict(int) for s in generator: s = tf.compat.as_text(s) if max_chars and (num_chars + len(s)) >= max_chars: s = s[:(max_chars - num_chars)] tokens = tokenizer.tokenize(s) tokens = _prepare_tokens_for_encode(tokens) for t in tokens: token_counts[t] += 1 if max_chars: num_chars += len(s) if num_chars > max_chars: break return token_counts
[ "Builds", "token", "counts", "from", "generator", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L388-L407
[ "def", "_token_counts_from_generator", "(", "generator", ",", "max_chars", ",", "reserved_tokens", ")", ":", "reserved_tokens", "=", "list", "(", "reserved_tokens", ")", "+", "[", "_UNDERSCORE_REPLACEMENT", "]", "tokenizer", "=", "text_encoder", ".", "Tokenizer", "(...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_validate_build_arguments
Validate arguments for SubwordTextEncoder.build_from_corpus.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def _validate_build_arguments(max_subword_length, reserved_tokens, target_vocab_size): """Validate arguments for SubwordTextEncoder.build_from_corpus.""" if max_subword_length <= 0: raise ValueError( "max_subword_length must be > 0. Note that memory and compute for " "building the vocabulary scale quadratically in the length of the " "longest token.") for t in reserved_tokens: if t.endswith("_") or not text_encoder.is_mixed_alphanum(t): raise ValueError( "Reserved tokens must not end with _ and they must contain a mix " "of alphanumeric and non-alphanumeric characters. For example, " "'<EOS>'.") # Minimum vocab size = bytes + pad + 1 minimum_vocab_size = text_encoder.NUM_BYTES + 1 + 1 if target_vocab_size < minimum_vocab_size: raise ValueError("target_vocab_size must be >= %d. Got %d" % (minimum_vocab_size, target_vocab_size))
def _validate_build_arguments(max_subword_length, reserved_tokens, target_vocab_size): """Validate arguments for SubwordTextEncoder.build_from_corpus.""" if max_subword_length <= 0: raise ValueError( "max_subword_length must be > 0. Note that memory and compute for " "building the vocabulary scale quadratically in the length of the " "longest token.") for t in reserved_tokens: if t.endswith("_") or not text_encoder.is_mixed_alphanum(t): raise ValueError( "Reserved tokens must not end with _ and they must contain a mix " "of alphanumeric and non-alphanumeric characters. For example, " "'<EOS>'.") # Minimum vocab size = bytes + pad + 1 minimum_vocab_size = text_encoder.NUM_BYTES + 1 + 1 if target_vocab_size < minimum_vocab_size: raise ValueError("target_vocab_size must be >= %d. Got %d" % (minimum_vocab_size, target_vocab_size))
[ "Validate", "arguments", "for", "SubwordTextEncoder", ".", "build_from_corpus", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L410-L428
[ "def", "_validate_build_arguments", "(", "max_subword_length", ",", "reserved_tokens", ",", "target_vocab_size", ")", ":", "if", "max_subword_length", "<=", "0", ":", "raise", "ValueError", "(", "\"max_subword_length must be > 0. Note that memory and compute for \"", "\"buildin...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_prepare_tokens_for_encode
Prepare tokens for encoding. Tokens followed by a single space have "_" appended and the single space token is dropped. If a token is _UNDERSCORE_REPLACEMENT, it is broken up into 2 tokens. Args: tokens: `list<str>`, tokens to prepare. Returns: `list<str>` prepared tokens.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def _prepare_tokens_for_encode(tokens): """Prepare tokens for encoding. Tokens followed by a single space have "_" appended and the single space token is dropped. If a token is _UNDERSCORE_REPLACEMENT, it is broken up into 2 tokens. Args: tokens: `list<str>`, tokens to prepare. Returns: `list<str>` prepared tokens. """ prepared_tokens = [] def _prepare_token(t, next_t): skip_next = False t = _escape(t) # If next token is a single space, add _ suffix to token and skip the # empty space. if next_t == " ": t += "_" skip_next = True return t, skip_next next_tokens = tokens[1:] + [None] skip_single_token = False for token, next_token in zip(tokens, next_tokens): if skip_single_token: skip_single_token = False continue # If the user-supplied string contains the underscore replacement string, # break it into 2 tokens and encode those separately. if token == _UNDERSCORE_REPLACEMENT: t1, t2 = _UNDERSCORE_REPLACEMENT[:2], _UNDERSCORE_REPLACEMENT[2:] t1, _ = _prepare_token(t1, None) t2, _ = _prepare_token(t2, next_token) prepared_tokens.append(t1) prepared_tokens.append(t2) continue token, skip_single_token = _prepare_token(token, next_token) prepared_tokens.append(token) return prepared_tokens
def _prepare_tokens_for_encode(tokens): """Prepare tokens for encoding. Tokens followed by a single space have "_" appended and the single space token is dropped. If a token is _UNDERSCORE_REPLACEMENT, it is broken up into 2 tokens. Args: tokens: `list<str>`, tokens to prepare. Returns: `list<str>` prepared tokens. """ prepared_tokens = [] def _prepare_token(t, next_t): skip_next = False t = _escape(t) # If next token is a single space, add _ suffix to token and skip the # empty space. if next_t == " ": t += "_" skip_next = True return t, skip_next next_tokens = tokens[1:] + [None] skip_single_token = False for token, next_token in zip(tokens, next_tokens): if skip_single_token: skip_single_token = False continue # If the user-supplied string contains the underscore replacement string, # break it into 2 tokens and encode those separately. if token == _UNDERSCORE_REPLACEMENT: t1, t2 = _UNDERSCORE_REPLACEMENT[:2], _UNDERSCORE_REPLACEMENT[2:] t1, _ = _prepare_token(t1, None) t2, _ = _prepare_token(t2, next_token) prepared_tokens.append(t1) prepared_tokens.append(t2) continue token, skip_single_token = _prepare_token(token, next_token) prepared_tokens.append(token) return prepared_tokens
[ "Prepare", "tokens", "for", "encoding", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L451-L496
[ "def", "_prepare_tokens_for_encode", "(", "tokens", ")", ":", "prepared_tokens", "=", "[", "]", "def", "_prepare_token", "(", "t", ",", "next_t", ")", ":", "skip_next", "=", "False", "t", "=", "_escape", "(", "t", ")", "# If next token is a single space, add _ s...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder.encode
Encodes text into a list of integers.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def encode(self, s): """Encodes text into a list of integers.""" s = tf.compat.as_text(s) tokens = self._tokenizer.tokenize(s) tokens = _prepare_tokens_for_encode(tokens) ids = [] for token in tokens: ids.extend(self._token_to_ids(token)) return text_encoder.pad_incr(ids)
def encode(self, s): """Encodes text into a list of integers.""" s = tf.compat.as_text(s) tokens = self._tokenizer.tokenize(s) tokens = _prepare_tokens_for_encode(tokens) ids = [] for token in tokens: ids.extend(self._token_to_ids(token)) return text_encoder.pad_incr(ids)
[ "Encodes", "text", "into", "a", "list", "of", "integers", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L80-L88
[ "def", "encode", "(", "self", ",", "s", ")", ":", "s", "=", "tf", ".", "compat", ".", "as_text", "(", "s", ")", "tokens", "=", "self", ".", "_tokenizer", ".", "tokenize", "(", "s", ")", "tokens", "=", "_prepare_tokens_for_encode", "(", "tokens", ")",...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder.decode
Decodes a list of integers into text.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def decode(self, ids): """Decodes a list of integers into text.""" ids = text_encoder.pad_decr(ids) subword_ids = ids del ids subwords = [] # Some ids correspond to bytes. Because unicode characters are composed of # possibly multiple bytes, we attempt to decode contiguous lists of bytes # all together. Invalid byte sequences are replaced with the unicode # replacement (i.e. unknown) character U+FFFD. prev_bytes = [] def consume_prev_bytes(): if prev_bytes: bytestr = b"".join(prev_bytes) bytes_text = bytestr.decode("utf-8", "replace") subwords.append(bytes_text) return [] for subword_id in subword_ids: subword = self._id_to_subword(subword_id) if isinstance(subword, six.binary_type): # Byte-encoded prev_bytes.append(subword) else: # If there were bytes previously, convert to unicode. prev_bytes = consume_prev_bytes() trimmed, add_space = _trim_underscore_and_tell(subword) subwords.append(trimmed) if add_space: subwords.append(" ") # If there were trailing bytes, convert to unicode. prev_bytes = consume_prev_bytes() return tf.compat.as_text("".join(subwords))
def decode(self, ids): """Decodes a list of integers into text.""" ids = text_encoder.pad_decr(ids) subword_ids = ids del ids subwords = [] # Some ids correspond to bytes. Because unicode characters are composed of # possibly multiple bytes, we attempt to decode contiguous lists of bytes # all together. Invalid byte sequences are replaced with the unicode # replacement (i.e. unknown) character U+FFFD. prev_bytes = [] def consume_prev_bytes(): if prev_bytes: bytestr = b"".join(prev_bytes) bytes_text = bytestr.decode("utf-8", "replace") subwords.append(bytes_text) return [] for subword_id in subword_ids: subword = self._id_to_subword(subword_id) if isinstance(subword, six.binary_type): # Byte-encoded prev_bytes.append(subword) else: # If there were bytes previously, convert to unicode. prev_bytes = consume_prev_bytes() trimmed, add_space = _trim_underscore_and_tell(subword) subwords.append(trimmed) if add_space: subwords.append(" ") # If there were trailing bytes, convert to unicode. prev_bytes = consume_prev_bytes() return tf.compat.as_text("".join(subwords))
[ "Decodes", "a", "list", "of", "integers", "into", "text", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L90-L126
[ "def", "decode", "(", "self", ",", "ids", ")", ":", "ids", "=", "text_encoder", ".", "pad_decr", "(", "ids", ")", "subword_ids", "=", "ids", "del", "ids", "subwords", "=", "[", "]", "# Some ids correspond to bytes. Because unicode characters are composed of", "# p...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder._token_to_ids
Convert a single token to a list of integer ids.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def _token_to_ids(self, token): """Convert a single token to a list of integer ids.""" # Check cache cache_location = hash(token) % self._cache_size cache_key, cache_value = self._token_to_ids_cache[cache_location] if cache_key == token: return cache_value subwords = self._token_to_subwords(token) ids = [] for subword in subwords: if subword == _UNDERSCORE_REPLACEMENT: ids.append(len(self._subwords) + ord("_")) continue subword_id = self._subword_to_id.get(subword) if subword_id is None: # Byte-encode ids.extend(self._byte_encode(subword)) else: ids.append(subword_id) # Update cache self._token_to_ids_cache[cache_location] = (token, ids) return ids
def _token_to_ids(self, token): """Convert a single token to a list of integer ids.""" # Check cache cache_location = hash(token) % self._cache_size cache_key, cache_value = self._token_to_ids_cache[cache_location] if cache_key == token: return cache_value subwords = self._token_to_subwords(token) ids = [] for subword in subwords: if subword == _UNDERSCORE_REPLACEMENT: ids.append(len(self._subwords) + ord("_")) continue subword_id = self._subword_to_id.get(subword) if subword_id is None: # Byte-encode ids.extend(self._byte_encode(subword)) else: ids.append(subword_id) # Update cache self._token_to_ids_cache[cache_location] = (token, ids) return ids
[ "Convert", "a", "single", "token", "to", "a", "list", "of", "integer", "ids", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L140-L164
[ "def", "_token_to_ids", "(", "self", ",", "token", ")", ":", "# Check cache", "cache_location", "=", "hash", "(", "token", ")", "%", "self", ".", "_cache_size", "cache_key", ",", "cache_value", "=", "self", ".", "_token_to_ids_cache", "[", "cache_location", "]...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder._byte_encode
Encode a single token byte-wise into integer ids.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def _byte_encode(self, token): """Encode a single token byte-wise into integer ids.""" # Vocab ids for all bytes follow ids for the subwords offset = len(self._subwords) if token == "_": return [len(self._subwords) + ord(" ")] return [i + offset for i in list(bytearray(tf.compat.as_bytes(token)))]
def _byte_encode(self, token): """Encode a single token byte-wise into integer ids.""" # Vocab ids for all bytes follow ids for the subwords offset = len(self._subwords) if token == "_": return [len(self._subwords) + ord(" ")] return [i + offset for i in list(bytearray(tf.compat.as_bytes(token)))]
[ "Encode", "a", "single", "token", "byte", "-", "wise", "into", "integer", "ids", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L166-L172
[ "def", "_byte_encode", "(", "self", ",", "token", ")", ":", "# Vocab ids for all bytes follow ids for the subwords", "offset", "=", "len", "(", "self", ".", "_subwords", ")", "if", "token", "==", "\"_\"", ":", "return", "[", "len", "(", "self", ".", "_subwords...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder._id_to_subword
Converts a subword integer ID to a subword string.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def _id_to_subword(self, subword_id): """Converts a subword integer ID to a subword string.""" if subword_id < 0 or subword_id >= (self.vocab_size - 1): raise ValueError("Received id %d which is invalid. Ids must be within " "[0, %d)." % (subword_id + 1, self.vocab_size)) if 0 <= subword_id < len(self._subwords): # Subword return self._subwords[subword_id] else: # Byte offset = len(self._subwords) subword_id -= offset bytestr = bytes(bytearray([subword_id])) return bytestr
def _id_to_subword(self, subword_id): """Converts a subword integer ID to a subword string.""" if subword_id < 0 or subword_id >= (self.vocab_size - 1): raise ValueError("Received id %d which is invalid. Ids must be within " "[0, %d)." % (subword_id + 1, self.vocab_size)) if 0 <= subword_id < len(self._subwords): # Subword return self._subwords[subword_id] else: # Byte offset = len(self._subwords) subword_id -= offset bytestr = bytes(bytearray([subword_id])) return bytestr
[ "Converts", "a", "subword", "integer", "ID", "to", "a", "subword", "string", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L174-L188
[ "def", "_id_to_subword", "(", "self", ",", "subword_id", ")", ":", "if", "subword_id", "<", "0", "or", "subword_id", ">=", "(", "self", ".", "vocab_size", "-", "1", ")", ":", "raise", "ValueError", "(", "\"Received id %d which is invalid. Ids must be within \"", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder._token_to_subwords
Greedily split token into subwords.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def _token_to_subwords(self, token): """Greedily split token into subwords.""" subwords = [] start = 0 while start < len(token): subword = None for end in range( min(len(token), start + self._max_subword_len), start, -1): candidate = token[start:end] if (candidate in self._subword_to_id or candidate == _UNDERSCORE_REPLACEMENT): subword = candidate subwords.append(subword) start = end break # No subword match found. Consume a single (unicode) character. if subword is None: subwords.append(token[start]) start += 1 return subwords
def _token_to_subwords(self, token): """Greedily split token into subwords.""" subwords = [] start = 0 while start < len(token): subword = None for end in range( min(len(token), start + self._max_subword_len), start, -1): candidate = token[start:end] if (candidate in self._subword_to_id or candidate == _UNDERSCORE_REPLACEMENT): subword = candidate subwords.append(subword) start = end break # No subword match found. Consume a single (unicode) character. if subword is None: subwords.append(token[start]) start += 1 return subwords
[ "Greedily", "split", "token", "into", "subwords", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L190-L211
[ "def", "_token_to_subwords", "(", "self", ",", "token", ")", ":", "subwords", "=", "[", "]", "start", "=", "0", "while", "start", "<", "len", "(", "token", ")", ":", "subword", "=", "None", "for", "end", "in", "range", "(", "min", "(", "len", "(", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder._init_from_list
Initializes the encoder from a list of subwords.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def _init_from_list(self, subwords): """Initializes the encoder from a list of subwords.""" subwords = [tf.compat.as_text(s) for s in subwords if s] self._subwords = subwords # Note that internally everything is 0-indexed. Padding is dealt with at the # end of encode and the beginning of decode. self._subword_to_id = {s: i for i, s in enumerate(subwords)} # We remember the maximum length of any subword to avoid having to # check arbitrarily long strings. self._max_subword_len = max( len(_UNDERSCORE_REPLACEMENT), max([len(s) for s in subwords] or [1])) # Initialize the cache self._cache_size = 2**20 self._token_to_ids_cache = [(None, None)] * self._cache_size # Setup tokenizer # Reserved tokens are all tokens that are mixed alphanum and non-alphanum. reserved_tokens = set([_UNDERSCORE_REPLACEMENT]) for t in self._subwords: if text_encoder.is_mixed_alphanum(t): reserved_tokens.add(t) self._tokenizer = text_encoder.Tokenizer( alphanum_only=False, reserved_tokens=reserved_tokens)
def _init_from_list(self, subwords): """Initializes the encoder from a list of subwords.""" subwords = [tf.compat.as_text(s) for s in subwords if s] self._subwords = subwords # Note that internally everything is 0-indexed. Padding is dealt with at the # end of encode and the beginning of decode. self._subword_to_id = {s: i for i, s in enumerate(subwords)} # We remember the maximum length of any subword to avoid having to # check arbitrarily long strings. self._max_subword_len = max( len(_UNDERSCORE_REPLACEMENT), max([len(s) for s in subwords] or [1])) # Initialize the cache self._cache_size = 2**20 self._token_to_ids_cache = [(None, None)] * self._cache_size # Setup tokenizer # Reserved tokens are all tokens that are mixed alphanum and non-alphanum. reserved_tokens = set([_UNDERSCORE_REPLACEMENT]) for t in self._subwords: if text_encoder.is_mixed_alphanum(t): reserved_tokens.add(t) self._tokenizer = text_encoder.Tokenizer( alphanum_only=False, reserved_tokens=reserved_tokens)
[ "Initializes", "the", "encoder", "from", "a", "list", "of", "subwords", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L213-L237
[ "def", "_init_from_list", "(", "self", ",", "subwords", ")", ":", "subwords", "=", "[", "tf", ".", "compat", ".", "as_text", "(", "s", ")", "for", "s", "in", "subwords", "if", "s", "]", "self", ".", "_subwords", "=", "subwords", "# Note that internally e...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder.save_to_file
Save the vocabulary to a file.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def save_to_file(self, filename_prefix): """Save the vocabulary to a file.""" # Wrap in single quotes to make it easier to see the full subword when # it has spaces and make it easier to search with ctrl+f. filename = self._filename(filename_prefix) lines = ["'%s'" % s for s in self._subwords] self._write_lines_to_file(filename, lines)
def save_to_file(self, filename_prefix): """Save the vocabulary to a file.""" # Wrap in single quotes to make it easier to see the full subword when # it has spaces and make it easier to search with ctrl+f. filename = self._filename(filename_prefix) lines = ["'%s'" % s for s in self._subwords] self._write_lines_to_file(filename, lines)
[ "Save", "the", "vocabulary", "to", "a", "file", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L243-L249
[ "def", "save_to_file", "(", "self", ",", "filename_prefix", ")", ":", "# Wrap in single quotes to make it easier to see the full subword when", "# it has spaces and make it easier to search with ctrl+f.", "filename", "=", "self", ".", "_filename", "(", "filename_prefix", ")", "li...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder.load_from_file
Extracts list of subwords from file.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def load_from_file(cls, filename_prefix): """Extracts list of subwords from file.""" filename = cls._filename(filename_prefix) lines, _ = cls._read_lines_from_file(filename) # Strip wrapping single quotes vocab_list = [line[1:-1] for line in lines] return cls(vocab_list=vocab_list)
def load_from_file(cls, filename_prefix): """Extracts list of subwords from file.""" filename = cls._filename(filename_prefix) lines, _ = cls._read_lines_from_file(filename) # Strip wrapping single quotes vocab_list = [line[1:-1] for line in lines] return cls(vocab_list=vocab_list)
[ "Extracts", "list", "of", "subwords", "from", "file", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L252-L258
[ "def", "load_from_file", "(", "cls", ",", "filename_prefix", ")", ":", "filename", "=", "cls", ".", "_filename", "(", "filename_prefix", ")", "lines", ",", "_", "=", "cls", ".", "_read_lines_from_file", "(", "filename", ")", "# Strip wrapping single quotes", "vo...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubwordTextEncoder.build_from_corpus
Builds a `SubwordTextEncoder` based on the `corpus_generator`. Args: corpus_generator: generator yielding `str`, from which subwords will be constructed. target_vocab_size: `int`, approximate size of the vocabulary to create. max_subword_length: `int`, maximum length of a subword. Note that memory and compute scale quadratically in the length of the longest token. max_corpus_chars: `int`, the maximum number of characters to consume from `corpus_generator` for the purposes of building the subword vocabulary. reserved_tokens: `list<str>`, list of tokens that will always be treated as whole tokens and not split up. Note that these must contain a mix of alphanumeric and non-alphanumeric characters (e.g. "<EOS>") and not end in an underscore. Returns: `SubwordTextEncoder`.
tensorflow_datasets/core/features/text/subword_text_encoder.py
def build_from_corpus(cls, corpus_generator, target_vocab_size, max_subword_length=20, max_corpus_chars=None, reserved_tokens=None): """Builds a `SubwordTextEncoder` based on the `corpus_generator`. Args: corpus_generator: generator yielding `str`, from which subwords will be constructed. target_vocab_size: `int`, approximate size of the vocabulary to create. max_subword_length: `int`, maximum length of a subword. Note that memory and compute scale quadratically in the length of the longest token. max_corpus_chars: `int`, the maximum number of characters to consume from `corpus_generator` for the purposes of building the subword vocabulary. reserved_tokens: `list<str>`, list of tokens that will always be treated as whole tokens and not split up. Note that these must contain a mix of alphanumeric and non-alphanumeric characters (e.g. "<EOS>") and not end in an underscore. Returns: `SubwordTextEncoder`. """ reserved_tokens = reserved_tokens or [] _validate_build_arguments( max_subword_length=max_subword_length, reserved_tokens=reserved_tokens, target_vocab_size=target_vocab_size) token_counts = _token_counts_from_generator( generator=corpus_generator, max_chars=max_corpus_chars, reserved_tokens=reserved_tokens) # Binary search on the minimum token count to build a vocabulary with # approximately the right size def _binary_search(min_token_count, max_token_count): """Binary search min_token_count to build SubwordTextEncoder vocab.""" candidate_min = (min_token_count + max_token_count) // 2 logging.info("SubwordTextEncoder build: trying min_token_count %d", candidate_min) encoder = cls._build_from_token_counts( token_counts=token_counts, min_token_count=candidate_min, reserved_tokens=reserved_tokens, num_iterations=4, max_subword_length=max_subword_length) vocab_size = encoder.vocab_size # Being within 1% of the target vocab size is ok target_achieved = ( abs(vocab_size - target_vocab_size) * 100 < target_vocab_size) if (target_achieved or min_token_count >= max_token_count or candidate_min <= 1): # Search complete return encoder # Recurse if vocab_size > target_vocab_size: next_encoder = _binary_search(candidate_min + 1, max_token_count) else: next_encoder = _binary_search(min_token_count, candidate_min - 1) # Return the one that's closest to the target_vocab_size if (abs(vocab_size - target_vocab_size) < abs(next_encoder.vocab_size - target_vocab_size)): return encoder else: return next_encoder # Get min and max token counts. min_token_count = max(min(token_counts.values()), 1) max_token_count = max(token_counts.values()) # Another option could be to do a binary search over *ranks* of the tokens. return _binary_search(min_token_count, max_token_count)
def build_from_corpus(cls, corpus_generator, target_vocab_size, max_subword_length=20, max_corpus_chars=None, reserved_tokens=None): """Builds a `SubwordTextEncoder` based on the `corpus_generator`. Args: corpus_generator: generator yielding `str`, from which subwords will be constructed. target_vocab_size: `int`, approximate size of the vocabulary to create. max_subword_length: `int`, maximum length of a subword. Note that memory and compute scale quadratically in the length of the longest token. max_corpus_chars: `int`, the maximum number of characters to consume from `corpus_generator` for the purposes of building the subword vocabulary. reserved_tokens: `list<str>`, list of tokens that will always be treated as whole tokens and not split up. Note that these must contain a mix of alphanumeric and non-alphanumeric characters (e.g. "<EOS>") and not end in an underscore. Returns: `SubwordTextEncoder`. """ reserved_tokens = reserved_tokens or [] _validate_build_arguments( max_subword_length=max_subword_length, reserved_tokens=reserved_tokens, target_vocab_size=target_vocab_size) token_counts = _token_counts_from_generator( generator=corpus_generator, max_chars=max_corpus_chars, reserved_tokens=reserved_tokens) # Binary search on the minimum token count to build a vocabulary with # approximately the right size def _binary_search(min_token_count, max_token_count): """Binary search min_token_count to build SubwordTextEncoder vocab.""" candidate_min = (min_token_count + max_token_count) // 2 logging.info("SubwordTextEncoder build: trying min_token_count %d", candidate_min) encoder = cls._build_from_token_counts( token_counts=token_counts, min_token_count=candidate_min, reserved_tokens=reserved_tokens, num_iterations=4, max_subword_length=max_subword_length) vocab_size = encoder.vocab_size # Being within 1% of the target vocab size is ok target_achieved = ( abs(vocab_size - target_vocab_size) * 100 < target_vocab_size) if (target_achieved or min_token_count >= max_token_count or candidate_min <= 1): # Search complete return encoder # Recurse if vocab_size > target_vocab_size: next_encoder = _binary_search(candidate_min + 1, max_token_count) else: next_encoder = _binary_search(min_token_count, candidate_min - 1) # Return the one that's closest to the target_vocab_size if (abs(vocab_size - target_vocab_size) < abs(next_encoder.vocab_size - target_vocab_size)): return encoder else: return next_encoder # Get min and max token counts. min_token_count = max(min(token_counts.values()), 1) max_token_count = max(token_counts.values()) # Another option could be to do a binary search over *ranks* of the tokens. return _binary_search(min_token_count, max_token_count)
[ "Builds", "a", "SubwordTextEncoder", "based", "on", "the", "corpus_generator", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/text/subword_text_encoder.py#L261-L336
[ "def", "build_from_corpus", "(", "cls", ",", "corpus_generator", ",", "target_vocab_size", ",", "max_subword_length", "=", "20", ",", "max_corpus_chars", "=", "None", ",", "reserved_tokens", "=", "None", ")", ":", "reserved_tokens", "=", "reserved_tokens", "or", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Higgs._generate_examples
Generate features given the directory path. Args: file_path: path where the csv file is stored Yields: The features, per row.
tensorflow_datasets/structured/higgs.py
def _generate_examples(self, file_path): """Generate features given the directory path. Args: file_path: path where the csv file is stored Yields: The features, per row. """ fieldnames = [ 'class_label', 'lepton_pT', 'lepton_eta', 'lepton_phi', 'missing_energy_magnitude', 'missing_energy_phi', 'jet_1_pt', 'jet_1_eta', 'jet_1_phi', 'jet_1_b-tag', 'jet_2_pt', 'jet_2_eta', 'jet_2_phi', 'jet_2_b-tag', 'jet_3_pt', 'jet_3_eta', 'jet_3_phi', 'jet_3_b-tag', 'jet_4_pt', 'jet_4_eta', 'jet_4_phi', 'jet_4_b-tag', 'm_jj', 'm_jjj', 'm_lv', 'm_jlv', 'm_bb', 'm_wbb', 'm_wwbb' ] with tf.io.gfile.GFile(file_path) as csvfile: reader = csv.DictReader(csvfile, fieldnames=fieldnames) for row in reader: yield row
def _generate_examples(self, file_path): """Generate features given the directory path. Args: file_path: path where the csv file is stored Yields: The features, per row. """ fieldnames = [ 'class_label', 'lepton_pT', 'lepton_eta', 'lepton_phi', 'missing_energy_magnitude', 'missing_energy_phi', 'jet_1_pt', 'jet_1_eta', 'jet_1_phi', 'jet_1_b-tag', 'jet_2_pt', 'jet_2_eta', 'jet_2_phi', 'jet_2_b-tag', 'jet_3_pt', 'jet_3_eta', 'jet_3_phi', 'jet_3_b-tag', 'jet_4_pt', 'jet_4_eta', 'jet_4_phi', 'jet_4_b-tag', 'm_jj', 'm_jjj', 'm_lv', 'm_jlv', 'm_bb', 'm_wbb', 'm_wwbb' ] with tf.io.gfile.GFile(file_path) as csvfile: reader = csv.DictReader(csvfile, fieldnames=fieldnames) for row in reader: yield row
[ "Generate", "features", "given", "the", "directory", "path", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/structured/higgs.py#L122-L144
[ "def", "_generate_examples", "(", "self", ",", "file_path", ")", ":", "fieldnames", "=", "[", "'class_label'", ",", "'lepton_pT'", ",", "'lepton_eta'", ",", "'lepton_phi'", ",", "'missing_energy_magnitude'", ",", "'missing_energy_phi'", ",", "'jet_1_pt'", ",", "'jet...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
CatsVsDogs._generate_examples
Generate Cats vs Dogs images and labels given a directory path.
tensorflow_datasets/image/cats_vs_dogs.py
def _generate_examples(self, archive): """Generate Cats vs Dogs images and labels given a directory path.""" num_skipped = 0 for fname, fobj in archive: res = _NAME_RE.match(fname) if not res: # README file, ... continue label = res.group(1).lower() if tf.compat.as_bytes("JFIF") not in fobj.peek(10): num_skipped += 1 continue yield { "image": fobj, "image/filename": fname, "label": label, } if num_skipped != _NUM_CORRUPT_IMAGES: raise ValueError("Expected %d corrupt images, but found %d" % ( _NUM_CORRUPT_IMAGES, num_skipped)) logging.warning("%d images were corrupted and were skipped", num_skipped)
def _generate_examples(self, archive): """Generate Cats vs Dogs images and labels given a directory path.""" num_skipped = 0 for fname, fobj in archive: res = _NAME_RE.match(fname) if not res: # README file, ... continue label = res.group(1).lower() if tf.compat.as_bytes("JFIF") not in fobj.peek(10): num_skipped += 1 continue yield { "image": fobj, "image/filename": fname, "label": label, } if num_skipped != _NUM_CORRUPT_IMAGES: raise ValueError("Expected %d corrupt images, but found %d" % ( _NUM_CORRUPT_IMAGES, num_skipped)) logging.warning("%d images were corrupted and were skipped", num_skipped)
[ "Generate", "Cats", "vs", "Dogs", "images", "and", "labels", "given", "a", "directory", "path", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/cats_vs_dogs.py#L87-L107
[ "def", "_generate_examples", "(", "self", ",", "archive", ")", ":", "num_skipped", "=", "0", "for", "fname", ",", "fobj", "in", "archive", ":", "res", "=", "_NAME_RE", ".", "match", "(", "fname", ")", "if", "not", "res", ":", "# README file, ...", "conti...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_load_chunk
Loads a data chunk as specified by the paths. Args: dat_path: Path to dat file of the chunk. cat_path: Path to cat file of the chunk. info_path: Path to info file of the chunk. Returns: Tuple with the dat, cat, info_arrays.
tensorflow_datasets/image/smallnorb.py
def _load_chunk(dat_path, cat_path, info_path): """Loads a data chunk as specified by the paths. Args: dat_path: Path to dat file of the chunk. cat_path: Path to cat file of the chunk. info_path: Path to info file of the chunk. Returns: Tuple with the dat, cat, info_arrays. """ dat_array = read_binary_matrix(dat_path) # Even if the image is gray scale, we need to add an extra channel dimension # to be compatible with tfds.features.Image. dat_array = np.expand_dims(dat_array, -1) cat_array = read_binary_matrix(cat_path) info_array = read_binary_matrix(info_path) info_array = np.copy(info_array) # Make read-only buffer array writable. # Azimuth values are 0, 2, 4, .., 34. We divide by 2 to get proper labels. info_array[:, 2] = info_array[:, 2] / 2 return dat_array, cat_array, info_array
def _load_chunk(dat_path, cat_path, info_path): """Loads a data chunk as specified by the paths. Args: dat_path: Path to dat file of the chunk. cat_path: Path to cat file of the chunk. info_path: Path to info file of the chunk. Returns: Tuple with the dat, cat, info_arrays. """ dat_array = read_binary_matrix(dat_path) # Even if the image is gray scale, we need to add an extra channel dimension # to be compatible with tfds.features.Image. dat_array = np.expand_dims(dat_array, -1) cat_array = read_binary_matrix(cat_path) info_array = read_binary_matrix(info_path) info_array = np.copy(info_array) # Make read-only buffer array writable. # Azimuth values are 0, 2, 4, .., 34. We divide by 2 to get proper labels. info_array[:, 2] = info_array[:, 2] / 2 return dat_array, cat_array, info_array
[ "Loads", "a", "data", "chunk", "as", "specified", "by", "the", "paths", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/smallnorb.py#L141-L164
[ "def", "_load_chunk", "(", "dat_path", ",", "cat_path", ",", "info_path", ")", ":", "dat_array", "=", "read_binary_matrix", "(", "dat_path", ")", "# Even if the image is gray scale, we need to add an extra channel dimension", "# to be compatible with tfds.features.Image.", "dat_a...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
read_binary_matrix
Reads and returns binary formatted matrix stored in filename. The file format is described on the data set page: https://cs.nyu.edu/~ylclab/data/norb-v1.0-small/ Args: filename: String with path to the file. Returns: Numpy array contained in the file.
tensorflow_datasets/image/smallnorb.py
def read_binary_matrix(filename): """Reads and returns binary formatted matrix stored in filename. The file format is described on the data set page: https://cs.nyu.edu/~ylclab/data/norb-v1.0-small/ Args: filename: String with path to the file. Returns: Numpy array contained in the file. """ with tf.io.gfile.GFile(filename, "rb") as f: s = f.read() # Data is stored in little-endian byte order. int32_dtype = np.dtype("int32").newbyteorder("<") # The first 4 bytes contain a magic code that specifies the data type. magic = int(np.frombuffer(s, dtype=int32_dtype, count=1)) if magic == 507333717: data_dtype = np.dtype("uint8") # uint8 does not have a byte order. elif magic == 507333716: data_dtype = np.dtype("int32").newbyteorder("<") else: raise ValueError("Invalid magic value for data type!") # The second 4 bytes contain an int32 with the number of dimensions of the # stored array. ndim = int(np.frombuffer(s, dtype=int32_dtype, count=1, offset=4)) # The next ndim x 4 bytes contain the shape of the array in int32. dims = np.frombuffer(s, dtype=int32_dtype, count=ndim, offset=8) # If the array has less than three dimensions, three int32 are still used to # save the shape info (remaining int32 are simply set to 1). The shape info # hence uses max(3, ndim) bytes. bytes_used_for_shape_info = max(3, ndim) * 4 # The remaining bytes are the array. data = np.frombuffer( s, dtype=data_dtype, offset=8 + bytes_used_for_shape_info) return data.reshape(tuple(dims))
def read_binary_matrix(filename): """Reads and returns binary formatted matrix stored in filename. The file format is described on the data set page: https://cs.nyu.edu/~ylclab/data/norb-v1.0-small/ Args: filename: String with path to the file. Returns: Numpy array contained in the file. """ with tf.io.gfile.GFile(filename, "rb") as f: s = f.read() # Data is stored in little-endian byte order. int32_dtype = np.dtype("int32").newbyteorder("<") # The first 4 bytes contain a magic code that specifies the data type. magic = int(np.frombuffer(s, dtype=int32_dtype, count=1)) if magic == 507333717: data_dtype = np.dtype("uint8") # uint8 does not have a byte order. elif magic == 507333716: data_dtype = np.dtype("int32").newbyteorder("<") else: raise ValueError("Invalid magic value for data type!") # The second 4 bytes contain an int32 with the number of dimensions of the # stored array. ndim = int(np.frombuffer(s, dtype=int32_dtype, count=1, offset=4)) # The next ndim x 4 bytes contain the shape of the array in int32. dims = np.frombuffer(s, dtype=int32_dtype, count=ndim, offset=8) # If the array has less than three dimensions, three int32 are still used to # save the shape info (remaining int32 are simply set to 1). The shape info # hence uses max(3, ndim) bytes. bytes_used_for_shape_info = max(3, ndim) * 4 # The remaining bytes are the array. data = np.frombuffer( s, dtype=data_dtype, offset=8 + bytes_used_for_shape_info) return data.reshape(tuple(dims))
[ "Reads", "and", "returns", "binary", "formatted", "matrix", "stored", "in", "filename", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/smallnorb.py#L167-L209
[ "def", "read_binary_matrix", "(", "filename", ")", ":", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "filename", ",", "\"rb\"", ")", "as", "f", ":", "s", "=", "f", ".", "read", "(", ")", "# Data is stored in little-endian byte order.", "int32_...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Smallnorb._split_generators
Returns splits.
tensorflow_datasets/image/smallnorb.py
def _split_generators(self, dl_manager): """Returns splits.""" filenames = { "training_dat": _TRAINING_URL_TEMPLATE.format(type="dat"), "training_cat": _TRAINING_URL_TEMPLATE.format(type="cat"), "training_info": _TRAINING_URL_TEMPLATE.format(type="info"), "testing_dat": _TESTING_URL_TEMPLATE.format(type="dat"), "testing_cat": _TESTING_URL_TEMPLATE.format(type="cat"), "testing_info": _TESTING_URL_TEMPLATE.format(type="info"), } files = dl_manager.download_and_extract(filenames) return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=1, gen_kwargs=dict( dat_path=files["training_dat"], cat_path=files["training_cat"], info_path=files["training_info"])), tfds.core.SplitGenerator( name=tfds.Split.TEST, num_shards=1, gen_kwargs=dict( dat_path=files["testing_dat"], cat_path=files["testing_cat"], info_path=files["testing_info"])), ]
def _split_generators(self, dl_manager): """Returns splits.""" filenames = { "training_dat": _TRAINING_URL_TEMPLATE.format(type="dat"), "training_cat": _TRAINING_URL_TEMPLATE.format(type="cat"), "training_info": _TRAINING_URL_TEMPLATE.format(type="info"), "testing_dat": _TESTING_URL_TEMPLATE.format(type="dat"), "testing_cat": _TESTING_URL_TEMPLATE.format(type="cat"), "testing_info": _TESTING_URL_TEMPLATE.format(type="info"), } files = dl_manager.download_and_extract(filenames) return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=1, gen_kwargs=dict( dat_path=files["training_dat"], cat_path=files["training_cat"], info_path=files["training_info"])), tfds.core.SplitGenerator( name=tfds.Split.TEST, num_shards=1, gen_kwargs=dict( dat_path=files["testing_dat"], cat_path=files["testing_cat"], info_path=files["testing_info"])), ]
[ "Returns", "splits", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/smallnorb.py#L86-L114
[ "def", "_split_generators", "(", "self", ",", "dl_manager", ")", ":", "filenames", "=", "{", "\"training_dat\"", ":", "_TRAINING_URL_TEMPLATE", ".", "format", "(", "type", "=", "\"dat\"", ")", ",", "\"training_cat\"", ":", "_TRAINING_URL_TEMPLATE", ".", "format", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Smallnorb._generate_examples
Generate examples for the Smallnorb dataset. Args: dat_path: Path to dat file of the chunk. cat_path: Path to cat file of the chunk. info_path: Path to info file of the chunk. Yields: Dictionaries with images and the different labels.
tensorflow_datasets/image/smallnorb.py
def _generate_examples(self, dat_path, cat_path, info_path): """Generate examples for the Smallnorb dataset. Args: dat_path: Path to dat file of the chunk. cat_path: Path to cat file of the chunk. info_path: Path to info file of the chunk. Yields: Dictionaries with images and the different labels. """ dat_arr, cat_arr, info_arr = _load_chunk(dat_path, cat_path, info_path) for image, category, info_vec in moves.zip(dat_arr, cat_arr, info_arr): yield { "image": image[0], "image2": image[1], "label_category": category, "instance": info_vec[0], "label_elevation": info_vec[1], "label_azimuth": info_vec[2], "label_lighting": info_vec[3], }
def _generate_examples(self, dat_path, cat_path, info_path): """Generate examples for the Smallnorb dataset. Args: dat_path: Path to dat file of the chunk. cat_path: Path to cat file of the chunk. info_path: Path to info file of the chunk. Yields: Dictionaries with images and the different labels. """ dat_arr, cat_arr, info_arr = _load_chunk(dat_path, cat_path, info_path) for image, category, info_vec in moves.zip(dat_arr, cat_arr, info_arr): yield { "image": image[0], "image2": image[1], "label_category": category, "instance": info_vec[0], "label_elevation": info_vec[1], "label_azimuth": info_vec[2], "label_lighting": info_vec[3], }
[ "Generate", "examples", "for", "the", "Smallnorb", "dataset", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/smallnorb.py#L116-L138
[ "def", "_generate_examples", "(", "self", ",", "dat_path", ",", "cat_path", ",", "info_path", ")", ":", "dat_arr", ",", "cat_arr", ",", "info_arr", "=", "_load_chunk", "(", "dat_path", ",", "cat_path", ",", "info_path", ")", "for", "image", ",", "category", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
build_dataset
Constructs a `tf.data.Dataset` from TFRecord files. Args: instruction_dicts: `list` of {'filepath':, 'mask':, 'offset_mask':} containing the information about which files and which examples to use. The boolean mask will be repeated and zipped with the examples from filepath. dataset_from_file_fn: function returning a `tf.data.Dataset` given a filename. shuffle_files: `bool`, Whether to shuffle the input filenames. parallel_reads: `int`, how many files to read in parallel. Returns: `tf.data.Dataset`
tensorflow_datasets/core/dataset_utils.py
def build_dataset(instruction_dicts, dataset_from_file_fn, shuffle_files=False, parallel_reads=64): """Constructs a `tf.data.Dataset` from TFRecord files. Args: instruction_dicts: `list` of {'filepath':, 'mask':, 'offset_mask':} containing the information about which files and which examples to use. The boolean mask will be repeated and zipped with the examples from filepath. dataset_from_file_fn: function returning a `tf.data.Dataset` given a filename. shuffle_files: `bool`, Whether to shuffle the input filenames. parallel_reads: `int`, how many files to read in parallel. Returns: `tf.data.Dataset` """ # First case: All examples are taken (No value skipped) if _no_examples_skipped(instruction_dicts): # Only use the filenames as instruction instruction_ds = tf.data.Dataset.from_tensor_slices([ d["filepath"] for d in instruction_dicts ]) build_ds_from_instruction = dataset_from_file_fn # Second case: Use the instructions to read the examples else: instruction_ds = _build_instruction_ds(instruction_dicts) build_ds_from_instruction = functools.partial( _build_ds_from_instruction, ds_from_file_fn=dataset_from_file_fn, ) # If shuffle is True, we shuffle the instructions/shards if shuffle_files: instruction_ds = instruction_ds.shuffle(len(instruction_dicts)) # Use interleave to parallel read files and decode records ds = instruction_ds.interleave( build_ds_from_instruction, cycle_length=parallel_reads, num_parallel_calls=tf.data.experimental.AUTOTUNE) return ds
def build_dataset(instruction_dicts, dataset_from_file_fn, shuffle_files=False, parallel_reads=64): """Constructs a `tf.data.Dataset` from TFRecord files. Args: instruction_dicts: `list` of {'filepath':, 'mask':, 'offset_mask':} containing the information about which files and which examples to use. The boolean mask will be repeated and zipped with the examples from filepath. dataset_from_file_fn: function returning a `tf.data.Dataset` given a filename. shuffle_files: `bool`, Whether to shuffle the input filenames. parallel_reads: `int`, how many files to read in parallel. Returns: `tf.data.Dataset` """ # First case: All examples are taken (No value skipped) if _no_examples_skipped(instruction_dicts): # Only use the filenames as instruction instruction_ds = tf.data.Dataset.from_tensor_slices([ d["filepath"] for d in instruction_dicts ]) build_ds_from_instruction = dataset_from_file_fn # Second case: Use the instructions to read the examples else: instruction_ds = _build_instruction_ds(instruction_dicts) build_ds_from_instruction = functools.partial( _build_ds_from_instruction, ds_from_file_fn=dataset_from_file_fn, ) # If shuffle is True, we shuffle the instructions/shards if shuffle_files: instruction_ds = instruction_ds.shuffle(len(instruction_dicts)) # Use interleave to parallel read files and decode records ds = instruction_ds.interleave( build_ds_from_instruction, cycle_length=parallel_reads, num_parallel_calls=tf.data.experimental.AUTOTUNE) return ds
[ "Constructs", "a", "tf", ".", "data", ".", "Dataset", "from", "TFRecord", "files", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/dataset_utils.py#L32-L76
[ "def", "build_dataset", "(", "instruction_dicts", ",", "dataset_from_file_fn", ",", "shuffle_files", "=", "False", ",", "parallel_reads", "=", "64", ")", ":", "# First case: All examples are taken (No value skipped)", "if", "_no_examples_skipped", "(", "instruction_dicts", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_build_instruction_ds
Create a dataset containing individual instruction for each shard. Each instruction is a dict: ``` { "filepath": tf.Tensor(shape=(), dtype=tf.string), "mask_offset": tf.Tensor(shape=(), dtype=tf.int64), "mask": tf.Tensor(shape=(100,), dtype=tf.bool), } ``` Args: instructions: `list[dict]`, the list of instruction dict Returns: instruction_ds: The dataset containing the instruction. The dataset size is the number of shard.
tensorflow_datasets/core/dataset_utils.py
def _build_instruction_ds(instructions): """Create a dataset containing individual instruction for each shard. Each instruction is a dict: ``` { "filepath": tf.Tensor(shape=(), dtype=tf.string), "mask_offset": tf.Tensor(shape=(), dtype=tf.int64), "mask": tf.Tensor(shape=(100,), dtype=tf.bool), } ``` Args: instructions: `list[dict]`, the list of instruction dict Returns: instruction_ds: The dataset containing the instruction. The dataset size is the number of shard. """ # Transpose the list[dict] into dict[list] tensor_inputs = { # offset_mask need to be converted to int64 explicitly k: np.array(vals, dtype=np.int64) if k == "mask_offset" else list(vals) for k, vals in utils.zip_dict(*instructions) } return tf.data.Dataset.from_tensor_slices(tensor_inputs)
def _build_instruction_ds(instructions): """Create a dataset containing individual instruction for each shard. Each instruction is a dict: ``` { "filepath": tf.Tensor(shape=(), dtype=tf.string), "mask_offset": tf.Tensor(shape=(), dtype=tf.int64), "mask": tf.Tensor(shape=(100,), dtype=tf.bool), } ``` Args: instructions: `list[dict]`, the list of instruction dict Returns: instruction_ds: The dataset containing the instruction. The dataset size is the number of shard. """ # Transpose the list[dict] into dict[list] tensor_inputs = { # offset_mask need to be converted to int64 explicitly k: np.array(vals, dtype=np.int64) if k == "mask_offset" else list(vals) for k, vals in utils.zip_dict(*instructions) } return tf.data.Dataset.from_tensor_slices(tensor_inputs)
[ "Create", "a", "dataset", "containing", "individual", "instruction", "for", "each", "shard", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/dataset_utils.py#L84-L109
[ "def", "_build_instruction_ds", "(", "instructions", ")", ":", "# Transpose the list[dict] into dict[list]", "tensor_inputs", "=", "{", "# offset_mask need to be converted to int64 explicitly", "k", ":", "np", ".", "array", "(", "vals", ",", "dtype", "=", "np", ".", "in...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_build_mask_ds
Build the mask dataset to indicate which element to skip. Args: mask: `tf.Tensor`, binary mask to apply to all following elements. This mask should have a length 100. mask_offset: `tf.Tensor`, Integer specifying from how much the mask should be shifted for the first element. Returns: mask_ds: `tf.data.Dataset`, a dataset returning False for examples to skip and True for examples to keep.
tensorflow_datasets/core/dataset_utils.py
def _build_mask_ds(mask, mask_offset): """Build the mask dataset to indicate which element to skip. Args: mask: `tf.Tensor`, binary mask to apply to all following elements. This mask should have a length 100. mask_offset: `tf.Tensor`, Integer specifying from how much the mask should be shifted for the first element. Returns: mask_ds: `tf.data.Dataset`, a dataset returning False for examples to skip and True for examples to keep. """ mask_ds = tf.data.Dataset.from_tensor_slices(mask) mask_ds = mask_ds.repeat() mask_ds = mask_ds.skip(mask_offset) return mask_ds
def _build_mask_ds(mask, mask_offset): """Build the mask dataset to indicate which element to skip. Args: mask: `tf.Tensor`, binary mask to apply to all following elements. This mask should have a length 100. mask_offset: `tf.Tensor`, Integer specifying from how much the mask should be shifted for the first element. Returns: mask_ds: `tf.data.Dataset`, a dataset returning False for examples to skip and True for examples to keep. """ mask_ds = tf.data.Dataset.from_tensor_slices(mask) mask_ds = mask_ds.repeat() mask_ds = mask_ds.skip(mask_offset) return mask_ds
[ "Build", "the", "mask", "dataset", "to", "indicate", "which", "element", "to", "skip", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/dataset_utils.py#L112-L128
[ "def", "_build_mask_ds", "(", "mask", ",", "mask_offset", ")", ":", "mask_ds", "=", "tf", ".", "data", ".", "Dataset", ".", "from_tensor_slices", "(", "mask", ")", "mask_ds", "=", "mask_ds", ".", "repeat", "(", ")", "mask_ds", "=", "mask_ds", ".", "skip"...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_build_ds_from_instruction
Map an instruction to a real datasets for one particular shard. Args: instruction: A `dict` of `tf.Tensor` containing the instruction to load the particular shard (filename, mask,...) ds_from_file_fn: `fct`, function which returns the dataset associated to the filename Returns: dataset: `tf.data.Dataset`, The shard loaded from the instruction
tensorflow_datasets/core/dataset_utils.py
def _build_ds_from_instruction(instruction, ds_from_file_fn): """Map an instruction to a real datasets for one particular shard. Args: instruction: A `dict` of `tf.Tensor` containing the instruction to load the particular shard (filename, mask,...) ds_from_file_fn: `fct`, function which returns the dataset associated to the filename Returns: dataset: `tf.data.Dataset`, The shard loaded from the instruction """ # Create the example and mask ds for this particular shard examples_ds = ds_from_file_fn(instruction["filepath"]) mask_ds = _build_mask_ds( mask_offset=instruction["mask_offset"], mask=instruction["mask"], ) # Zip the mask and real examples ds = tf.data.Dataset.zip((examples_ds, mask_ds)) # Filter according to the mask (only keep True) ds = ds.filter(lambda example, mask: mask) # Only keep the examples ds = ds.map(lambda example, mask: example) return ds
def _build_ds_from_instruction(instruction, ds_from_file_fn): """Map an instruction to a real datasets for one particular shard. Args: instruction: A `dict` of `tf.Tensor` containing the instruction to load the particular shard (filename, mask,...) ds_from_file_fn: `fct`, function which returns the dataset associated to the filename Returns: dataset: `tf.data.Dataset`, The shard loaded from the instruction """ # Create the example and mask ds for this particular shard examples_ds = ds_from_file_fn(instruction["filepath"]) mask_ds = _build_mask_ds( mask_offset=instruction["mask_offset"], mask=instruction["mask"], ) # Zip the mask and real examples ds = tf.data.Dataset.zip((examples_ds, mask_ds)) # Filter according to the mask (only keep True) ds = ds.filter(lambda example, mask: mask) # Only keep the examples ds = ds.map(lambda example, mask: example) return ds
[ "Map", "an", "instruction", "to", "a", "real", "datasets", "for", "one", "particular", "shard", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/dataset_utils.py#L131-L156
[ "def", "_build_ds_from_instruction", "(", "instruction", ",", "ds_from_file_fn", ")", ":", "# Create the example and mask ds for this particular shard", "examples_ds", "=", "ds_from_file_fn", "(", "instruction", "[", "\"filepath\"", "]", ")", "mask_ds", "=", "_build_mask_ds",...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
as_numpy
Converts a `tf.data.Dataset` to an iterable of NumPy arrays. `as_numpy` converts a possibly nested structure of `tf.data.Dataset`s and `tf.Tensor`s to iterables of NumPy arrays and NumPy arrays, respectively. Args: dataset: a possibly nested structure of `tf.data.Dataset`s and/or `tf.Tensor`s. graph: `tf.Graph`, optional, explicitly set the graph to use. Returns: A structure matching `dataset` where `tf.data.Dataset`s are converted to generators of NumPy arrays and `tf.Tensor`s are converted to NumPy arrays.
tensorflow_datasets/core/dataset_utils.py
def as_numpy(dataset, graph=None): """Converts a `tf.data.Dataset` to an iterable of NumPy arrays. `as_numpy` converts a possibly nested structure of `tf.data.Dataset`s and `tf.Tensor`s to iterables of NumPy arrays and NumPy arrays, respectively. Args: dataset: a possibly nested structure of `tf.data.Dataset`s and/or `tf.Tensor`s. graph: `tf.Graph`, optional, explicitly set the graph to use. Returns: A structure matching `dataset` where `tf.data.Dataset`s are converted to generators of NumPy arrays and `tf.Tensor`s are converted to NumPy arrays. """ nested_ds = dataset del dataset # Flatten flat_ds = tf.nest.flatten(nested_ds) flat_np = [] # Type check for Tensors and Datasets for ds_el in flat_ds: types = [type(el) for el in flat_ds] types = tf.nest.pack_sequence_as(nested_ds, types) if not (isinstance(ds_el, tf.Tensor) or tf_compat.is_dataset(ds_el)): raise ValueError("Arguments to as_numpy must be tf.Tensors or " "tf.data.Datasets. Got: %s" % types) if tf.executing_eagerly(): # Eager mode for ds_el in flat_ds: if isinstance(ds_el, tf.Tensor): np_el = ds_el.numpy() elif tf_compat.is_dataset(ds_el): np_el = _eager_dataset_iterator(ds_el) else: assert False flat_np.append(np_el) else: # Graph mode # First create iterators for datasets with utils.maybe_with_graph(graph, create_if_none=False): ds_iters = [ tf.compat.v1.data.make_one_shot_iterator(ds_el).get_next() for ds_el in flat_ds if tf_compat.is_dataset(ds_el) ] ds_iters = [_graph_dataset_iterator(ds_iter, graph) for ds_iter in ds_iters] # Then create numpy arrays for tensors with utils.nogpu_session(graph) as sess: # Shared session for tf.Tensor # Calling sess.run once so that randomness is shared. np_arrays = sess.run([tensor for tensor in flat_ds if not tf_compat.is_dataset(tensor)]) # Merge the dataset iterators and np arrays iter_ds = iter(ds_iters) iter_array = iter(np_arrays) flat_np = [ next(iter_ds) if tf_compat.is_dataset(ds_el) else next(iter_array) for ds_el in flat_ds ] # Nest return tf.nest.pack_sequence_as(nested_ds, flat_np)
def as_numpy(dataset, graph=None): """Converts a `tf.data.Dataset` to an iterable of NumPy arrays. `as_numpy` converts a possibly nested structure of `tf.data.Dataset`s and `tf.Tensor`s to iterables of NumPy arrays and NumPy arrays, respectively. Args: dataset: a possibly nested structure of `tf.data.Dataset`s and/or `tf.Tensor`s. graph: `tf.Graph`, optional, explicitly set the graph to use. Returns: A structure matching `dataset` where `tf.data.Dataset`s are converted to generators of NumPy arrays and `tf.Tensor`s are converted to NumPy arrays. """ nested_ds = dataset del dataset # Flatten flat_ds = tf.nest.flatten(nested_ds) flat_np = [] # Type check for Tensors and Datasets for ds_el in flat_ds: types = [type(el) for el in flat_ds] types = tf.nest.pack_sequence_as(nested_ds, types) if not (isinstance(ds_el, tf.Tensor) or tf_compat.is_dataset(ds_el)): raise ValueError("Arguments to as_numpy must be tf.Tensors or " "tf.data.Datasets. Got: %s" % types) if tf.executing_eagerly(): # Eager mode for ds_el in flat_ds: if isinstance(ds_el, tf.Tensor): np_el = ds_el.numpy() elif tf_compat.is_dataset(ds_el): np_el = _eager_dataset_iterator(ds_el) else: assert False flat_np.append(np_el) else: # Graph mode # First create iterators for datasets with utils.maybe_with_graph(graph, create_if_none=False): ds_iters = [ tf.compat.v1.data.make_one_shot_iterator(ds_el).get_next() for ds_el in flat_ds if tf_compat.is_dataset(ds_el) ] ds_iters = [_graph_dataset_iterator(ds_iter, graph) for ds_iter in ds_iters] # Then create numpy arrays for tensors with utils.nogpu_session(graph) as sess: # Shared session for tf.Tensor # Calling sess.run once so that randomness is shared. np_arrays = sess.run([tensor for tensor in flat_ds if not tf_compat.is_dataset(tensor)]) # Merge the dataset iterators and np arrays iter_ds = iter(ds_iters) iter_array = iter(np_arrays) flat_np = [ next(iter_ds) if tf_compat.is_dataset(ds_el) else next(iter_array) for ds_el in flat_ds ] # Nest return tf.nest.pack_sequence_as(nested_ds, flat_np)
[ "Converts", "a", "tf", ".", "data", ".", "Dataset", "to", "an", "iterable", "of", "NumPy", "arrays", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/dataset_utils.py#L176-L242
[ "def", "as_numpy", "(", "dataset", ",", "graph", "=", "None", ")", ":", "nested_ds", "=", "dataset", "del", "dataset", "# Flatten", "flat_ds", "=", "tf", ".", "nest", ".", "flatten", "(", "nested_ds", ")", "flat_np", "=", "[", "]", "# Type check for Tensor...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_load_data
Loads the images and latent values into Numpy arrays.
tensorflow_datasets/image/shapes3d.py
def _load_data(filepath): """Loads the images and latent values into Numpy arrays.""" with h5py.File(filepath, "r") as h5dataset: image_array = np.array(h5dataset["images"]) # The 'label' data set in the hdf5 file actually contains the float values # and not the class labels. values_array = np.array(h5dataset["labels"]) return image_array, values_array
def _load_data(filepath): """Loads the images and latent values into Numpy arrays.""" with h5py.File(filepath, "r") as h5dataset: image_array = np.array(h5dataset["images"]) # The 'label' data set in the hdf5 file actually contains the float values # and not the class labels. values_array = np.array(h5dataset["labels"]) return image_array, values_array
[ "Loads", "the", "images", "and", "latent", "values", "into", "Numpy", "arrays", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/shapes3d.py#L151-L158
[ "def", "_load_data", "(", "filepath", ")", ":", "with", "h5py", ".", "File", "(", "filepath", ",", "\"r\"", ")", "as", "h5dataset", ":", "image_array", "=", "np", ".", "array", "(", "h5dataset", "[", "\"images\"", "]", ")", "# The 'label' data set in the hdf...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_discretize
Discretizes array values to class labels.
tensorflow_datasets/image/shapes3d.py
def _discretize(a): """Discretizes array values to class labels.""" arr = np.asarray(a) index = np.argsort(arr) inverse_index = np.zeros(arr.size, dtype=np.intp) inverse_index[index] = np.arange(arr.size, dtype=np.intp) arr = arr[index] obs = np.r_[True, arr[1:] != arr[:-1]] return obs.cumsum()[inverse_index] - 1
def _discretize(a): """Discretizes array values to class labels.""" arr = np.asarray(a) index = np.argsort(arr) inverse_index = np.zeros(arr.size, dtype=np.intp) inverse_index[index] = np.arange(arr.size, dtype=np.intp) arr = arr[index] obs = np.r_[True, arr[1:] != arr[:-1]] return obs.cumsum()[inverse_index] - 1
[ "Discretizes", "array", "values", "to", "class", "labels", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/shapes3d.py#L163-L171
[ "def", "_discretize", "(", "a", ")", ":", "arr", "=", "np", ".", "asarray", "(", "a", ")", "index", "=", "np", ".", "argsort", "(", "arr", ")", "inverse_index", "=", "np", ".", "zeros", "(", "arr", ".", "size", ",", "dtype", "=", "np", ".", "in...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Shapes3d._generate_examples
Generate examples for the Shapes3d dataset. Args: filepath: path to the Shapes3d hdf5 file. Yields: Dictionaries with images and the different labels.
tensorflow_datasets/image/shapes3d.py
def _generate_examples(self, filepath): """Generate examples for the Shapes3d dataset. Args: filepath: path to the Shapes3d hdf5 file. Yields: Dictionaries with images and the different labels. """ # Simultaneously iterating through the different data sets in the hdf5 # file will be slow with a single file. Instead, we first load everything # into memory before yielding the samples. image_array, values_array = _load_data(filepath) # We need to calculate the class labels from the float values in the file. labels_array = np.zeros_like(values_array, dtype=np.int64) for i in range(values_array.shape[1]): labels_array[:, i] = _discretize(values_array[:, i]) # pylint: disable=unsupported-assignment-operation for image, labels, values in moves.zip(image_array, labels_array, values_array): yield { "image": image, "label_floor_hue": labels[0], "label_wall_hue": labels[1], "label_object_hue": labels[2], "label_scale": labels[3], "label_shape": labels[4], "label_orientation": labels[5], "value_floor_hue": values[0], "value_wall_hue": values[1], "value_object_hue": values[2], "value_scale": values[3], "value_shape": values[4], "value_orientation": values[5], }
def _generate_examples(self, filepath): """Generate examples for the Shapes3d dataset. Args: filepath: path to the Shapes3d hdf5 file. Yields: Dictionaries with images and the different labels. """ # Simultaneously iterating through the different data sets in the hdf5 # file will be slow with a single file. Instead, we first load everything # into memory before yielding the samples. image_array, values_array = _load_data(filepath) # We need to calculate the class labels from the float values in the file. labels_array = np.zeros_like(values_array, dtype=np.int64) for i in range(values_array.shape[1]): labels_array[:, i] = _discretize(values_array[:, i]) # pylint: disable=unsupported-assignment-operation for image, labels, values in moves.zip(image_array, labels_array, values_array): yield { "image": image, "label_floor_hue": labels[0], "label_wall_hue": labels[1], "label_object_hue": labels[2], "label_scale": labels[3], "label_shape": labels[4], "label_orientation": labels[5], "value_floor_hue": values[0], "value_wall_hue": values[1], "value_object_hue": values[2], "value_scale": values[3], "value_shape": values[4], "value_orientation": values[5], }
[ "Generate", "examples", "for", "the", "Shapes3d", "dataset", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/shapes3d.py#L113-L148
[ "def", "_generate_examples", "(", "self", ",", "filepath", ")", ":", "# Simultaneously iterating through the different data sets in the hdf5", "# file will be slow with a single file. Instead, we first load everything", "# into memory before yielding the samples.", "image_array", ",", "val...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_parse_and_clean_wikicode
Strips formatting and unwanted sections from raw page content.
tensorflow_datasets/text/wikipedia.py
def _parse_and_clean_wikicode(raw_content): """Strips formatting and unwanted sections from raw page content.""" wikicode = tfds.core.lazy_imports.mwparserfromhell.parse(raw_content) # Filters for references, tables, and file/image links. re_rm_wikilink = re.compile( "^(?:File|Image|Media):", flags=re.IGNORECASE | re.UNICODE) def rm_wikilink(obj): return bool(re_rm_wikilink.match(six.text_type(obj.title))) def rm_tag(obj): return six.text_type(obj.tag) in {"ref", "table"} def rm_template(obj): return obj.name.lower() in { "reflist", "notelist", "notelist-ua", "notelist-lr", "notelist-ur", "notelist-lg"} def try_remove_obj(obj, section): try: section.remove(obj) except ValueError: # For unknown reasons, objects are sometimes not found. pass section_text = [] # Filter individual sections to clean. for section in wikicode.get_sections( flat=True, include_lead=True, include_headings=True): for obj in section.ifilter_wikilinks(matches=rm_wikilink, recursive=True): try_remove_obj(obj, section) for obj in section.ifilter_templates(matches=rm_template, recursive=True): try_remove_obj(obj, section) for obj in section.ifilter_tags(matches=rm_tag, recursive=True): try_remove_obj(obj, section) section_text.append(section.strip_code().strip()) return "\n\n".join(section_text)
def _parse_and_clean_wikicode(raw_content): """Strips formatting and unwanted sections from raw page content.""" wikicode = tfds.core.lazy_imports.mwparserfromhell.parse(raw_content) # Filters for references, tables, and file/image links. re_rm_wikilink = re.compile( "^(?:File|Image|Media):", flags=re.IGNORECASE | re.UNICODE) def rm_wikilink(obj): return bool(re_rm_wikilink.match(six.text_type(obj.title))) def rm_tag(obj): return six.text_type(obj.tag) in {"ref", "table"} def rm_template(obj): return obj.name.lower() in { "reflist", "notelist", "notelist-ua", "notelist-lr", "notelist-ur", "notelist-lg"} def try_remove_obj(obj, section): try: section.remove(obj) except ValueError: # For unknown reasons, objects are sometimes not found. pass section_text = [] # Filter individual sections to clean. for section in wikicode.get_sections( flat=True, include_lead=True, include_headings=True): for obj in section.ifilter_wikilinks(matches=rm_wikilink, recursive=True): try_remove_obj(obj, section) for obj in section.ifilter_templates(matches=rm_template, recursive=True): try_remove_obj(obj, section) for obj in section.ifilter_tags(matches=rm_tag, recursive=True): try_remove_obj(obj, section) section_text.append(section.strip_code().strip()) return "\n\n".join(section_text)
[ "Strips", "formatting", "and", "unwanted", "sections", "from", "raw", "page", "content", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/text/wikipedia.py#L234-L269
[ "def", "_parse_and_clean_wikicode", "(", "raw_content", ")", ":", "wikicode", "=", "tfds", ".", "core", ".", "lazy_imports", ".", "mwparserfromhell", ".", "parse", "(", "raw_content", ")", "# Filters for references, tables, and file/image links.", "re_rm_wikilink", "=", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Wikipedia._build_pcollection
Build PCollection of examples in the raw (text) form.
tensorflow_datasets/text/wikipedia.py
def _build_pcollection(self, pipeline, filepaths, language): """Build PCollection of examples in the raw (text) form.""" beam = tfds.core.lazy_imports.apache_beam def _extract_content(filepath): """Extracts article content from a single WikiMedia XML file.""" logging.info("generating examples from = %s", filepath) with tf.io.gfile.GFile(filepath) as f: for _, elem in etree.iterparse(f, events=("end",)): if not elem.tag.endswith("page"): continue namespace = elem.tag[:-4] title = elem.find("./{0}title".format(namespace)).text ns = elem.find("./{0}ns".format(namespace)).text # Filter pages that are not in the "main" namespace. if ns != "0": continue raw_content = elem.find( "./{0}revision/{0}text".format(namespace)).text elem.clear() # Filter redirects. if raw_content is None or raw_content.lower().startswith("#redirect"): beam.metrics.Metrics.counter(language, "filtered-redirects").inc() continue beam.metrics.Metrics.counter(language, "extracted-examples").inc() yield (title, raw_content) def _clean_content(inputs): """Cleans raw wikicode to extract text.""" title, raw_content = inputs try: text = _parse_and_clean_wikicode(raw_content) except ( tfds.core.lazy_imports.mwparserfromhell.parser.ParserError) as e: beam.metrics.Metrics.counter(language, "parser-error").inc() logging.error("mwparserfromhell ParseError: %s", e) return beam.metrics.Metrics.counter(language, "cleaned-examples").inc() yield { "title": title, "text": text } return ( pipeline | beam.Create(filepaths) | beam.FlatMap(_extract_content) | beam.FlatMap(_clean_content) )
def _build_pcollection(self, pipeline, filepaths, language): """Build PCollection of examples in the raw (text) form.""" beam = tfds.core.lazy_imports.apache_beam def _extract_content(filepath): """Extracts article content from a single WikiMedia XML file.""" logging.info("generating examples from = %s", filepath) with tf.io.gfile.GFile(filepath) as f: for _, elem in etree.iterparse(f, events=("end",)): if not elem.tag.endswith("page"): continue namespace = elem.tag[:-4] title = elem.find("./{0}title".format(namespace)).text ns = elem.find("./{0}ns".format(namespace)).text # Filter pages that are not in the "main" namespace. if ns != "0": continue raw_content = elem.find( "./{0}revision/{0}text".format(namespace)).text elem.clear() # Filter redirects. if raw_content is None or raw_content.lower().startswith("#redirect"): beam.metrics.Metrics.counter(language, "filtered-redirects").inc() continue beam.metrics.Metrics.counter(language, "extracted-examples").inc() yield (title, raw_content) def _clean_content(inputs): """Cleans raw wikicode to extract text.""" title, raw_content = inputs try: text = _parse_and_clean_wikicode(raw_content) except ( tfds.core.lazy_imports.mwparserfromhell.parser.ParserError) as e: beam.metrics.Metrics.counter(language, "parser-error").inc() logging.error("mwparserfromhell ParseError: %s", e) return beam.metrics.Metrics.counter(language, "cleaned-examples").inc() yield { "title": title, "text": text } return ( pipeline | beam.Create(filepaths) | beam.FlatMap(_extract_content) | beam.FlatMap(_clean_content) )
[ "Build", "PCollection", "of", "examples", "in", "the", "raw", "(", "text", ")", "form", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/text/wikipedia.py#L176-L231
[ "def", "_build_pcollection", "(", "self", ",", "pipeline", ",", "filepaths", ",", "language", ")", ":", "beam", "=", "tfds", ".", "core", ".", "lazy_imports", ".", "apache_beam", "def", "_extract_content", "(", "filepath", ")", ":", "\"\"\"Extracts article conte...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
download_and_prepare
Generate data for a given dataset.
tensorflow_datasets/scripts/download_and_prepare.py
def download_and_prepare(builder): """Generate data for a given dataset.""" print("download_and_prepare for dataset {}...".format(builder.info.full_name)) dl_config = download_config() if isinstance(builder, tfds.core.BeamBasedBuilder): beam = tfds.core.lazy_imports.apache_beam # TODO(b/129149715): Restore compute stats. Currently skipped because not # beam supported. dl_config.compute_stats = tfds.download.ComputeStatsMode.SKIP dl_config.beam_options = beam.options.pipeline_options.PipelineOptions() builder.download_and_prepare( download_dir=FLAGS.download_dir, download_config=dl_config, ) termcolor.cprint(str(builder.info.as_proto), attrs=["bold"]) if FLAGS.debug: dataset = builder.as_dataset(split=tfds.Split.TRAIN) pdb.set_trace() del dataset
def download_and_prepare(builder): """Generate data for a given dataset.""" print("download_and_prepare for dataset {}...".format(builder.info.full_name)) dl_config = download_config() if isinstance(builder, tfds.core.BeamBasedBuilder): beam = tfds.core.lazy_imports.apache_beam # TODO(b/129149715): Restore compute stats. Currently skipped because not # beam supported. dl_config.compute_stats = tfds.download.ComputeStatsMode.SKIP dl_config.beam_options = beam.options.pipeline_options.PipelineOptions() builder.download_and_prepare( download_dir=FLAGS.download_dir, download_config=dl_config, ) termcolor.cprint(str(builder.info.as_proto), attrs=["bold"]) if FLAGS.debug: dataset = builder.as_dataset(split=tfds.Split.TRAIN) pdb.set_trace() del dataset
[ "Generate", "data", "for", "a", "given", "dataset", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/scripts/download_and_prepare.py#L113-L135
[ "def", "download_and_prepare", "(", "builder", ")", ":", "print", "(", "\"download_and_prepare for dataset {}...\"", ".", "format", "(", "builder", ".", "info", ".", "full_name", ")", ")", "dl_config", "=", "download_config", "(", ")", "if", "isinstance", "(", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
BBoxFeature.encode_example
See base class for details.
tensorflow_datasets/core/features/bounding_boxes.py
def encode_example(self, bbox): """See base class for details.""" # Validate the coordinates for coordinate in bbox: if not isinstance(coordinate, float): raise ValueError( 'BBox coordinates should be float. Got {}.'.format(bbox)) if not 0.0 <= coordinate <= 1.0: raise ValueError( 'BBox coordinates should be between 0 and 1. Got {}.'.format(bbox)) if bbox.xmax < bbox.xmin or bbox.ymax < bbox.ymin: raise ValueError( 'BBox coordinates should have min <= max. Got {}.'.format(bbox)) return super(BBoxFeature, self).encode_example( [bbox.ymin, bbox.xmin, bbox.ymax, bbox.xmax] )
def encode_example(self, bbox): """See base class for details.""" # Validate the coordinates for coordinate in bbox: if not isinstance(coordinate, float): raise ValueError( 'BBox coordinates should be float. Got {}.'.format(bbox)) if not 0.0 <= coordinate <= 1.0: raise ValueError( 'BBox coordinates should be between 0 and 1. Got {}.'.format(bbox)) if bbox.xmax < bbox.xmin or bbox.ymax < bbox.ymin: raise ValueError( 'BBox coordinates should have min <= max. Got {}.'.format(bbox)) return super(BBoxFeature, self).encode_example( [bbox.ymin, bbox.xmin, bbox.ymax, bbox.xmax] )
[ "See", "base", "class", "for", "details", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/bounding_boxes.py#L60-L76
[ "def", "encode_example", "(", "self", ",", "bbox", ")", ":", "# Validate the coordinates", "for", "coordinate", "in", "bbox", ":", "if", "not", "isinstance", "(", "coordinate", ",", "float", ")", ":", "raise", "ValueError", "(", "'BBox coordinates should be float....
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_load_data
Yields (labels, np_image) tuples.
tensorflow_datasets/image/cifar.py
def _load_data(path, labels_number=1): """Yields (labels, np_image) tuples.""" with tf.io.gfile.GFile(path, "rb") as f: data = f.read() offset = 0 max_offset = len(data) - 1 while offset < max_offset: labels = np.frombuffer(data, dtype=np.uint8, count=labels_number, offset=offset).reshape((labels_number,)) # 1 byte per label, 1024 * 3 = 3072 bytes for the image. offset += labels_number img = (np.frombuffer(data, dtype=np.uint8, count=3072, offset=offset) .reshape((3, _CIFAR_IMAGE_SIZE, _CIFAR_IMAGE_SIZE)) .transpose((1, 2, 0)) ) offset += 3072 yield labels, img
def _load_data(path, labels_number=1): """Yields (labels, np_image) tuples.""" with tf.io.gfile.GFile(path, "rb") as f: data = f.read() offset = 0 max_offset = len(data) - 1 while offset < max_offset: labels = np.frombuffer(data, dtype=np.uint8, count=labels_number, offset=offset).reshape((labels_number,)) # 1 byte per label, 1024 * 3 = 3072 bytes for the image. offset += labels_number img = (np.frombuffer(data, dtype=np.uint8, count=3072, offset=offset) .reshape((3, _CIFAR_IMAGE_SIZE, _CIFAR_IMAGE_SIZE)) .transpose((1, 2, 0)) ) offset += 3072 yield labels, img
[ "Yields", "(", "labels", "np_image", ")", "tuples", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/cifar.py#L191-L207
[ "def", "_load_data", "(", "path", ",", "labels_number", "=", "1", ")", ":", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "path", ",", "\"rb\"", ")", "as", "f", ":", "data", "=", "f", ".", "read", "(", ")", "offset", "=", "0", "max_...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Cifar10._split_generators
Returns SplitGenerators.
tensorflow_datasets/image/cifar.py
def _split_generators(self, dl_manager): """Returns SplitGenerators.""" cifar_path = dl_manager.download_and_extract(self._cifar_info.url) cifar_info = self._cifar_info cifar_path = os.path.join(cifar_path, cifar_info.prefix) # Load the label names for label_key, label_file in zip(cifar_info.label_keys, cifar_info.label_files): labels_path = os.path.join(cifar_path, label_file) with tf.io.gfile.GFile(labels_path) as label_f: label_names = [name for name in label_f.read().split("\n") if name] self.info.features[label_key].names = label_names # Define the splits def gen_filenames(filenames): for f in filenames: yield os.path.join(cifar_path, f) return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=10, gen_kwargs={"filepaths": gen_filenames(cifar_info.train_files)}), tfds.core.SplitGenerator( name=tfds.Split.TEST, num_shards=1, gen_kwargs={"filepaths": gen_filenames(cifar_info.test_files)}), ]
def _split_generators(self, dl_manager): """Returns SplitGenerators.""" cifar_path = dl_manager.download_and_extract(self._cifar_info.url) cifar_info = self._cifar_info cifar_path = os.path.join(cifar_path, cifar_info.prefix) # Load the label names for label_key, label_file in zip(cifar_info.label_keys, cifar_info.label_files): labels_path = os.path.join(cifar_path, label_file) with tf.io.gfile.GFile(labels_path) as label_f: label_names = [name for name in label_f.read().split("\n") if name] self.info.features[label_key].names = label_names # Define the splits def gen_filenames(filenames): for f in filenames: yield os.path.join(cifar_path, f) return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=10, gen_kwargs={"filepaths": gen_filenames(cifar_info.train_files)}), tfds.core.SplitGenerator( name=tfds.Split.TEST, num_shards=1, gen_kwargs={"filepaths": gen_filenames(cifar_info.test_files)}), ]
[ "Returns", "SplitGenerators", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/cifar.py#L79-L108
[ "def", "_split_generators", "(", "self", ",", "dl_manager", ")", ":", "cifar_path", "=", "dl_manager", ".", "download_and_extract", "(", "self", ".", "_cifar_info", ".", "url", ")", "cifar_info", "=", "self", ".", "_cifar_info", "cifar_path", "=", "os", ".", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Cifar10._generate_examples
Generate CIFAR examples as dicts. Shared across CIFAR-{10, 100}. Uses self._cifar_info as configuration. Args: filepaths (list[str]): The files to use to generate the data. Yields: The cifar examples, as defined in the dataset info features.
tensorflow_datasets/image/cifar.py
def _generate_examples(self, filepaths): """Generate CIFAR examples as dicts. Shared across CIFAR-{10, 100}. Uses self._cifar_info as configuration. Args: filepaths (list[str]): The files to use to generate the data. Yields: The cifar examples, as defined in the dataset info features. """ label_keys = self._cifar_info.label_keys for path in filepaths: for labels, np_image in _load_data(path, len(label_keys)): row = dict(zip(label_keys, labels)) row["image"] = np_image yield row
def _generate_examples(self, filepaths): """Generate CIFAR examples as dicts. Shared across CIFAR-{10, 100}. Uses self._cifar_info as configuration. Args: filepaths (list[str]): The files to use to generate the data. Yields: The cifar examples, as defined in the dataset info features. """ label_keys = self._cifar_info.label_keys for path in filepaths: for labels, np_image in _load_data(path, len(label_keys)): row = dict(zip(label_keys, labels)) row["image"] = np_image yield row
[ "Generate", "CIFAR", "examples", "as", "dicts", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/cifar.py#L110-L127
[ "def", "_generate_examples", "(", "self", ",", "filepaths", ")", ":", "label_keys", "=", "self", ".", "_cifar_info", ".", "label_keys", "for", "path", "in", "filepaths", ":", "for", "labels", ",", "np_image", "in", "_load_data", "(", "path", ",", "len", "(...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
disallow_positional_args
Requires function to be called using keyword arguments.
tensorflow_datasets/core/api_utils.py
def disallow_positional_args(wrapped=None, allowed=None): """Requires function to be called using keyword arguments.""" # See # https://wrapt.readthedocs.io/en/latest/decorators.html#decorators-with-optional-arguments # for decorator pattern. if wrapped is None: return functools.partial(disallow_positional_args, allowed=allowed) @wrapt.decorator def disallow_positional_args_dec(fn, instance, args, kwargs): ismethod = instance is not None _check_no_positional(fn, args, ismethod, allowed=allowed) _check_required(fn, kwargs) return fn(*args, **kwargs) return disallow_positional_args_dec(wrapped)
def disallow_positional_args(wrapped=None, allowed=None): """Requires function to be called using keyword arguments.""" # See # https://wrapt.readthedocs.io/en/latest/decorators.html#decorators-with-optional-arguments # for decorator pattern. if wrapped is None: return functools.partial(disallow_positional_args, allowed=allowed) @wrapt.decorator def disallow_positional_args_dec(fn, instance, args, kwargs): ismethod = instance is not None _check_no_positional(fn, args, ismethod, allowed=allowed) _check_required(fn, kwargs) return fn(*args, **kwargs) return disallow_positional_args_dec(wrapped)
[ "Requires", "function", "to", "be", "called", "using", "keyword", "arguments", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/api_utils.py#L39-L54
[ "def", "disallow_positional_args", "(", "wrapped", "=", "None", ",", "allowed", "=", "None", ")", ":", "# See", "# https://wrapt.readthedocs.io/en/latest/decorators.html#decorators-with-optional-arguments", "# for decorator pattern.", "if", "wrapped", "is", "None", ":", "retu...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_required_args
Returns arguments of fn with default=REQUIRED_ARG.
tensorflow_datasets/core/api_utils.py
def _required_args(fn): """Returns arguments of fn with default=REQUIRED_ARG.""" spec = getargspec(fn) if not spec.defaults: return [] arg_names = spec.args[-len(spec.defaults):] return [name for name, val in zip(arg_names, spec.defaults) if val is REQUIRED_ARG]
def _required_args(fn): """Returns arguments of fn with default=REQUIRED_ARG.""" spec = getargspec(fn) if not spec.defaults: return [] arg_names = spec.args[-len(spec.defaults):] return [name for name, val in zip(arg_names, spec.defaults) if val is REQUIRED_ARG]
[ "Returns", "arguments", "of", "fn", "with", "default", "=", "REQUIRED_ARG", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/api_utils.py#L67-L75
[ "def", "_required_args", "(", "fn", ")", ":", "spec", "=", "getargspec", "(", "fn", ")", "if", "not", "spec", ".", "defaults", ":", "return", "[", "]", "arg_names", "=", "spec", ".", "args", "[", "-", "len", "(", "spec", ".", "defaults", ")", ":", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
download_gcs_file
Download a file from GCS, optionally to a file.
tensorflow_datasets/core/utils/gcs_utils.py
def download_gcs_file(path, out_fname=None, prefix_filter=None): """Download a file from GCS, optionally to a file.""" url = posixpath.join(GCS_BUCKET, path) if prefix_filter: url += "?prefix=%s" % prefix_filter stream = bool(out_fname) resp = requests.get(url, stream=stream) if not resp.ok: raise ValueError("GCS bucket inaccessible") if out_fname: with tf.io.gfile.GFile(out_fname, "wb") as f: for chunk in resp.iter_content(1024): f.write(chunk) else: return resp.content
def download_gcs_file(path, out_fname=None, prefix_filter=None): """Download a file from GCS, optionally to a file.""" url = posixpath.join(GCS_BUCKET, path) if prefix_filter: url += "?prefix=%s" % prefix_filter stream = bool(out_fname) resp = requests.get(url, stream=stream) if not resp.ok: raise ValueError("GCS bucket inaccessible") if out_fname: with tf.io.gfile.GFile(out_fname, "wb") as f: for chunk in resp.iter_content(1024): f.write(chunk) else: return resp.content
[ "Download", "a", "file", "from", "GCS", "optionally", "to", "a", "file", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/gcs_utils.py#L34-L48
[ "def", "download_gcs_file", "(", "path", ",", "out_fname", "=", "None", ",", "prefix_filter", "=", "None", ")", ":", "url", "=", "posixpath", ".", "join", "(", "GCS_BUCKET", ",", "path", ")", "if", "prefix_filter", ":", "url", "+=", "\"?prefix=%s\"", "%", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
gcs_files
List all files in GCS bucket.
tensorflow_datasets/core/utils/gcs_utils.py
def gcs_files(prefix_filter=None): """List all files in GCS bucket.""" top_level_xml_str = download_gcs_file("", prefix_filter=prefix_filter) xml_root = ElementTree.fromstring(top_level_xml_str) filenames = [el[0].text for el in xml_root if el.tag.endswith("Contents")] return filenames
def gcs_files(prefix_filter=None): """List all files in GCS bucket.""" top_level_xml_str = download_gcs_file("", prefix_filter=prefix_filter) xml_root = ElementTree.fromstring(top_level_xml_str) filenames = [el[0].text for el in xml_root if el.tag.endswith("Contents")] return filenames
[ "List", "all", "files", "in", "GCS", "bucket", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/gcs_utils.py#L52-L57
[ "def", "gcs_files", "(", "prefix_filter", "=", "None", ")", ":", "top_level_xml_str", "=", "download_gcs_file", "(", "\"\"", ",", "prefix_filter", "=", "prefix_filter", ")", "xml_root", "=", "ElementTree", ".", "fromstring", "(", "top_level_xml_str", ")", "filenam...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
gcs_dataset_info_files
Return paths to GCS files in the given dataset directory.
tensorflow_datasets/core/utils/gcs_utils.py
def gcs_dataset_info_files(dataset_dir): """Return paths to GCS files in the given dataset directory.""" prefix = posixpath.join(GCS_DATASET_INFO_DIR, dataset_dir, "") # Filter for this dataset filenames = [el for el in gcs_files(prefix_filter=prefix) if el.startswith(prefix) and len(el) > len(prefix)] return filenames
def gcs_dataset_info_files(dataset_dir): """Return paths to GCS files in the given dataset directory.""" prefix = posixpath.join(GCS_DATASET_INFO_DIR, dataset_dir, "") # Filter for this dataset filenames = [el for el in gcs_files(prefix_filter=prefix) if el.startswith(prefix) and len(el) > len(prefix)] return filenames
[ "Return", "paths", "to", "GCS", "files", "in", "the", "given", "dataset", "directory", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/gcs_utils.py#L60-L66
[ "def", "gcs_dataset_info_files", "(", "dataset_dir", ")", ":", "prefix", "=", "posixpath", ".", "join", "(", "GCS_DATASET_INFO_DIR", ",", "dataset_dir", ",", "\"\"", ")", "# Filter for this dataset", "filenames", "=", "[", "el", "for", "el", "in", "gcs_files", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
is_dataset_on_gcs
If the dataset is available on the GCS bucket gs://tfds-data/datasets.
tensorflow_datasets/core/utils/gcs_utils.py
def is_dataset_on_gcs(dataset_name): """If the dataset is available on the GCS bucket gs://tfds-data/datasets.""" dir_name = posixpath.join(GCS_DATASETS_DIR, dataset_name) return len(gcs_files(prefix_filter=dir_name)) > 2
def is_dataset_on_gcs(dataset_name): """If the dataset is available on the GCS bucket gs://tfds-data/datasets.""" dir_name = posixpath.join(GCS_DATASETS_DIR, dataset_name) return len(gcs_files(prefix_filter=dir_name)) > 2
[ "If", "the", "dataset", "is", "available", "on", "the", "GCS", "bucket", "gs", ":", "//", "tfds", "-", "data", "/", "datasets", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/utils/gcs_utils.py#L69-L72
[ "def", "is_dataset_on_gcs", "(", "dataset_name", ")", ":", "dir_name", "=", "posixpath", ".", "join", "(", "GCS_DATASETS_DIR", ",", "dataset_name", ")", "return", "len", "(", "gcs_files", "(", "prefix_filter", "=", "dir_name", ")", ")", ">", "2" ]
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_run_kaggle_command
Run kaggle command with subprocess.
tensorflow_datasets/core/download/kaggle.py
def _run_kaggle_command(command_args, competition_name): """Run kaggle command with subprocess.""" try: output = sp.check_output(command_args) return tf.compat.as_text(output) except sp.CalledProcessError as err: output = err.output _log_command_output(output, error=True) if output.startswith(b"404"): logging.error(_NOT_FOUND_ERR_MSG, competition_name) raise logging.error(_ERR_MSG, competition_name) raise
def _run_kaggle_command(command_args, competition_name): """Run kaggle command with subprocess.""" try: output = sp.check_output(command_args) return tf.compat.as_text(output) except sp.CalledProcessError as err: output = err.output _log_command_output(output, error=True) if output.startswith(b"404"): logging.error(_NOT_FOUND_ERR_MSG, competition_name) raise logging.error(_ERR_MSG, competition_name) raise
[ "Run", "kaggle", "command", "with", "subprocess", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/download/kaggle.py#L138-L150
[ "def", "_run_kaggle_command", "(", "command_args", ",", "competition_name", ")", ":", "try", ":", "output", "=", "sp", ".", "check_output", "(", "command_args", ")", "return", "tf", ".", "compat", ".", "as_text", "(", "output", ")", "except", "sp", ".", "C...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
KaggleCompetitionDownloader.competition_files
List of competition files.
tensorflow_datasets/core/download/kaggle.py
def competition_files(self): """List of competition files.""" command = [ "kaggle", "datasets" if "/" in self._competition_name else "competitions", "files", "-v", self._competition_name, ] output = _run_kaggle_command(command, self._competition_name) return sorted([ line.split(",")[0] for line in output.split("\n")[1:] if line ])
def competition_files(self): """List of competition files.""" command = [ "kaggle", "datasets" if "/" in self._competition_name else "competitions", "files", "-v", self._competition_name, ] output = _run_kaggle_command(command, self._competition_name) return sorted([ line.split(",")[0] for line in output.split("\n")[1:] if line ])
[ "List", "of", "competition", "files", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/download/kaggle.py#L96-L108
[ "def", "competition_files", "(", "self", ")", ":", "command", "=", "[", "\"kaggle\"", ",", "\"datasets\"", "if", "\"/\"", "in", "self", ".", "_competition_name", "else", "\"competitions\"", ",", "\"files\"", ",", "\"-v\"", ",", "self", ".", "_competition_name", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
KaggleCompetitionDownloader.competition_urls
Returns 'kaggle://' urls.
tensorflow_datasets/core/download/kaggle.py
def competition_urls(self): """Returns 'kaggle://' urls.""" return [ KaggleFile(self._competition_name, fname).to_url() for fname in self.competition_files # pylint: disable=not-an-iterable ]
def competition_urls(self): """Returns 'kaggle://' urls.""" return [ KaggleFile(self._competition_name, fname).to_url() for fname in self.competition_files # pylint: disable=not-an-iterable ]
[ "Returns", "kaggle", ":", "//", "urls", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/download/kaggle.py#L111-L116
[ "def", "competition_urls", "(", "self", ")", ":", "return", "[", "KaggleFile", "(", "self", ".", "_competition_name", ",", "fname", ")", ".", "to_url", "(", ")", "for", "fname", "in", "self", ".", "competition_files", "# pylint: disable=not-an-iterable", "]" ]
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc