-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Datasets] Add from_tf
#29591
Merged
Merged
[Datasets] Add from_tf
#29591
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c6af524
Add `from_tf`
bveeramani 6429cc2
Merge branch 'master' into bveeramani/from_tf
bveeramani b6d0d06
Address TODOs
bveeramani 0358c43
Update test
bveeramani 4f8cd39
Update requirements-doc.txt
bveeramani dd2cf8f
Fix doctest
bveeramani 3f8f63b
Update read_api.py
bveeramani 052f4c5
Merge branch 'master' into bveeramani/from_tf
bveeramani 82525e7
Merge remote-tracking branch 'upstream/master' into bveeramani/from_tf
bveeramani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ | |
import pandas | ||
import pyarrow | ||
import pyspark | ||
import tensorflow as tf | ||
|
||
|
||
T = TypeVar("T") | ||
|
@@ -1326,6 +1327,58 @@ def convert(ds: "datasets.Dataset") -> Dataset[ArrowRow]: | |
) | ||
|
||
|
||
@PublicAPI | ||
def from_tf( | ||
dataset: "tf.data.Dataset", | ||
) -> Dataset: | ||
"""Create a dataset from a TensorFlow dataset. | ||
|
||
This function is inefficient. Use it to read small datasets or prototype. | ||
|
||
.. warning:: | ||
If your dataset is large, this function may execute slowly or raise an | ||
out-of-memory error. To avoid issues, read the underyling data with a function | ||
like :meth:`~ray.data.read_images`. | ||
|
||
.. note:: | ||
This function isn't paralellized. It loads the entire dataset into the head | ||
node's memory before moving the data to the distributed object store. | ||
|
||
Examples: | ||
>>> import ray | ||
>>> import tensorflow_datasets as tfds | ||
>>> dataset, _ = tfds.load('cifar10', split=["train", "test"]) | ||
>>> dataset = ray.data.from_tf(dataset) | ||
>>> dataset | ||
Dataset(num_blocks=200, num_rows=50000, schema={id: binary, image: ArrowTensorType(shape=(32, 32, 3), dtype=uint8), label: int64}) | ||
>>> dataset.take(1) # doctest: +SKIP | ||
[{'id': b'train_16399', 'image': array([[[143, 96, 70], | ||
[141, 96, 72], | ||
[135, 93, 72], | ||
..., | ||
[ 96, 37, 19], | ||
[105, 42, 18], | ||
[104, 38, 20]], | ||
|
||
..., | ||
|
||
[[195, 161, 126], | ||
[187, 153, 123], | ||
[186, 151, 128], | ||
..., | ||
[212, 177, 147], | ||
[219, 185, 155], | ||
[221, 187, 157]]], dtype=uint8), 'label': 7}] | ||
|
||
Args: | ||
dataset: A TensorFlow dataset. | ||
|
||
Returns: | ||
A :class:`Dataset` that contains the samples stored in the TensorFlow dataset. | ||
""" # noqa: E501 | ||
return from_items(list(dataset.as_numpy_iterator())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misc. thing to note that I hit before in tests: |
||
|
||
|
||
def _df_to_block(df: "pandas.DataFrame") -> Block[ArrowRow]: | ||
stats = BlockExecStats.builder() | ||
import pyarrow as pa | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we expand this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a method called
Dataset.to_tf
. I prefertensorflow
overtf
, but we should be consistent.What do you think? We could always rename
Dataset.to_tf
toDataset.to_tensorflow
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right... Maybe we can:
from_tensorflow
.to_tensorflow
.to_tf
redirect toto_tensorflow
and eventually deprecate it.But will leave to folks like @clarkzinzow @c21 to make the call here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd actually vote for renaming these to
from_tf_dataset
,to_tf_dataset
,from_torch_dataset
,to_torch_dataset
, etc. This is less ambiguous ("Am I getting a TensorFlow tensor or dataset with this API?"), discoverable, and better matches what the rest of the ecosystem is doing, e.g. HuggingFace and Petastorm.I think that
tf
is a common enough shorthand that we shouldn't expand it totensorflow
, but happy to hear others' opinions, and maybe we can do a quick ecosystem check to verify this.