Cat vs Dog Classifier
A PyTorch image classifier that distinguishes cats from dogs, trained on the Kaggle Cat and Dog dataset.
Models Included
| File |
Architecture |
Val Accuracy |
Description |
mlp_classifier.pth |
MLP |
61.1% |
Fully connected baseline |
simple_cnn.pth |
Simple CNN |
72.6% |
2-layer CNN from scratch |
transfer_learning_fc_only.pth |
ResNet18 (FC only) |
97.6% |
Frozen backbone, FC head |
transfer_learning_layer4_fc.pth |
ResNet18 (Layer4+FC) |
98.1% |
Partially unfrozen ResNet18 |
results.json |
- |
- |
Training/validation metrics |
Usage
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
model = models.resnet18()
model.fc = nn.Linear(model.fc.in_features, 2)
model.load_state_dict(torch.load("transfer_learning_layer4_fc.pth", weights_only=True))
model.eval()
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
])
image = Image.open("cat.jpg")
tensor = transform(image).unsqueeze(0)
with torch.no_grad():
logits = model(tensor)
probs = torch.softmax(logits, dim=1)
predicted = torch.argmax(probs, dim=1).item()
classes = ["cat", "dog"]
print(f"Prediction: {classes[predicted]} ({probs[0][predicted]:.1%})")
Training Details
- Dataset: Kaggle Cat and Dog (8,007 train / 2,025 test)
- Input: 224x224 RGB, normalized to [-1, 1]
- Optimizer: Adam (lr=0.001)
- Loss: CrossEntropyLoss
- Epochs: MLP(3), CNN(5), Transfer Learning(3)
Results
| Model |
Train Acc |
Val Acc |
Train Loss |
Val Loss |
| MLP |
66.1% |
61.1% |
123.8 |
36.2 |
| CNN |
88.6% |
70.4% |
53.0 |
36.0 |
| Transfer Learning (FC only) |
96.5% |
97.2% |
18.1 |
3.7 |
| Transfer Learning (Layer4+FC) |
98.6% |
98.1% |
8.1 |
3.0 |