【问题标题】:SVM training for binary classifier always give class 0二元分类器的 SVM 训练总是给出 0 类
【发布时间】:2020-02-17 03:38:35
【问题描述】:

我正在使用SVM classifier 制作一个香蕉检测器项目。我有358 用于训练的图像样本,并使用test-size=0.2random_state=42 进行训练测试拆分。

我的数据集如下所示:

我已用01 将每个图像标记为文件名postfix。但是,classification_report(...) 总是返回:

Accuracy: 0.7352941176470589
UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
  'precision', 'predicted', average, warn_for)
              precision    recall  f1-score   support

           0       0.74      1.00      0.85        50
           1       0.00      0.00      0.00        18

    accuracy                           0.74        68
   macro avg       0.37      0.50      0.42        68
weighted avg       0.54      0.74      0.62        68

1 类在表摘要中始终有 0.00

我的完整源代码:

import os
import zipfile
import numpy as np
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
from sklearn.externals import joblib
import cv2

zip_ref = zipfile.ZipFile("dataset.zip", "r")
zip_ref.extractall()
zip_ref.close()

path = "bananas_dataset"
img_files = [(os.path.join(root, name))
    for root, dirs, files in os.walk(path)
    for name in files if name.endswith((".jpg"))]

winSize = (32, 32)
blockSize = (16, 16)
blockStride = (8, 8)
cellSize = (8, 8)
nbins = 9
derivAperture = 1
winSigma = -1.
histogramNormType = 0
L2HysThreshold = 0.2
gammaCorrection = 1
nlevels = 64
useSignedGradients = True

hog = cv2.HOGDescriptor(winSize, blockSize, blockStride,
    cellSize, nbins, derivAperture, winSigma, histogramNormType,
    L2HysThreshold, gammaCorrection, nlevels, useSignedGradients)

features = np.zeros((1, 324), np.float32)
labels = np.zeros(1, np.int64)
for i in img_files:
    img = cv2.imread(i)
    resized_img = cv2.resize(img, winSize)
    descriptor = np.transpose(hog.compute(resized_img))
    features = np.vstack((features, descriptor))
    labels = np.vstack((labels, int(i[-5])))

features = np.delete(features, (0), axis=0)
labels = np.delete(labels, (0), axis=0).ravel()

X_train, X_test, y_train, y_test = train_test_split(features,
                                                    labels,
                                                    test_size=0.2,
                                                    random_state=42)
print("X_train: {}, y_train: {}".format(X_train.shape, y_train.shape))
print("X_test: {}, y_test: {}".format(X_test.shape, y_test.shape))

clf = svm.SVC()
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)
print("Accuracy: {}".format(accuracy_score(y_test, y_pred)))

print("Classification report:")
print(classification_report(y_test, y_pred))
joblib.dump(clf, "banana_hog_svm_clf.pkl")

这导致我的预测过程总是返回类 0 作为结果。为什么会这样?

【问题讨论】:

  • 我认为 SVM 不推荐用于此类任务。通常在计算机视觉问题中,您需要卷积神经网络(提取特征)。
  • 我采用了github.com/lmzh123/ships_detection 方法,因为它与我的任务相似。它适用于 SVM。

标签: python machine-learning svm


【解决方案1】:

这可能是由于标签不平衡造成的。例如,如果 10% 的标签属于第 1 类,而 90% 的标签属于第 2 类,那么 SVM 将创建一个准确率为 90% 的模型,其中所有内容都被预测为第 2 类。

检查类标签的分布会有所帮助。

【讨论】:

  • 但是,在github.com/lmzh123/ships_detection 示例中,它有 3,600 个样本,其中 2,900 个用于第 2 类,700 个用于第 1 类。这也是不平衡的。
  • 那么性能如何?我没有看到该页面上报告的任何结果。你有什么理由 100% 依赖 GitHub 页面的功能?是不是很有名的代码包?
  • 准确率相当不错,94%左右。分类报告也是 0 级和 1 级之间的平衡。
  • 当它是一个不平衡的数据集时,准确率总是很好,因为我们通常有一个被支配的类。这就是我们应该检查少数类错误分类率或使用 F1 等其他措施的原因
猜你喜欢
  • 2015-07-12
  • 1970-01-01
  • 2012-06-27
  • 2016-07-24
  • 1970-01-01
  • 2016-12-20
  • 2016-01-22
  • 1970-01-01
  • 2013-08-12
相关资源
最近更新 更多