如何使用Tensorflow.data.Dataset API获取随机数据?

时间:2019-07-14 19:45:50

标签: python-3.x tensorflow-datasets

我想获取“洗牌后的image_data,并且它的batch_size在”每次迭代”中为4”。 但是,我只是得到了一些随机的image_augmentations,并且没有随机播放。 如何在每次迭代中获取不同的图像。

这是我制作数据集并返回batch_size图片的代码:

def process_data(file_path):
# Get total number of images and it's contents 
samples, img_contents = resize_imgs(file_path)

# Create Dataset object, then shuffle and batch
Dataset   = tf.data.Dataset.from_tensor_slices(img_contents)
Dataset   = Dataset.batch(BATCH_SIZE)
Dataset   = Dataset.shuffle(samples)

# Create feedable iterator
handle = tf.placeholder(tf.string, shape = [])
feedable_iterator = tf.data.Iterator.from_string_handle(handle, 
Dataset.output_types)

# next_batch_op
next_batch = feedable_iterator.get_next()

# one_shot_handle
one_shot_handle   = Dataset.make_one_shot_iterator().string_handle()

with tf.Session() as sess:
    one_shot_string_handle = sess.run(one_shot_handle)
    feed_dict              = {handle : one_shot_string_handle}
    img_batch              = sess.run(next_batch, feed_dict = feed_dict)

    # Image Augmentaion, flip, brightness, contrast
    img_batch = tf.image.random_flip_left_right(img_batch)    
    img_batch = tf.image.random_brightness(img_batch, max_delta = 0.1)
    img_batch = tf.image.random_contrast(img_batch, lower= 0.9,upper= 1.1)

return img_batch

当我运行此代码时:

# get batch_size images
imgs_batch  = process_data(path)
tmp = []

with tf.Session() as sess:
# Two batch for test, each batch have 4 images
  for _ in range(2):
     tmp.append(sess.run(imgs_batch))

显示图像

plt.imshow(tmp[0][1]) plt.imshow(tmp[1][1]) 我得到以下结果:“两个批次具有相同的4张图像,并且没有随机播放!” Blob:https://stackoverflow.com/6ef6abc5-b851-402a-acd1-5dd491b2b591 blob:https://stackoverflow.com/ef419983-023a-4851-93c0-c6a33b82db70

我希望在每个sess.run(imgs_batch)中都能获得不同的图像,而不仅仅是进行一些图像增强,请帮帮我!

0 个答案:

没有答案