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

TensorFlow加载本地无标签图像用于自编码器训练的问题求解

问题原因

你遇到的报错核心是两点:

  1. Keras的fit接口在传入生成器/tf.data数据集作为输入时,要求输入对象直接返回(input, target)配对数据,不支持分开传入x和y两个独立迭代器,所以才会抛出'y' argument is not supported的错误。
  2. flow_from_directory默认要求传入的路径下存在子文件夹(按类别划分),直接传入图像所在的low/high路径时,找不到下一级子文件夹就会识别不到图像。

推荐解决方案(用image_dataset_from_directory+tf.data拼接)

这个方案实现最简单,性能也更好,适配你现有的文件夹结构:

步骤1:分别加载low、high的训练/验证集

注意固定随机种子,保证两个数据集的拆分、打乱顺序完全一致,确保同名的高低分辨率图是配对的:

import tensorflow as tf
from tensorflow import keras

# 统一配置参数
IMG_SIZE = (480, 270)
BATCH_SIZE = 8
VAL_SPLIT = 0.2
SEED = 42 # 固定种子保证两个数据集拆分对齐

# 加载低分辨率图像数据集
low_train = keras.utils.image_dataset_from_directory(
    "path/to/data/low",
    labels=None,
    color_mode="rgb",
    batch_size=BATCH_SIZE,
    image_size=IMG_SIZE,
    shuffle=True,
    validation_split=VAL_SPLIT,
    subset="training",
    seed=SEED
)
low_val = keras.utils.image_dataset_from_directory(
    "path/to/data/low",
    labels=None,
    color_mode="rgb",
    batch_size=BATCH_SIZE,
    image_size=IMG_SIZE,
    shuffle=True,
    validation_split=VAL_SPLIT,
    subset="validation",
    seed=SEED
)

# 加载高分辨率图像数据集,参数和低分辨率完全一致
high_train = keras.utils.image_dataset_from_directory(
    "path/to/data/high",
    labels=None,
    color_mode="rgb",
    batch_size=BATCH_SIZE,
    image_size=IMG_SIZE,
    shuffle=True,
    validation_split=VAL_SPLIT,
    subset="training",
    seed=SEED
)
high_val = keras.utils.image_dataset_from_directory(
    "path/to/data/high",
    labels=None,
    color_mode="rgb",
    batch_size=BATCH_SIZE,
    image_size=IMG_SIZE,
    shuffle=True,
    validation_split=VAL_SPLIT,
    subset="validation",
    seed=SEED
)

步骤2:拼接为配对数据集并归一化

用tf.data.Dataset.zip把高低分辨率数据集拼接成(low_img, high_img)的配对结构,再做归一化处理:

# 拼接训练、验证集
train_ds = tf.data.Dataset.zip((low_train, high_train))
val_ds = tf.data.Dataset.zip((low_val, high_val))

# 归一化到0-1范围,和之前ImageDataGenerator的rescale逻辑一致
def preprocess(low_img, high_img):
    return low_img / 255.0, high_img / 255.0

# 加预取逻辑提升训练性能
train_ds = train_ds.map(preprocess, num_parallel_calls=tf.data.AUTOTUNE).prefetch(tf.data.AUTOTUNE)
val_ds = val_ds.map(preprocess, num_parallel_calls=tf.data.AUTOTUNE).prefetch(tf.data.AUTOTUNE)

步骤3:修改训练代码

直接传入拼接好的数据集即可,不需要额外传y参数,也不用重复指定batch_size:

model.fit(
    train_ds,
    epochs=15,
    validation_data=val_ds
)

备选方案(用ImageDataGenerator实现)

如果你坚持要用ImageDataGenerator,可以写一个简单的配对生成器:

def paired_gen(low_iter, high_iter):
    while True:
        low_batch = low_iter.next()
        high_batch = high_iter.next()
        yield low_batch, high_batch

# 按你之前的写法生成low、high的迭代器,注意class_mode设为None,两个迭代器的batch_size、seed、shuffle参数完全一致
train_gen = paired_gen(train_low_iterator, train_high_iterator)
val_gen = paired_gen(validation_low_iterator, validation_high_iterator)

# 训练时需要指定每轮的步数,因为自定义生成器是无限循环的
model.fit(
    train_gen,
    epochs=15,
    steps_per_epoch=len(train_low_iterator),
    validation_data=val_gen,
    validation_steps=len(validation_low_iterator)
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 07:54:04