简体   繁体   English

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

[英]How to store CNN extracted features to train a SVM classifier

Using the 2D CNN shown below to extract features from images, how I can store the extracted features in order to train an SVM to classify the features?使用如下所示的 2D CNN 从图像中提取特征,我如何存储提取的特征以训练 SVM 对特征进行分类?

Model:模型:

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'))

Extracting features with:提取特征:

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)

Steps:脚步:

  • Storing these extracted features from my image dataset in order to train an SVM classifier.从我的图像数据集中存储这些提取的特征以训练 SVM 分类器。

  • Using train_test_split() to split the train and test data使用train_test_split()拆分训练和测试数据

  • Train the classifier:训练分类器:

     clf = svm.SVC() clf.fit(X, y)

I need to know how to do this.我需要知道如何做到这一点。

You can try saving and loading them as HDF5 file format.您可以尝试将它们保存和加载为 HDF5 文件格式。 It has several advantages over pickle.与泡菜相比,它有几个优点。 It's much faster to save and load (especially for large arrays).保存和加载速度要快得多(特别是对于大型数组)。

To do so, you need to install h5py package.为此,您需要安装h5py包。 Example codes for saving and loading are as follows:保存和加载示例代码如下:

For saving:为了节省:

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()

For loading用于装载

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM