Computer Vision · Beginner level

Beginner level · Module 6: Introduction to Image Classification · Lesson 1 of 3

Training a Classical Image Classifier

Learners combine their colour and HOG features, split the data and train an SVM and a logistic regression classifier.

In this lesson you will

  • Prepare feature matrices and labels
  • Train and compare two classifiers

Key concepts

feature matrix X and labels ytrain_test_split with stratifyStandardScalerSVCLogisticRegression

The lesson

Stack the feature vectors into a matrix X (one row per image) and the category names into y. Split with train_test_split(X, y, test_size=0.25, stratify=y, random_state=42) so that every category appears in both sets.

Scale features with StandardScaler inside a pipeline, then train sklearn.svm.SVC (try kernel='rbf') and LogisticRegression(max_iter=1000). Both train in seconds on a few hundred images on any laptop.

Make sure near-identical photos (for example several shots of the same mango) are not split across training and test, or the test score will be unrealistically high.

Step by step

  1. Build X from colour and HOG features and y from folder names.
  2. Split by photo group so that near-duplicates stay together.
  3. Train SVC and LogisticRegression pipelines.
  4. Report test accuracy for both.

Practice activity

Train two classifiers on the project features and record their test accuracy.

Expected result: Two trained models with honestly measured accuracy and no near-duplicate leakage.

Check your understanding

  1. Why use stratify in train_test_split?

    Show answer

    So that each category appears in the same proportions in training and test sets.

  2. Why keep near-identical photos in the same split?

    Show answer

    Otherwise the model is tested on almost the same images it trained on, inflating the score.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.