File size: 2,169 Bytes
fb141c8 |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
---
title: Cat and Dog Sketch Classifier
emoji: 🐱🐶
tags:
- image-classification
- quickdraw
- cat
- dog
license: mit
---
# Cat and Dog Sketch Classifier
This is a machine learning model trained to differentiate between sketches of cats and dogs. It was built as part of a learning project to understand how AI models work and how to train them.
## Model Details
- **Model Type**: Convolutional Neural Network (CNN)
- **Training Data**: Quick, Draw! dataset (cat and dog sketches)
- **License**: MIT License
- **Supported Tasks**: Image Classification
## Usage
To use this model, you can follow these steps:
1. **Load the Model**:
```python
import torch
from model import SimpleCNN
model = SimpleCNN()
model.load_state_dict(torch.load('cat_dog_classifier.bin'))
model.eval()
```
2. **Predict an Image**:
```python
from PIL import Image
import numpy as np
import torch
def predict_image(model, image):
# Preprocess the image
if isinstance(image, Image.Image):
image = image.resize((28, 28)).convert('L')
image = np.array(image).astype('float32') / 255.0
elif isinstance(image, np.ndarray):
if image.shape != (28, 28):
image = Image.fromarray(image).resize((28, 28)).convert('L')
image = np.array(image).astype('float32') / 255.0
else:
raise ValueError("Image must be a PIL Image or NumPy array.")
image = image.reshape(1, 1, 28, 28)
image_tensor = torch.tensor(image).to(device)
# Get prediction
model.eval()
with torch.no_grad():
output = model(image_tensor)
_, predicted = torch.max(output.data, 1)
return 'cat' if predicted.item() == 0 else 'dog'
# Example usage
image = Image.open('path/to/your/image.png')
prediction = predict_image(model, image)
print(prediction)
```
## Training the Model
To train the model yourself, use the provided `train_cat_dog_classifier.py` script.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
|