寮步网站建设价钱,东莞人才网最新招聘信息,wordpress 招聘 插件,wordpress 指定前言
在AI快速发展的今天#xff0c;我们作为算法开发人员#xff0c;也应该有一些趁手的工具帮助我们快速开发并验证自己的想法#xff0c;Gradio可以实现快速搭建和共享的功能#xff0c;能够展示出一个前端界面#xff0c;把我们的算法包裹起来#xff0c;快速验证算…前言
在AI快速发展的今天我们作为算法开发人员也应该有一些趁手的工具帮助我们快速开发并验证自己的想法Gradio可以实现快速搭建和共享的功能能够展示出一个前端界面把我们的算法包裹起来快速验证算法的有效性文档也丰富通俗易懂喜欢的小伙伴可以学一手。
环境安装
使用pip install安装
gradio
gradio_client
torch
torchvision
代码
完整代码链接
链接: https://pan.baidu.com/s/1x4ZTN7cTASv8cZe_iBoYVw
提取码: r425
from torchvision import transforms
import gradio as gr
import torchmodel torch.hub.load(pytorch/vision:v0.6.0, resnet18, pretrainedTrue).eval()# 打开文本文件
file_path labels.txt # 将文件路径替换为你实际的文本文件路径
with open(file_path, r) as file:# 读取文件内容labels file.readlines()
#去掉每个label的换行符
labels [label.rstrip() for label in labels]#预测函数
def predict(inp):inp transforms.ToTensor()(inp).unsqueeze(0)with torch.no_grad():prediction torch.nn.functional.softmax(model(inp)[0], dim0)confidences {labels[i]: float(prediction[i]) for i in range(1000)}return confidences#fn表示触发函数当我们点击提交按钮时触发predict函数进行推理
#inputs表示图像会被转换为PIL.Image格式
#outputs将以label的形式展示出来
#examples可以将图片作为示例展示出来供用户使用
gr.Interface(fnpredict,inputsgr.Image(typepil),outputsgr.Label(num_top_classes3),examples[sheep.jpeg, panda.jpg, dog.jpg, tiger.jpg]).launch()
运行 点击http://127.0.0.1:7860,会出现前端展示页面
页面展示 参考链接 Gradio Interface Docs Image Classification In Pytorch