在自定义数据集上训练SVM分类器时出错

时间:2018-04-05 19:05:04

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

我一直在尝试使用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.

1 个答案:

答案 0 :(得分:0)

对于线性SVC,您的X应该是(样本,特征)形状的二维数组,并且您的Y应该是具有形状(样本)的一维数组。

您正在训练您的SVC图像,这是2-D。样本* 2-D图像将产生3-D向量,SVC不能将其作为输入。因此,您必须先将图像展平为1-D向量,然后将其输入SVC。