keras 如何存储CNN提取的特征以训练SVM分类器

egmofgnx  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(196)

使用下面所示的2D CNN从图像中提取特征,我如何存储提取的特征,以便训练SVM对特征进行分类?
产品型号:

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 150, 150)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(4))
model.add(Activation('softmax'))

提取要素时使用:

layer_name = 'layer_name'
intermediate_layer_model = Model(inputs=model.input,
                                 outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)

操作步骤:

  • 存储从我的图像数据集中提取的这些特征,以便训练SVM分类器。
  • 使用train_test_split()拆分训练数据和测试数据
  • 训练分类器:
clf = svm.SVC()
clf.fit(X, y)

我需要知道怎么做。

jogvjijk

jogvjijk1#

你可以试着把它们保存和加载成HDF5文件格式。它比pickle有几个优点。它的保存和加载速度要快得多(特别是对于大数组)。
需要安装h5py包,保存和加载的代码示例如下:

用于保存:

import h5py
h5f = h5py.File('your_file_name.h5', 'w')
h5f.create_dataset('layer_model', data=intermediate_layer_model)
h5f.create_dataset('output', data=intermediate_output)
h5f.close()

用于装载

import h5py
h5f = h5py.File('your_file_name.h5', 'r')
intermediate_layer_model = h5f['layer_model'][:]
intermediate_output = h5f['output'][:]
h5f.close()
rjee0c15

rjee0c152#

本文将帮助您使用CNN模型提取特征。
https://www.kdnuggets.com/2018/12/solve-image-classification-problem-quickly-easily.html/2
你将把所有图像的特征提取到一个numpy数组中,然后你可以决定将其存储为CSV文件或任何其他文件格式,如HDF5。

相关问题