【问题标题】:Tensorflow Dataset Split Images into TilesTensorflow 数据集将图像分割成图块
【发布时间】:2020-11-28 00:13:37
【问题描述】:

我正在按如下方式加载我的数据集,

ds =  tf.data.Dataset.list_files("/images/*.png")

train_size = int(0.8 * len(ds))
train_ds = ds.take(train_size)
train_ds = train_ds.map(load_sample)
//Splite each image into N smaller tiles
train_ds = train_ds.map(preprocessing_train, num_parallel_calls=AUTOTUNE)
train_ds = train_ds.repeat()
train_ds = train_ds.batch(batch_size)
train_ds = train_ds.prefetch(buffer_size=AUTOTUNE)

图像很大,我需要在推断后进行一些测量,所以我想避免调整它们的大小。我有一个单独的功能,它将拍摄图像并将其拆分为图块,例如。如果图像是 512,512 我想要 256,256 个图块,它返回 2x2x256x256 我想在这 256,256 个图块上训练网络(在推理期间,我还将在更小的图块上运行它,而不是组合起来以获得原始图片)。使用Dataset 如何在map(load_sample)train_ds.map(preprocessing_train) 之前将图像拆分为图块。

【问题讨论】:

  • 您可以使用您的自定义函数调用map
  • @Lescurel 不会返回 2x2 瓦片的返回序列吗?不是图像序列
  • 您可以重塑为 (N, W, H, C) 并在之后调用 unbatch。

标签: python tensorflow tensorflow2.0


【解决方案1】:

您可以重塑平铺张量并调用 unbatch 以消除额外维度。

import tensorflow as tf
a = tf.expand_dims(tf.eye(6),0)
a = tf.concat([a,a,a,a], 0) # getting a dataset of 4 (6,6) "images"
ds = tf.data.Dataset.from_tensor_slices(a) # ds shape is (6,6)
# Mimicking your tiling op
tiling_op = lambda b :tf.reshape(b, (2,2,3,3)) 
ds = ds.map(tiling_op) # ds shape is now (2,2,3,3)
reshape_op = lambda b: tf.reshape(b, (-1,3,3))
ds = ds.map(reshape_op) # ds shape is now (4,3,3)
# getting rid of the tiled dimension
ds = ds.unbatch() # ds shape is now (3,3)

【讨论】:

    猜你喜欢
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2017-05-24
    • 1970-01-01
    • 2023-02-02
    • 2020-10-25
    • 1970-01-01
    相关资源
    最近更新 更多