Skip to content

Commit 302db61

Browse files
committed
Removed tfio.Dataset exposure, instead added tfio.IOTensor.to_dataset()
Instead of exposing a tfio.Dataset, I have added a `tfio.IOTensor.to_dataset()` which I think makes much more sense to avoid the confusion. Signed-off-by: Yong Tang <[email protected]>
1 parent e1b65ed commit 302db61

File tree

4 files changed

+46
-37
lines changed

4 files changed

+46
-37
lines changed

tensorflow_io/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@
1818
from __future__ import print_function
1919

2020
from tensorflow_io.core.python.ops.io_tensor import IOTensor
21-
from tensorflow_io.core.python.ops.dataset_ops import Dataset

tensorflow_io/core/python/ops/dataset_ops.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

tensorflow_io/core/python/ops/io_tensor.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import tensorflow as tf
2323
from tensorflow_io.core.python.ops import core_ops
24+
from tensorflow_io.core.python.ops import data_ops
2425
from tensorflow_io.kafka.python.ops.kafka_ops import kafka_ops
2526

2627
class _IOBaseTensor(object):
@@ -261,6 +262,48 @@ def to_tensor(self, **kwargs):
261262
with tf.name_scope(kwargs.get("name", "IOToTensor")):
262263
return self.__getitem__(slice(None, None))
263264

265+
#=============================================================================
266+
# Dataset Conversions
267+
#=============================================================================
268+
269+
def to_dataset(self):
270+
"""Converts this `IOTensor` into a `tf.data.Dataset`.
271+
272+
Example:
273+
274+
```python
275+
```
276+
277+
Args:
278+
279+
Returns:
280+
A `tf.data.Dataset` with value obtained from this `IOTensor`.
281+
"""
282+
class _IOTensorDataset(data_ops.BaseDataset):
283+
"""_IOTensorDataset"""
284+
285+
def __init__(self, dtype, shape, resource, function):
286+
start = 0
287+
stop = shape[0]
288+
capacity = 4096
289+
entry_start = list(range(start, stop, capacity))
290+
entry_stop = entry_start[1:] + [stop]
291+
dataset = data_ops.BaseDataset.from_tensor_slices((
292+
tf.constant(entry_start, tf.int64),
293+
tf.constant(entry_stop, tf.int64))).map(
294+
lambda start, stop: function(
295+
resource, start, stop, 1, dtype=dtype)).apply(
296+
tf.data.experimental.unbatch())
297+
self._dataset = dataset
298+
self._resource = resource
299+
self._function = function
300+
shape = shape[1:]
301+
super(_IOTensorDataset, self).__init__(
302+
self._dataset._variant_tensor, [dtype], [shape]) # pylint: disable=protected-access
303+
304+
return _IOTensorDataset(
305+
self._dtype, self._shape, self._resource, self._function)
306+
264307
class IOIterableTensor(_IOBaseTensor):
265308
"""IOIterableTensor"""
266309

tests/test_audio_eager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
if not (hasattr(tf, "version") and tf.version.VERSION.startswith("2.")):
2525
tf.compat.v1.enable_eager_execution()
2626
import tensorflow_io as tfio # pylint: disable=wrong-import-position
27+
import tensorflow_io.audio as audio_io # pylint: disable=wrong-import-position
2728

2829
audio_path = os.path.join(
2930
os.path.dirname(os.path.abspath(__file__)),
@@ -37,14 +38,14 @@ def test_audio_dataset():
3738

3839
f = lambda x: float(x) / (1 << 15)
3940

40-
audio_dataset = tfio.Dataset.from_audio(audio_path)
41+
audio_dataset = audio_io.WAVDataset(audio_path)
4142
i = 0
4243
for v in audio_dataset:
4344
assert audio_v.audio[i].numpy() == f(v.numpy())
4445
i += 1
4546
assert i == 5760
4647

47-
audio_dataset = tfio.Dataset.from_audio(audio_path).batch(2)
48+
audio_dataset = tfio.IOTensor.from_audio(audio_path).to_dataset().batch(2)
4849
i = 0
4950
for v in audio_dataset:
5051
assert audio_v.audio[i].numpy() == f(v[0].numpy())

0 commit comments

Comments
 (0)