当前位置: 首页 > news >正文

网站建设与规划心得辽宁省建设科学研究院网站

网站建设与规划心得,辽宁省建设科学研究院网站,泰安中推网络科技有限公司,福建西南建设有限公司网站前言 fastapi是目前一个比较流行的python web框架#xff0c;在大模型日益流行的今天#xff0c;其云端部署和应用大多数都是基于fastapi框架。所以掌握和理解fastapi框架基本代码和用法尤显重要。 需要注意的是#xff0c;fastapi主要是通过app对象提供了web服务端的实现代…前言 fastapi是目前一个比较流行的python web框架在大模型日益流行的今天其云端部署和应用大多数都是基于fastapi框架。所以掌握和理解fastapi框架基本代码和用法尤显重要。 需要注意的是fastapi主要是通过app对象提供了web服务端的实现代码对于一个完整应用来说还需要uvicorn组件来启动web服务如果想要可视化UI的话可以考虑使用streamlit前端。 代码 大家可以基于下面这个简单代码例子(参考GitHub - markthink/streamlit-fastapi-model稍有修改)来加深理解。一共三个python源文件: segmentation.py(获取pytorch deeplabv3模型和推理该模型实现图像分割), ui.py(基于streamlit构造webUI供用户来选择图片并显示结果)和server.py(基于fastapi编写服务端函数来响应前端UI发来的/segmentation消息)。 segmentation.py: import io, torch from PIL import Image from torchvision import transformsdef get_segmentator():model torch.hub.load(pytorch/vision:v0.10.0, deeplabv3_mobilenet_v3_large, pretrainedTrue)model.eval()return modeldef get_segments(model, binary_image, max_size512):input_image Image.open(io.BytesIO(binary_image)).convert(RGB)width, height input_image.sizeresize_factor min(max_size/width,max_size/height)resize_image input_image.resize((int(input_image.width * resize_factor),int(input_image.height*resize_factor)))preprocess transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean[0.485,0.456,0.406],std[0.229,0.224,0.225])])input_tensor preprocess(resize_image)input_batch input_tensor.unsqueeze(0)with torch.no_grad():output model(input_batch)[out][0]output_predictions output.argmax(0)# create a color pallette, selecting a color for each classpalette torch.tensor([2 ** 25 - 1, 2 ** 15 - 1, 2 ** 21 - 1])colors torch.as_tensor([i for i in range(21)])[:, None] * palettecolors (colors % 255).numpy().astype(uint8)r Image.fromarray(output_predictions.byte().cpu().numpy()).resize(input_image.size)r.putpalette(colors)return r ui.py: import io, requests import streamlit as st from PIL import Image from requests_toolbelt.multipart.encoder import MultipartEncoder#backend http://fastapi:8000/segmentation backend http://0.0.0.0:8000/segmentationdef process(image, server_url:str):m MultipartEncoder(fields{file: (filename, image, image/jpeg)})r requests.post(server_url, datam, headers{Content-Type:m.content_type}, timeout8000)return rst.title(DeepLabV3 image segmentation) st.write(AI inference demo for fastapi calling pytorch model)input_image st.file_uploader(pls input one image) if st.button(get image segmentation):col1, col2 st.columns(2)if input_image:segments process(input_image, backend)original_image Image.open(input_image).convert(RGB)segmented_image Image.open(io.BytesIO(segments.content)).convert(RGB)col1.header(original version)col1.image(original_image, use_column_widthTrue)col2.header(segmentation version)col2.image(segmented_image, use_column_widthTrue)else:st.write(pls input one image) server.py: import io from segmentation import get_segmentator, get_segments from starlette.responses import Response from fastapi import FastAPI, Filemodel get_segmentator()app FastAPI(titleDeeplabv3 image segmentation,descriptiondemo for deploying pytorch models with fastapi,version0.1.0 )app.post(/segmentation) def get_segmentation(file:bytesFile(...)):print(hello post)segmented_img get_segments(model, file)bytes_io io.BytesIO()segmented_img.save(bytes_io, formatPNG)return Response(bytes_io.getvalue(), media_typeimage/png) 这三个文件放在一个目录下面启动两个terminal窗口分别输入命令  uvicorn server:app --host 0.0.0.0 --port 8000streamlit run ui.py 全部代码在CPUubuntu20.04上运行成功无需GPU加速。 webui如下图所示 首先点击Browse file按钮选择待分割图片然后点击get image segmentation按钮就可以看到原始图片和分割结果。
http://www.sczhlp.com/news/157640/

相关文章:

  • 怎么用宝塔做网站实木餐桌椅移动网站建设
  • 网站制作 建站WordPress封面生成
  • 长沙网站建设长沙公司做网页推广要多少钱
  • 做网站买服务器怎么样茂名建站价格
  • 网站360做的标记如何取消电商需要多少投入
  • 教做缝纫的网站html在线模板
  • 网站备案知识国内知名的app开发
  • 手机端网站外部链接如何去优化网络营销软件代理
  • 天津市市建设与管理局网站软装设计方案ppt
  • 专门做物理的网站企排排官网
  • ubuntu安装pbc库
  • 《电路基础》第六章学习笔记
  • 四川营销网站建设微信管理系统免费版
  • 空间 建网站江西省城乡建设厅网站
  • 下列关于网站开发中网友上传品牌商城网站开发
  • 寮步做网站公司宁波市政务网站建设和管理标准
  • 建立带数据库的网站三明住房建设局网站
  • 重庆直播网站平台建设平台直播
  • 网站如何静态化宣传手册的制作
  • 网站优化策略分析论文怎么搞一个服务器建设网站
  • 中国铁塔公司招聘网站顶尖设计
  • 凡科建的网站怎么样四川省城乡和住房建设厅网站首页
  • 如何用微信公众号做企业网站网站建设的内容是什么
  • 怎样建设电子商务网站制作一号店网站
  • 国外用什么做网站上海集团网站建设
  • 小说网站开发为什么很多公司没自己的网站
  • 做刀网站长沙网站建设定制
  • 泵网站建设网站突然打不开是什么原因
  • 临沧网站建设临沧网络域名怎么注册
  • 门户网站做公众号的好处开发小程序哪家好