A convolutional neural network for 16-class scene image categorization, implemented in PyTorch. The project covers the full pipeline — data preprocessing, a fully-specified baseline CNN, and an improved model that pushes validation accuracy higher through architectural and training-policy changes.
The task is to predict the scene category of a 32×32 RGB image from 16 classes: bathroom, bedroom, bowling alley, castle, classroom, clothing store, dining hall, golf course, hospital, library, office, restaurant, shopping mall, swimming pool, train station, and volleyball court.
- Training set: 32,000 images (2,000 per class)
- Validation set: 6,400 images (400 per class)
- Test set: 9,600 images (held-out labels)
The architecture is driven by a netspec_opts specification dictionary, so the network (kernel sizes, filter counts, strides, layer types) is described as data and constructed programmatically — making it straightforward to iterate from the baseline to improved variants.
The preprocessing stage (create_dataset.py) supports:
- Per-pixel mean subtraction (zero-centering) using statistics computed from the training set and applied consistently to train/val/test.
- Optional contrast normalization — element-wise division by the per-pixel standard deviation.
- Optional whitening — decorrelating pixels using training-set correlations.
- Caching of preprocessed tensors to disk to avoid recomputation.
A compact three-block convolutional network built with PyTorch's Sequential API:
| Block | Layers | Filters | Kernel | Stride |
|---|---|---|---|---|
| 1 | Conv → BatchNorm → ReLU | 16 | 3 | 1 |
| 2 | Conv → BatchNorm → ReLU | 32 | 3 | 2 |
| 3 | Conv → BatchNorm → ReLU → AvgPool | 64 | 3 | 2 |
| Head | Conv (1×1) → class logits | 16 | 1 | 1 |
Each convolutional block uses batch normalization for training stability (mitigating vanishing/exploding gradients) and ReLU non-linearities. The baseline reaches ~64% validation accuracy.
Training policy: SGD with momentum 0.9, weight decay 1e-4, batch size 128, learning rate 0.1 for 20 epochs then 0.01 for 5 more.
The improved variant targets higher test-set accuracy through a combination of:
- Deeper feature extraction / wider filter counts in later layers to enlarge the effective receptive field and capture higher-level semantic structure.
- Data augmentation (
torchvision.transforms— random crops, flips, color jitter) to reduce overfitting. - Optional contrast normalization / whitening and a tuned learning-rate schedule with early stopping.
Design decisions were guided by train-vs-validation curves to diagnose over- vs. under-fitting and by receptive-field reasoning at the final convolutional layer.
.
├── create_dataset.py # preprocessing + TensorDataset creation
├── cnn_categorization.py # netspec + train_opts + entry point
├── cnn_categorization_base.py # baseline architecture
├── cnn_categorization_improved.py # improved architecture
├── train.py # training + checkpointing
├── create_submission.py # evaluation / prediction export
└── README.md
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install torch torchvision numpy# Train the baseline model
python cnn_categorization.py
# Train the improved model
python cnn_categorization.py improvedModel checkpoints are saved per epoch to the configured experiment directory.
- Building CNNs from convolution / batch-norm / ReLU / pooling primitives via a spec-driven architecture.
- The role of receptive-field growth and filter depth in capturing scene-level semantics.
- Diagnosing and combating overfitting with augmentation, regularization, and early stopping.
Python · PyTorch · torchvision · NumPy