Deep learning 合并两个CNN';s

Deep learning 合并两个CNN';s,deep-learning,keras,Deep Learning,Keras,我想在Keras中将两个CNN合并成一个,我的意思是,我想让神经网络拍摄两张图像,在单独的CNN中处理每一张图像,然后将它们连接到一个平坦层,并使用完全连接的层来完成最后的工作,这里是我所做的: # Start With First Branch ############################################################ branch_one = Sequential() # Adding The Convolution branch_one.ad

我想在Keras中将两个CNN合并成一个,我的意思是,我想让神经网络拍摄两张图像,在单独的CNN中处理每一张图像,然后将它们连接到一个平坦层,并使用完全连接的层来完成最后的工作,这里是我所做的:

# Start With First Branch ############################################################
branch_one = Sequential()

# Adding The Convolution
branch_one.add(Conv2D(32, (3,3),input_shape = (64,64,3) , activation = 'relu'))
branch_one.add(Conv2D(32, (3, 3), activation='relu'))

# Doing The Pooling Phase
branch_one.add(MaxPooling2D(pool_size=(2, 2)))
branch_one.add(Dropout(0.25))
branch_one.add(Flatten())

# Start With Second Branch ############################################################

branch_two = Sequential()

# Adding The Convolution
branch_two.add(Conv2D(32, (3,3),input_shape = (64,64,3) , activation = 'relu'))
branch_two.add(Conv2D(32, (3, 3), activation='relu'))

# Doing The Pooling Phase
branch_two.add(MaxPooling2D(pool_size=(2, 2)))
branch_two.add(Dropout(0.25))
branch_two.add(Flatten())

# Making The Combinition ##########################################################
final = Sequential()
final.add(Concatenate([branch_one, branch_two]))
final.add(Dense(units = 128, activation = "relu"))
final.add(Dense(units = 1, activation = "sigmoid"))

# Doing The Compilation
final.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
# Adding and Pushing The Images to CNN

# use ImageDataGenerator to preprocess the data

from keras.preprocessing.image import ImageDataGenerator

# augment the data that we have
train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)

# prepare training data
X1 = train_datagen.flow_from_directory('./ddsm1000_resized/images/train',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

X2 = train_datagen.flow_from_directory('./ddsm1000_resized_canny/images/train',

                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

# prepare test data
Y1 = test_datagen.flow_from_directory('./ddsm1000_resized/images/test',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')
Y2 = test_datagen.flow_from_directory('./ddsm1000_resized_canny/images/test',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')
final.fit_generator([X1, X2], steps_per_epoch = (8000 / 32), epochs = 1, validation_data = [Y1,Y2], validation_steps = 2000)
凯拉斯告诉我

RuntimeError:您必须在使用模型之前编译它


我认为CNN没有输入数据的形状,所以我能在这里做什么??谢谢

请按以下说明进行更改:

from keras.layers import Merge
...
...

# Making The Combinition ##########################################################
final = Sequential()
final.add(Merge([branch_one, branch_two], mode = 'concat'))

...
...

定义连接。这个问题是AI写的吗?连接是两个分支的合并行为我想我记得我用这种格式连接了两个层。我的意思是,语法不是一个常见的Keras,还有另外一个层,比如:input1=Input(shape=(16,))x1=densite(8,activation='relu')(input1)。它是在函数API中,而不是每个人在第一个MNIST示例中使用的顺序。我能够用函数API创建这样一个模型,只要检查一下就可以了。我想你只是忘记了添加model.compile,正如错误中所述。检查