使用tensorflow生成discreate数据集的.zip



我有两个虚拟图像数据集,第一个数据集有三个元素,第二个数据集有6个元素。

like first dataset images name = [1.png, 2.png, 3.png]

第二个数据集图像名称= [1_1.png, 1_1.png, 2_1.png, 2_1.png, 3_1.png, 3_2.png]

我试着弄清楚,如何使这些数据集的zip以这样一种方式映射这两个数据集[1.png必须映射1_1.png和1_1.png], [2.png必须映射2_1.png和2_2.png]等等。这可能吗?这是我试图实现的代码,但我真的不知道如何做到这一点。


import os
import tensorflow as tf
X=tf.data.Dataset.list_files('D:/test/clear/*.png',shuffle=False)
Y=tf.data.Dataset.list_files('D:/test/haze/*.png',shuffle=False)
paired=tf.data.Dataset.zip((X,Y))
for x in paired:
print(x)
结果

(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\1.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\1_1.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\2.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\1_2.png'>)

Results I want

(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\1.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\1_1.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\1.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\1_2.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\2.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\2_1.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\2.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\2_2.png'>)

(这是我在StackOverflow上写的第一个答案,所以我希望它会足够清晰,没有太多的格式错误。)

我现在能想到的最简单的方法是复制x的文件名。

这些是我使用的虚拟文件路径列表:

files_x = ["D:\test\clear\1.png", "D:\test\clear\2.png", "D:\test\clear\3.png"] 
files_y = ["D:\test\haze\1_1.png", "D:\test\haze\1_2.png",  "D:\test\haze\2_1.png", "D:\test\haze\2_2.png", "D:\test\haze\3_1.png", "D:\test\haze\3_2.png"]

首先,根据文件路径列表创建一个数据集。

ds_files_x_dup = tf.data.Dataset.from_tensor_slices(files_x)

然后可以通过应用tf来重复这些元素。通过map函数对每个元素重复。然而,这导致重复的元素被分组为一个样本。为了得到一个每个样本只有一个元素的数据集,你必须在数据集上应用flat_map。

ds_files_x_dup = ds_files_x_dup.map(lambda x: tf.repeat(x,2))
ds_files_x_dup = ds_files_x_dup.flat_map(lambda x: tf.data.Dataset.from_tensor_slices(x))

现在您只需要基于files_y:

创建数据集
ds_files_y = tf.data.Dataset.from_tensor_slices(files_y)

并将两者压缩在一起:

paired = tf.data.Dataset.zip((ds_files_x_dup, ds_files_y))

则pair的元素为:

(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\1.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\1_1.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\1.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\1_2.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\2.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\2_1.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\2.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\2_2.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\3.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\3_1.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\clear\3.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'D:\test\haze\3_2.png'>)

最新更新