我想使用1D和2D CNN构建合并的CNN模型,但是我尝试了多种构建方法,但是这种方法与我合作,但是我不知道为什么在使用model_combined.summary()
时会出现此错误。
我已附上两张图片,其中包含1D和2D CNN summary of 1D CNN的摘要,
summary of 2D CNN
非常感谢您!
ValueError Traceback (most recent call last)
<ipython-input-20-3c58e6d04c4d> in <module>()
60 #opt = RMSprop(lr=0.001, rho=0.9)
61 model_combined.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])
---> 62 print(model_combined.summary())
/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in summary(self, line_length, positions, print_fn)
1305 if not self.built:
1306 raise ValueError(
-> 1307 'This model has not yet been built. '
1308 'Build the model first by calling build() '
1309 'or calling fit() with some data. '
ValueError: This model has not yet been built. Build the model first by calling build() or calling fit() with some data. Or specify input_shape or batch_input_shape in the first layer for automatic build.
这是代码,
from keras.models import Sequential, Model
from keras.layers.core import Dense, Activation
from keras.layers.convolutional import Conv2D , Conv1D
from keras.layers import Conv2D, Conv1D,MaxPooling2D, Reshape, Concatenate, Dropout , MaxPooling1D
# Optimizers
from keras.optimizers import Adagrad
from keras.optimizers import Adam
from keras.optimizers import SGD
from keras.optimizers import RMSprop
# ----------------------- 1D CNN ----------------------
model_1D = Sequential()
# 1
model_1D.add(Conv1D(32, kernel_size= 5 , strides=1, activation='relu')) # input shape after preprocessing
model_1D.add(MaxPooling1D(pool_size= 4, strides=4))
# 2
model_1D.add(Conv1D(32, kernel_size= 5 , strides=1 , activation='relu'))
model_1D.add(MaxPooling1D(pool_size= 4, strides=4))
# 3
model_1D.add(Conv1D(64, kernel_size= 5 , strides=1 , activation='relu'))
model_1D.add(MaxPooling1D(pool_size= 4, strides=4))
# 4
model_1D.add(Conv1D(64, kernel_size= 5 , strides=1 , activation='relu'))
model_1D.add(MaxPooling1D(pool_size= 2, strides=2))
# 5
model_1D.add(Conv1D(128, kernel_size= 5 , strides= 1 , activation='relu'))
model_1D.add(MaxPooling1D(pool_size= 2, strides= 2))
# 6
model_1D.add(Conv1D(128, kernel_size= 5 , strides= 1 , activation='relu'))
model_1D.add(MaxPooling1D(pool_size= 2, strides= 2))
model_1D.add(Dense(9 , activation='relu')) # K
# model_1D.add(Dense(7, activation='softmax'))
model_1D.build(input_shape = (None,7380, 128000))
#print(model_1D.summary())
# ----------------------- 2D CNN ----------------------
model_2D = Sequential()
model_2D.add(Conv2D(32, kernel_size=(3, 3) , strides=(1,1), activation='relu'))
model_2D.add(Conv2D(32, kernel_size=(3, 3) , strides=(1,1), activation='relu'))
model_2D.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model_2D.add(Conv2D(32, kernel_size=(3, 3) , strides=(1,1), activation='relu'))
model_2D.add(Conv2D(32, kernel_size=(3, 3) , strides=(1,1), activation='relu'))
model_2D.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model_2D.add(Dense(9 , activation='relu')) # K
# model_2D.add(Dense(7, activation='softmax'))
model_2D.build (input_shape = (7380, 128, 251, 1))
# print(model_2D.summary())
# ----------------------- Merged CNN ----------------------
merged = Concatenate([model_2D, model_1D])
model_combined = Sequential()
model_combined.add(merged)
model_combined.add(Dense(7, activation='softmax'))
opt = Adam(lr=0.0001)
#opt = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
#opt = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#opt = RMSprop(lr=0.001, rho=0.9)
model_combined.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])
print(model_combined.summary())
答案 0 :(得分:2)
您可以通过仅在第一层为1D-CNN和2D-CNN提供输入形状的方式来尝试使用功能性API,而不是顺序API。然后通过在1D-CNN和2D-CNN中的Dense层之前添加flatten可以解决您的问题。
您可以按照以下修改的代码进行操作。
一维CNN:
# ----------------------- 1D CNN ----------------------
in_1D = Input((7380, 128000))
# 1
model_1D = Conv1D(32, kernel_size= 5 , strides=1, activation='relu')(in_1D)
model_1D = MaxPooling1D(pool_size= 4, strides=4)(model_1D)
# 2
model_1D = Conv1D(32, kernel_size= 5 , strides=1 , activation='relu')(model_1D)
model_1D = MaxPooling1D(pool_size= 4, strides=4)(model_1D)
# 3
model_1D = Conv1D(64, kernel_size= 5 , strides=1 , activation='relu')(model_1D)
model_1D = MaxPooling1D(pool_size= 4, strides=4)(model_1D)
# 4
model_1D = Conv1D(64, kernel_size= 5 , strides=1 , activation='relu')(model_1D)
model_1D = MaxPooling1D(pool_size= 2, strides=2)(model_1D)
# 5
model_1D = Conv1D(128, kernel_size= 5 , strides= 1 , activation='relu')(model_1D)
model_1D = MaxPooling1D(pool_size= 2, strides= 2)(model_1D)
# 6
model_1D = Conv1D(128, kernel_size= 5 , strides= 1 , activation='relu')(model_1D)
model_1D = MaxPooling1D(pool_size= 2, strides= 2)(model_1D)
model_1D = Flatten()(model_1D)
model_1D = Dense(9 , activation='softmax')(model_1D)
2D-CNN:
# ----------------------- 2D CNN ----------------------
in_2D = Input((7380, 128, 251))
model_2D = Conv2D(32, kernel_size=(3, 3) , strides=(1,1), activation='relu')(in_2D)
model_2D = Conv2D(32, kernel_size=(3, 3) , strides=(1,1), activation='relu')(model_2D)
model_2D = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(model_2D)
model_2D = Conv2D(32, kernel_size=(3, 3) , strides=(1,1), activation='relu')(model_2D)
model_2D = Conv2D(32, kernel_size=(3, 3) , strides=(1,1), activation='relu')(model_2D)
model_2D = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(model_2D)
model_2D = Flatten()(model_2D)
model_2D = Dense(9 , activation='relu')(model_2D)
合并:
merged = Concatenate()([model_2D, model_1D])
output = Dense(7, activation='softmax')(merged)
model_combined = Model(inputs=[in_2D, in_1D], outputs=[output])
model_combined.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model_combined.summary()
输出:
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 7380, 128000) 0
__________________________________________________________________________________________________
conv1d_1 (Conv1D) (None, 7376, 32) 20480032 input_1[0][0]
__________________________________________________________________________________________________
max_pooling1d_1 (MaxPooling1D) (None, 1844, 32) 0 conv1d_1[0][0]
__________________________________________________________________________________________________
conv1d_2 (Conv1D) (None, 1840, 32) 5152 max_pooling1d_1[0][0]
__________________________________________________________________________________________________
max_pooling1d_2 (MaxPooling1D) (None, 460, 32) 0 conv1d_2[0][0]
__________________________________________________________________________________________________
conv1d_3 (Conv1D) (None, 456, 64) 10304 max_pooling1d_2[0][0]
__________________________________________________________________________________________________
input_2 (InputLayer) (None, 7380, 128, 25 0
__________________________________________________________________________________________________
max_pooling1d_3 (MaxPooling1D) (None, 114, 64) 0 conv1d_3[0][0]
__________________________________________________________________________________________________
conv2d_1 (Conv2D) (None, 7378, 126, 32 72320 input_2[0][0]
__________________________________________________________________________________________________
conv1d_4 (Conv1D) (None, 110, 64) 20544 max_pooling1d_3[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D) (None, 7376, 124, 32 9248 conv2d_1[0][0]
__________________________________________________________________________________________________
max_pooling1d_4 (MaxPooling1D) (None, 55, 64) 0 conv1d_4[0][0]
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D) (None, 3688, 62, 32) 0 conv2d_2[0][0]
__________________________________________________________________________________________________
conv1d_5 (Conv1D) (None, 51, 128) 41088 max_pooling1d_4[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D) (None, 3686, 60, 32) 9248 max_pooling2d_1[0][0]
__________________________________________________________________________________________________
max_pooling1d_5 (MaxPooling1D) (None, 25, 128) 0 conv1d_5[0][0]
__________________________________________________________________________________________________
conv2d_4 (Conv2D) (None, 3684, 58, 32) 9248 conv2d_3[0][0]
__________________________________________________________________________________________________
conv1d_6 (Conv1D) (None, 21, 128) 82048 max_pooling1d_5[0][0]
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D) (None, 1842, 29, 32) 0 conv2d_4[0][0]
__________________________________________________________________________________________________
max_pooling1d_6 (MaxPooling1D) (None, 10, 128) 0 conv1d_6[0][0]
__________________________________________________________________________________________________
flatten_2 (Flatten) (None, 1709376) 0 max_pooling2d_2[0][0]
__________________________________________________________________________________________________
flatten_1 (Flatten) (None, 1280) 0 max_pooling1d_6[0][0]
__________________________________________________________________________________________________
dense_2 (Dense) (None, 9) 15384393 flatten_2[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 9) 11529 flatten_1[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 18) 0 dense_2[0][0]
dense_1[0][0]
__________________________________________________________________________________________________
dense_3 (Dense) (None, 7) 133 concatenate_1[0][0]
==================================================================================================
Total params: 36,135,287
Trainable params: 36,135,287
Non-trainable params: 0