可以使用sliding window
批处理操作实现tf.data.Dataset
:
例:
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
输出:
(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))