如何将从CNN提取的特征与其他标量特征合并/合并?

时间:2018-08-13 18:49:04

标签: python scikit-learn keras deep-learning computer-vision

我有一个包含10、000张图像的数据集,每个图像都有5个二进制标签。我正在尝试为这5个课程训练5个分类器。我决定使用像VGG或ResNet这样的CNN从图像中提取特征。我接下来要做的是将这些“深度特征”与4个二进制标签结合起来,并预测缺失的标签。 合并这些功能是我遇到的麻烦。

让我们说说从VGG 16中提取的每个图像特征是一个大小为4096的向量。我应如何在该特征向量中添加其他特征?

我进行了一些搜索,发现了类似的问题。虹膜数据集。为了对花朵图像进行分类,每朵花朵都有一些标签,例如花瓣长度和花瓣宽度。每个图像的特征向量都是一个简单的列表,其中包含两个值,花瓣长度和花瓣宽度。

我是否应该创建一个大小等于4100的向量,并在其末尾添加其他4个标签?还是应该为每个图像创建一个1 * 5 numpy数组,该数组中的第一个元素是4096向量,其他4个元素设置为图像的4个标量标签?

1 个答案:

答案 0 :(得分:2)

您想提供卷积特征和附加标签作为两个单独的输入,并将它们连接到最终分类器中。

最小工作示例:

from keras.layers import Input, Dense, Concatenate
from keras.models import Model
from keras.applications import VGG16
import numpy as np

# Some random images, labels and target label
images = np.random.rand(10, 64, 64, 3)
labels = np.random.randint(0, 1, size=(10, 4))
target = np.random.randint(0, 1, size=(10, 1))

# Extract VGG16 features for the images
image_input = Input((64, 64, 3))
model = VGG16(include_top=False, weights='imagenet')
features = model.predict(images)
features = np.reshape(features, (10, -1))  # 2048 features per image 

# Two input layers: one for the image features, one for additional labels
feature_input = Input((2048,), name='feature_input')
label_input = Input((4, ), name='label_input')

# Concatenate the features
concatenate_layer = Concatenate(name='concatenation')([feature_input, label_input]) 
dense = Dense(16)(concatenate_layer)
output = Dense(1, name='output_layer', activation='sigmoid')(dense)

# To define the model, pass list of input layers
model = Model(inputs=[feature_input, label_input], outputs=output)
model.compile(optimizer='sgd', loss='binary_crossentropy')

# To fit the model, pass a list of inputs arrays
model.fit(x=[features, labels], y=target)

还可以查看Keras' functional API guide,其中包含许多多输入/多输出模型的示例。