【发布时间】:2021-08-01 11:21:33
【问题描述】:
我正在使用带有 Tensorflow 2 库的深度学习来预测房价。
我有 3 个属性(浴室、卧室、面积)的数据和每所房子的图像作为我的数据集。 (近 2000 个样本)
我正在构建 3 个深度学习模型:
- 回归模型 (model_reg):使用 3 个属性——效果很好
- 使用图像的 CNN 模型 (img_model) - 运行良好
- 结合以上两个模型 (model_combined) -- 出错
回归模型:
我使用 Tensorflow2.0 构建了一个深度神经网络 (DNN) 模型,其中 3 个属性作为特征,价格作为标签。
我可以拟合 DNN 并可以预测房价。
注意:我在构建此模型时使用了 tf.data.Dataset 结合 X_train、y_train。
model_reg.fit(X_reg_train_dataset, batch_size=BATCH_SIZE, epochs=EPOCHS,
callbacks=[stop_callback], verbose=1)
63/63 [==============================] - 0s 2ms/step - loss: 0.3993 - mae: 0.4256 - mse: 0.3993
CNN 模型: 接下来,我使用房屋图像作为特征和价格作为标签来构建其他 CNN。
这也很好,我可以预测房价。
注意:我使用了 tensorflow.keras.preprocessing 中的 ImageDataGenerator 来构建生成器。
hist = img_model.fit(
train_images_generator,
steps_per_epoch = train_images_generator.samples // BATCH_SIZE ,
validation_data = test_images_generator,
validation_steps = test_images_generator.samples // BATCH_SIZE,
epochs = EPOCHS,
callbacks=[stop_callback], verbose=1)
49/49 [==============================] - 59s 1s/step - loss: 1741.6321 - mae: 20.8221 - mse: 1741.6321 - val_loss: 755833241600.0000 - val_mae: 768718.3125 - val_mse: 755833241600.0000
最后,我使用 Concatenate() 层适当地合并了这两个模型。 现在我有 2 个输入需要传递给模型。 因此,我使用具有 2 个输入的功能 API 定义了一个模型。
model_combined = Model(inputs=[input_layer_reg, img_input_layer], outputs=[output_layer_combined])
optimizer = tf.keras.optimizers.Adam(0.01)
model_combined.compile(loss='mae', optimizer=optimizer, metrics=['mae', 'mse'])
到目前为止,它运行良好,生成的模型看起来也很好。
尝试使用 fit 进行训练时出错:
model_combined.fit([X_reg_train_dataset, train_images_generator], epochs=EPOCHS,
callbacks=[stop_callback], verbose=1,
batch_size=BATCH_SIZE )
ValueError: Failed to find data adapter that can handle input:
(<class 'list'> containing values of types {"<class 'tensorflow.python.keras.preprocessing.image.DataFrameIterator'>",
"<class 'tensorflow.python.data.ops.dataset_ops.BatchDataset'>"}),
<class 'NoneType'>
问题:如何将 2 个输入:tf.data.Dataset 和 ImageDataGenerator 传递给 Tensorflow 模型?
【问题讨论】:
-
能否也提供model_combined?
-
img_model: for image based price prediction using input - train_images_generator. | model_reg: for numerical features for price prediction - X_reg_train_dataset | and in model_combined I am trying to use the same 2 inputs in a list - [X_reg_train_dataset, train_images_generator] -
是的,但是你是如何构建model_combined和功能API的,层是什么等等?
-
是的,我使用了函数式 API
input_layer_reg = tf.keras.Input(shape=(X_reg_train.shape[1],), name='input_layer_reg') img_input_layer = tf.keras.Input(shape=input_shape, name='img_input_layer')以上是用于单个模型的相同输入层......和我用于组合模型的相同输入层。model_combined = Model(inputs=[input_layer_reg, img_input_layer], outputs=[output_layer_combined])添加了一个 Concatenated 层以在其间组合,并在末尾添加 2 个密集层 -
据我所知,您无法将 Dataset 对象连接到 ImageDataGenerator ,因此在处理后您可以连接可能有效的结果,这没有意义。
标签: tensorflow conv-neural-network tensorflow2.0