This project implements a machine learning pipeline to classify images into three texture classes—grass, sand, and stairs—based on their color and textural properties. The pipeline extracts first-order color features and second-order Gray-Level Co-occurrence Matrix (GLCM) features, and uses nested cross-validation to tune and evaluate models.
- Introduction
- Project Structure
- Data Preparation
- Methodology
- Model Training and Evaluation
- Results and Discussion
- Installation and Setup
- Usage
- Dependencies
- References
The goal of this project is to develop a machine learning model capable of classifying images into one of three texture categories. Image URLs are provided in text files (for grass, sand, and stairs), and images undergo processing before feature extraction and model evaluation.
- MLPR25_exercise_Iftikhar_Momina.ipynb: Jupyter Notebook containing the complete pipeline.
- Data Files: Text files (
grass.txt,sand.txt,stairs.txt) containing image URLs. - Visualizations & Outputs: Sample images, pairplots, histograms, PCA plots, confusion matrices, and summary tables from GridSearchCV and nested CV.
The dataset comprises image URLs divided into three categories:
- Grass
- Sand
- Stairs
- Resize: Images are resized to a consistent dimension (e.g., 128×128 or 256×256 pixels).
- Grayscale Conversion: Converted to grayscale for texture analysis.
- Quantization: Grayscale images are quantized to 8 levels (0–7) to enhance GLCM computation.
- Drive Mounting: Uses Google Colab to mount Google Drive and access text files.
- Image Processing: Functions are provided to download, resize, convert to grayscale, and quantize each image.
-
First Order Color Features:
- Computes mean and variance for each RGB channel.
-
Second Order Texture Features:
- Extracts GLCM features by calculating the correlation for pixel distances of 1 and 2 and angles of 0° and 90°.
- The chosen distances capture both local fine textures and broader texture patterns.
- Pairplots: To visualize relationships between standardized GLCM features.
- Histograms: Overlaid histograms for each Z-scored feature across classes.
- PCA: Principal Component Analysis is used to visualize clusters and assess class separability.
Three classifiers are tuned using GridSearchCV with 5-fold stratified cross-validation:
-
Ridge Classifier:
- Parameter: L2 regularization strength (
alphavalues: 0.001, 0.01, 0.1, 1.0) - Best Result:
alpha=0.001with an accuracy of ~62.7%.
- Parameter: L2 regularization strength (
-
Random Forest Classifier:
- Parameters:
n_estimators: 100 to 300 (step 50)max_features:'sqrt','log2', orNonebootstrap: True/False
- Best Result: e.g.,
{bootstrap=True, max_features=None, n_estimators=100}with ~55.1% accuracy. - Feature Importances: Corr_Dist1_Angle0 is typically the most significant.
- Parameters:
-
Multi-Layer Perceptron (MLP) Classifier:
- Parameters:
- Hidden layer sizes from 15 to 40 neurons (step 5)
- Activation functions:
tanhandrelu - Solvers:
sgdandadam - Regularization (
alpha): [0.01, 0.1, 1] - Validation fraction: 0.1 and 0.3
- Best Result: e.g.,
{hidden_layer_sizes: (30,), activation: tanh, solver: adam, validation_fraction: 0.1, alpha: 0.1}with ~63.2% accuracy.
- Parameters:
Nested cross-validation is used for unbiased performance estimation:
- Inner Loop: 4-fold stratified cross-validation for hyperparameter tuning.
- Outer Loop: 5-fold stratified cross-validation for performance estimation.
The evaluation reports:
- Best hyperparameters per outer fold.
- Accuracy on each fold and an overall mean accuracy.
- Confusion matrices.
- For Random Forest, additional feature importances.
-
Ridge Classifier:
- Mean outer accuracy: ~62.7%
- Sample Confusion Matrix:
[[39, 18, 5], [ 4, 44, 13], [16, 13, 33]]
-
Random Forest:
- Mean outer accuracy: ~53.5%
- Key Feature: Corr_Dist1_Angle0.
-
MLP Classifier:
- Mean outer accuracy: ~58.4%
- Best Model: The Ridge Classifier performs best with the current feature set, indicating that a simple linear model with L2 regularization can effectively discriminate between textures.
- Limitations:
- Moderate accuracy suggests that GLCM features alone might not capture complex textures fully.
- Variability in image resolution and dataset size may affect performance.
- Improvements:
- Incorporate additional features (e.g., deeper texture features or color-texture fusion).
- Use data augmentation to expand and diversify the dataset.
- Experiment with advanced deep learning approaches.
- Mount Google Drive:
In Google Colab, mount your drive to access data files.from google.colab import drive drive.mount('/content/drive')
Use the command below to install the necessary packages:
pip install numpy pandas matplotlib scikit-image scikit-learn seaborn
Place grass.txt, sand.txt, and stairs.txt in the specified directory (e.g., /content/drive/MyDrive/MLPR/).
- Open the Notebook:
OpenMachine learning and pattern recognition.ipynbin Google Colab. - Run the Notebook Cells Sequentially:
- Data loading and preprocessing.
- Image processing (resize, grayscale, quantization).
- Feature extraction and visualization.
- Hyperparameter tuning and nested cross-validation.
- Analysis of output plots and evaluation metrics.
- Python: 3.x
- Libraries: