opencv 3.2 python svm problem

asked 2017-05-23 11:19:18 -0600

lanka gravatar image

updated 2017-05-23 12:30:11 -0600

hi ,I have trouble with predict hog image data with SVM.i have trained my own model using two image categories.and labelled as 0 and 1. created svm_data.dat file based on this example http://docs.opencv.org/3.1.0/dd/d3b/t...

import cv2
import numpy as np


SZ=20
bin_n = 16 # Number of bins
affine_flags = cv2.WARP_INVERSE_MAP|cv2.INTER_LINEAR
def hog(img):
    gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
    gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
    mag, ang = cv2.cartToPolar(gx, gy)
    bins = np.int32(bin_n*ang/(2*np.pi))    # quantizing binvalues in (0...16)
    bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
    mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
    hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
    hist = np.hstack(hists)     # hist is a 64 bit vector
    return hist
svm=cv2.ml.SVM_load('svm_data.dat') // train data
img=cv2.imread('test.jpg',1)
test=hog(img)
test1 = np.float32(test).reshape(-1,64)
y=svm.predict(test1)

print y

######results for any image gives below one
(0.0, array([[ 0.]], dtype=float32))
edit retag flag offensive close merge delete

Comments

what's the "trouble", - exactly ?

btw, somehow, i think, you want a grayscale image as input, not a color one.

berak gravatar imageberak ( 2017-05-23 11:25:56 -0600 )edit

actually, i need predict my input image(test.jpg) using SVM model...but always its gives the same answer((0.0, array([[ 0.]], dtype=float32)).

lanka gravatar imagelanka ( 2017-05-23 12:16:08 -0600 )edit

that's probably not the answer, you wanted, but still - a valid one. ;(

so, if your prediction was bad, so was your train input, probably, which is ?

berak gravatar imageberak ( 2017-05-23 12:19:38 -0600 )edit

sorry...my question is ..i have trained svm model using negative and positive colour image data.size 100*80. for that, i have used (http://docs.opencv.org/3.1.0/dd/d3b/t...) this example is given in website (little modified for my purpose..I think its fine). then save to data file. after reading a random image from my image collection(same size colour image)..predict with svm.predict function to get label result..problem is its always given for the zero(0) for all positives and negative samples. this is the problem I have faced.

lanka gravatar imagelanka ( 2017-05-23 12:41:37 -0600 )edit

how do you setup the SVM?

(and usually, HOG features are made from grayscale, not color images)

berak gravatar imageberak ( 2017-05-24 00:03:14 -0600 )edit

this is how i made SVM data file...yes it is a grayscale image

positive_path='E:/work/2017/SVM/pos'
negative_path='E:/work/2017/SVM/neg'
 Get positive samples
for filename in glob.glob(os.path.join(positive_path, '*.jpg')):
    img = cv2.imread(filename, 0)
    hist = hog(img)
    samples.append(hist)
    labels.append(1)
 Get negative samples
for filename in glob.glob(os.path.join(negative_path, '*.jpg')):
    img = cv2.imread(filename, 0)
    hist = hog(img)
    samples.append(hist)
    labels.append(0)

samples = np.float32(samples)
labels = np.array(labels)

svm = cv2.ml.SVM_create()
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setType(cv2.ml.SVM_C_SVC)
svm.setC(2.67)
svm.setGamma(5.383)

svm.train(samples, cv2.ml.ROW_SAMPLE,labels)
svm.save('svm_data.dat')
lanka gravatar imagelanka ( 2017-05-24 00:23:51 -0600 )edit

hmm, LINEAR should be ok, gamma is not used for linear, maybe the C value is too low ?

if you're using grayscale images now for training, don't forget to update your test code, too !

berak gravatar imageberak ( 2017-05-24 00:31:52 -0600 )edit

yes i have updated for img=cv2.imread('test.jpg',1) to img=cv2.imread('test.jpg',0) and tested again...not working yet.

lanka gravatar imagelanka ( 2017-05-24 00:35:58 -0600 )edit

any error in above code explanation??

lanka gravatar imagelanka ( 2017-05-24 01:10:39 -0600 )edit

hmm, not really.

did you try trainAuto() ?

normalizing the hog histograms might be a good idea, too.

berak gravatar imageberak ( 2017-05-24 01:18:15 -0600 )edit