Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to train HoG manually in Python?

Hi guys, I'm trying to train my SVM detector and set it on the SVM in detectMultiscale-method implementation of HoG. I want to do it with the Inria person data set in order to achieve maybe the same results, as the guys, who invented the method. Unfortunately I don't have an idea what I'm doing wrong, but I dont detect any people on my test-picture (basically it is a one big picture containing some of the learned images). Below I'm posting my code, but if you have an elegant solution please feel free to share. Thanks.

import glob
import cv2
from sklearn import svm

pos_im = glob.glob('D:/Yasen/Tools/TrainSVM/TrainSVM/TrainSVM/Training_images/pos/*')
neg_im = glob.glob('D:/Yasen/Tools/TrainSVM/TrainSVM/TrainSVM/Training_images/neg/*')
test_im = glob.glob('D:/Yasen/Tools/TrainSVM/TrainSVM/TrainSVM/Testing_images/*')
im_test = 'D:/Yasen/Tools/TrainSVM/TrainSVM/TrainSVM/Testsequenz2.png'
img = cv2.imread(im_test)

win_size=(64,128)
block_size=(16,16)
block_stride=(8,8)
cell_size=(8,8)
nrbins=9
cnt=0
pos_feat = np.zeros((len(pos_im),3780)).astype(np.float32)
neg_feat = np.zeros((len(neg_im),3780)).astype(np.float32)


hog = cv2.HOGDescriptor(win_size,block_size,block_stride,cell_size,nrbins)

for file in pos_im:
    im_temp_pos= cv2.imread(file)
    im_temp_pos = cv2.resize(im_temp_pos,win_size)
    pos_feat[cnt,:] = np.reshape(hog.compute(im_temp_pos),3780)
    cnt = cnt + 1
cnt = 0
for file in neg_im:
    im_temp_neg =cv2.imread(file)
    im_temp_neg = cv2.resize(im_temp_neg,win_size)
    neg_feat[cnt,:] = np.reshape(hog.compute(im_temp_neg),3780)
    cnt=cnt+1

feat = np.float32(np.concatenate((pos_feat,neg_feat),axis=0))
pos_resp = np.repeat(1,len(pos_im))
neg_resp = np.repeat(-1,len(neg_im))
responces = np.concatenate((pos_resp,neg_resp),axis=0)

clf = svm.SVC(C=0.01,kernel = 'linear')
clf.fit(feat,responces)
svm_classificator = np.array(my_svm().coef_)
hog = cv2.HOGDescriptor(win_size,block_size,block_stride,cell_size,nrbins)

hog.setSVMDetector(svm_classificator)
winStr = (16,16)
pad = (16,16)
sc = 1.02

start = time.time()
found,w = hog.detectMultiScale(img,winStride=winStr,padding=pad,scale=sc)
end = time.time()

How to train HoG manually in Python?

Hi guys, I'm trying to train my SVM detector and set it on the SVM in detectMultiscale-method implementation of HoG. I want to do it with the Inria person data set in order to achieve maybe the same results, as the guys, who invented the method. Unfortunately I don't have an idea what I'm doing wrong, but I dont detect any people on my test-picture (basically it is a one big picture containing some of the learned images). Below I'm posting my code, but if you have an elegant solution please feel free to share. Thanks.

import glob
import cv2
from sklearn import svm

pos_im = glob.glob('D:/Yasen/Tools/TrainSVM/TrainSVM/TrainSVM/Training_images/pos/*')
neg_im = glob.glob('D:/Yasen/Tools/TrainSVM/TrainSVM/TrainSVM/Training_images/neg/*')
test_im = glob.glob('D:/Yasen/Tools/TrainSVM/TrainSVM/TrainSVM/Testing_images/*')
im_test = 'D:/Yasen/Tools/TrainSVM/TrainSVM/TrainSVM/Testsequenz2.png'
img = cv2.imread(im_test)

win_size=(64,128)
block_size=(16,16)
block_stride=(8,8)
cell_size=(8,8)
nrbins=9
cnt=0
pos_feat = np.zeros((len(pos_im),3780)).astype(np.float32)
neg_feat = np.zeros((len(neg_im),3780)).astype(np.float32)


hog = cv2.HOGDescriptor(win_size,block_size,block_stride,cell_size,nrbins)

for file in pos_im:
    im_temp_pos= cv2.imread(file)
    im_temp_pos = cv2.resize(im_temp_pos,win_size)
    pos_feat[cnt,:] = np.reshape(hog.compute(im_temp_pos),3780)
    cnt = cnt + 1
cnt = 0
for file in neg_im:
    im_temp_neg =cv2.imread(file)
    im_temp_neg = cv2.resize(im_temp_neg,win_size)
    neg_feat[cnt,:] = np.reshape(hog.compute(im_temp_neg),3780)
    cnt=cnt+1

feat = np.float32(np.concatenate((pos_feat,neg_feat),axis=0))
pos_resp = np.repeat(1,len(pos_im))
neg_resp = np.repeat(-1,len(neg_im))
responces = np.concatenate((pos_resp,neg_resp),axis=0)

clf = svm.SVC(C=0.01,kernel = 'linear')
clf.fit(feat,responces)
svm_classificator = np.array(my_svm().coef_)
hog = cv2.HOGDescriptor(win_size,block_size,block_stride,cell_size,nrbins)

hog.setSVMDetector(svm_classificator)
winStr = (16,16)
pad = (16,16)
sc = 1.02

start = time.time()
found,w = hog.detectMultiScale(img,winStride=winStr,padding=pad,scale=sc)
end = time.time()