File size: 950 Bytes
daa014f
 
 
 
 
 
 
 
 
 
 
 
198eb21
daa014f
 
e29d86f
daa014f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237779e
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
import requests
import gradio as gr
import torch

from timm import create_model
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform


IMAGENET_1K_URL = 'https://storage.googleapis.com/bit_models/ilsvrc2012_1k_wordnet_lemmas.txt'
IMAGENET_1K_LABELS = requests.get(IMAGENET_1K_URL).text.strip().split('\n')

model = create_model('resnet50', pretrained=True)

transform = create_transform(
    **resolve_data_config({}, model=model))


model.eval()


def predict(image):
    img = image.convert('RGB')
    transformed_image = transform(img).unsqueeze(0)
    with torch.no_grad():
        out = model(transformed_image)
    probabilities = torch.nn.functional.softmax(out[0], dim=0)
    values, indices = torch.topk(probabilities, k=5)
    return {IMAGENET_1K_LABELS[i]: v.item() for i, v in zip(indices, values)}


gr.Interface(predict, gr.inputs.Image(type='pil'),
             outputs='label').launch()