You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于HOG描述符训练线性SVM人体检测分类器时出现训练错误

Fixing HOG + Linear SVM Training for Human Detection

It looks like you’re hitting snags with your HOG-SVM setup, and there are a few key issues in your current code that we can sort out step by step:

Common Problems in Your Approach

  • Incorrect Feature Matrix Shape: You initialized x as a (3780, 2) array, but HOG descriptors are 1D vectors (typically thousands of elements long). Your feature matrix should follow the shape (number_of_samples, descriptor_length) instead.
  • Incomplete HOG Computation: Your code cuts off before calculating the HOG features for your samples—this is critical because the SVM relies on these descriptors to learn patterns.
  • Single Sample Limitation: Training an SVM with just one positive and one negative sample won’t yield a useful detector. Classifiers need diverse examples to generalize, but let’s first fix the code to work with your test setup.

Corrected Code Example

Here’s a complete, working version that properly computes HOG descriptors and trains a Linear SVM:

import numpy as np
import cv2
from sklearn.svm import LinearSVC

# Initialize HOG descriptor with standard human detection parameters
hog = cv2.HOGDescriptor(
    winSize=(64, 128),
    blockSize=(16, 16),
    blockStride=(8, 8),
    cellSize=(8, 8),
    nbins=9
)

# Load and preprocess positive/negative samples
positive_img = cv2.imread('G:/Project/Database/db/positive_sample.jpg', cv2.IMREAD_GRAYSCALE)
negative_img = cv2.imread('G:/Project/Database/db/negative_sample.jpg', cv2.IMREAD_GRAYSCALE)

# Resize images to match HOG window size (critical for consistent descriptor length)
positive_img = cv2.resize(positive_img, (64, 128))
negative_img = cv2.resize(negative_img, (64, 128))

# Compute flattened HOG descriptors for each sample
hog_positive = hog.compute(positive_img).flatten()
hog_negative = hog.compute(negative_img).flatten()

# Build feature matrix and label array
X = np.vstack([hog_positive, hog_negative])
y = np.array([1, 0])  # 1 = human, 0 = non-human

# Train the Linear SVM
svm = LinearSVC()
svm.fit(X, y)

# Optional: Test the classifier with a sample image
test_img = cv2.imread('test_human.jpg', cv2.IMREAD_GRAYSCALE)
test_hog = hog.compute(cv2.resize(test_img, (64, 128))).flatten()
prediction = svm.predict(test_hog.reshape(1, -1))
print(f"Prediction: {'Human detected' if prediction[0] == 1 else 'No human'}")

Key Fixes Explained

  1. HOG Parameter Setup: We use industry-standard parameters for pedestrian detection (64x128 window size, which aligns with common human dataset dimensions).
  2. Image Resizing: All samples must match the HOG window size to ensure the descriptor length is consistent across inputs.
  3. Feature Matrix Construction: Flattened HOG descriptors are stacked into a 2D array where each row represents one sample.
  4. Clear Labeling: Explicit labels for positive and negative samples are required for supervised training.

Critical Tips for Reliable Results

  • Use More Samples: Training with two samples will not produce a functional detector. The Pascal human dataset has hundreds of samples—aim for at least 50 positive and 100 negative examples.
  • Data Augmentation: For positive samples, add rotations, flips, or small shifts to increase diversity and improve generalization.
  • Hard Negative Mining: After initial training, use the classifier to find false positives in negative images and add those to your training set to refine accuracy.

内容的提问来源于stack exchange,提问作者Chetan Birajdar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:13:14