Spaces:
Sleeping
Sleeping
Commit
Β·
137ced3
1
Parent(s):
a754499
add gradio app demo
Browse files- README.md +64 -2
- app.py +92 -0
- examples/cat.jpg +0 -0
- examples/dog.jpg +0 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: ImageNet1k
|
| 3 |
-
emoji: π
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
|
@@ -9,4 +9,66 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: ImageNet1k
|
| 3 |
+
emoji: π π
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
|
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# ImageNet1k Classification Demo
|
| 13 |
+
|
| 14 |
+
This is a Gradio web application that demonstrates image classification using a ResNet50 model trained on the ImageNet1k dataset. The model can classify images into 1000 different categories.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
|
| 18 |
+
- Upload and classify any image
|
| 19 |
+
- Get top 5 predictions with confidence scores
|
| 20 |
+
- Real-time inference
|
| 21 |
+
- User-friendly interface
|
| 22 |
+
- Example images included
|
| 23 |
+
|
| 24 |
+
## Technical Details
|
| 25 |
+
|
| 26 |
+
### Model Architecture
|
| 27 |
+
- Base Model: ResNet50
|
| 28 |
+
- Training Dataset: ImageNet1k (1000 classes)
|
| 29 |
+
- Input Size: 224x224 pixels
|
| 30 |
+
- Preprocessing: Standard ImageNet normalization (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 31 |
+
|
| 32 |
+
### Dependencies
|
| 33 |
+
- gradio: Web interface framework
|
| 34 |
+
- torch: PyTorch deep learning framework
|
| 35 |
+
- torchvision: Computer vision utilities
|
| 36 |
+
- Pillow: Image processing
|
| 37 |
+
|
| 38 |
+
## Usage
|
| 39 |
+
|
| 40 |
+
1. Upload an image using the interface
|
| 41 |
+
2. The model will process the image and return:
|
| 42 |
+
- Top 5 predicted classes
|
| 43 |
+
- Confidence scores for each prediction
|
| 44 |
+
|
| 45 |
+
## Tips for Best Results
|
| 46 |
+
|
| 47 |
+
- Use clear, well-lit images
|
| 48 |
+
- Ensure the main subject is centered and clearly visible
|
| 49 |
+
- The model works best with common objects, animals, and scenes
|
| 50 |
+
- Both color and black & white images are supported
|
| 51 |
+
- Images will be automatically resized to 224x224
|
| 52 |
+
|
| 53 |
+
## Local Setup
|
| 54 |
+
|
| 55 |
+
1. Clone the repository
|
| 56 |
+
2. Install dependencies:
|
| 57 |
+
```bash
|
| 58 |
+
pip install -r requirements.txt
|
| 59 |
+
```
|
| 60 |
+
3. Place your trained model weights as `model_best.pth.tar` in the root directory
|
| 61 |
+
4. Run the application:
|
| 62 |
+
```bash
|
| 63 |
+
python app.py
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
## Model Weights
|
| 67 |
+
|
| 68 |
+
The model weights (`model_best.pth.tar`) should be placed in the same directory as `app.py`. The weights file contains a ResNet50 model trained on ImageNet1k.
|
| 69 |
+
|
| 70 |
+
## Links
|
| 71 |
+
|
| 72 |
+
- [GitHub Repository](https://github.com/dhairyag/ImageNet1k_ResNet50)
|
| 73 |
+
- [Hugging Face Space](https://huggingface.co/spaces/dhairyashil/ImageNet1k)
|
| 74 |
+
|
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision.models as models
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load the ImageNet class labels
|
| 8 |
+
import json
|
| 9 |
+
import urllib.request
|
| 10 |
+
|
| 11 |
+
# Download ImageNet class labels
|
| 12 |
+
labels_url = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
|
| 13 |
+
labels = urllib.request.urlopen(labels_url).read().decode('utf-8').split('\n')
|
| 14 |
+
|
| 15 |
+
# Initialize model
|
| 16 |
+
model = models.resnet50()
|
| 17 |
+
num_classes = 1000 # ImageNet1k classes
|
| 18 |
+
|
| 19 |
+
# Load your trained weights
|
| 20 |
+
checkpoint = torch.load('model_best.pth.tar', map_location=torch.device('cpu'))
|
| 21 |
+
if 'state_dict' in checkpoint:
|
| 22 |
+
state_dict = checkpoint['state_dict']
|
| 23 |
+
# Remove 'module.' prefix if model was trained with DataParallel
|
| 24 |
+
state_dict = {k.replace('module.', ''): v for k, v in state_dict.items()}
|
| 25 |
+
model.load_state_dict(state_dict)
|
| 26 |
+
else:
|
| 27 |
+
model.load_state_dict(checkpoint)
|
| 28 |
+
|
| 29 |
+
model.eval()
|
| 30 |
+
|
| 31 |
+
# Define image transforms
|
| 32 |
+
transform = transforms.Compose([
|
| 33 |
+
transforms.Resize(256),
|
| 34 |
+
transforms.CenterCrop(224),
|
| 35 |
+
transforms.ToTensor(),
|
| 36 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 37 |
+
std=[0.229, 0.224, 0.225])
|
| 38 |
+
])
|
| 39 |
+
|
| 40 |
+
def predict(image):
|
| 41 |
+
# Ensure image is in RGB format
|
| 42 |
+
if image.mode != 'RGB':
|
| 43 |
+
image = image.convert('RGB')
|
| 44 |
+
|
| 45 |
+
# Apply transforms
|
| 46 |
+
input_tensor = transform(image)
|
| 47 |
+
input_batch = input_tensor.unsqueeze(0)
|
| 48 |
+
|
| 49 |
+
# Get prediction
|
| 50 |
+
with torch.no_grad():
|
| 51 |
+
output = model(input_batch)
|
| 52 |
+
|
| 53 |
+
# Get probabilities
|
| 54 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 55 |
+
|
| 56 |
+
# Get top 5 predictions
|
| 57 |
+
top5_prob, top5_indices = torch.topk(probabilities, 5)
|
| 58 |
+
|
| 59 |
+
# Format results as dictionary
|
| 60 |
+
results = {}
|
| 61 |
+
for prob, idx in zip(top5_prob, top5_indices):
|
| 62 |
+
class_name = labels[idx]
|
| 63 |
+
results[class_name] = float(prob)
|
| 64 |
+
|
| 65 |
+
return results
|
| 66 |
+
|
| 67 |
+
# Create Gradio interface
|
| 68 |
+
title = "ImageNet1k Classification"
|
| 69 |
+
description = """Upload an image and the model will predict its category using the ImageNet1k classification system.
|
| 70 |
+
Tips for best results:
|
| 71 |
+
- Use clear, well-lit images; ensure the main subject is centered and clearly visible
|
| 72 |
+
- The model works best with common objects, animals, and scenes
|
| 73 |
+
- Images can be any size - they'll be automatically resized to 224x224
|
| 74 |
+
- Both color and black & white images are supported
|
| 75 |
+
|
| 76 |
+
The model will show the top 5 most likely categories with confidence scores.
|
| 77 |
+
Link to github repo: [https://github.com/dhairyag/ImageNet1k_ResNet50](https://github.com/dhairyag/ImageNet1k_ResNet50)
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
iface = gr.Interface(
|
| 81 |
+
fn=predict,
|
| 82 |
+
inputs=gr.Image(type="pil"),
|
| 83 |
+
outputs=gr.Label(num_top_classes=5),
|
| 84 |
+
title=title,
|
| 85 |
+
description=description,
|
| 86 |
+
examples=[
|
| 87 |
+
["examples/dog.jpg"],
|
| 88 |
+
["examples/cat.jpg"],
|
| 89 |
+
],
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
iface.launch(share=True)
|
examples/cat.jpg
ADDED
|
examples/dog.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
Pillow
|