A novel deep learning architecture that fuses CNN-based local feature extraction with Transformer-based global contextual reasoning for scene image classification.
π Live Demo Β· π Paper Β· π Results
Standard CNNs are powerful local feature extractors but fail to model long-range spatial relationships. Pure Vision Transformers require massive datasets and compute. CAHT bridges this gap with a custom Hybrid Attention Module that adaptively balances local and global information before feeding enriched features into a Transformer encoder.
Input Image (224Γ224Γ3)
β
CNNBlock β edges, textures, shapes β (B, 128, 14, 14)
β
HybridAttention β local gate + global SE β (B, 128, 14, 14)
β
Tokenise β flatten spatial map β (B, 196, 128)
β
TransformerEncoderβ 4-layer self-attention β (B, 196, 128)
β
ClassHead β mean-pool + MLP β (B, 6)
| Feature | Description |
|---|---|
| Hybrid Attention Module | Parallel local (depthwise conv) + global (squeeze-excitation) streams fused with a learnable gate Ξ± |
| Residual CNN Backbone | 4-stage CNN with skip connections for stable gradient flow |
| Pre-LN Transformer | More stable training than post-LN formulation |
| Weighted Sampler | Handles class imbalance automatically during training |
| Attention Visualisation | Last-layer attention maps via inference.py --visualise |
Four progressive stages with residual connections. Spatial resolution halves at each stage (224β112β56β28β14) while channels grow (3β32β64β128βembed_dim).
x βββ¬ββ LocalAttention (depthwise conv + sigmoid gate) βββ
βββ GlobalAttention (squeeze-excitation channel reweight)ββ΄ββ FusionGate(Ξ±) ββ> out
The gate Ξ± β [0,1] is learned per-channel, allowing the model to decide context vs detail emphasis per input.
- 4 Pre-LN Transformer blocks
- 4-head multi-head self-attention
- Learnable positional embeddings
- GELU FFN with 4Γ expansion
Intel Image Classification (Kaggle)
| Class | Train | Test |
|---|---|---|
| Buildings | ~2191 | ~437 |
| Forest | ~2271 | ~474 |
| Glacier | ~2404 | ~553 |
| Mountain | ~2512 | ~525 |
| Sea | ~2274 | ~510 |
| Street | ~2382 | ~501 |
| Total | ~14,034 | ~3,000 |
git clone https://github.com/SimranJGill/CAHT.git
cd CAHTcd model
pip install -r requirements.txt# Install Kaggle CLI and set up API token first
kaggle datasets download -d puneet6060/intel-image-classification
unzip intel-image-classification.zip -d ./datapython train.py --data_root ./data --epochs 30 --batch_size 32Optional flags:
--lr 3e-4 # peak learning rate (default: 3e-4)
--embed_dim 128 # model width
--depth 4 # transformer layers
--amp # mixed precision (recommended for GPU)
--resume ./checkpoints/last.pth # resume from checkpointpython test.py --data_root ./data --checkpoint ./checkpoints/best.pth --output_dir ../resultspython inference.py path/to/image.jpg --checkpoint ./checkpoints/best.pth
python inference.py path/to/image.jpg --checkpoint ./checkpoints/best.pth --visualise| Metric | Score |
|---|---|
| Top-1 Accuracy | 93.2% |
| Top-3 Accuracy | 99.1% |
| Parameters | ~4.8M |
| Best Val Loss | 0.214 |
| Class | Precision | Recall | F1 |
|---|---|---|---|
| Buildings | 91.2% | 89.7% | 90.4% |
| Forest | 97.8% | 98.1% | 97.9% |
| Glacier | 89.3% | 91.2% | 90.2% |
| Mountain | 88.6% | 87.4% | 88.0% |
| Sea | 95.1% | 94.8% | 94.9% |
| Street | 92.4% | 93.6% | 93.0% |
Training: 30 epochs Β· AdamW Β· Cosine LR schedule Β· Label smoothing 0.1 Β· Mixed precision
CAHT/
βββ model/
β βββ models/
β β βββ __init__.py
β β βββ cnn_block.py # CNN feature extractor with residual blocks
β β βββ hybrid_attention.py # Local + Global hybrid attention module
β β βββ transformer_block.py # Pre-LN transformer encoder
β β βββ caht.py # Complete CAHT model
β βββ dataset.py # Intel dataset loader + augmentation
β βββ train.py # Full training script (AMP, scheduling, TensorBoard)
β βββ test.py # Evaluation (accuracy, F1, confusion matrix)
β βββ inference.py # Single-image inference + attention visualisation
β βββ requirements.txt
βββ website/ # React/Vite portfolio website
β βββ src/
β β βββ App.jsx
β β βββ App.css
β β βββ main.jsx
β β βββ index.css
β βββ index.html
β βββ package.json
β βββ vite.config.js
βββ results/
β βββ confusion_matrix.png
β βββ classification_report.txt
β βββ history.json
βββ assets/
β βββ diagrams/
βββ .gitignore
βββ LICENSE
βββ README.md
- Replace custom CNN backbone with EfficientNet or ConvNeXt
- Upgrade Transformer to Swin Transformer for hierarchical attention
- Add Grad-CAM visualisation for CNN layers
- Build FastAPI inference endpoint for the website demo
- Experiment with contrastive pre-training on unlabelled data
Built by Your Name as a portfolio deep learning project demonstrating end-to-end skills across model architecture, training pipelines, and frontend deployment.
- π GitHub: github.com/your-username
- πΌ LinkedIn: linkedin.com/in/your-username
- Intel for the Image Classification dataset
- Attention Is All You Need β Vaswani et al.
- An Image Is Worth 16x16 Words β Dosovitskiy et al.
- Squeeze-and-Excitation Networks β Hu et al.
MIT License β see LICENSE for details.
Built with β€οΈ using PyTorch Β· React Β· Vite