首页 > 解决方案 > 如何使用 image_dataset_from_directory 为 AutoKeras ImageRegressor 构建 tf.Data.Dataset?

问题描述

如何为 AutoKeras ImageRegressor ( https://autokeras.com/tutorial/image_regression/ ) 构建 tf.Data.Dataset?

我使用的一些步骤来自页面:https ://autokeras.com/tutorial/load/

我可以通过这种方式从单个文件夹中加载图像:

batch_size = 32
img_height = 180
img_width = 180

data_dir = "/data"

x_train = ak.image_dataset_from_directory(
    data_dir,
    validation_split=0,
    subset="training",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

然后我可以使用 numpy 加载相应的响应值(通过一些额外的处理来获得一个 numpy 数组,每个图像都有 1 个值):

y_train = np.genfromtxt("data.txt", delimiter='')

然后我可以使用 take 和 skip 分割生成的数据集,同时获取 numpy 数组的一个子集:

train_size = int(0.8 * n_rows)
val_size = int(0.2 * n_rows)

x_train_sm = x_train.take(train_size)
x_val_sm = x_train.skip(train_size)  

y_train_sm = y_train[0:train_size]
y_val_sm = y_train[train_size:]

但是运行回归拟合给了我这个问题:

reg = ak.ImageRegressor(max_trials=max_trials, metrics=['mean_squared_error'], overwrite=False)
reg.fit(x_train_sm, y_train_sm, validation_split=0.2, batch_size=batch_size)

带有错误消息:

ValueError: Expected y to be None when x is tf.data.Dataset.

标签: pythontensorflowkerasauto-keras

解决方案


推荐阅读