SivaResearch
commited on
Commit
·
22cbf4e
1
Parent(s):
6620552
Files Added for combo classifiers
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
model_names = [
|
5 |
+
"apple/mobilevit-small",
|
6 |
+
"facebook/deit-base-patch16-224",
|
7 |
+
"facebook/convnext-base-224",
|
8 |
+
"google/vit-base-patch16-224",
|
9 |
+
"google/mobilenet_v2_1.4_224",
|
10 |
+
"microsoft/resnet-50",
|
11 |
+
"microsoft/swin-base-patch4-window7-224",
|
12 |
+
"microsoft/beit-base-patch16-224",
|
13 |
+
"nvidia/mit-b0",
|
14 |
+
"shi-labs/nat-base-in1k-224",
|
15 |
+
"shi-labs/dinat-base-in1k-224",
|
16 |
+
]
|
17 |
+
|
18 |
+
|
19 |
+
def process(image_file, top_k):
|
20 |
+
labels = []
|
21 |
+
for m in model_names:
|
22 |
+
p = pipeline("image-classification", model=m)
|
23 |
+
pred = p(image_file)
|
24 |
+
labels.append({x["label"]: x["score"] for x in pred[:top_k]})
|
25 |
+
return labels
|
26 |
+
|
27 |
+
|
28 |
+
# Inputs
|
29 |
+
image = gr.Image(type="filepath", label="Upload an image")
|
30 |
+
top_k = gr.Slider(minimum=1, maximum=5, step=1, value=5, label="Top k classes")
|
31 |
+
|
32 |
+
# Output
|
33 |
+
labels = [gr.Label(label=m) for m in model_names]
|
34 |
+
|
35 |
+
description = "This Space lets you quickly compare the most popular image classifiers available on the hub, including the recent NAT and DINAT models. All of them have been fine-tuned on the ImageNet-1k dataset. Anecdotally, the three sample images have been generated with a Stable Diffusion model :)"
|
36 |
+
|
37 |
+
iface = gr.Interface(
|
38 |
+
theme="huggingface",
|
39 |
+
description=description,
|
40 |
+
layout="horizontal",
|
41 |
+
fn=process,
|
42 |
+
inputs=[image, top_k],
|
43 |
+
outputs=labels,
|
44 |
+
examples=[
|
45 |
+
["bike.jpg", 5],
|
46 |
+
["car.jpg", 5],
|
47 |
+
["food.jpg", 5],
|
48 |
+
],
|
49 |
+
allow_flagging="never",
|
50 |
+
)
|
51 |
+
|
52 |
+
iface.launch()
|