我做了一个接受两个输入的模型。当我用两个numpy数组来拟合模型时,它可以工作。下面是一个例子:
model.fit(x=[image_input, other_features], y = y, epochs=epochs)
但是,我的问题是,other_features
是一个numpy数组,而image_input
使用tf.keras.preprocessing.image_dataset_from_directory
加载keras。我面临的问题是:
image_input
中正确地给y?当我只使用一个输入image_input
训练模型时,y
被打包在其中,所以我不必在另一个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'>
发布于 2021-04-19 12:56:59
好吧,我解决了。我会写详细的解决方案,因为我看到了类似的问题,多次发帖,没有答案。这是混合输入,解决方案是依赖自定义生成器。
第一步是制作自定义生成器。您必须返回输入+输出的列表/数据。I followed this link to make mine。下面是我的生成器的示例代码:
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()] # I have two input: image + feature vector
batch_y = list() # output
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]) # convert each list to array
batch_x[1] = np.array(batch_x[1])
batch_y = np.array(batch_y)
yield batch_x, batch_y
然后,利用函数tensorflow建立模型。当您匹配数据时,使用所需的args调用生成器:
history = model.fit(generator('train'),
validation_data = generator('validate'))
https://stackoverflow.com/questions/67151256
复制相似问题