0

I have been trying to train a linear SVM classifier using HOG descriptors. I have a collection of dataset available at http://pascal.inrialpes.fr/data/human/ The objective is to train the classifier to detect humans. In doing so, first I tried training a single positive image and single negative image to labels 1,2 as in the following code:

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)

The error:

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 Answers1

0

For a linear SVC, your X should be a 2-D array in the shape of (samples, features), and your Y should be a 1-D array with shape (samples,).

You are training your SVC on images, which are 2-D. Samples * a 2-D image would result in a 3-D vector, which the SVC cannot take as an input. Because of this, you must first flatten your images first into 1-D vectors and then feed them into the SVC.

Primusa
  • 13,136
  • 3
  • 33
  • 53