在 CNN 提取的特征之上使用 SVM - 如何进行 Multi-Class 分类?
Using SVM on top of CNN extracted features - How to do Multi-Class classification?
在 MNIST 数据集中,有 10 个 classification 输出。现在,我喜欢使用 SVM 作为这个 classification 任务的 classifier。我使用 CNN 架构(不包括顶层或 classifier)首先从原始图像中提取特征,然后将其拟合到 SVM classifier.
SVM 是一个二进制 classifier,所以我们可以使用 One-vs-One 或 One-vs-Rest 做这种事情的方法。我使用以下来自 sci-kit learn 官方文档的实现代码。但是无法意识到我在哪里指定关于 multi-class 标签的模型或者这是 One-One 或 One-Rest方法。
数据集形状如下所示:
train : (2045, 32, 32)
label : (2045, 10)
使用non-top CNN代码提取特征后,我们得到:
train : (7636, 256) < - cnn_Xtrain
label : (7636,) < - Ytrain
我试过的 SVM classifier
# SVC classifier
SVMC = SVC(probability=True)
svc_param_grid = {'kernel': ['rbf'],
'gamma': [0.0001, 0.001],
'C': [1, 10, 50]}
gsSVMC = GridSearchCV(SVMC, param_grid = svc_param_grid, cv = K_fold,
scoring="accuracy", n_jobs= -1, verbose = 1)
gsSVMC.fit(cnn_Xtrain, Ytrain) # fitting extracted features
SVMC_best = gsSVMC.best_estimator_
在这个 classifier 中,SVM 如何理解这是一个多 class 问题或一对一或一对多的问题?打分结果对我来说比较可疑,我的评价确实接近98%。网格搜索( RBF )中指定的内核是否对此负责?或者我只是在这里做错了什么?
此外,使用CNN代码从原始图像中提取特征然后将其拟合到SVM或类似的classifier中是否可以?
在分类器的 'decision_function_shape' 参数中设置使用 one-vs-rest 或 one-vs-one 的决策天气(参见 doc for svc)。上面写着:
Whether to return a one-vs-rest (‘ovr’) decision function of shape (n_samples, n_classes) as all other classifiers, or the original one-vs-one (‘ovo’) decision function of libsvm which has shape (n_samples, n_classes * (n_classes - 1) / 2). However, one-vs-one (‘ovo’) is always used as multi-class strategy.
Changed in version 0.19: decision_function_shape is ‘ovr’ by default.
New in version 0.17: decision_function_shape=’ovr’ is recommended.
Changed in version 0.17: Deprecated decision_function_shape=’ovo’ and None.
所以现在 one vs rest 是默认值,因为您没有指定此参数,它可能是您的代码中使用的参数。
关于你关于在拟合前使用CNN进行特征提取的问题:一般来说应该是可以的。但是,使用正确的内核实际上并不是必需的。如果你想减少你的特征向量的维度,你可以只使用 pca or non linear embedding methods like manifold embedding 来获得更少的特征。
希望这对您有所帮助。
在 MNIST 数据集中,有 10 个 classification 输出。现在,我喜欢使用 SVM 作为这个 classification 任务的 classifier。我使用 CNN 架构(不包括顶层或 classifier)首先从原始图像中提取特征,然后将其拟合到 SVM classifier.
SVM 是一个二进制 classifier,所以我们可以使用 One-vs-One 或 One-vs-Rest 做这种事情的方法。我使用以下来自 sci-kit learn 官方文档的实现代码。但是无法意识到我在哪里指定关于 multi-class 标签的模型或者这是 One-One 或 One-Rest方法。
数据集形状如下所示:
train : (2045, 32, 32)
label : (2045, 10)
使用non-top CNN代码提取特征后,我们得到:
train : (7636, 256) < - cnn_Xtrain
label : (7636,) < - Ytrain
我试过的 SVM classifier
# SVC classifier
SVMC = SVC(probability=True)
svc_param_grid = {'kernel': ['rbf'],
'gamma': [0.0001, 0.001],
'C': [1, 10, 50]}
gsSVMC = GridSearchCV(SVMC, param_grid = svc_param_grid, cv = K_fold,
scoring="accuracy", n_jobs= -1, verbose = 1)
gsSVMC.fit(cnn_Xtrain, Ytrain) # fitting extracted features
SVMC_best = gsSVMC.best_estimator_
在这个 classifier 中,SVM 如何理解这是一个多 class 问题或一对一或一对多的问题?打分结果对我来说比较可疑,我的评价确实接近98%。网格搜索( RBF )中指定的内核是否对此负责?或者我只是在这里做错了什么?
此外,使用CNN代码从原始图像中提取特征然后将其拟合到SVM或类似的classifier中是否可以?
在分类器的 'decision_function_shape' 参数中设置使用 one-vs-rest 或 one-vs-one 的决策天气(参见 doc for svc)。上面写着:
Whether to return a one-vs-rest (‘ovr’) decision function of shape (n_samples, n_classes) as all other classifiers, or the original one-vs-one (‘ovo’) decision function of libsvm which has shape (n_samples, n_classes * (n_classes - 1) / 2). However, one-vs-one (‘ovo’) is always used as multi-class strategy. Changed in version 0.19: decision_function_shape is ‘ovr’ by default. New in version 0.17: decision_function_shape=’ovr’ is recommended. Changed in version 0.17: Deprecated decision_function_shape=’ovo’ and None.
所以现在 one vs rest 是默认值,因为您没有指定此参数,它可能是您的代码中使用的参数。
关于你关于在拟合前使用CNN进行特征提取的问题:一般来说应该是可以的。但是,使用正确的内核实际上并不是必需的。如果你想减少你的特征向量的维度,你可以只使用 pca or non linear embedding methods like manifold embedding 来获得更少的特征。
希望这对您有所帮助。