Merged 1D-CNN and 2D-CNN

亡梦爱人 提交于 2020-03-04 06:53:35

问题


I want to build a merged CNN model using 1D and 2D CNN but i tried many ways to build it but this one worked with me but i don't know why i get this error when using model_combined.summary(). I have attached two images which contain the summary of 1D & 2D CNN summary of 1D CNN , summary of 2D CNN

Thank you very much!

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. 

Here is the code,

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())

来源:https://stackoverflow.com/questions/60268647/merged-1d-cnn-and-2d-cnn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!