【发布时间】:2022-08-27 22:27:59
【问题描述】:
我是 tensorflow(和一般的 python)的新手,我很难理解张量的这些特性。我正在使用tf.keras.utils.image_dataset_from_directory() 来获取图像和标签(类)的数据集。我想使用 filter() 按类过滤 imgaes。就像是,
full_ds = tf.keras.utils.image_dataset_from_directory(
'the_path',
image_size=(SIZE,SIZE),
)
fibrosis_ds = full_ds.filter(lambda x, y: y==0 ) # y == 0 for fibrosis
这给出了错误
值错误:
predicate无效。predicate必须返回一个tf.bool标量张量,但它的返回类型是 NoneTensorSpec()。如果我在 lambda 中打印 y ,则输出为
张量("args_1:0", shape=(None,), dtype=int32)
如果我循环打印
for x, y in full_ds: print(y) break输出是
tf.Tensor([1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1], shape=(32,), dtype=int32)
这是有道理的,因为 image_dataset_from_directory() 的默认值为 32。此数组中的 0 代表纤维化,而 1 是不同的分类(法线)。
如何让 lambda 与 filter() 一起使用。
【问题讨论】:
标签: python tensorflow filter tensorflow-datasets