File size: 1,033 Bytes
d4bddcf 2411be5 a7418a6 2411be5 a7418a6 d4bddcf 8eed7c4 3d2c6fa 2411be5 2fd03ec 2411be5 2fd03ec 2411be5 2fd03ec 3d2c6fa 2fd03ec |
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 |
from transformers import AutoFeatureExtractor, ResNetForImageClassification
import torch
# from datasets import load_dataset
# dataset = load_dataset("huggingface/cats-image")
# image = dataset["test"]["image"][0]
feature_extractor = AutoFeatureExtractor.from_pretrained("microsoft/resnet-50")
model = ResNetForImageClassification.from_pretrained("microsoft/resnet-50")
import gradio as gr
def segment(image):
inputs = feature_extractor(image, return_tensors="pt")
# with torch.no_grad():
# logits = model(**inputs).logits
# model predicts one of the 1000 ImageNet classes
# predicted_label = logits.argmax(-1).item()
# return model.config.id2label[predicted_label]
with torch.no_grad():
prediction = torch.nn.functional.softmax(model(**inputs)[0], dim=0)
return {model.config.id2label[i]: float(prediction[i]) for i in range(3)}
# gr.Interface(fn=segment, inputs="image", outputs="text").launch()
gr.Interface(fn=segment, inputs="image", outputs="label").launch() |