L1n*_*nu5 0 python computer-vision scikit-learn deep-learning keras
我有一个包含 10, 000 张图像的数据集,每个图像有 5 个二进制标签。我正在尝试为这 5 个类训练 5 个分类器。我决定使用像 VGG 或 ResNet 这样的 CNN 从图像中提取特征。我接下来要做的是将这些“深度特征”与 4 个二元标签结合起来,并预测缺失的标签。结合这些功能是我遇到麻烦的地方。
假设从 VGG 16 中提取的每个图像的特征是一个大小为 4096 的向量。我应该如何将其他特征添加到这个特征向量中?
我做了一些搜索,发现了一个类似的问题。鸢尾花数据集。为了对花朵图像进行分类,每朵花都有一些标签,例如花瓣长度和花瓣宽度。每个图像的特征向量是一个简单的列表,具有两个值,花瓣长度和花瓣宽度。
我应该创建一个大小等于 4100 的向量,并在其末尾添加我的其他 4 个标签吗?!或者我应该为每个图像创建一个 1 * 5 numpy 数组,该数组中的第一个元素是 4096 向量,而其他 4 个元素设置为图像的 4 个标量标签?
您希望将卷积特征和附加标签作为两个单独的输入提供,并将它们连接到最终分类器中。
最小工作示例:
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)
Run Code Online (Sandbox Code Playgroud)
另请查看Keras 的功能 API 指南,其中包含大量多输入/多输出模型的示例。
归档时间: |
|
查看次数: |
4816 次 |
最近记录: |