我正在尝试实现一个CNN模型
来将一些图像分类到相应的类别中。图像尺寸为64x64x3
。我的数据集包含25,000张图像,还有一个CSV
文件,其中包含14个预提取特征
,如颜色、长度等。
我想构建一个CNN
模型,该模型在训练和预测时同时使用图像数据和这些特征。我该如何在Python
中使用Keras
实现这样的模型?
回答:
我将假设你可以无障碍地导入数据,并且你已经将x数据分成了图像和特征部分,同时你有y数据作为每张图像的标签。
你可以使用keras的函数式API来让神经网络接受多个输入。
from keras.models import Modelfrom keras.layers import Conv2D, Dense, Input, Embedding, multiply, Reshape, concatenateimg = 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])