繁体   English   中英

TensorFlow 数据集将平铺图像添加到批量维度

[英]TensorFlow Dataset adding tiled images to batch dimension

如果我有一个已创建到图块中的图像数据集,那么将图块维度与批处理维度相结合的最佳方法是什么?

例如,我的输入文件的形状为 (300,300,3) 一个具有 300x300 像素的典型 RGB 图像。

我进行了预处理并创建了一个创建新形状的图块数据集: (?,100,128,128,3) 所以我从原始图像创建了 100 个大小为 30x30 的图块,并将每个图块重新整形为 128x128 像素,然后缓存数据集并创建了一个批量与维度?

现在我想将瓷砖组合成批次维度并获得以下形状:(?,128,128,3)

我已经尝试将数据集映射到这个 function:

def reshape_image(image_batch):

    return tf.reshape(image_batch, (-1,128,128,3))

但这似乎不起作用,因为它导致迭代器挂在这个调用上:

image_test = next(iter(image_ds))

正如我所想,如果您熟悉 Tensorflow 操作,答案就相当简单,希望这个问题不会太令人困惑,它可以帮助那里的人。

#load/preprocess images from paths
image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)
#split images into tiles so (X,Y,C) -> (N,X,Y,C) where N is the number of tiles
image_ds = image_ds.map(split_image, num_parallel_calls=AUTOTUNE)
#resize tiled images from 30x30 to 128x128, implementation doesn't really matter
image_ds = image_ds.map(resize_image, num_parallel_calls=AUTOTUNE)

#finally the answer!! use 'flat_map', 'unstack', and 'from_tensor_slices'
#tiled_images is of shape (N,X,Y,C)

def flat_map_impl(tiled_images):

  #You return a new Dataset
  #Unstack by default creates a list of tensors based on the first dimension
  #therefore tf.unstack(tiled_images) is a list of size N with (X,Y,C) shaped elements
  #finally from_tensor_slices creates a new dataset where each element is of shape (X,Y,C)
  return tf.data.Dataset.from_tensor_slices(tf.unstack(tiled_images))

#call flat_map_impl with flat_map on the dataset
image_ds = image_ds.flat_map(flat_map_impl)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM