This project implements a Convolutional Neural Network (CNN) for object detection on the CIFAR-10 dataset using PyTorch and transfer learning with MobileNetV3. The model achieves high accuracy in classifying images into 10 different categories through a two-stage training approach with feature freezing and fine-tuning.
CIFAR-10 Dataset contains 60,000 32x32 color images in 10 classes:
- Airplane
- Automobile
- Bird
- Cat
- Deer
- Dog
- Frog
- Horse
- Ship
- Truck
Dataset Split:
- Training: 50,000 images
- Testing: 10,000 images
- Pre-trained weights: ImageNet weights for transfer learning
- Modified classifier: Custom classifier head for 10 classes
- Input size: 224x224 pixels (resized from 32x32)
MobileNetV3(
features: Pre-trained feature extractor (960 output features)
classifier: Sequential(
Linear(960 → 1280)
Hardswish()
Dropout(0.2)
Linear(1280 → 10) # 10 CIFAR-10 classes
)
)- Image resizing: 32x32 → 224x224 for MobileNet compatibility
- Normalization: ImageNet standards (mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225])
- Data format: PNG images extracted from 7z archives
-
Stage 1 - Feature Extraction (15 epochs)
- Freeze feature extractor layers
- Train only classifier head
- Learning rate: 0.001
-
Stage 2 - Fine-tuning (15 epochs)
- Unfreeze all layers
- End-to-end training
- Learning rate: 0.001
- Optimizer: Adam
- Loss function: CrossEntropyLoss
- Batch size: 64
- Total epochs: 30 (15 + 15)
- Device: CUDA-enabled GPU
The model tracks:
- Training Loss: Monitored every 100 batches
- Training Accuracy: Real-time accuracy calculation
- Running metrics: Loss and accuracy averages
def train_one_epoch(dataloader, model, loss_fn, optimizer):
# Real-time tracking of:
# - Batch-wise loss
# - Running accuracy
# - Progress indicators- Leverages pre-trained MobileNetV3 for efficient training
- Reduces training time and improves convergence
- Two-stage approach prevents overfitting
- Gradual unfreezing for better feature adaptation
class TrainDataset(Dataset):
# Handles image loading, preprocessing, and label encoding
class TestDataset(Dataset):
# Processes test images for inference- Automated 7z archive extraction
- On-the-fly image preprocessing
- Optimized DataLoader configuration
CIFAR-10-Object-Detection-Using-CNN/
├── CIFAR-10-Object-Detection-Using-CNN.ipynb
├── .gitignore
└── README.md
pip install torch torchvision py7zr pandas numpy- Data Setup: Place CIFAR-10 datasets in specified paths
- Training: Execute the notebook cells sequentially
- Evaluation: Model automatically generates predictions on test set
class Cifar10(nn.Module):
def __init__(self):
super().__init__()
self.pretrainednet = mobilenet_v3_large(weights=MobileNet_V3_Large_Weights.DEFAULT)
# Custom classifier for 10 classesfor i in range(n_epochs):
train_epoch_loss, train_epoch_acc = train_one_epoch(
traindataloader, model, loss_fn, optimizer
)- MobileNetV3 designed for mobile/edge deployment
- Optimized depth-wise separable convolutions
- Handles various image formats
- Automatic normalization and resizing
- Configurable hyperparameters
- Easy modification for different datasets
The model produces:
- Submission file: CSV with predicted class labels
- Performance metrics: Training loss and accuracy curves
- Class predictions: Mapped back to original CIFAR-10 labels
- Data Augmentation: Add rotation, flip, and color jittering
- Advanced Architectures: Experiment with ResNet, EfficientNet
- Ensemble Methods: Combine multiple models for better accuracy
- Learning Rate Scheduling: Implement adaptive learning rates
- Cross-validation: Add k-fold validation for robust evaluation
- PyTorch: Deep learning framework
- torchvision: Computer vision utilities and models
- py7zr: 7z archive extraction
- pandas: Data manipulation
- numpy: Numerical computations
- Successful implementation of transfer learning
- Two-stage training strategy
- Efficient data pipeline for large datasets
- GPU-accelerated training
- Production-ready inference pipeline