【发布时间】:2020-11-09 19:49:12
【问题描述】:
我正在使用 Keras 函数 API 构建多输入网络,但我很难找到并理解正确的输入数据格式。
我有两个主要输入:
- 一个是图像,它会抛出一个经过微调的 ResNet50 CNN
- 第二个是一个简单的 numpy 数组 (X_train),其中包含有关图像的元数据(图像的位置和大小)。这会抛出一个简单的密集网络。
我从数据帧加载图像,其中包含元数据和对应图像的文件路径。 我使用 ImageDataGenerator 和 flow_from_dataframe 方法来加载我的图像:
datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
train_flow = datagen.flow_from_dataframe(
dataframe=df_train,
x_col="cropped_img_filepath",
y_col="category",
batch_size=batch_size,
shuffle=False,
class_mode="categorical",
target_size=(224,224)
)
我可以使用它们自己的数据分别训练两个网络,直到这里没有问题。
然后将两个不同网络的两个输出组合成一个密集网络,输出一个 10 位的概率向量:
# Create the input for the final dense network using the output of both the dense MLP and CNN
combinedInput = concatenate([cnn.output, mlp.output])
x = Dense(512, activation="relu")(combinedInput)
x = Dense(256, activation="relu")(x)
x = Dense(128, activation="relu")(x)
x = Dense(32, activation="relu")(x)
x = Dense(10, activation="softmax")(x)
model = Model(inputs=[cnn.input, mlp.input], outputs=x)
# Compile the model
opt = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss="categorical_crossentropy",
metrics=['accuracy'],
optimizer=opt)
# Train the model
model_history = model.fit(x=(train_flow, X_train),
y=y_train,
epochs=1,
batch_size=batch_size)
但是,当我无法训练整个网络时,我会收到以下错误:
ValueError: 无法找到可以处理输入的数据适配器:(
我了解我没有为我的输入数据使用正确的输入格式。
我可以用 train_flow 训练我的 CNN,用 X_train 训练我的密集网络,所以我希望这能奏效。
你知道如何将图像数据和 num 数组组合成一个多输入数组吗?
感谢您提供的所有信息!
【问题讨论】:
-
您能否描述一下您是如何创建 X_train 的?它看起来不像是错误中的 Numpy 数组?
-
感谢您的快速回复!它确实是一个numpy.ndarray,我认为它与数组相同。我通过在我的 df_train DataFrame 上使用 Pandas.DataFrame.to_numpy() 方法构建它
-
我认为您需要一个生成 x1、x2、y 的自定义生成器。请看一下这个链接:github.com/keras-team/keras/issues/8130#issuecomment-336855177
标签: python tensorflow keras input deep-learning