【问题标题】:Separate Positive and Negative Samples for SVM Custom Object Detector为 SVM 自定义对象检测器分离正负样本
【发布时间】:2017-12-15 17:09:50
【问题描述】:

我正在尝试通过在 OpenCV 上使用 HOG+SVM 方法来训练自定义对象检测器。

我已经成功地使用下面的代码行从我的正样本和负样本中提取了 HOG 特征:

import cv2

hog = cv2.HOGDescriptor()
def poshoggify():

        for i in range(1,20):
            image = cv2.imread("/Users/munirmalik/cvprojek/cod/pos/" + str(i)+ ".jpg")
            (winW, winH) = (500, 500)

            for resized in pyramid(image, scale=1.5):
                # loop over the sliding window for each layer of the pyramid
                for (x, y, window) in sliding_window(resized, stepSize=32, windowSize=(winW, winH)):
                    # if the window does not meet our desired window size, ignore it
                    if window.shape[0] != winH or window.shape[1] != winW:
                        continue

                    img_pos = hog.compute(image)
                    np.savetxt('posdata.txt',img_pos)

                    return img_pos

以及负样本的等效函数。

如何格式化数据,让 SVM 知道哪个是正的,哪个是负的?

此外,我如何将这种训练转化为通过网络摄像头检测所需对象的“测试”?

【问题讨论】:

  • 您想使用哪个库/功能进行 svm 训练? Afaik 通常为每个类添加 1 或 0 或 -1 或任何其他数字
  • @Micka 我很擅长 sklearn 的 svm 或 opencv 中内置的 SVM 函数。我不太清楚你所说的每个班级的人数是什么意思。

标签: python-2.7 opencv svm


【解决方案1】:

如何格式化数据,让 SVM 知道哪个是正的,哪个是负的?

您现在将创建另一个名为labels 的列表,该列表将存储与相应图像关联的类值。例如,如果您有一组如下所示的特征训练集:

features = [pos_features1, pos_features2, neg_features1, neg_features2, neg_features3, neg_features4]

你会有一个相应的标签类,比如

labels = [1, 1, 0, 0, 0, 0]

然后你可以像这样将它提供给一个分类器:

clf=LinearSVC(C=1.0,  class_weight='balanced')
clf.fit(features,labels)

此外,如何将这种训练转化为通过网络摄像头检测所需对象的“测试”?

在训练之前,您应该将标记数据集(groundtruth)拆分为训练和测试数据集。您可以使用skilearns KFold module 来执行此操作。

【讨论】:

  • 我已经创建了特征和标签。我尝试将它们输入到我的分类器中,但我收到此错误“TypeError:float() 参数必须是字符串或数字”。这很奇怪,因为它们都是数字。我需要进一步格式化它们吗?
  • 这应该作为一个单独的问题提出。没有看到错误发生的实际行很难调试,
猜你喜欢
  • 2021-05-22
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 2016-03-13
  • 2020-05-26
  • 2021-10-23
  • 2019-04-10
  • 2019-01-13
相关资源
最近更新 更多