Skip to content

🎧 Cats & Dogs Audio Classification — Overview

A binary audio classification project that distinguishes cat meowing from dog barking using raw .wav files. Demonstrates end-to-end ML pipeline construction — from unstructured audio data through feature engineering to trained deep learning and classical ML classifiers.


ML Pipeline

flowchart LR
    A[Raw .wav Files<br/>277 samples] --> B[EDA<br/>Signal lengths, sample rates]
    B --> C[Resampling<br/>Normalise to 22,050 Hz]
    C --> D[Length Normalisation<br/>Pad/truncate → 160,000 samples]
    D --> E[MFCC Extraction<br/>13 coefficients per frame]
    E --> F[CNN Training<br/>4-layer Conv2d + 5-Fold CV]
    E --> G[Logistic Regression<br/>Baseline comparison]
    F --> H[Inference<br/>Per-sample prediction]
    G --> H

Dataset

Property Value
Total samples ~277 .wav files
Classes Cat (164 samples), Dog (113 samples)
Sample rate 22,050 Hz
Fixed length 160,000 samples (~7.3 seconds)

Technology Stack

Layer Technology Purpose
Language Python 3.11 Application runtime
Deep Learning PyTorch (torch.nn, torch.optim) CNN architecture, training loops
Audio Processing torchaudio, librosa MFCC, mel spectrograms, STFT, resampling
Traditional ML scikit-learn Logistic Regression baseline, K-Fold CV
Data Handling Pandas, NumPy DataFrames, array operations
Visualisation Matplotlib, librosa.display Spectrograms, waveforms, histograms
Environment Google Colab, Jupyter Notebooks Collaborative development

Methodology

1. Exploratory Data Analysis

  • Catalogued all audio files and inspected class distribution
  • Analysed signal length distribution (histogram) to determine the optimal fixed-length target
  • Verified consistent sample rates across the dataset

2. Audio Feature Engineering

Feature What It Captures
MFCC (13 coefficients) Timbral/tonal characteristics weighted to human auditory perception
Mel Spectrogram Time-frequency energy distribution on perceptual scale
STFT Raw frequency decomposition per time window
Spectral Centroid "Centre of gravity" of frequency content (overall pitch)
Spectral Rolloff Frequency threshold containing most energy (high-freq characterisation)

3. Custom PyTorch Data Pipeline

A production-quality CatsAndDogsDataset class extending torch.utils.data.Dataset:

  • Resampling — normalises all audio to 22,050 Hz
  • Length normalisation — zero-pads short clips, truncates long clips to 160,000 samples
  • On-the-fly MFCC transformtorchaudio.transforms.MFCC(n_mfcc=13, n_fft=400, hop_length=160, n_mels=23)
  • Label encoding — binary classification (0 = cat, 1 = dog) parsed from filename
  • Integrated with DataLoader for batched training (batch_size=32)

4. CNN Architecture

A 4-layer Convolutional Neural Network built from scratch:

Input (1 × 13 × T MFCC)
  → Conv2d(1→16, 3×3) + ReLU + MaxPool2d
  → Conv2d(16→32, 3×3) + ReLU + MaxPool2d
  → Conv2d(32→64, 3×3) + ReLU + MaxPool2d
  → Conv2d(64→128, 3×3) + ReLU + MaxPool2d
  → Flatten
  → Linear → 2 classes
  → Softmax → Prediction

Training configuration:

Parameter Value Rationale
Optimizer Adam Adaptive learning rates per-parameter; robust default
Learning rate 0.001 Adam's recommended default; proven stable starting point
Loss function CrossEntropyLoss Standard for multi-class classification
Epochs 10 Small dataset overfits quickly; sufficient for convergence
Validation 5-Fold Cross-Validation Robust evaluation using all samples; reliable with only 277 data points

5. Classical ML Baseline

A Logistic Regression model using scikit-learn:

  • 20 MFCCs extracted per file via librosa.feature.mfcc
  • Additional features: spectral centroid, spectral rolloff
  • Standard train/test split evaluation
  • Demonstrates methodological rigour — always compare deep learning against simpler approaches

Design Decisions

Why MFCC over raw audio?

Raw audio is 160,000 numbers per sample — high-dimensional and semantically sparse. MFCCs compress this into a 13 × T matrix that captures perceptually meaningful frequency content, weighted to match human hearing (mel scale). This gives the CNN a compact, information-dense input.

Why ReLU activation?

  • No vanishing gradient — sigmoid/tanh squash gradients toward zero in deep networks, causing earlier layers to stop learning. ReLU's gradient is 0 or 1.
  • Computational efficiency — simple threshold comparison vs. exponential functions
  • Sparsity — neurons with negative input output zero, creating efficient sparse representations

Why MaxPool2d?

  • Translation invariance — detects patterns regardless of their exact temporal position (a meow at t=1s and t=3s are the same pattern)
  • Progressive abstraction — each layer captures increasingly high-level features (from "energy spike at 500Hz" to "repeated harmonic pattern typical of barking")
  • Max over Average — preserves the strongest feature activation in a region, avoiding dilution of distinctive signals

Why double channels at each layer? (16 → 32 → 64 → 128)

MaxPool halves spatial dimensions at each stage, reducing available positional information. Doubling channels compensates by providing more "vocabulary" to represent increasingly abstract pattern combinations. This follows the balanced-computation principle established by VGGNet — each layer performs roughly equal work.

Why 160,000 samples as fixed length?

Neural networks require fixed-size inputs. The average signal length in the dataset (determined via EDA) was ~160,000 samples (~7.3 seconds at 22,050 Hz). This minimises both information loss from truncation and wasted computation from excessive zero-padding.

Why 5-Fold Cross-Validation over a single train/test split?

With only 277 samples, a single 80/20 split gives a test set of ~55 samples — too small for reliable accuracy estimation. K-Fold ensures every sample is used for testing exactly once, providing a more statistically robust performance metric and revealing model stability across different data partitions.

Why compare CNN against Logistic Regression?

The simplest solution should always be evaluated first. If Logistic Regression achieves 95% accuracy, a complex CNN isn't justified. This comparison demonstrates awareness that model complexity must be earned — a principle critical in production ML where simpler models are cheaper to deploy and maintain.


Skills Demonstrated

Competency Evidence
Deep Learning (PyTorch) Custom CNN architecture, training loops, loss functions, optimizers
Audio/Signal Processing MFCC, mel spectrograms, STFT, spectral features, resampling
Feature Engineering Domain-specific extraction from unstructured audio data
Data Pipeline Design Custom PyTorch Dataset + DataLoader with on-the-fly transforms
Model Evaluation K-Fold cross-validation for robust performance estimation
Classical ML Logistic Regression baseline for methodological comparison
EDA Statistical analysis of signal properties, distribution visualisation
Unstructured Data Working with raw .wav audio — not tabular or image data
End-to-End ML Data loading → EDA → preprocessing → feature extraction → training → inference