File size: 1,218 Bytes
d976184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0809f6
c80591e
d976184
 
 
 
 
 
 
c80591e
d976184
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import gradio as gr
import timm
import hyenapixel.models
import torch
import numpy as np
from PIL import Image
 
with open("imagenet.txt") as file:
    class_names = [line.rstrip() for line in file]
 
def predict(model_name, image):
    model = timm.create_model(model_name, pretrained=True)
    model.eval()
    image_size = 224
    if "_384" in model_name:
        image_size = 384
    transform = timm.data.create_transform(image_size)
    input_tensor = transform(image).unsqueeze(0)
    with torch.no_grad():
        output = model(input_tensor)
    output_np = torch.softmax(output, dim=1)[0].numpy()
    return {clsname: prob for clsname, prob in zip(class_names, output_np)}
 
interface = gr.Interface(
    fn=predict,
    inputs=[
        gr.Dropdown(label="Select Model", value="hb_former_b36", choices=["hpx_former_s18", "hpx_former_s18_384", "hb_former_s18", "c_hpx_former_s18", "hpx_a_former_s18", "hb_a_former_s18", "hpx_former_b36", "hb_former_b36"]),
        gr.Image(type="pil", label="Upload Image")
    ],
    outputs=gr.Label(label="Prediction", num_top_classes=10),
    title="Image Classification",
    description="Choose a model and upload an image to predict the class."
)
 
interface.launch()