Spaces:
Sleeping
Sleeping
File size: 969 Bytes
7384ab4 a40d9e6 7384ab4 a40d9e6 7b6d133 |
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 |
import torch
model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval() # evaluation
import requests # 用於發送 HTTP 請求
from PIL import Image
from torchvision import transforms
# Download human-readable labels for ImageNet.
response = requests.get("https://git.io/JJkYN")
labels = response.text.split("\n")
def predict(inp):
inp = transforms.ToTensor()(inp).unsqueeze(0) # Resnet18輸入格式:(batch_size=1, C, H, W),batch_size=1表示一次可以處理單張圖片
with torch.no_grad(): # no_grad:不需要計算gradient
prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
return confidences
import gradio as gr
gr.Interface(fn=predict,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=3),
examples=["./images/lion.jpg", "./images/cheetah.jpg"]).launch(auth=("admin", "pass1234")) |