同时具有图像数据和预提取特征的 CNN 模型
CNN model with both image data and pre-extracted features
我正在尝试实现一个 CNN model
来将一些图像分类到它们对应的 类。图片大小 64x64x3
。我的数据集包含 25,000 张图像和一个 CSV
文件,其中包含 14 pre-extracted features
颜色、长度等
我想构建一个 CNN
模型,利用图像数据和特征进行训练和预测。我如何在 Python
和 Keras
中实现这样的模型?
我将开始假设您可以毫无问题地导入数据,并且您已经将 x 数据分离为图像和特征,并将 y 数据作为每个图像的标签。
您可以使用 keras 函数 api 让神经网络接受多个输入。
from keras.models import Model
from keras.layers import Conv2D, Dense, Input, Embedding, multiply, Reshape, concatenate
img = Input(shape=(64, 64, 3))
features = Input(shape=(14,))
embedded = Embedding(input_dim=14, output_dim=60*32)(features)
embedded = Reshape(target_shape=(14, 60,32))(embedded)
encoded = Conv2D(32, (3, 3), activation='relu')(img)
encoded = Conv2D(32, (3, 3), activation='relu')(encoded)
x = concatenate([embedded, encoded], axis=1)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
main_output = Dense(1, activation='sigmoid', name='main_output')(x)
model = Model([img, features], [main_output])
我正在尝试实现一个 CNN model
来将一些图像分类到它们对应的 类。图片大小 64x64x3
。我的数据集包含 25,000 张图像和一个 CSV
文件,其中包含 14 pre-extracted features
颜色、长度等
我想构建一个 CNN
模型,利用图像数据和特征进行训练和预测。我如何在 Python
和 Keras
中实现这样的模型?
我将开始假设您可以毫无问题地导入数据,并且您已经将 x 数据分离为图像和特征,并将 y 数据作为每个图像的标签。
您可以使用 keras 函数 api 让神经网络接受多个输入。
from keras.models import Model
from keras.layers import Conv2D, Dense, Input, Embedding, multiply, Reshape, concatenate
img = Input(shape=(64, 64, 3))
features = Input(shape=(14,))
embedded = Embedding(input_dim=14, output_dim=60*32)(features)
embedded = Reshape(target_shape=(14, 60,32))(embedded)
encoded = Conv2D(32, (3, 3), activation='relu')(img)
encoded = Conv2D(32, (3, 3), activation='relu')(encoded)
x = concatenate([embedded, encoded], axis=1)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
main_output = Dense(1, activation='sigmoid', name='main_output')(x)
model = Model([img, features], [main_output])