Sliding window of a batch in Tensorflow using Dataset API
Answering both the original post and Answering @cabbage_soup's comment to vijay's response:
To achieve an efficient sliding window the following code can be used.
data = data.window(size=batch_size, stride=1, shift=1, drop_remainder=True )
data = data.interleave( lambda *window: tf.data.Dataset.zip(tuple([w.batch(batch_size) for w in window])), cycle_length=10, block_length=10 ,num_parallel_calls=4 )
Interleave is used instead of flat_map as it allows processing to be done in parallel during this window transformation.
Refer to the documentation to choose values for cycle_length, block_length and num_parallel_calls that are appropriate for your hardware and data.
Can be achieved using sliding window
batch operation for tf.data.Dataset
:
Example:
from tensorflow.contrib.data.python.ops import sliding
imgs = tf.constant(['img0','img1', 'img2','img3', 'img4','img5', 'img6', 'img7'])
labels = tf.constant([0, 0, 0, 1, 1, 1, 0, 0])
# create TensorFlow Dataset object
data = tf.data.Dataset.from_tensor_slices((imgs, labels))
# sliding window batch
window = 4
stride = 1
data = data.apply(sliding.sliding_window_batch(window, stride))
# create TensorFlow Iterator object
iterator = tf.data.Iterator.from_structure(data.output_types,data.output_shapes)
next_element = iterator.get_next()
# create initialization ops
init_op = iterator.make_initializer(data)
with tf.Session() as sess:
# initialize the iterator on the data
sess.run(init_op)
while True:
try:
elem = sess.run(next_element)
print(elem)
except tf.errors.OutOfRangeError:
print("End of dataset.")
break
Output:
(array([b'img0', b'img1', b'img2', b'img3'], dtype=object), array([0, 0, 0, 1], dtype=int32))
(array([b'img1', b'img2', b'img3', b'img4'], dtype=object), array([0, 0, 1, 1], dtype=int32))
(array([b'img2', b'img3', b'img4', b'img5'], dtype=object), array([0, 1, 1, 1], dtype=int32))
(array([b'img3', b'img4', b'img5', b'img6'], dtype=object), array([1, 1, 1, 0], dtype=int32))
(array([b'img4', b'img5', b'img6', b'img7'], dtype=object), array([1, 1, 0, 0], dtype=int32))
With tensorflow >= 2.1, it is possible to use the window(), flat_map() and batch() functions to get desired results.
Example:
## Sample data list
x_train = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90]
## Constants
batch_size = 10
shift_window_size = 1
## Create tensor slices
train_d = tf.data.Dataset.from_tensor_slices(x_train)
## Create dataset of datasets with a specific window and shift size
train_d = train_d.window(size=batch_size,shift=shift_window_size, drop_remainder=True)
## Define a function to create a flat dataset from the dataset of datasets
def create_seqeunce_ds(chunk):
return chunk.batch(batch_size, drop_remainder=True)
## Create a dataset using a map with mapping function defined above
train_d = train_d.flat_map(create_seqeunce_ds)
## Check the contents
for item in train_d:
print(item)
Output:
tf.Tensor([ 1 2 3 4 5 6 7 8 9 10], shape=(10,), dtype=int32)
tf.Tensor([ 2 3 4 5 6 7 8 9 10 20], shape=(10,), dtype=int32)
tf.Tensor([ 3 4 5 6 7 8 9 10 20 30], shape=(10,), dtype=int32)
tf.Tensor([ 4 5 6 7 8 9 10 20 30 40], shape=(10,), dtype=int32)
tf.Tensor([ 5 6 7 8 9 10 20 30 40 50], shape=(10,), dtype=int32)
tf.Tensor([ 6 7 8 9 10 20 30 40 50 60], shape=(10,), dtype=int32)
tf.Tensor([ 7 8 9 10 20 30 40 50 60 70], shape=(10,), dtype=int32)
tf.Tensor([ 8 9 10 20 30 40 50 60 70 80], shape=(10,), dtype=int32)
tf.Tensor([ 9 10 20 30 40 50 60 70 80 90], shape=(10,), dtype=int32)
More details can be found here: TF Data Guide