【问题标题】:Concatenate non-image feature vector (dimension 3x1) with the image feature vector (dimension Nx1) at the flattend layer of CNN & fed to dense layer在 CNN 的扁平层将非图像特征向量(维度 3x1)与图像特征向量(维度 Nx1)连接并馈送到密集层
【发布时间】:2020-07-14 08:25:42
【问题描述】:

我正在处理一个包含 15 个类别的胸部 X 射线图像数据集。图像文件名保留在具有一些非图像值的 CSV 文件中。图像数据集分为训练、测试、验证。我使用 imagedatagenerator 来增强图像。

|---------------------|------------------|---------------|
|      Image Index    |  Patient Gender  | View Position |
|---------------------|------------------|---------------|
|   00008236_001.png  |         1        |       0       |
|---------------------|------------------|---------------|
|   00016410_014.png  |         0        |       1       |
|---------------------|------------------|---------------|
|   00014751_001.png  |         1        |       0       |
|---------------------|------------------|---------------|
|   00020318_012.png  |         1        |       1       |
|---------------------|------------------|---------------|

[[包含非图像特征的CSV文件(患者性别和胸部X光图像视图位置编码为{0,1})]

我想将这两列的值与 CNN 的扁平层连接起来。

我尝试了以下代码,但显示错误。

train_set_features = train_set[['View Position','Patient Gender']]
input_features =train_set_att.values # Shape=(90771, 2)

from keras.applications import *
from keras.layers import GlobalAveragePooling2D, Dense, Dropout, Flatten,Concatenate
from keras.models import Sequential

base_model = MobileNet( include_top=False,input_shape=(224,224,3))
x = base_model.output
x = Flatten()(x)  #output shape = (None,7168)
non_image_features = Input(shape=[2,], name="non_image") #output shape = (None,2)
x= concatenate([x, non_image_features]) #output shape = (None,7170)

# and a logistic layer
predictions = Dense(15, activation="sigmoid",name='visualized_layer')(x)

model = Model(inputs=[base_model.input,non_image_features], outputs=predictions)
opt = Adam(learning_rate=0.001)
model.compile(optimizer=opt, loss='binary_crossentropy',metrics=['binary_accuracy','mae'])
history = model.fit_generator([train_generator,input_features ],
                              validation_data=valid_generator,
                              steps_per_epoch=100, 
                              validation_steps=25,
                              epochs =64,)

predicted_values = model.predict_generator(test_generator, steps = len(test_generator))

这是用扁平层连接值的正确方法吗?

【问题讨论】:

    标签: python-3.x tensorflow keras conv-neural-network


    【解决方案1】:

    您的代码存在一些问题。首先,你尝试调用concatenate,但是没有这个功能。请注意,您导入的函数名为 Concatenate

    其次,如果您尝试调用此函数,则需要以不同的方式进行。查看文档以了解其工作原理(查找使用示例):https://keras.io/api/layers/merging_layers/concatenate/

    最后,如果您仍然遇到问题,请向我们报告您收到的错误,以便我们尝试找出究竟是什么问题。

    【讨论】:

    • 我检查了 keras 文档中的 Cancatenation。然后,我更改了我的代码。 non_image_features = Input(shape=[2,], name="non_image") x= Concatenate(axis=1)([x, non_image_features]) predictions = Dense(15, activation="sigmoid",name='visualized_layer')(x)编辑后模型编译成功。但是 model.fit_generator 显示了这个错误。 TypeError: 'list' object is not an iterator 代码:history = model.fit_generator([train_generator,input_features ], validation_data=valid_generator,epochs =64,)
    • 我对@9​​87654329@ 感到困惑。我该如何纠正它?
    【解决方案2】:

    首先,正如已经指出的那样,它是Concatenate,而不是concatenate

    根据文档,fit_generator 需要一个生成器,而不是列表 [train_generator,input_features ]。生成器预计会在每次迭代时产生一个元组 - (inputs, targets)。每批(在每次迭代中生成)通常包含大约 64 或 128 个训练示例。

    在您的情况下,第一个参数很可能是 train_generator 而不是 [train_generator,input_features ]

    【讨论】:

      猜你喜欢
      • 2020-10-23
      • 2021-12-27
      • 2018-08-17
      • 1970-01-01
      • 2011-12-15
      • 2016-05-07
      • 1970-01-01
      • 2019-08-22
      • 2021-03-27
      相关资源
      最近更新 更多