我需要在SVM的预测时计算历史吗? [英] Do I need to calculate hist at prediction time in SVM?

查看:59
本文介绍了我需要在SVM的预测时计算历史吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码训练我的数据集:

I am training my dataset with the below code:

for file in glob.glob('C:\*.png'):
    image = cv2.imread(file, 1)
    image = cv2.resize(img, (60, 120))
    hog = cv2.HOGDescriptor((60,120), (8,8), (4,4), (4,4), 9)
    hist = hog.compute(image)
    samples.append(hist)
    labels.append(-1)

我正在使用hist = hog.compute(image).该代码在训练部分中,但是当我执行预测部分时:

I am using hist = hog.compute(image). This code is in the training part, but when I do the prediction part:

hog = cv2.HOGDescriptor((60,120), (8,8), (4,4), (4,4), 9)
svm = cv2.ml.SVM_load('svm_data.xml')
sv = svm.getSupportVectors()
rho, alpha, svidx = svm.getDecisionFunction(0)
svm_new = np.append(sv, -rho)
hog.setSVMDetector(svm_new)

我没有使用hist = hog.compute(image),结果也不是很好.使用Multiscale时是否需要在预测部分中使用hog.compute?

I am not using hist = hog.compute(image), and my results are not as good. Do I need to use hog.compute in prediction part while using Multiscale?

found, w = hog.detectMultiScale(img,hitThreshold=0,winStride=(8,8),padding=(16,16), scale=1.05, finalThreshold = 2.0,useMeanshiftGrouping=False)

当我尝试使用它时,它给出了一个错误,没有它,我不会得到很好的结果.我在训练部分或预测部分做错了吗?

When I try to use it, it gives an error, and without it, I am not getting good results. Am I doing wrong in the training part or in the prediction part?

更新:用于培训SVM的完整代码:

Update: Complete code using for training SVM:

samples = []
labels = []    
for filename in glob.glob('C:\*.png'):
    img = cv2.imread(filename, 0)
    img = cv2.resize(img, (160, 320))
    hog = cv2.HOGDescriptor((160,320), (16,16), (8,8), (8,8), 9)
    hist = hog.compute(img)
    samples.append(hist)
    labels.append(+1)

for file in glob.glob("C:\\*.jpg"):
    img = cv2.imread(file, 0)
    img = cv2.resize(img, (160, 320))
    hog = cv2.HOGDescriptor((160,320), (16,16), (8,8), (8,8), 9)
    hist = hog.compute(img)
    samples.append(hist)
    labels.append(-1)

# Convert objects to Numpy Objects
samples = np.float32(samples)
labels = np.array(labels)

# Shuffle Samples
rand = np.random.RandomState(321)
shuffle = rand.permutation(len(samples))
samples = samples[shuffle]
labels = labels[shuffle]

# Create SVM classifier
svm = cv2.ml.SVM_create()
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)
# Train
svm.train(samples, cv2.ml.ROW_SAMPLE, labels)
svm.save('C:\svm_data.xml')

用于预测的代码:

sample=[]
hog = cv2.HOGDescriptor((160,320), (16,16), (8,8), (8,8), 9)
svm = cv2.ml.SVM_load('C:\svm_data.xml')
sv = svm.getSupportVectors()
rho, alpha, svidx = svm.getDecisionFunction(0)
svm_new = np.append(sv, -rho)
hog.setSVMDetector(svm_new)

for file in glob.glob("C:\\Test\\*.jpg"): 
    img = cv2.imread(file, 0) 
    img = cv2.resize(img, (160, 320))
    hog = cv2.HOGDescriptor((160,320), (16,16), (8,8), (8,8), 9)
found, w = hog.detectMultiScale(img,hitThreshold=0,winStride=(8,8),padding=(16,16), scale=1.05, finalThreshold = 2.0,useMeanshiftGrouping=False)
   for (x, y, w, h) in found:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
   cv2.imshow("Image", img)
   cv2.waitKey()

推荐答案

根据您的代码,所有示例均属于同一类:

According to your code, all the samples belong to the same class:

labels.append(-1)

您的SVM分类器无法从中学到任何东西.您需要同时向SVM提供正面示例(标记为1)和负面示例(通常标记为0或-1).如果您的数据集平衡,那将很有帮助:正图像和负图像的数量大致相同.

Your SVM classifier can't learn anything from this. You need to present to SVM both positive examples (labeled as 1) and negative ones (usually labeled as 0 or -1). It would be helpful if your dataset is balanced: that is amount of positive and negative images is roughly the same.

在对SVM进行正确的培训之后,使用hog.detectMultiScale()hog.detect()使hog(通过hog.setSVMDetector())意识到它会自动"报告正匹配.它结合了两个操作:计算HOG描述符,并使用提供的SVM对它们进行分类.另外,hog.detectMultiScale()自动增加图像并可以对重叠的检测进行分组.

After your SVM is trained properly, and hog is made aware of it (by hog.setSVMDetector()) using hog.detectMultiScale() or hog.detect() will "automatically" report positive matches. It combines two operation: calculates HOG descriptors and classifies them using provided SVM. In addition hog.detectMultiScale() automatically increases the image and optionally groups the overlapped detections.

现在为什么在训练阶段需要hog.compute(image):这将计算原始HOG描述符.这是分类器的输入.这些描述符只是按照特定方式计算的一堆数字,它们本身并不表示图像中是否存在要寻找的对象.要做出此决定,您需要某种分类器,而SVM只是一种可能的选择.您不必使用它,它通常可以产生很好的效果,并且默认情况下已包含它.

Now why you need hog.compute(image) on the training phase: this calculates raw HOG descriptors. This is the input to your classifier. Those descriptors is just a bunch of numbers calculated in specific way, and by themselves do not indicate if there is an object that you are looking for in the image. To make this decision, you need some kind of a classifier, and SVM is just a possible choice. You do not have to use it, it just usually produces very good results, and is included as a default.

更新 在OpenCV 示例中了解如何进行预测:

Update See how prediction is done in the OpenCV example:

这篇关于我需要在SVM的预测时计算历史吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆