Spaces:
Sleeping
Sleeping
First commit for the app
Browse files- README.md +23 -7
- app.py +51 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,13 +1,29 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: ResNet50 ImageNet Classifier
|
3 |
+
emoji: 🖼️
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.12.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: mit
|
11 |
---
|
12 |
|
13 |
+
# ResNet-50 ImageNet Classifier
|
14 |
+
|
15 |
+
This is a demo for ResNet-50 model trained on ImageNet dataset.
|
16 |
+
|
17 |
+
## Model Details
|
18 |
+
|
19 |
+
- **Architecture:** ResNet-50
|
20 |
+
- **Framework:** PyTorch
|
21 |
+
- **Input:** Images (224x224 RGB)
|
22 |
+
- **Output:** 1000 class probabilities
|
23 |
+
|
24 |
+
## Usage
|
25 |
+
|
26 |
+
1. Upload an image
|
27 |
+
2. Get top-5 class predictions with probabilities
|
28 |
+
|
29 |
+
## Model Configuration
|
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForImageClassification
|
3 |
+
import torch
|
4 |
+
import torchvision.transforms as transforms
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Load model from Hub instead of local file
|
8 |
+
def load_model():
|
9 |
+
model = AutoModelForImageClassification.from_pretrained(
|
10 |
+
"YOUR_USERNAME/resnet-imagenet",
|
11 |
+
trust_remote_code=True
|
12 |
+
)
|
13 |
+
model.eval()
|
14 |
+
return model
|
15 |
+
|
16 |
+
# Preprocessing
|
17 |
+
transform = transforms.Compose([
|
18 |
+
transforms.Resize(256),
|
19 |
+
transforms.CenterCrop(224),
|
20 |
+
transforms.ToTensor(),
|
21 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
22 |
+
std=[0.229, 0.224, 0.225])
|
23 |
+
])
|
24 |
+
|
25 |
+
# Inference function
|
26 |
+
def predict(image):
|
27 |
+
model = load_model()
|
28 |
+
|
29 |
+
# Preprocess image
|
30 |
+
img = Image.fromarray(image)
|
31 |
+
img = transform(img).unsqueeze(0)
|
32 |
+
|
33 |
+
# Inference
|
34 |
+
with torch.no_grad():
|
35 |
+
output = model(img)
|
36 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
37 |
+
|
38 |
+
# Get top 5 predictions
|
39 |
+
top5_prob, top5_catid = torch.topk(probabilities, 5)
|
40 |
+
return {f"Class {i}": float(prob) for i, prob in zip(top5_catid, top5_prob)}
|
41 |
+
|
42 |
+
# Create Gradio interface
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=predict,
|
45 |
+
inputs=gr.Image(),
|
46 |
+
outputs=gr.Label(num_top_classes=5),
|
47 |
+
title="ResNet Image Classification",
|
48 |
+
description="Upload an image to classify it using ResNet"
|
49 |
+
)
|
50 |
+
|
51 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
transformers
|
4 |
+
gradio
|
5 |
+
Pillow
|