Keyboard Sound Classification Using CNN
A deep learning project that classifies keyboard types from audio recordings using Mel spectrograms and a Convolutional Neural Network (CNN).
Overview
This project explores how machine learning can identify different keyboard types based on their acoustic characteristics. Audio recordings are transformed into Mel spectrograms, which represent sound frequencies over time. A CNN learns patterns from these spectrograms to predict the keyboard category.
The project was developed using Python, PyTorch, and Librosa in Google Colab.
Keyboard Classes
The model predicts one of four keyboard categories:
| Label | Keyboard type |
|---|---|
| 0 | Blue switch |
| 1 | Red switch |
| 2 | Membrane |
| 3 | Pantograph |
Model Architecture
The classifier uses a CNN with:
- Convolutional layers for extracting audio features
- Batch normalization for training stability
- ReLU activation functions
- Max pooling for dimensionality reduction
- Adaptive average pooling
- Dropout to help reduce overfitting
- A fully connected output layer with four classes
The model is trained using cross-entropy loss and the Adam optimizer.
Audio Processing
The preprocessing pipeline includes:
- Loading audio recordings from WAV files.
- Resampling audio to 22,050 Hz.
- Padding or truncating audio to a fixed duration of 3 seconds.
- Generating 64-bin Mel spectrograms.
- Converting spectrograms to decibel values.
- Normalizing features before passing them to the CNN.
Repository Files
keyboard_sound_classification_cnn.ipynb: Notebook containing the training, evaluation, and testing workflow.keyboard_audio_classifier.pth: Saved PyTorch model checkpoint.
Getting Started
Requirements
pip install torch librosa soundfile numpy pandas scikit-learn matplotlib seaborn
Run the Notebook
- Open
keyboard_sound_classification_cnn.ipynbin Google Colab. - Obtain the dataset from its original source and extract it.
- Update the dataset paths if necessary.
- Run the notebook cells in order to preprocess the audio, train the CNN, and evaluate its performance.
Load the Trained Model
The .pth file contains the trained model weights and configuration metadata. To make predictions, recreate the same CNN architecture and use the same audio preprocessing pipeline as in the notebook.
Evaluation
The model is evaluated using a held-out test dataset.
Evaluation metrics include:
- Accuracy
- Precision
- Recall
- F1-score
- Confusion matrix
Test accuracy: Add your measured test accuracy here after running the evaluation.
Actual performance depends on the dataset, recording conditions, and similarity between training and test audio.
Limitations
- The model classifies keyboard types, not individual keys or typed characters.
- It does not predict passwords or reconstruct text from typing sounds.
- Performance on recordings from unfamiliar keyboards or noisy environments may be limited.
- The reported test performance should not be interpreted as a guarantee of real-world accuracy.
Future Improvements
- Expand the training dataset with more recording conditions.
- Apply data augmentation to improve robustness.
- Compare CNNs with alternative audio-classification architectures.
- Add a simple interface for uploading WAV files and viewing predictions.
- Evaluate generalization on recordings from keyboards not present in the training set.
Learning Objectives
This project demonstrates practical experience with:
- Audio preprocessing and feature extraction
- Mel spectrograms
- Convolutional Neural Networks
- PyTorch model training and evaluation
- Multiclass classification
- Saving and reusing trained models
License
The code in this repository may be released under the MIT License if you choose to license it that way. Check the original dataset's license before redistributing any dataset files or derived assets.
Author: Eyosyas Yoseph
Project type: Deep Learning ยท Audio Classification ยท Computer Vision and AI Portfolio