Add ResNet50 cat/dog classifier (~94% acc) + model card
Browse files- README.md +77 -0
- cat_dog_classifier.pt +3 -0
- config.json +17 -0
README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
pipeline_tag: image-classification
|
| 4 |
+
library_name: pytorch
|
| 5 |
+
inference: false
|
| 6 |
+
tags:
|
| 7 |
+
- pytorch
|
| 8 |
+
- resnet
|
| 9 |
+
- transfer-learning
|
| 10 |
+
- image-classification
|
| 11 |
+
- grad-cam
|
| 12 |
+
- computer-vision
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# Cat vs Dog Classifier π±πΆ
|
| 16 |
+
|
| 17 |
+
[](https://opensource.org/licenses/MIT)
|
| 18 |
+
[](https://github.com/mtkl6/cat-dog-classifier)
|
| 19 |
+
|
| 20 |
+
A **ResNet50 transfer-learning** classifier that distinguishes cats from dogs at
|
| 21 |
+
**~94% validation accuracy (AUC 0.98)**, trained in two stages on the
|
| 22 |
+
Oxford-IIIT Pet dataset.
|
| 23 |
+
|
| 24 |
+
Full training code, Grad-CAM inference, and a complete beginner's guide:
|
| 25 |
+
π **https://github.com/mtkl6/cat-dog-classifier**
|
| 26 |
+
|
| 27 |
+
> β οΈ The inference widget is disabled because this is a custom head on a
|
| 28 |
+
> torchvision backbone (not a `transformers` model) β load it with the snippet below.
|
| 29 |
+
|
| 30 |
+
## Files
|
| 31 |
+
|
| 32 |
+
| File | What |
|
| 33 |
+
|---|---|
|
| 34 |
+
| `cat_dog_classifier.pt` | trained weights (raw `state_dict`, ~90 MB) |
|
| 35 |
+
| `config.json` | architecture & preprocessing metadata |
|
| 36 |
+
|
| 37 |
+
## Usage
|
| 38 |
+
|
| 39 |
+
```python
|
| 40 |
+
import torch, torch.nn as nn
|
| 41 |
+
from torchvision import models, transforms
|
| 42 |
+
from huggingface_hub import hf_hub_download
|
| 43 |
+
from PIL import Image
|
| 44 |
+
|
| 45 |
+
model = models.resnet50()
|
| 46 |
+
model.fc = nn.Sequential(nn.Dropout(0.4), nn.Linear(2048, 1))
|
| 47 |
+
weights = hf_hub_download("mtkl6/cat-dog-classifier", "cat_dog_classifier.pt")
|
| 48 |
+
model.load_state_dict(torch.load(weights, weights_only=True))
|
| 49 |
+
model.eval()
|
| 50 |
+
|
| 51 |
+
tf = transforms.Compose([
|
| 52 |
+
transforms.Resize((224, 224)), transforms.ToTensor(),
|
| 53 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
| 54 |
+
])
|
| 55 |
+
x = tf(Image.open("pet.jpg").convert("RGB")).unsqueeze(0)
|
| 56 |
+
p_dog = torch.sigmoid(model(x)).item()
|
| 57 |
+
print("dog" if p_dog > 0.5 else "cat", f"({max(p_dog, 1 - p_dog):.1%})")
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
Labels: **0 = cat, 1 = dog**. The model outputs a single logit; apply `sigmoid`
|
| 61 |
+
and threshold at 0.5.
|
| 62 |
+
|
| 63 |
+
## Training
|
| 64 |
+
|
| 65 |
+
| | |
|
| 66 |
+
|---|---|
|
| 67 |
+
| Backbone | ResNet50 (`IMAGENET1K_V1`), head `Dropout(0.4) β Linear(2048, 1)` |
|
| 68 |
+
| Stage 1 | frozen backbone, head only β `lr 1e-3`, 10 epochs β 86.3% val |
|
| 69 |
+
| Stage 2 | fine-tune `layer4` β `lr 1e-5`, 10 epochs β **94.2% val, AUC 0.98** |
|
| 70 |
+
| Loss / optim | `BCEWithLogitsLoss`, Adam, `ReduceLROnPlateau` |
|
| 71 |
+
| Input | 224Γ224 RGB, ImageNet normalization |
|
| 72 |
+
| Dataset | [Oxford-IIIT Pet](https://www.robots.ox.ac.uk/~vgg/data/pets/) (37 breeds β binary) |
|
| 73 |
+
|
| 74 |
+
## License
|
| 75 |
+
|
| 76 |
+
Code & weights: **MIT**. Dataset: [Oxford-IIIT Pet](https://www.robots.ox.ac.uk/~vgg/data/pets/)
|
| 77 |
+
(Parkhi et al., 2012), used under its own research/educational terms.
|
cat_dog_classifier.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b38e598c03bd94d546a394905907691675fc2c18794f6bdcbbb8a09069105b34
|
| 3 |
+
size 94357505
|
config.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "resnet50",
|
| 3 |
+
"task": "image-classification",
|
| 4 |
+
"num_labels": 1,
|
| 5 |
+
"id2label": {"0": "cat", "1": "dog"},
|
| 6 |
+
"image_size": 224,
|
| 7 |
+
"normalization": {
|
| 8 |
+
"mean": [0.485, 0.456, 0.406],
|
| 9 |
+
"std": [0.229, 0.224, 0.225]
|
| 10 |
+
},
|
| 11 |
+
"backbone": "torchvision resnet50 (IMAGENET1K_V1)",
|
| 12 |
+
"head": "Dropout(0.4) -> Linear(2048, 1)",
|
| 13 |
+
"output": "single logit; sigmoid(logit) > 0.5 => dog",
|
| 14 |
+
"weights_file": "cat_dog_classifier.pt",
|
| 15 |
+
"library_name": "pytorch",
|
| 16 |
+
"license": "mit"
|
| 17 |
+
}
|