File size: 2,535 Bytes
efd7519
a014e55
efd7519
 
 
 
a014e55
efd7519
 
 
2952455
 
 
efd7519
a014e55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2952455
a014e55
 
 
 
 
 
 
 
 
 
 
 
 
 
2952455
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---
title: Pet Breed Classifier
colorFrom: green
colorTo: yellow
sdk: gradio
sdk_version: 6.30.0
python_version: '3.14'
app_file: app.py
pinned: false
---
# Please note I created a folder called codeWithNotes, these include notes to discribe what each CLASS and function is doing. The notes are enclosed the '# region # endregion'
These comments can be collapsed in the IDE by clicking the small arrow to the left of the comment (next to the line number)


# Pet Breed Classifier

An image classification web application that identifies the breed of a pet from a photograph and reports whether the animal is a cat or a dog.

## Problem Statement

- **Task type:** Multi-class image classification.
- **Dataset:** Oxford-IIIT Pet (`torchvision.datasets.OxfordIIITPet`).
- **Input:** An RGB photograph of a cat or dog, resized and normalized to a fixed tensor size.
- **Output:** A predicted breed from 37 classes (25 dog breeds and 12 cat breeds), plus the species derived from the predicted breed.
- **Core challenge:** Breeds within a species are visually similar, and the dataset provides roughly 100 images per class with variation in pose, lighting, and background.

## Application Outputs

- Top three predicted breeds with confidence scores.
- Detected species (cat or dog).
- Single-image inference latency in seconds.

## Repository Structure

```text
app.py                  Gradio application entry point
pet_breed_model.pth     Trained model weights (state_dict)
requirements.txt        Runtime dependencies for deployment
src/model.py            Model architecture
src/dataset.py          Transforms, splits, and DataLoaders
src/utils.py            Species mapping, timed inference, and metrics helpers
notebooks/train.ipynb   Training and evaluation workflow
images/                 Sample photos shown as examples in the web interface
```

## Local Setup

The project uses Python 3.14 and [uv](https://docs.astral.sh/uv/).

```bash
uv sync
uv run python app.py
```

## Training

Training is performed in `notebooks/train.ipynb` on a Google Colab NVIDIA T4 GPU. The resulting weights are saved to `pet_breed_model.pth` and loaded by `app.py` for CPU inference.

Tried training with both CNN and ResNet18 model to compare. ImageNet-1K dataset on which standard ResNet-18 weights are pretrained is specifically trained with 1.28 million images. Which the weights being pretrained, this means we ar enot starting from scratch. The CNN filter is learning from square 1, making it need a lot more images to be more accurate.