Spaces:
Sleeping
Sleeping
Upload 5 files
Browse filesUpload needed files
British_Shorthair(/350/213/261/345/234/213/347/237/255/346/257/233/350/262/223).jpg
ADDED
|
Samoyed(/350/226/251/346/221/251/350/200/266).jpg
ADDED
|
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchvision import transforms
|
| 3 |
+
from torchvision import models
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Use CPU
|
| 9 |
+
device = torch.device('cpu')
|
| 10 |
+
|
| 11 |
+
# Load the model ResNet-50 model architecture
|
| 12 |
+
model = models.resnet50(pretrained=False)
|
| 13 |
+
|
| 14 |
+
# Load model's weight to CPU
|
| 15 |
+
model = torch.load('resnet50_model_weights.pth', map_location=device)
|
| 16 |
+
model.eval()
|
| 17 |
+
|
| 18 |
+
# Define the image preprocessing
|
| 19 |
+
transform = transforms.Compose([
|
| 20 |
+
transforms.Resize((224, 224)),
|
| 21 |
+
transforms.ToTensor(),
|
| 22 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 23 |
+
])
|
| 24 |
+
|
| 25 |
+
# Define the class names
|
| 26 |
+
class_names = ['Abyssinian', 'American Bulldog', 'American Pit Bull Terrier', 'Basset Hound', 'Beagle', 'Bengal', 'Birman', 'Bombay',
|
| 27 |
+
'Boxer', 'British Shorthair', 'Chihuahua', 'Egyptian Mau', 'English Cocker Spaniel', 'English Setter', 'German Shorthaired',
|
| 28 |
+
'Great Pyrenees', 'Havanese', 'Japanese Chin', 'Keeshond', 'Leonberger', 'Maine Coon', 'Miniature Pinscher', 'Newfoundland',
|
| 29 |
+
'Persian', 'Pomeranian', 'Pug', 'Ragdoll', 'Russian Blue', 'Saint Bernard', 'Samoyed', 'Scottish Terrier', 'Shiba Inu',
|
| 30 |
+
'Siamese', 'Sphynx', 'Staffordshire Bull Terrier', 'Wheaten Terrier', 'Yorkshire Terrier']
|
| 31 |
+
|
| 32 |
+
# Define the predict function
|
| 33 |
+
def classify_image(image):
|
| 34 |
+
image = transform(image).unsqueeze(0).to(device) # Ensure image data is processed on CPU
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
outputs = model(image)
|
| 37 |
+
_, predicted = torch.max(outputs, 1)
|
| 38 |
+
return class_names[predicted.item()]
|
| 39 |
+
|
| 40 |
+
# Custom Gradio interface title, description, and article
|
| 41 |
+
title = 'Oxford Pet ππ'
|
| 42 |
+
description = 'A ResNet50-based computer vision model for classifying images of pets from the Oxford-IIIT Pet Dataset. The model can recognize 37 different pet breeds, including cats and dogs.'
|
| 43 |
+
article = 'https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/The%20Oxford-IIIT%20Pet%20Project'
|
| 44 |
+
|
| 45 |
+
# Gradio interface
|
| 46 |
+
examples = [["examples/" + img] for img in os.listdir('examples')]
|
| 47 |
+
|
| 48 |
+
demo = gr.Interface(fn=classify_image, # Map input to output function
|
| 49 |
+
inputs=gr.Image(type="pil"), # Image input
|
| 50 |
+
outputs=[gr.Label(num_top_classes=1, label="Predictions")], # Predicted label
|
| 51 |
+
examples=examples, # Example images
|
| 52 |
+
title=title,
|
| 53 |
+
description=description,
|
| 54 |
+
article=article)
|
| 55 |
+
|
| 56 |
+
# Launch the demo
|
| 57 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.3.0
|
| 2 |
+
torchvision==0.18.0
|
| 3 |
+
gradio==4.42.0
|
| 4 |
+
Pillow==9.2.0
|
resnet50_model_weights.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fa799a82adff86b3fe384f7b86e925d75e8f434255cff9464b2ceeeec1cd5b69
|
| 3 |
+
size 94657902
|