Skip to content

Latest commit

 

History

History
173 lines (128 loc) · 4.12 KB

File metadata and controls

173 lines (128 loc) · 4.12 KB

Image Classifier

中文 | English

A traditional machine learning-based image classification system supporting multiple feature extraction methods and classification algorithms.

Features

Feature Extraction

Type Method Description
Keypoint SIFT Scale-Invariant Feature Transform
Dense HOG Histogram of Oriented Gradients
Dense LBP Multi-scale Local Binary Patterns
Fused fused HOG + LBP + Color Histogram
Fused hog_lbp HOG + LBP
Fused hog_color HOG + Color Histogram

Classifiers

Classifier Features
SVM Supports linear, RBF, polynomial kernels
Random Forest Ensemble method, robust performance
XGBoost State-of-the-art gradient boosting
KNN Simple baseline

CIFAR-10 Accuracy

Feature SVM-rbf XGBoost
HOG 50.7% 47.1%
Fused 53.9% 51.8%

Installation

pip install -r requirements.txt

# macOS requires libomp (XGBoost dependency)
brew install libomp

Usage

Command Line

# Run full pipeline
python main.py all -m 500

# Specify feature types
python main.py all -f fused hog lbp -m 500

# Specify classifiers
python main.py all -m 500 -c xgboost svm

# Save results to database
python main.py all -m 500 --save-to-db

# Step-by-step execution
python main.py extract -m 500    # Extract features
python main.py train -m 500      # Train models
python main.py classify -m 500   # Evaluate models

# Visualize results
python main.py visualize <experiment_id>

Python API

from src.classifier import ImageClassifier

# Create classifier
clf = ImageClassifier('fused')  # Options: fused, hog, lbp, sift

# Train
clf.train_all(max_samples=500)

# Predict
import cv2
image = cv2.imread('test.jpg')
result = clf.predict_class(image, classifier_type='xgboost')
print(f'Class: {result}')

# Evaluate
correct, total = clf.evaluate(classifier_type='svm', kernel='rbf', max_samples=100)
print(f'Accuracy: {correct}/{total} = {correct/total*100:.1f}%')

Project Structure

simple-classifier/
├── config/
│   └── settings.py        # Configuration
├── src/
│   ├── classifier.py      # Core classifier
│   ├── features/
│   │   └── extractor.py   # Feature extraction
│   ├── database/          # Database operations
│   └── visualization/     # Visualization
├── data/
│   ├── train_set/         # Training data
│   └── test_set/          # Test data
├── models/                # Saved models
├── main.py
└── requirements.txt

Data Format

data/
├── train_set/
│   ├── airplane/
│   │   ├── airplane (1).png
│   │   └── ...
│   ├── automobile/
│   └── ...
└── test_set/
    └── ...

Data Source

This project uses CIFAR-10 dataset for image classification. CIFAR-10 consists of:

  • 60,000 32x32 color images in 10 classes
  • 50,000 training images and 10,000 test images
  • Classes: airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck

Download and extract the dataset:

# Download from official source
wget https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
tar -xzf cifar-10-python.tar.gz

# Or use the provided script to convert to image files
python scripts/prepare_cifar10.py

Note: The data/ directory is not included in the repository. Please download the dataset separately.

Configuration

Edit config/settings.py:

# Classifier parameters
CLASSIFIER_PARAMS = {
    'svm': {'C': 10.0, 'gamma': 'scale'},
    'random_forest': {'n_estimators': 200, 'max_depth': 20},
    'xgboost': {'n_estimators': 200, 'max_depth': 8},
    'knn': {'n_neighbors': 10},
}

# Vocabulary size
VOCABULARY_SIZE = 200

Notes

  • For small images (e.g., CIFAR-10 32x32), use fused or hog features
  • Keypoint features (SIFT) perform poorly on small images
  • Database storage is disabled by default; use --save-to-db to enable

License

MIT