Keras多输出模型

时间:2018-05-06 14:02:49

标签: keras convolutional-neural-network

我正在使用Keras功能API构建具有多个(五个)输出和相同输入的模型,以便同时预测数据的不同属性(在我的情况下是图像)。 / p>

该模型的摘要如下(大写字母是已经预训练的VGG16上添加的图层):

model summary

馈送到CNN的数据形状如下:

# input images
('x_train shape:', (23706, 224, 224, 3))

('Head_1 shape:', (23706, 26))
('Head_2 shape:', (23706,))
('Head_3 shape:', (23706,))
('Head_4 shape:', (23706,))
('Head_5 shape:', (23706,))

当我只将一个输出放到我的网络时,训练没有问题,但是当所有输出(甚至其中两个)都存在时,我收到以下错误:

Traceback (most recent call last):
history = model.fit_generator(datagen.flow(x_train, train_targets_list, batch_size=batch_size)
.
.
.
.

ValueError: could not broadcast input array from shape (23706,26) into shape (23706)

知道我做错了吗?

文档中是否有任何工作示例描述了多输出模型的类似案例?

# dimensions of our images.
img_width, img_height = 224, 224

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

input_tensor = Input(shape=input_shape, name='IMAGES')


base_model = VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)

x = base_model.output

x = GlobalAveragePooling2D(name='GAP')(x)

x = Dense(256, activation='relu', name='FC1')(x)
x = Dropout(0.5, name='DROPOUT')(x)


head_1 = Dense(26, activation='sigmoid', name='PREDICTION1') (x)
head_2 = Dense (1, name='PREDICTION2')(x)
head_3 = Dense (1, name='PREDICTION3')(x)
head_4 = Dense (1, name='PREDICTION4')(x)
head_5 = Dense (1, name='PREDICTION5')(x)

outputs_list = [head_1, head_2, head_3, head_4, head_5]


model = Model(inputs=input_tensor, outputs=outputs_list)



for layer in base_model.layers:
    layer.trainable = False


losses_list = ['binary_crossentropy','mse','mse','mse', 'mse']

model.compile(optimizer=SGD(lr=0.0001, momentum=0.9),
              loss=losses_list,
              metrics=['accuracy'])


print x_train.shape -> (23706, 224, 224, 3)

for y in train_targets_list:
    print len(y)

23706
23706
23706
23706
23706

0 个答案:

没有答案