fiftyone.utils.tf#
TensorFlow utilities.
Functions:
|
Creates a |
|
Creates a |
|
Creates a |
|
Creates a |
|
Creates a |
|
Creates a |
|
Writes the given |
Classes:
|
Class for writing TFRecords to disk. |
|
Base class for sample parsers that ingest |
|
Parser for image classification samples stored as TFRecords. |
|
Parser for samples in TF Object Detection API format. |
Base class for |
|
Importer for TF image classification datasets stored on disk. |
|
Importer for TF detection datasets stored on disk. |
|
|
Base class for |
Exporter that writes an image classification dataset to disk as TFRecords. |
|
Exporter that writes an object detection dataset to disk as TFRecords in the TF Object Detection API format. |
|
|
Base class for sample writers that emit |
Class for generating |
|
Class for generating |
- fiftyone.utils.tf.from_images_dir(images_dir, recursive=True, force_rgb=False, num_parallel_calls=None)#
Creates a
tf.data.Datasetfor the given directory of images.- Parameters:
images_dir – a directory of images
recursive (True) – whether to recursively traverse subdirectories
force_rgb (False) – whether to force convert all images to RGB
num_parallel_calls (None) – the number of samples to read asynchronously in parallel. See https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map for details
- Returns:
a
tf.data.Datasetthat emits decoded images
- fiftyone.utils.tf.from_images_patt(images_patt, force_rgb=False, num_parallel_calls=None)#
Creates a
tf.data.Datasetfor the given glob pattern of images.- Parameters:
images_patt – a glob pattern of images like
/path/to/images/*.jpgforce_rgb (False) – whether to force convert all images to RGB
num_parallel_calls (None) – the number of samples to read asynchronously in parallel. See https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map for details
- Returns:
a
tf.data.Datasetthat emits decoded images
- fiftyone.utils.tf.from_images(image_paths, force_rgb=False, num_parallel_calls=None)#
Creates a
tf.data.Datasetfor the given list of images.- Parameters:
image_paths – an iterable of image paths
force_rgb (False) – whether to force convert all images to RGB
num_parallel_calls (None) – the number of samples to read asynchronously in parallel. See https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map for details
- Returns:
a
tf.data.Datasetthat emits decoded images
- fiftyone.utils.tf.from_image_paths_and_labels(image_paths, labels, force_rgb=False, num_parallel_calls=None)#
Creates a
tf.data.Datasetfor an image classification dataset stored as a list of image paths and labels.- Parameters:
image_paths – an iterable of image paths
labels – an iterable of labels
force_rgb (False) – whether to force convert all images to RGB
num_parallel_calls (None) – the number of samples to read asynchronously in parallel. See https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map for details
- Returns:
a
tf.data.Datasetthat emits(img, label)pairs
- fiftyone.utils.tf.from_image_classification_dir_tree(dataset_dir, force_rgb=False, num_parallel_calls=None)#
Creates a
tf.data.Datasetfor the given image classification dataset directory tree.The directory should have the following format:
<dataset_dir>/ <classA>/ <image1>.<ext> <image2>.<ext> ... <classB>/ <image1>.<ext> <image2>.<ext> ...
- Parameters:
dataset_dir – the dataset directory
force_rgb (False) – whether to force convert all images to RGB
num_parallel_calls (None) – the number of samples to read asynchronously in parallel. See https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map for details
- Returns:
a tuple of
dataset: a
tf.data.Dataset` that emits ``(img, label)pairsclasses: a list of class label strings
- fiftyone.utils.tf.from_tf_records(tf_records_patt, buffer_size=None, num_parallel_reads=None)#
Creates a
tf.data.Datasetfor the TFRecords at the given path(s).- Parameters:
tf_records_patt – the path (or glob pattern of paths) to the TFRecords file(s) to load
buffer_size (None) – an optional buffer size, in bytes, to use when reading the records. Reasonable values are 1-100MBs
num_parallel_reads (None) – an optional number of files to read in parallel. If a negative value is passed, this parameter is set to the number of CPU cores on the host machine. By default, the files are read in series
- Returns:
a
tf.data.Datasetthat emitstf.train.Exampleprotos
- fiftyone.utils.tf.write_tf_records(examples, tf_records_path, num_shards=None)#
Writes the given
tf.train.Exampleprotos to disk as TFRecords.- Parameters:
examples – an iterable that emits
tf.train.Exampleprotostf_records_path – the path to write the
.tfrecordsfile. If sharding is requested-%%05d-of-%%05dis appended to the pathnum_shards (None) – an optional number of shards to split the records into (using a round robin strategy)
- class fiftyone.utils.tf.TFRecordsWriter(tf_records_path, num_shards=None)#
Bases:
objectClass for writing TFRecords to disk.
Example Usage:
with TFRecordsWriter("/path/for/tf.records", num_shards=5) as writer: for tf_example in tf_examples: writer.write(tf_example)
- Parameters:
tf_records_path – the path to write the
.tfrecordsfile. If sharding is requested-%%05d-of-%%05dis appended to the pathnum_shards (None) – an optional number of shards to split the records into (using a round robin strategy). If omitted, no sharding is used
Methods:
write(tf_example)Writres the
tf.train.Exampleproto to disk.- write(tf_example)#
Writres the
tf.train.Exampleproto to disk.- Parameters:
tf_example – a
tf.train.Exampleproto
- class fiftyone.utils.tf.TFRecordSampleParser(force_rgb=False)#
Bases:
LabeledImageSampleParserBase class for sample parsers that ingest
tf.train.Exampleprotos containing labeled images.- Parameters:
force_rgb (False) – whether to force convert all images to RGB
Methods:
Returns the image from the current sample.
Returns the label for the current sample.
Clears the current sample.
Returns the image metadata for the current sample.
Returns the image path for the current sample.
with_sample(sample)Sets the current sample so that subsequent calls to methods of this parser will return information from the given sample.
Attributes:
The current sample.
Whether this parser produces
fiftyone.core.metadata.ImageMetadatainstances for samples that it parses.Whether this parser produces paths to images on disk for samples that it parses.
The
fiftyone.core.labels.Labelclass(es) returned by this parser.- get_image()#
Returns the image from the current sample.
- Returns:
a numpy image
- get_label()#
Returns the label for the current sample.
- Returns:
a
fiftyone.core.labels.Labelinstance, or a dictionary mapping field names tofiftyone.core.labels.Labelinstances, orNoneif the sample is unlabeled
- clear_sample()#
Clears the current sample.
Also clears any cached sample information stored by the parser.
- property current_sample#
The current sample.
- Raises:
ValueError – if there is no current sample
- get_image_metadata()#
Returns the image metadata for the current sample.
- Returns:
a
fiftyone.core.metadata.ImageMetadatainstance
- get_image_path()#
Returns the image path for the current sample.
- Returns:
the path to the image on disk
- property has_image_metadata#
Whether this parser produces
fiftyone.core.metadata.ImageMetadatainstances for samples that it parses.
- property has_image_path#
Whether this parser produces paths to images on disk for samples that it parses.
- property label_cls#
The
fiftyone.core.labels.Labelclass(es) returned by this parser.This can be any of the following:
a
fiftyone.core.labels.Labelclass. In this case, the parser is guaranteed to return labels of this typea list or tuple of
fiftyone.core.labels.Labelclasses. In this case, the parser can produce a single label field of any of these typesa dict mapping keys to
fiftyone.core.labels.Labelclasses. In this case, the parser will return label dictionaries with keys and value-types specified by this dictionary. Not all keys need be present in the imported labelsNone. In this case, the parser makes no guarantees about the labels that it may return
- with_sample(sample)#
Sets the current sample so that subsequent calls to methods of this parser will return information from the given sample.
Guaranteed to call
clear_sample()before setting the current sample.- Parameters:
sample – a sample
- class fiftyone.utils.tf.TFImageClassificationSampleParser(force_rgb=False)#
Bases:
TFRecordSampleParserParser for image classification samples stored as TFRecords.
This implementation supports samples that are
tf.train.Exampleprotos whose features follow the format described in this page.- Parameters:
force_rgb (False) – whether to force convert all images to RGB
Attributes:
The
fiftyone.core.labels.Labelclass(es) returned by this parser.Whether this parser produces paths to images on disk for samples that it parses.
Whether this parser produces
fiftyone.core.metadata.ImageMetadatainstances for samples that it parses.The current sample.
Methods:
Returns the image metadata for the current sample.
Clears the current sample.
Returns the image from the current sample.
Returns the image path for the current sample.
Returns the label for the current sample.
with_sample(sample)Sets the current sample so that subsequent calls to methods of this parser will return information from the given sample.
- property label_cls#
The
fiftyone.core.labels.Labelclass(es) returned by this parser.This can be any of the following:
a
fiftyone.core.labels.Labelclass. In this case, the parser is guaranteed to return labels of this typea list or tuple of
fiftyone.core.labels.Labelclasses. In this case, the parser can produce a single label field of any of these typesa dict mapping keys to
fiftyone.core.labels.Labelclasses. In this case, the parser will return label dictionaries with keys and value-types specified by this dictionary. Not all keys need be present in the imported labelsNone. In this case, the parser makes no guarantees about the labels that it may return
- property has_image_path#
Whether this parser produces paths to images on disk for samples that it parses.
- property has_image_metadata#
Whether this parser produces
fiftyone.core.metadata.ImageMetadatainstances for samples that it parses.
- get_image_metadata()#
Returns the image metadata for the current sample.
- Returns:
a
fiftyone.core.metadata.ImageMetadatainstance
- clear_sample()#
Clears the current sample.
Also clears any cached sample information stored by the parser.
- property current_sample#
The current sample.
- Raises:
ValueError – if there is no current sample
- get_image()#
Returns the image from the current sample.
- Returns:
a numpy image
- get_image_path()#
Returns the image path for the current sample.
- Returns:
the path to the image on disk
- get_label()#
Returns the label for the current sample.
- Returns:
a
fiftyone.core.labels.Labelinstance, or a dictionary mapping field names tofiftyone.core.labels.Labelinstances, orNoneif the sample is unlabeled
- with_sample(sample)#
Sets the current sample so that subsequent calls to methods of this parser will return information from the given sample.
Guaranteed to call
clear_sample()before setting the current sample.- Parameters:
sample – a sample
- class fiftyone.utils.tf.TFObjectDetectionSampleParser(force_rgb=False)#
Bases:
TFRecordSampleParserParser for samples in TF Object Detection API format.
This implementation supports samples that are
tf.train.Exampleprotos whose features follow the format described in this page.- Parameters:
force_rgb (False) – whether to force convert all images to RGB
Attributes:
The
fiftyone.core.labels.Labelclass(es) returned by this parser.Whether this parser produces paths to images on disk for samples that it parses.
Whether this parser produces
fiftyone.core.metadata.ImageMetadatainstances for samples that it parses.The current sample.
Methods:
Returns the image metadata for the current sample.
Clears the current sample.
Returns the image from the current sample.
Returns the image path for the current sample.
Returns the label for the current sample.
with_sample(sample)Sets the current sample so that subsequent calls to methods of this parser will return information from the given sample.
- property label_cls#
The
fiftyone.core.labels.Labelclass(es) returned by this parser.This can be any of the following:
a
fiftyone.core.labels.Labelclass. In this case, the parser is guaranteed to return labels of this typea list or tuple of
fiftyone.core.labels.Labelclasses. In this case, the parser can produce a single label field of any of these typesa dict mapping keys to
fiftyone.core.labels.Labelclasses. In this case, the parser will return label dictionaries with keys and value-types specified by this dictionary. Not all keys need be present in the imported labelsNone. In this case, the parser makes no guarantees about the labels that it may return
- property has_image_path#
Whether this parser produces paths to images on disk for samples that it parses.
- property has_image_metadata#
Whether this parser produces
fiftyone.core.metadata.ImageMetadatainstances for samples that it parses.
- get_image_metadata()#
Returns the image metadata for the current sample.
- Returns:
a
fiftyone.core.metadata.ImageMetadatainstance
- clear_sample()#
Clears the current sample.
Also clears any cached sample information stored by the parser.
- property current_sample#
The current sample.
- Raises:
ValueError – if there is no current sample
- get_image()#
Returns the image from the current sample.
- Returns:
a numpy image
- get_image_path()#
Returns the image path for the current sample.
- Returns:
the path to the image on disk
- get_label()#
Returns the label for the current sample.
- Returns:
a
fiftyone.core.labels.Labelinstance, or a dictionary mapping field names tofiftyone.core.labels.Labelinstances, orNoneif the sample is unlabeled
- with_sample(sample)#
Sets the current sample so that subsequent calls to methods of this parser will return information from the given sample.
Guaranteed to call
clear_sample()before setting the current sample.- Parameters:
sample – a sample
- class fiftyone.utils.tf.TFRecordsLabeledImageDatasetImporter(dataset_dir=None, tf_records_path=None, images_dir=None, image_format=None, force_rgb=False, max_samples=None)#
Bases:
LabeledImageDatasetImporter,ImportPathsMixinBase class for
fiftyone.utils.data.importers.LabeledImageDatasetImporterinstances that importtf.train.Exampleprotos containing labeled images.This class assumes that the input TFRecords only contain the images themselves and not their paths on disk, and, therefore, the images are read in-memory and written to the provided
images_dirduring import.- Parameters:
dataset_dir (None) – the dataset directory. If omitted,
tf_records_pathmust be providedtf_records_path (None) –
an optional parameter that enables explicit control over the location of the TF records. Can be any of the following:
a filename like
"tf.records"or glob pattern like"*.records-*-of-*"specifying the location of the records indataset_diran absolute filepath or glob pattern for the records. In this case,
dataset_dirhas no effect on the location of the records
If None, the parameter will default to
*record*images_dir (None) – the directory in which the images will be written. If not provided, the images will be unpacked into
dataset_dirimage_format (None) – the image format to use to write the images to disk. By default,
fiftyone.config.default_image_extis usedforce_rgb (False) – whether to force convert all images to RGB
max_samples (None) – a maximum number of samples to import. By default, all samples are imported
Attributes:
Whether this importer produces a dataset info dictionary.
Whether this importer produces
fiftyone.core.metadata.ImageMetadatainstances for each image.The
fiftyone.core.labels.Labelclass(es) returned by this importer.Methods:
setup()Performs any necessary setup before importing the first sample in the dataset.
close(*args)Performs any necessary actions after the last sample has been imported.
Returns the dataset info for the dataset.
- property has_dataset_info#
Whether this importer produces a dataset info dictionary.
- property has_image_metadata#
Whether this importer produces
fiftyone.core.metadata.ImageMetadatainstances for each image.
- setup()#
Performs any necessary setup before importing the first sample in the dataset.
This method is called when the importer’s context manager interface is entered,
DatasetImporter.__enter__().
- close(*args)#
Performs any necessary actions after the last sample has been imported.
This method is called when the importer’s context manager interface is exited,
DatasetImporter.__exit__().- Parameters:
*args – the arguments to
DatasetImporter.__exit__()
- get_dataset_info()#
Returns the dataset info for the dataset.
By convention, this method should be called after all samples in the dataset have been imported.
- Returns:
a dict of dataset info
- property label_cls#
The
fiftyone.core.labels.Labelclass(es) returned by this importer.This can be any of the following:
a
fiftyone.core.labels.Labelclass. In this case, the importer is guaranteed to return labels of this typea list or tuple of
fiftyone.core.labels.Labelclasses. In this case, the importer can produce a single label field of any of these typesa dict mapping keys to
fiftyone.core.labels.Labelclasses. In this case, the importer will return label dictionaries with keys and value-types specified by this dictionary. Not all keys need be present in the imported labelsNone. In this case, the importer makes no guarantees about the labels that it may return
- class fiftyone.utils.tf.TFImageClassificationDatasetImporter(dataset_dir=None, tf_records_path=None, images_dir=None, image_format=None, force_rgb=False, max_samples=None)#
Bases:
TFRecordsLabeledImageDatasetImporterImporter for TF image classification datasets stored on disk.
This class assumes that the input TFRecords only contain the images themselves and not their paths on disk, and, therefore, the images are read in-memory and written to the provided
images_dirduring import.See this page for format details.
- Parameters:
dataset_dir (None) – the dataset directory. If omitted,
tf_records_pathmust be providedtf_records_path (None) –
an optional parameter that enables explicit control over the location of the TF records. Can be any of the following:
a filename like
"tf.records"or glob pattern like"*.records-*-of-*"specifying the location of the records indataset_diran absolute filepath or glob pattern for the records. In this case,
dataset_dirhas no effect on the location of the records
If None, the parameter will default to
*record*images_dir (None) – the directory in which the images will be written. If not provided, the images will be unpacked into
dataset_dirimage_format (None) – the image format to use to write the images to disk. By default,
fiftyone.config.default_image_extis usedforce_rgb (False) – whether to force convert all images to RGB
max_samples (None) – a maximum number of samples to import. By default, all samples are imported
Attributes:
The
fiftyone.core.labels.Labelclass(es) returned by this importer.Whether this importer produces a dataset info dictionary.
Whether this importer produces
fiftyone.core.metadata.ImageMetadatainstances for each image.Methods:
close(*args)Performs any necessary actions after the last sample has been imported.
Returns the dataset info for the dataset.
setup()Performs any necessary setup before importing the first sample in the dataset.
- property label_cls#
The
fiftyone.core.labels.Labelclass(es) returned by this importer.This can be any of the following:
a
fiftyone.core.labels.Labelclass. In this case, the importer is guaranteed to return labels of this typea list or tuple of
fiftyone.core.labels.Labelclasses. In this case, the importer can produce a single label field of any of these typesa dict mapping keys to
fiftyone.core.labels.Labelclasses. In this case, the importer will return label dictionaries with keys and value-types specified by this dictionary. Not all keys need be present in the imported labelsNone. In this case, the importer makes no guarantees about the labels that it may return
- close(*args)#
Performs any necessary actions after the last sample has been imported.
This method is called when the importer’s context manager interface is exited,
DatasetImporter.__exit__().- Parameters:
*args – the arguments to
DatasetImporter.__exit__()
- get_dataset_info()#
Returns the dataset info for the dataset.
By convention, this method should be called after all samples in the dataset have been imported.
- Returns:
a dict of dataset info
- property has_dataset_info#
Whether this importer produces a dataset info dictionary.
- property has_image_metadata#
Whether this importer produces
fiftyone.core.metadata.ImageMetadatainstances for each image.
- setup()#
Performs any necessary setup before importing the first sample in the dataset.
This method is called when the importer’s context manager interface is entered,
DatasetImporter.__enter__().
- class fiftyone.utils.tf.TFObjectDetectionDatasetImporter(dataset_dir=None, tf_records_path=None, images_dir=None, image_format=None, force_rgb=False, max_samples=None)#
Bases:
TFRecordsLabeledImageDatasetImporterImporter for TF detection datasets stored on disk.
This class assumes that the input TFRecords only contain the images themselves and not their paths on disk, and, therefore, the images are read in-memory and written to the provided
images_dirduring import.See this page for format details.
- Parameters:
dataset_dir (None) – the dataset directory. If omitted,
tf_records_pathmust be providedtf_records_path (None) –
an optional parameter that enables explicit control over the location of the TF records. Can be any of the following:
a filename like
"tf.records"or glob pattern like"*.records-*-of-*"specifying the location of the records indataset_diran absolute filepath or glob pattern for the records. In this case,
dataset_dirhas no effect on the location of the records
If None, the parameter will default to
*record*images_dir (None) – the directory in which the images will be written. If not provided, the images will be unpacked into
dataset_dirimage_format (None) – the image format to use to write the images to disk. By default,
fiftyone.config.default_image_extis usedforce_rgb (False) – whether to force convert all images to RGB
max_samples (None) – a maximum number of samples to import. By default, all samples are imported
Attributes:
The
fiftyone.core.labels.Labelclass(es) returned by this importer.Whether this importer produces a dataset info dictionary.
Whether this importer produces
fiftyone.core.metadata.ImageMetadatainstances for each image.Methods:
close(*args)Performs any necessary actions after the last sample has been imported.
Returns the dataset info for the dataset.
setup()Performs any necessary setup before importing the first sample in the dataset.
- property label_cls#
The
fiftyone.core.labels.Labelclass(es) returned by this importer.This can be any of the following:
a
fiftyone.core.labels.Labelclass. In this case, the importer is guaranteed to return labels of this typea list or tuple of
fiftyone.core.labels.Labelclasses. In this case, the importer can produce a single label field of any of these typesa dict mapping keys to
fiftyone.core.labels.Labelclasses. In this case, the importer will return label dictionaries with keys and value-types specified by this dictionary. Not all keys need be present in the imported labelsNone. In this case, the importer makes no guarantees about the labels that it may return
- close(*args)#
Performs any necessary actions after the last sample has been imported.
This method is called when the importer’s context manager interface is exited,
DatasetImporter.__exit__().- Parameters:
*args – the arguments to
DatasetImporter.__exit__()
- get_dataset_info()#
Returns the dataset info for the dataset.
By convention, this method should be called after all samples in the dataset have been imported.
- Returns:
a dict of dataset info
- property has_dataset_info#
Whether this importer produces a dataset info dictionary.
- property has_image_metadata#
Whether this importer produces
fiftyone.core.metadata.ImageMetadatainstances for each image.
- setup()#
Performs any necessary setup before importing the first sample in the dataset.
This method is called when the importer’s context manager interface is entered,
DatasetImporter.__enter__().
- class fiftyone.utils.tf.TFRecordsDatasetExporter(export_dir=None, tf_records_path=None, num_shards=None, image_format=None, force_rgb=False)#
Bases:
LabeledImageDatasetExporter,ExportPathsMixinBase class for
fiftyone.utils.data.exporters.LabeledImageDatasetExporterinstances that export labeled images as TFRecords datasets on disk.- Parameters:
export_dir (None) – the directory to write the export. This has no effect if
tf_records_pathis an absolute pathtf_records_path (None) –
an optional parameter that enables explicit control over the location of the TF records. Can be any of the following:
a filename like
"tf.records"specifying the location of the records inexport_diran absolute filepath for the records. In this case,
export_dirhas no effect on the location of the records
If None, the parameter will default to
tf.recordsnum_shards (None) – an optional number of shards to split the records into (using a round robin strategy). If specified,
-%%05d-of-%%05dis appended to the records pathimage_format (None) – the image format to use when writing in-memory images to disk. By default,
fiftyone.config.default_image_extis usedforce_rgb (False) – whether to force convert all images to RGB
Attributes:
Whether this exporter requires
fiftyone.core.metadata.ImageMetadatainstances for each sample being exported.The
fiftyone.core.labels.Labelclass(es) exported by this exporter.Methods:
setup()Performs any necessary setup before exporting the first sample in the dataset.
export_sample(image_or_path, label[, metadata])Exports the given sample to the dataset.
close(*args)Performs any necessary actions after the last sample has been exported.
log_collection(sample_collection)Logs any relevant information about the
fiftyone.core.collections.SampleCollectionwhose samples will be exported.- property requires_image_metadata#
Whether this exporter requires
fiftyone.core.metadata.ImageMetadatainstances for each sample being exported.
- setup()#
Performs any necessary setup before exporting the first sample in the dataset.
This method is called when the exporter’s context manager interface is entered,
DatasetExporter.__enter__().
- export_sample(image_or_path, label, metadata=None)#
Exports the given sample to the dataset.
- Parameters:
image_or_path – an image or the path to the image on disk
label – an instance of
label_cls(), or a dictionary mapping field names tofiftyone.core.labels.Labelinstances, orNoneif the sample is unlabeledmetadata (None) – a
fiftyone.core.metadata.ImageMetadatainstance for the sample. Only required whenrequires_image_metadata()isTrue
- close(*args)#
Performs any necessary actions after the last sample has been exported.
This method is called when the exporter’s context manager interface is exited,
DatasetExporter.__exit__().- Parameters:
*args – the arguments to
DatasetExporter.__exit__()
- property label_cls#
The
fiftyone.core.labels.Labelclass(es) exported by this exporter.This can be any of the following:
a
fiftyone.core.labels.Labelclass. In this case, the exporter directly exports labels of this typea list or tuple of
fiftyone.core.labels.Labelclasses. In this case, the exporter can export a single label field of any of these typesa dict mapping keys to
fiftyone.core.labels.Labelclasses. In this case, the exporter can handle label dictionaries with value-types specified by this dictionary. Not all keys need be present in the exported label dictsNone. In this case, the exporter makes no guarantees about the labels that it can export
- log_collection(sample_collection)#
Logs any relevant information about the
fiftyone.core.collections.SampleCollectionwhose samples will be exported.Subclasses can optionally implement this method if their export format can record information such as the
fiftyone.core.collections.SampleCollection.info()of the collection being exported.By convention, this method must be optional; i.e., if it is not called before the first call to
export_sample(), then the exporter must make do without any information about thefiftyone.core.collections.SampleCollection(which may not be available, for example, if the samples being exported are not stored in a collection).- Parameters:
sample_collection – the
fiftyone.core.collections.SampleCollectionwhose samples will be exported
- class fiftyone.utils.tf.TFImageClassificationDatasetExporter(export_dir=None, tf_records_path=None, num_shards=None, image_format=None, force_rgb=False)#
Bases:
TFRecordsDatasetExporterExporter that writes an image classification dataset to disk as TFRecords.
See this page for format details.
- Parameters:
export_dir (None) – the directory to write the export. Can be omitted if
tf_records_pathis providedtf_records_path (None) –
an optional parameter that enables explicit control over the location of the TF records. Can be any of the following:
a filename like
"tf.records"specifying the location of the records inexport_diran absolute filepath for the records. In this case,
export_dirhas no effect on the location of the records
If None, the parameter will default to
tf.recordsnum_shards (None) – an optional number of shards to split the records into (using a round robin strategy). If specified,
-%%05d-of-%%05dis appended to the records pathimage_format (None) – the image format to use when writing in-memory images to disk. By default,
fiftyone.config.default_image_extis usedforce_rgb (False) – whether to force convert all images to RGB
Attributes:
The
fiftyone.core.labels.Labelclass(es) exported by this exporter.Whether this exporter requires
fiftyone.core.metadata.ImageMetadatainstances for each sample being exported.Methods:
close(*args)Performs any necessary actions after the last sample has been exported.
export_sample(image_or_path, label[, metadata])Exports the given sample to the dataset.
log_collection(sample_collection)Logs any relevant information about the
fiftyone.core.collections.SampleCollectionwhose samples will be exported.setup()Performs any necessary setup before exporting the first sample in the dataset.
- property label_cls#
The
fiftyone.core.labels.Labelclass(es) exported by this exporter.This can be any of the following:
a
fiftyone.core.labels.Labelclass. In this case, the exporter directly exports labels of this typea list or tuple of
fiftyone.core.labels.Labelclasses. In this case, the exporter can export a single label field of any of these typesa dict mapping keys to
fiftyone.core.labels.Labelclasses. In this case, the exporter can handle label dictionaries with value-types specified by this dictionary. Not all keys need be present in the exported label dictsNone. In this case, the exporter makes no guarantees about the labels that it can export
- close(*args)#
Performs any necessary actions after the last sample has been exported.
This method is called when the exporter’s context manager interface is exited,
DatasetExporter.__exit__().- Parameters:
*args – the arguments to
DatasetExporter.__exit__()
- export_sample(image_or_path, label, metadata=None)#
Exports the given sample to the dataset.
- Parameters:
image_or_path – an image or the path to the image on disk
label – an instance of
label_cls(), or a dictionary mapping field names tofiftyone.core.labels.Labelinstances, orNoneif the sample is unlabeledmetadata (None) – a
fiftyone.core.metadata.ImageMetadatainstance for the sample. Only required whenrequires_image_metadata()isTrue
- log_collection(sample_collection)#
Logs any relevant information about the
fiftyone.core.collections.SampleCollectionwhose samples will be exported.Subclasses can optionally implement this method if their export format can record information such as the
fiftyone.core.collections.SampleCollection.info()of the collection being exported.By convention, this method must be optional; i.e., if it is not called before the first call to
export_sample(), then the exporter must make do without any information about thefiftyone.core.collections.SampleCollection(which may not be available, for example, if the samples being exported are not stored in a collection).- Parameters:
sample_collection – the
fiftyone.core.collections.SampleCollectionwhose samples will be exported
- property requires_image_metadata#
Whether this exporter requires
fiftyone.core.metadata.ImageMetadatainstances for each sample being exported.
- setup()#
Performs any necessary setup before exporting the first sample in the dataset.
This method is called when the exporter’s context manager interface is entered,
DatasetExporter.__enter__().
- class fiftyone.utils.tf.TFObjectDetectionDatasetExporter(export_dir=None, tf_records_path=None, num_shards=None, image_format=None, force_rgb=False, classes=None)#
Bases:
TFRecordsDatasetExporterExporter that writes an object detection dataset to disk as TFRecords in the TF Object Detection API format.
See this page for format details.
- Parameters:
export_dir (None) – the directory to write the export. Can be omitted if
tf_records_pathis providedtf_records_path (None) –
an optional parameter that enables explicit control over the location of the TF records. Can be any of the following:
a filename like
"tf.records"specifying the location of the records inexport_diran absolute filepath for the records. In this case,
export_dirhas no effect on the location of the records
If None, the parameter will default to
tf.recordsnum_shards (None) – an optional number of shards to split the records into (using a round robin strategy). If specified,
-%%05d-of-%%05dis appended to the records pathimage_format (None) – the image format to use when writing in-memory images to disk. By default,
fiftyone.config.default_image_extis usedforce_rgb (False) – whether to force convert all images to RGB
classes (None) – the list of possible class labels
Attributes:
The
fiftyone.core.labels.Labelclass(es) exported by this exporter.Whether this exporter requires
fiftyone.core.metadata.ImageMetadatainstances for each sample being exported.Methods:
close(*args)Performs any necessary actions after the last sample has been exported.
export_sample(image_or_path, label[, metadata])Exports the given sample to the dataset.
log_collection(sample_collection)Logs any relevant information about the
fiftyone.core.collections.SampleCollectionwhose samples will be exported.setup()Performs any necessary setup before exporting the first sample in the dataset.
- property label_cls#
The
fiftyone.core.labels.Labelclass(es) exported by this exporter.This can be any of the following:
a
fiftyone.core.labels.Labelclass. In this case, the exporter directly exports labels of this typea list or tuple of
fiftyone.core.labels.Labelclasses. In this case, the exporter can export a single label field of any of these typesa dict mapping keys to
fiftyone.core.labels.Labelclasses. In this case, the exporter can handle label dictionaries with value-types specified by this dictionary. Not all keys need be present in the exported label dictsNone. In this case, the exporter makes no guarantees about the labels that it can export
- close(*args)#
Performs any necessary actions after the last sample has been exported.
This method is called when the exporter’s context manager interface is exited,
DatasetExporter.__exit__().- Parameters:
*args – the arguments to
DatasetExporter.__exit__()
- export_sample(image_or_path, label, metadata=None)#
Exports the given sample to the dataset.
- Parameters:
image_or_path – an image or the path to the image on disk
label – an instance of
label_cls(), or a dictionary mapping field names tofiftyone.core.labels.Labelinstances, orNoneif the sample is unlabeledmetadata (None) – a
fiftyone.core.metadata.ImageMetadatainstance for the sample. Only required whenrequires_image_metadata()isTrue
- log_collection(sample_collection)#
Logs any relevant information about the
fiftyone.core.collections.SampleCollectionwhose samples will be exported.Subclasses can optionally implement this method if their export format can record information such as the
fiftyone.core.collections.SampleCollection.info()of the collection being exported.By convention, this method must be optional; i.e., if it is not called before the first call to
export_sample(), then the exporter must make do without any information about thefiftyone.core.collections.SampleCollection(which may not be available, for example, if the samples being exported are not stored in a collection).- Parameters:
sample_collection – the
fiftyone.core.collections.SampleCollectionwhose samples will be exported
- property requires_image_metadata#
Whether this exporter requires
fiftyone.core.metadata.ImageMetadatainstances for each sample being exported.
- setup()#
Performs any necessary setup before exporting the first sample in the dataset.
This method is called when the exporter’s context manager interface is entered,
DatasetExporter.__enter__().
- class fiftyone.utils.tf.TFExampleGenerator(force_rgb=False)#
Bases:
objectBase class for sample writers that emit
tf.train.Exampleprotos.- Parameters:
force_rgb (False) – whether to force convert all images to RGB
Methods:
make_tf_example(image_or_path, label, *args, ...)Makes a
tf.train.Examplefor the given data.- make_tf_example(image_or_path, label, *args, **kwargs)#
Makes a
tf.train.Examplefor the given data.- Parameters:
image_or_path – an image or the path to the image on disk
label – a
fiftyone.core.labels.Label*args – subclass-specific positional arguments
**kwargs – subclass-specific keyword arguments
- Returns:
a
tf.train.Example
- class fiftyone.utils.tf.TFImageClassificationExampleGenerator(force_rgb=False)#
Bases:
TFExampleGeneratorClass for generating
tf.train.Exampleprotos for samples in TF image classification format.See this page for format details.
- Parameters:
force_rgb (False) – whether to force convert all images to RGB
Methods:
make_tf_example(image_or_path, classification)Makes a
tf.train.Examplefor the given data.- make_tf_example(image_or_path, classification, filename=None)#
Makes a
tf.train.Examplefor the given data.- Parameters:
image_or_path – an image or the path to the image on disk
classification – a
fiftyone.core.labels.Classificationinstance, orNonefilename (None) – a filename for the image. Required when
image_or_pathis an image, in which case the extension of this filename determines the encoding used. Ifimage_or_pathis the path to an image, this is optional; by default, the basename ofimage_pathis used
- Returns:
a
tf.train.Example
- class fiftyone.utils.tf.TFObjectDetectionExampleGenerator(force_rgb=False, classes=None)#
Bases:
TFExampleGeneratorClass for generating
tf.train.Exampleprotos for samples in TF Object Detection API format.See this page for format details.
- Parameters:
force_rgb (False) – whether to force convert all images to RGB
classes (None) – the list of possible class labels
Methods:
make_tf_example(image_or_path, detections[, ...])Makes a
tf.train.Examplefor the given data.- make_tf_example(image_or_path, detections, filename=None)#
Makes a
tf.train.Examplefor the given data.- Parameters:
image_or_path – an image or the path to the image on disk
detections – a
fiftyone.core.labels.Detectionsinstance, orNonefilename (None) – a filename for the image. Required when
image_or_pathis an image, in which case the extension of this filename determines the encoding used. Ifimage_or_pathis the path to an image, this is optional; by default, the basename ofimage_pathis used
- Returns:
a
tf.train.Example