我们是数据科学的新手,我们正在尝试合并两种不同的CNN模型(一个具有2个班级,另一个具有3个班级)。 模型的代码是:
性别模型
#initialize the model along with the input shape
model = Sequential()
inputShape = (height, width, depth)
chanDim = -1
if K.image_data_format() == 'channels_first':
inputShape = (depth, height, width)
chanDim = 1
# CONV -> RELU -> MAXPOOL
model.add(Convolution2D(64, (3,3), padding='same', input_shape=inputShape))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dropout(0.25))
# (CONV -> RELU)*2 -> AVGPOOL
model.add(Convolution2D(128, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(Convolution2D(128, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(AveragePooling2D(pool_size=(3,3) ))
model.add(Dropout(0.25))
# CONV -> RELU -> MAXPOOL
model.add(Convolution2D(256, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dropout(0.25))
# CONV -> RELU -> AVGPOOL
model.add(Convolution2D(512, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(AveragePooling2D(pool_size=(3,3)))
model.add(Dropout(0.25))
# DENSE -> RELU
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.25))
# DENSE -> RELU
model.add(Dense(512))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.25))
# sigmoid -> just to check the accuracy with this (softmax would work too)
model.add(Dense(classes))
model.add(Activation('sigmoid'))
return model
model = build(img_size, img_size, 3, 2)
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
种族模型:
#initialize the model along with the input shape
model = Sequential()
inputShape = (height, width, depth)
chanDim = -1
if K.image_data_format() == 'channels_first':
inputShape = (depth, height, width)
chanDim = 1
# CONV -> RELU -> MAXPOOL
model.add(Convolution2D(64, (3,3), padding='same', input_shape=inputShape))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dropout(0.25))
# (CONV -> RELU)*2 -> AVGPOOL
model.add(Convolution2D(128, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(Convolution2D(128, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(AveragePooling2D(pool_size=(3,3) ))
model.add(Dropout(0.25))
# CONV -> RELU -> MAXPOOL
model.add(Convolution2D(256, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dropout(0.25))
# CONV -> RELU -> AVGPOOL
model.add(Convolution2D(512, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=chanDim))
model.add(AveragePooling2D(pool_size=(3,3)))
model.add(Dropout(0.25))
# DENSE -> RELU
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.25))
# DENSE -> RELU
model.add(Dense(512))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.25))
# softmax
model.add(Dense(classes))
model.add(Activation('softmax'))
return model
model = build(img_size, img_size, 3, 3)
model.compile(loss= 'categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
我们尝试将模型与串联的keras函数合并,但未能取消描述如何合并具有不同类数的两个模型。 我们的目标是:给定一张照片,我们希望同时预测性别和种族 感谢您的关注。
答案 0 :(得分:0)
我们叫第一个模型PS C:\Users\USER\OneDrive\Documents\Projects\Vagrant2> vagrant package --base Vagrant2_default_1598799939605_1106
==> Vagrant2_default_1598799939605_1106: Exporting VM...
==> Vagrant2_default_1598799939605_1106: Compressing package to: C:/Users/USER/OneDrive/Documents/Projects/Vagrant2/package.box
和第二个模型model_1
。
您需要做的第一步是将模型的输入更改为一些通用输入。
model_2
下一步,使用这些输入和输出创建模型
inputs = keras.layers.Input(shape=inputShape)
outputs_1 = model_1(inputs)
outputs_2 = model_2(inputs
现在,模型具有一个输入和两个输出。您可以从单个输入获得两个预测。
如果模型具有相同的名称和/或模型的层具有相同的名称,请使用以下代码重命名模型和模型的层。
new_model = keras.Model(inputs=inputs, outputs=[outputs_1, outputs_2])