Spaces:
Configuration error
Configuration error
First commit
Browse files- README.md +11 -13
- app.py +51 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,14 +1,12 @@
|
|
1 |
-
|
2 |
-
title: Imagenet1KResnetImageClassifier
|
3 |
-
emoji: 📉
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: red
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 5.9.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: apache-2.0
|
11 |
-
short_description: Imagenet 1K Image Classification model using ResNet Architec
|
12 |
-
---
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ResNet Image Classifier
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
This is a demo of ResNet trained on ImageNet for image classification.
|
4 |
+
|
5 |
+
## Usage
|
6 |
+
1. Upload an image
|
7 |
+
2. Get top-5 predictions
|
8 |
+
|
9 |
+
## Model Details
|
10 |
+
- Architecture: ResNet-50
|
11 |
+
- Dataset: ImageNet
|
12 |
+
- Training Details: [Add your training details]
|
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,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
gradio
|
4 |
+
Pillow
|