我试图复制cnn并合并它们,这样我就得到一个双路径架构。但得到的错误。我使用的是keras 2.1.6版本。

Need help with python coding.I tried to concatenate 2 CNN models.Model #1 has 3 convolution layers followed by a dense layer.Model2 also has the same architecture. I’m trying to concatenate the output of these cnns and have another dense layer。我将代码包括在内,供您参考。


model1 = Sequential()
# Conv Layer 1
model1.add(layers.SeparableConv2D(32, (9, 9), activation='relu', input_shape=input_shape))
model1.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))

# Conv Layer 2
model1.add(layers.SeparableConv2D(64, (9, 9), activation='relu'))
model1.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))

# Conv Layer 3
model1.add(layers.SeparableConv2D(128, (9, 9), activation='relu'))
model1.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))

# model.add(layers.SeparableConv2D(256, (9, 9), activation='relu'))
# model.add(layers.MaxPooling2D(2, 2))
# Flatten the data for upcoming dense layer
model1.add(layers.Flatten())

model1.add(layers.Dropout(0.5))
model1.add(layers.Dense(512, activation='relu'))
#model1.add(layers.Dense(output_classes,) activation='relu'))
#model1.build(input_shape = (input_shape)


model2 = Sequential()
# Conv Layer 1
model2.add(layers.SeparableConv2D(32, (9, 9), activation='relu', input_shape=input_shape))
model2.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))

# Conv Layer 2
model2.add(layers.SeparableConv2D(64, (9, 9), activation='relu'))
model2.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))

# Conv Layer 3
model2.add(layers.SeparableConv2D(128, (9, 9), activation='relu'))
model2.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))

# model.add(layers.SeparableConv2D(256, (9, 9), activation='relu'))
# model.add(layers.MaxPooling2D(2, 2))
# Flatten the data for upcoming dense layer
model2.add(layers.Flatten())

model2.add(layers.Dropout(0.5))
model2.add(layers.Dense(512, activation='relu'))
#model2.add(layers.Dense(output_classes, activation='relu'))

comb_model = Sequential()

x1=model1.output
x2=model2.output
comb_model.layers.Concatenate([x1,x2],axis=-1)
comb_model.add(layers.Dense(512, activation='relu'))
comb_model.add(layers.Dropout(0.6))
comb_model.add(layers.Dense(output_classes, activation=tf.nn.softmax))

print(comb_model.summary())

显示的错误是


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-28-267f93f5102f> in <module>()
      3 x1=model1.output
      4 x2=model2.output
----> 5 comb_model.layers.Concatenate([x1,x2],axis=-1)
      6 comb_model.add(layers.Dense(512, activation='relu'))
      7 comb_model.add(layers.Dropout(0.6))

AttributeError: 'list' object has no attribute 'Concatenate'

解决方案:

你能不能像这样创建你的组合模型呢?

x1=model1.output
x2=model2.output
concat = layers.Concatenate()([x1,x2])
dense1 = layers.Dense(512, activation='relu')(concat)
dropout = layers.Dropout(0.6)(dens1)
dense2 = layers.Dense(output_classes, activation=tf.nn.softmax)(dropout)
comb_model = tf.keras.Model(inputs=[model1.input, model2.input], outputs=dense2)

希望这个能用。

给TA打赏
共{{data.count}}人
人已打赏
未分类

无法在Windows 10上减少Docker最大磁盘镜像大小。

2022-9-8 0:11:21

未分类

用while循环重新运行程序后,异常方法不工作。

2022-9-8 0:22:16

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索