我创建了一个接收两个输入的模型。当我使用两个numpy数组拟合模型时,它能正常工作。以下是一个示例:
model.fit(x=[image_input, other_features], y = y, epochs=epochs)
然而,我的难题在于other_features
是一个numpy数组,而image_input
是通过keras使用tf.keras.preprocessing.image_dataset_from_directory
加载的。我面临的问题是:
- 如何从
image_input
中正确地提取y
?当我仅用一个输入image_input
训练模型时,y
是包含在其中的,因此我无需在其他参数中指定它。 - 如何将
BatchDataset
与numpy.array
结合使用?当我尝试这样做时,我收到了以下错误:
ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'tensorflow.python.data.ops.dataset_ops.BatchDataset'>", "<class 'numpy.ndarray'>"}), <class 'NoneType'>
回答:
好的,我已经解决了这个问题。我将详细写出解决方案,因为我看到类似的提问多次出现却没有答案。这是一个混合输入的问题,解决方法是依赖自定义生成器。
第一步是创建自定义生成器。你需要返回一个包含你的输入和输出的列表/字典。我参考了这个链接来创建我的生成器。以下是我生成器的样本代码:
def generator(subset, batch_size=256): i = 0 DIR = f"data/{subset}" image_files = pd.read_csv(f"{DIR}.csv") while True: batch_x = [list(), list()] # 我有两个输入:图像 + 特征向量 batch_y = list() # 输出 for b in range(batch_size): if i == len(image_files): i = 0 filename = image_files.loc[i, "filename"] label = image_files.loc[i, "Class"] image_file_path = f'{DIR}/{label}/{filename}' i += 1 image = cv2.imread(image_file_path, 0) batch_x[0].append(image) feat = get_feature_vector(filename) batch_x[1].append(feat) batch_y.append(one_hot(label)) batch_x[0] = np.array(batch_x[0]) # 将每个列表转换为数组 batch_x[1] = np.array(batch_x[1]) batch_y = np.array(batch_y) yield batch_x, batch_y
然后,使用函数式TensorFlow创建模型。当你拟合数据时,调用你的生成器并传递所需的参数:
history = model.fit(generator('train'), validation_data = generator('validate'))