How to train an SVM classifier on a satellite image using Python
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Support Vector Machines can work very well for satellite image classification when training data is limited and feature spaces are moderate. The key is converting raster pixels and labels into a clean tabular dataset, then applying consistent preprocessing. This guide demonstrates a full workflow in Python using rasterio and scikit-learn.
Prepare Features and Labels From Raster Data
For supervised training, you need predictor bands and labeled pixels. A common setup is one multiband image for features and one single-band raster for class labels.
If class imbalance is strong, stratified splitting and class weighting are critical for stable results.
Train an SVM With Scaled Features
SVM performance is sensitive to feature scale, so use a pipeline with StandardScaler. The code below performs a train and validation split and reports classification metrics.
class_weight="balanced" helps when some land-cover classes have far fewer training pixels than others.
Tune Hyperparameters With Cross-Validation
For production use, tune C and gamma using cross-validation. Keep search space realistic to avoid costly experiments with little gain.
Choose your scoring metric based on project goals. Macro F1 is often better than accuracy for imbalanced satellite classes.
Predict the Full Image and Save Classified Raster
After training, classify all pixels and write a new raster map with class ids.
If unlabeled areas should remain background, add a mask step before final export to restore original no-data semantics.
Data Quality and Feature Engineering Notes
SVM can perform strongly with spectral bands alone, but adding derived indices often improves class separability. Typical additions include normalized difference vegetation index and texture features computed over local windows.
Also ensure alignment between feature and label rasters. Different projections, resolutions, or extents can silently corrupt training labels. Always verify spatial metadata before flattening arrays.
Common Pitfalls
- Training on misaligned feature and label rasters, which produces misleading metrics.
- Skipping scaling before SVM, causing unstable decision boundaries.
- Evaluating only overall accuracy while minority classes fail.
- Predicting full images without managing no-data regions explicitly.
- Running large hyperparameter grids without stratified validation splits.
Summary
- Convert multiband raster data into a clean feature matrix with labeled pixels.
- Use a scaling plus SVM pipeline for stable training behavior.
- Tune
Candgammawith cross-validation and class-aware metrics. - Reconstruct full-image predictions and write typed GeoTIFF output.
- Validate raster alignment and class balance to avoid silent modeling errors.
Related reading
- How to train and evaluate simultaneously in Object Detection API ?
- How to train Tensorflow Object Detection images that do not contain objects?
- How to translateor shift images in tensorflow
- How to use a tensorflow graph in opencv c?
- How to train and predict using bag of words?
- How to train image pixel data in libsvm format to use for recognition with Java
- How to transform items using sklearn Pipeline?
- How to trigger message send of Fastapi websocket outside of Fastapi app
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.