Spaces:
Build error
Build error
added vggcrop
Browse files
app.py
CHANGED
|
@@ -8,6 +8,13 @@ from huggingface_hub import hf_hub_download
|
|
| 8 |
from PIL import Image
|
| 9 |
import numpy as np
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Load the model checkpoint from Hugging Face
|
| 12 |
checkpoint_path = hf_hub_download(repo_id="ttoosi/resnet50_robust_face", filename="100_checkpoint.pt")
|
| 13 |
|
|
@@ -44,10 +51,24 @@ def predict(image):
|
|
| 44 |
with torch.no_grad():
|
| 45 |
output = model(image) # Perform inference on CPU
|
| 46 |
_, predicted_class = output.max(1)
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Create the Gradio interface
|
| 50 |
-
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Launch the interface
|
| 53 |
iface.launch()
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
import numpy as np
|
| 10 |
|
| 11 |
+
from datasets import load_dataset
|
| 12 |
+
import random
|
| 13 |
+
|
| 14 |
+
# Load the VGGFace2 dataset using Hugging Face's datasets library
|
| 15 |
+
ds = load_dataset("chronopt-research/cropped-vggface2-224")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
# Load the model checkpoint from Hugging Face
|
| 19 |
checkpoint_path = hf_hub_download(repo_id="ttoosi/resnet50_robust_face", filename="100_checkpoint.pt")
|
| 20 |
|
|
|
|
| 51 |
with torch.no_grad():
|
| 52 |
output = model(image) # Perform inference on CPU
|
| 53 |
_, predicted_class = output.max(1)
|
| 54 |
+
# Fetch 9 random samples from the predicted class
|
| 55 |
+
class_samples = ds.filter(lambda example: example['label'] == predicted_class.item())['train']
|
| 56 |
+
sample_images = random.sample(list(class_samples), min(len(class_samples), 9))
|
| 57 |
+
|
| 58 |
+
sample_images_urls = [sample['image'] for sample in sample_images]
|
| 59 |
+
|
| 60 |
+
return f"Predicted class: {predicted_class.item()}", sample_images_urls
|
| 61 |
+
|
| 62 |
+
# # Create the Gradio interface
|
| 63 |
+
# iface = gr.Interface(fn=predict, inputs=gr.Image(type="numpy"), outputs="text") # Updated from gr.inputs.Image to gr.Image
|
| 64 |
|
| 65 |
# Create the Gradio interface
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=predict,
|
| 68 |
+
inputs=gr.Image(type="numpy"),
|
| 69 |
+
outputs=[gr.Textbox(label="Predicted Class"), gr.Gallery(label="Class Samples")],
|
| 70 |
+
title="ResNet-50 VGGFace2 Classifier"
|
| 71 |
+
)
|
| 72 |
|
| 73 |
# Launch the interface
|
| 74 |
iface.launch()
|