A PyTorch image classifier that distinguishes cats from dogs, trained on the Kaggle Cat and Dog dataset.
Fuente del modelo
Descripción de la fuente
A PyTorch image classifier that distinguishes cats from dogs, trained on the Kaggle Cat and Dog dataset.
Fuentes
1 fuenteVerificado 10 sept
Artefactos del modelo
4 artefactosExtractos de fuentes
2 extractos| 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 |
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
# Load model
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()
# Preprocess
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)
# Predict
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%})")
| 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 |
transfer_learning_fc_only.pth
pth · 42,7 MB · SHA-256 0343cc65d245…2405 · Hugging Face
--- license: mit tags: - image-classification - cats-dogs - pytorch - resnet18 - cnn - transfer-learning metrics: - accuracy --- # Cat vs Dog Classifier A PyTorch image classifier that distinguishes cats from dogs, trained on the [Kaggle Cat and Dog dataset](https://www.kaggle.com/datasets/tongpython/cat-and-dog). ## 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 ```python import torch import torch.nn as nn from torchvision import models, transforms from PIL import Image # Load model 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() # Preprocess 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) # Predict 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 |
Source context: 0 downloads · 1 likes · Pipeline image-classification · Repo 24f2004275/cat-dog-classifier