CoolFace
Apppublic

CodeGitte/multi_label_image_classification

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py32 linesDownload Raw Back to root
1import requests2 3import gradio as gr4import torch 5from timm import create_model6from timm.data import resolve_data_config7from timm_data.transforms_factory import create_transform 8 9IMGAGENET_1K_URL = "https://storage.googleapis.com/bit_models/ilsvrc2012_wordnet_lemmas.txt"10LABELS = requests.get(IMGAGENET_1K_URL).text.strip().split('\n')11 12model = create_model('restnet50', pretrained = True)13 14transform = create_transform(15    **resolve_data_config({}, model=model)16)17model.eval()18 19def predict_fn(img):20    img = img.convert('RGB')21    img = transform(img).unsqueeze(0)22 23    with torch.no_grad():24        out = model(img)25 26    probabilities = torch.nn.functional.softmax(out[0], dim=0)27 28    values, indices = torch.topk(probabilities, k=5)29 30    return {LABELS[i]: v.item() for i, v in zip(indices, values)}31 32gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs='label').launch()