Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Dog Breeds Classifier (AlexNet Fine-tuned Model)
|
| 3 |
+
|
| 4 |
+
## Model Description
|
| 5 |
+
This is a fine-tuned version of the **AlexNet** model, designed to classify images into one of 10 different dog breeds. The original AlexNet architecture was pre-trained on the ImageNet dataset, and this version has been specifically adapted to classify dog breeds based on a custom dataset containing images of various dogs.
|
| 6 |
+
|
| 7 |
+
## Model Details
|
| 8 |
+
### Architecture
|
| 9 |
+
- **Base Model**: AlexNet
|
| 10 |
+
- **Final Layer**: The final fully connected layer has been modified to classify 10 dog breeds.
|
| 11 |
+
- Input to the last fully connected layer: 4096 features
|
| 12 |
+
- Output: 10 classes (one for each breed)
|
| 13 |
+
|
| 14 |
+
### Dataset
|
| 15 |
+
The dataset used for fine-tuning consists of images of 10 different dog breeds, organized into training, validation, and test sets.
|
| 16 |
+
|
| 17 |
+
#### Dog Breeds Included:
|
| 18 |
+
1. Beagle
|
| 19 |
+
2. Chihuahua
|
| 20 |
+
3. Corgi
|
| 21 |
+
4. Dalmation
|
| 22 |
+
5. Doberman
|
| 23 |
+
6. Golden Retriever
|
| 24 |
+
7. Maltese
|
| 25 |
+
8. Poodle
|
| 26 |
+
9. Shiba Inu
|
| 27 |
+
10. Siberian Husky
|
| 28 |
+
|
| 29 |
+
#### Data Format
|
| 30 |
+
- **Image Format**: JPG
|
| 31 |
+
- **Resolution**: The images were resized to 227x227 pixels to match AlexNet’s input requirements.
|
| 32 |
+
- **Dataset Structure**:
|
| 33 |
+
- `train/`: Contains the training images of dog breeds.
|
| 34 |
+
- `valid/`: Contains the validation images of dog breeds.
|
| 35 |
+
- `test/`: Contains the test images of dog breeds.
|
| 36 |
+
|
| 37 |
+
#### Source:
|
| 38 |
+
The dataset was obtained from Kaggle. You can access it [here](https://www.kaggle.com/datasets/gpiosenka/70-dog-breedsimage-data-set/data?select=dogs.csv).
|
| 39 |
+
|
| 40 |
+
## Training
|
| 41 |
+
The AlexNet model was fine-tuned using the following setup:
|
| 42 |
+
- **Optimizer**: SGD
|
| 43 |
+
- **Learning Rate**: 0.001
|
| 44 |
+
- **Momentum**: 0.9
|
| 45 |
+
- **Loss Function**: CrossEntropyLoss
|
| 46 |
+
- **Training Epochs**: 10
|
| 47 |
+
- **Batch Size**: 32
|
| 48 |
+
|
| 49 |
+
## Usage
|
| 50 |
+
You can load and use this model for inference by following the code snippet below.
|
| 51 |
+
|
| 52 |
+
```python
|
| 53 |
+
# Install Required Libraries
|
| 54 |
+
pip install torch torchvision huggingface_hub
|
| 55 |
+
|
| 56 |
+
import torch
|
| 57 |
+
import torch.nn as nn
|
| 58 |
+
from torchvision import models
|
| 59 |
+
from huggingface_hub import hf_hub_download
|
| 60 |
+
|
| 61 |
+
# Load the fine-tuned AlexNet model
|
| 62 |
+
model = models.alexnet(pretrained=False)
|
| 63 |
+
num_features = model.classifier[6].in_features
|
| 64 |
+
model.classifier[6] = nn.Linear(num_features, 10)
|
| 65 |
+
|
| 66 |
+
# Download model weights from Hugging Face Hub
|
| 67 |
+
model_path = hf_hub_download(repo_id="pramudyalyza/dog-breeds-alexnet", filename="alexnet_model.bin")
|
| 68 |
+
|
| 69 |
+
# Load the model state
|
| 70 |
+
model.load_state_dict(torch.load(model_path))
|
| 71 |
+
model.eval()
|
| 72 |
+
|
| 73 |
+
# Example inference
|
| 74 |
+
from PIL import Image
|
| 75 |
+
from torchvision import transforms
|
| 76 |
+
|
| 77 |
+
# Define transformation
|
| 78 |
+
transform = transforms.Compose([
|
| 79 |
+
transforms.Resize((227, 227)),
|
| 80 |
+
transforms.ToTensor(),
|
| 81 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 82 |
+
])
|
| 83 |
+
|
| 84 |
+
# Load and preprocess an example image
|
| 85 |
+
image = Image.open("path_to_image.jpg")
|
| 86 |
+
image = transform(image).unsqueeze(0)
|
| 87 |
+
|
| 88 |
+
# Perform inference
|
| 89 |
+
with torch.no_grad():
|
| 90 |
+
output = model(image)
|
| 91 |
+
predicted_class = output.argmax(dim=1)
|
| 92 |
+
print(f"Predicted Dog Breed: {predicted_class.item()}")
|
| 93 |
+
```
|