代码之家  ›  专栏  ›  技术社区  ›  user9165727

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

  •  1
  • user9165727  · 技术社区  · 7 年前

    使用下面显示的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)  
      

    我需要知道怎么做。

    1 回复  |  直到 4 年前
        1
  •  1
  •   Ary    7 年前

    您可以尝试将其保存并加载为HDF5文件格式。它比泡菜有几个优点。它的保存和加载速度更快(特别是对于大型阵列)。

    为此,您需要安装 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()