This repository contains Python code for selecting the optimal bands from hyperspectral images using the Gray Wolf Optimization (GWO) algorithm. The GWO algorithm helps select the most informative spectral bands, enhancing the classification accuracy of hyperspectral images. Two methods are implemented for classification: K-Nearest Neighbors (KNN) and Random Forest classifiers.
The repository includes two main files:
GWO_KNN.py
: Uses a K-Nearest Neighbors (KNN) classifier for band selection.GWO_RandomForest.py
: Alternative approach that uses a Random Forest classifier for band selection.
Both files utilize Gray Wolf Optimization to identify the optimal subset of spectral bands for classification.
- Python 3.x
- NumPy
- Scikit-Learn
- Scipy
- Matplotlib
GrayWolfOpt
(Gray Wolf Optimization algorithm, should be implemented or imported)CompositeImages
andImagePlots
(modules for visualization)
This file performs band selection using the GWO algorithm and classifies using the K-Nearest Neighbors classifier.
- Objective Function: Defines the objective function for GWO. The function minimizes the classification error rate based on selected spectral bands.
- Dataset Loading: Loads the hyperspectral dataset (
Indian_pines_corrected.mat
for data andIndian_pines_gt.mat
for labels). - Optimization: The GWO algorithm optimizes the selected bands to minimize the error rate.
- Visualization: Selected bands are visualized as a composite image.
python GWO_KNN.py
An alternative approach where band selection is performed using the Random Forest classifier instead of KNN. This script is structured similarly to main.py, with the following differences:
- Classifier: Uses RandomForestClassifier with 100 estimators to evaluate the fitness of the selected bands.
- Parameters: Adjusted parameters for GWO, including a higher number of bands and wolves.
- Visualization: Visualizes the selected bands as a composite image.
python GWO_RandomForest.py
The objective function in both scripts takes in a set of selected bands, trains a classifier (KNN or Random Forest), and returns the error rate based on classification accuracy. The goal is to minimize this error rate, which represents the fitness score for GWO.
The dataset used is the Indian Pines hyperspectral dataset. The data is reshaped to (pixels, bands) format, and unlabeled pixels (background) are removed for accurate band selection.
The GWO algorithm optimizes the band selection process. The algorithm is configured with:
dim: Number of bands to select.
lb and ub: Lower and upper bounds for band indices.
num_wolves and max_iter: Parameters defining the number of wolves and iterations.
A timer function displays the elapsed time for the optimization process.
Upon running each script, the output includes:
Best Bands Selected: The optimal subset of bands for classification.
Best Score (Error Rate): The minimized classification error achieved.
Composite Image: A visualization of the selected bands.
- Gray Wolf Optimizer: Inspired by the social hierarchy and hunting strategy of gray wolves.
- Indian Pines Dataset: Commonly used hyperspectral dataset for classification tasks.