【问题标题】:Error while training SVM classifier on custom dataset [duplicate]在自定义数据集上训练 SVM 分类器时出错 [重复]
【发布时间】:2018-09-15 17:01:54
【问题描述】:

我一直在尝试使用 HOG 描述符训练线性 SVM 分类器。我在http://pascal.inrialpes.fr/data/human/ 有一个可用的数据集集合 目标是训练分类器来检测人类。在这样做时,首先我尝试将单个正图像和单个负图像训练到标签 1,2,如下面的代码所示:

import numpy as np
import cv2
from sklearn.svm import LinearSVC
x=np.zeros((3780,2))
x=np.array(x)

#positive image
pimg=cv2.imread('G:/Project/Database/db/pos/crop_000010.png',0)
pimg=cv2.resize(pimg,(68,128))
phog = cv2.HOGDescriptor()
pdescriptor = phog.compute(pimg)

#negative image
nimg=cv2.imread('G:/Project/Database/db/neg/1.jpg',0)
nhog = cv2.HOGDescriptor()
ndescriptor = nhog.compute(nimg)

label=[1,2] 
x=[pdescriptor,ndescriptor]
clf = LinearSVC()
clf.fit(x,label)

错误:

Traceback (most recent call last):

File "<ipython-input-6-215ad33848c8>", line 1, in <module>
runfile('G:/Project/Python programs/tr/training.py', wdir='G:/Project/Python 
programs/tr')

File "C:\ProgramData\Anaconda3\lib\site- 
packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site- 
packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "G:/Project/Python programs/tr/training.py", line 31, in <module>
clf.fit(x,label)

File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\svm\classes.py", 
line 227, in fit
dtype=np.float64, order="C")

File "C:\ProgramData\Anaconda3\lib\site- 
packages\sklearn\utils\validation.py", line 573, in check_X_y
ensure_min_features, warn_on_dtype, estimator)

File "C:\ProgramData\Anaconda3\lib\site- 
packages\sklearn\utils\validation.py", line 451, in check_array
% (array.ndim, estimator_name))

ValueError: Found array with dim 3. Estimator expected <= 2.

【问题讨论】:

    标签: python machine-learning scikit-learn scikit-image


    【解决方案1】:

    对于线性 SVC,您的 X 应该是形状为 (samples, features) 的二维数组,而您的 Y 应该是形状为 (samples,) 的一维数组。

    您正在用二维图像训练 SVC。样本 * 2-D 图像将产生 3-D 矢量,SVC 无法将其作为输入。因此,您必须首先将图像展平为一维向量,然后将它们输入 SVC。

    【讨论】:

    • 感谢您的成功,我必须将 x 展平为一维数组,然后将其重塑为 2,-1,因为我使用了两个样本。
    猜你喜欢
    • 2013-07-04
    • 2022-10-08
    • 2015-08-14
    • 2012-02-21
    • 1970-01-01
    • 2017-07-27
    • 2019-06-28
    • 2023-04-09
    • 2014-05-18
    相关资源
    最近更新 更多