在keras中连接四个CNN模型



我想在Keras中将四个CNN(pop1、pop2、pop3和pop4(合并为一个。我的目标是建立一个分类器,能够将图像分配给三种可能的结果。以下是如何连接CNN:

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Concatenate
from keras.models import Model
# Start With Pop1 Branch ############################################################
classifier_pop1 = Sequential()
classifier_pop1.add(Conv2D(1, (3,3), input_shape = (4009, 36, 1), activation = 'relu'))
classifier_pop1.add(MaxPooling2D(pool_size = (3,3)))
classifier_pop1.add(Conv2D(1, (3,3), activation = 'relu'))
classifier_pop1.add(MaxPooling2D(pool_size = (3,3)))
classifier_pop1.add(Flatten())
classifier_pop1.add(Dense(units = 300, activation = 'relu'))
classifier_pop1.add(Dense(units = 24, activation = 'relu'))
# Start With Pop2 Branch ############################################################
classifier_pop2 = Sequential()
classifier_pop2.add(Conv2D(1, (3,3), input_shape = (4009, 36, 1), activation = 'relu'))
classifier_pop2.add(MaxPooling2D(pool_size = (3,3)))
classifier_pop2.add(Conv2D(1, (3,3), activation = 'relu'))
classifier_pop2.add(MaxPooling2D(pool_size = (3,3)))
classifier_pop2.add(Flatten())
classifier_pop2.add(Dense(units = 300, activation = 'relu'))
classifier_pop2.add(Dense(units = 24, activation = 'relu'))
# Start With Pop3 Branch ############################################################
classifier_pop3 = Sequential()
classifier_pop3.add(Conv2D(1, (3,3), input_shape = (4009, 32, 1), activation = 'relu'))
classifier_pop3.add(MaxPooling2D(pool_size = (3,3)))
classifier_pop3.add(Conv2D(1, (3,3), activation = 'relu'))
classifier_pop3.add(MaxPooling2D(pool_size = (3,3)))
classifier_pop3.add(Flatten())
classifier_pop3.add(Dense(units = 300, activation = 'relu'))
classifier_pop3.add(Dense(units = 24, activation = 'relu'))
# Start With Pop4 Branch ############################################################
classifier_pop4 = Sequential()
classifier_ pop4.add(Conv2D(1, (3,3), input_shape = (4009, 18, 1), activation = 'relu'))
classifier_ pop4.add(MaxPooling2D(pool_size = (3,3)))
classifier_ pop4.add(Conv2D(1, (3,3), activation = 'relu'))
classifier_ pop4.add(MaxPooling2D(pool_size = (3,3)))
classifier_ pop4.add(Flatten())
classifier_ pop4.add(Dense(units = 300, activation = 'relu'))
classifier_ pop4.add(Dense(units = 24, activation = 'relu'))
# Making The Combinition ##########################################################
model_concat = Concatenate()([classifier_pop1.output,classifier_pop2.output,classifier_pop3.output,classifier_ pop4.output])
model_concat = Dense(3, activation='softmax')(model_concat)
model = Model(inputs=[classifier_pop1.input,classifier_pop2.input,classifier_pop3.input,classifier_ pop4.input], outputs=model_concat)

型号_批次

## Compiling the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

我怀疑我的代码中的错误与下面的代码有关。我有四个文件夹(pop1、pop2、pop3和pop4(,其中包含三个子文件夹(Model1、Model2和Model3(。我不知道如何在这个部分使用imageGenerator。我刚刚关注了另一篇帖子:合并两个CNN;s

##Image preprocessing
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set_pop1 = train_datagen.flow_from_directory('./Datasets/training_set/Pop1',
target_size = (4009, 36),
batch_size = 100,
class_mode = 'categorical',
color_mode='grayscale',
shuffle=True)
test_set_pop1 = test_datagen.flow_from_directory('./Datasets/test_set/Pop1',
target_size = (4009, 36),
batch_size = 100,
class_mode = 'categorical',
color_mode='grayscale',
shuffle=False)
training_set_pop2 = train_datagen.flow_from_directory('./Datasets/training_set/Pop2',
target_size = (4009, 36),
batch_size = 100,
class_mode = 'categorical',
color_mode='grayscale',
shuffle=True)
test_set_pop2 = test_datagen.flow_from_directory('./Datasets/test_set/Pop2',
target_size = (4009, 36),
batch_size = 100,
class_mode = 'categorical',
color_mode='grayscale',
shuffle=False)

training_set_pop3 = train_datagen.flow_from_directory('./Datasets/training_set/Pop3',
target_size = (4009, 32),
batch_size = 100,
class_mode = 'categorical',
color_mode='grayscale',
shuffle=True)
test_set_pop3 = test_datagen.flow_from_directory('./Datasets/test_set/Pop3',
target_size = (4009, 32),
batch_size = 100,
class_mode = 'categorical',
color_mode='grayscale',
shuffle=False)
training_set_pop4 = train_datagen.flow_from_directory('./Datasets/training_set/Planiceps',
target_size = (4009, 18),
batch_size = 100,
class_mode = 'categorical',
color_mode='grayscale',
shuffle=True)
test_set_pop4 = test_datagen.flow_from_directory('./Datasets/test_set/Planiceps',
target_size = (4009, 18),
batch_size = 100,
class_mode = 'categorical',
color_mode='grayscale',
shuffle=False) 
model.fit([training_set_pop1,training_set_pop2,training_set_pop3,training_set_pop4],
steps_per_epoch = int(2400/100),
epochs = 4,
validation_data = [test_set_pop1,test_set_pop2,test_set_pop3,test_set_pop4],
validation_steps = int(600/100))

然后,我得到了这个错误:

ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'keras_preprocessing.image.directory_iterator.DirectoryIterator'>"}), <class 'NoneType'>

也许您可以尝试以下操作,因为您不能将生成器作为列表传递:

model.fit(zip(training_set_pop1,training_set_pop2,training_set_pop3,training_set_pop4),
steps_per_epoch = int(2400/100),
epochs = 4,
validation_data = zip(test_set_pop1,test_set_pop2,test_set_pop3,test_set_pop4),
validation_steps = int(600/100))