You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

image_dataset_from_directory返回列表而非Dataset对象问题排查

问题解决:image_dataset_from_directory返回list而非tf.data.Dataset

你遇到的问题根源在于设置了subset="both"参数——当这个参数被指定时,image_dataset_from_directory不会返回单个tf.data.Dataset对象,而是返回一个包含训练集和验证集的列表(或元组),这就是你打印类型得到list的原因。

解决方法很简单,只需要将返回值拆分为两个变量,分别对应训练集和验证集:

train_ds, val_ds = img_utils.image_dataset_from_directory(
        directory,
        labels=None,
        label_mode=None,
        shuffle=True,
        seed=123,
        validation_split=0.2,
        subset="both",
        interpolation="bilinear",
        data_format=None,
        verbose=True,
    )

拆分后,train_ds和val_ds都是标准的tf.data.Dataset对象,此时再对它们调用as_numpy_iterator()就不会抛出AttributeError了。你可以打印type(train_ds)验证,会显示类似<class 'tensorflow.python.data.ops.dataset_ops.BatchDataset'>的结果。

内容的提问来源于stack exchange,提问作者Adarsh Nair

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 16:07:02