Spaces:
Runtime error
Runtime error
autonomous019
commited on
Commit
•
c47a41a
1
Parent(s):
e836d24
adding ui interface template
Browse files
app.py
CHANGED
@@ -4,8 +4,8 @@ from PIL import Image
|
|
4 |
import requests
|
5 |
import matplotlib.pyplot as plt
|
6 |
import gradio as gr
|
7 |
-
|
8 |
-
|
9 |
|
10 |
|
11 |
# option 1: load with randomly initialized weights (train from scratch)
|
@@ -13,7 +13,7 @@ import gradio as gr
|
|
13 |
config = ViTConfig(num_hidden_layers=12, hidden_size=768)
|
14 |
model = ViTForImageClassification(config)
|
15 |
|
16 |
-
print(config)
|
17 |
|
18 |
feature_extractor = ViTFeatureExtractor()
|
19 |
|
@@ -24,3 +24,34 @@ feature_extractor = ViTFeatureExtractor()
|
|
24 |
image = "cats.jpg"
|
25 |
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import requests
|
5 |
import matplotlib.pyplot as plt
|
6 |
import gradio as gr
|
7 |
+
from transformers import ImageClassificationPipeline, PerceiverForImageClassificationConvProcessing, PerceiverFeatureExtractor
|
8 |
+
import torch
|
9 |
|
10 |
|
11 |
# option 1: load with randomly initialized weights (train from scratch)
|
|
|
13 |
config = ViTConfig(num_hidden_layers=12, hidden_size=768)
|
14 |
model = ViTForImageClassification(config)
|
15 |
|
16 |
+
#print(config)
|
17 |
|
18 |
feature_extractor = ViTFeatureExtractor()
|
19 |
|
|
|
24 |
image = "cats.jpg"
|
25 |
|
26 |
|
27 |
+
torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
|
28 |
+
torch.hub.download_url_to_file('https://storage.googleapis.com/perceiver_io/dalmation.jpg', 'dog.jpg')
|
29 |
+
|
30 |
+
feature_extractor = PerceiverFeatureExtractor.from_pretrained("deepmind/vision-perceiver-conv")
|
31 |
+
model = PerceiverForImageClassificationConvProcessing.from_pretrained("deepmind/vision-perceiver-conv")
|
32 |
+
|
33 |
+
image_pipe = ImageClassificationPipeline(model=model, feature_extractor=feature_extractor)
|
34 |
+
|
35 |
+
def classify_image(image):
|
36 |
+
results = image_pipe(image)
|
37 |
+
# convert to format Gradio expects
|
38 |
+
output = {}
|
39 |
+
for prediction in results:
|
40 |
+
predicted_label = prediction['label']
|
41 |
+
score = prediction['score']
|
42 |
+
output[predicted_label] = score
|
43 |
+
return output
|
44 |
+
|
45 |
+
image = gr.inputs.Image(type="pil")
|
46 |
+
label = gr.outputs.Label(num_top_classes=5)
|
47 |
+
examples = [["cats.jpg"], ["dog.jpg"]]
|
48 |
+
title = "Interactive demo: Perceiver for image classification"
|
49 |
+
description = "Demo for classifying images with Perceiver IO. To use it, simply upload an image or use the example images below and click 'submit' to let the model predict the 5 most probable ImageNet classes. Results will show up in a few seconds."
|
50 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2107.14795'>Perceiver IO: A General Architecture for Structured Inputs & Outputs</a> | <a href='https://deepmind.com/blog/article/building-architectures-that-can-handle-the-worlds-data/'>Official blog</a></p>"
|
51 |
+
|
52 |
+
gr.Interface(fn=classify_image, inputs=image, outputs=label, title=title, description=description, examples=examples, enable_queue=True).launch(debug=True)
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|