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

深度学习pytorch之hub模块

pytorchhub模块里面有很多模型
https://pytorch.org/hub/
github网址:https://github.com/pytorch/pytorch

import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'fcn_resnet50', pretrained=True)
# or
# model = torch.hub.load('pytorch/vision:v0.10.0', 'fcn_resnet101', pretrained=True)
model.eval()
All pre-trained models expect input images normalized in the same way, i.e. mini-batches of 3-channel RGB images of shape (N, 3, H, W), where N is the number of images, H and W are expected to be at least 224 pixels. The images have to be loaded in to a range of [0, 1] and then normalized using mean = [0.485, 0.456, 0.406] and std = [0.229, 0.224, 0.225].The model returns an OrderedDict with two Tensors that are of the same height and width as the input Tensor, but with 21 classes. output['out'] contains the semantic masks, and output['aux'] contains the auxillary loss values per-pixel. In inference mode, output['aux'] is not useful. So, output['out'] is of shape (N, 21, H, W). More documentation can be found here.# Download an example image from the pytorch website
import urllib
url, filename = ("https://github.com/pytorch/hub/raw/master/images/deeplab1.png", "deeplab1.png")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# sample execution (requires torchvision)
from PIL import Image
from torchvision import transforms
input_image = Image.open(filename)
input_image = input_image.convert("RGB")
preprocess = transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model# move the input and model to GPU for speed if available
if torch.cuda.is_available():input_batch = input_batch.to('cuda')model.to('cuda')with torch.no_grad():output = model(input_batch)['out'][0]
output_predictions = output.argmax(0)
The output here is of shape (21, H, W), and at each location, there are unnormalized probabilities corresponding to the prediction of each class. To get the maximum prediction of each class, and then use it for a downstream task, you can do output_predictions = output.argmax(0).Here’s a small snippet that plots the predictions, with each color being assigned to each class (see the visualized image on the left).# create a color pallette, selecting a color for each class
palette = torch.tensor([2 ** 25 - 1, 2 ** 15 - 1, 2 ** 21 - 1])
colors = torch.as_tensor([i for i in range(21)])[:, None] * palette
colors = (colors % 255).numpy().astype("uint8")# plot the semantic segmentation predictions of 21 classes in each color
r = Image.fromarray(output_predictions.byte().cpu().numpy()).resize(input_image.size)
r.putpalette(colors)import matplotlib.pyplot as plt
plt.imshow(r)
# plt.show()
Model Description
FCN-ResNet is constructed by a Fully-Convolutional Network model, using a ResNet-50 or a ResNet-101 backbone. The pre-trained models have been trained on a subset of COCO train2017, on the 20 categories that are present in the Pasca

在这里插入图片描述

在这里插入图片描述

根据灰色的部分复制相应的代码

http://www.lryc.cn/news/225728.html

相关文章:

  • LeetCode 2258. 逃离火灾:BFS
  • C# PaddleInference.PP-HumanSeg 人像分割 替换背景色
  • Java 变量初始化的两种方式和优缺点比较
  • 15.三数之和
  • 竞赛选题 深度学习疲劳驾驶检测 opencv python
  • PROFINET和UDP、MODBUS-RTU通信速度对比实验
  • CSS3 多媒体查询、网格布局
  • SpringBoot基础(九)-- 配置文件优先级
  • C++ static关键字
  • Anaconda Powershell Prompt和Anaconda Prompt的区别
  • 关于tcp发送成功但对端无法接收情况的思考
  • 01-解码-H264转YUV
  • keepalived+Nginx+邮件
  • CMakeCache.txt有什么用
  • ZYNQ_project:key_breath
  • 设计模式 (原则)
  • LeetCode 每日一题 2023/11/6-2023/11/12
  • Linux 基于 LVM 逻辑卷的磁盘管理【简明教程】
  • CTFHUB-WEB-SQL注入
  • 案例分享:某汽车企业通过龙智拓展Jira功能,实现高效项目管理
  • 【算法与数据结构】40、LeetCode组合总和 II
  • Flink SQL -- 命令行的使用
  • asp.net core把所有接口和实现类批量注入到容器
  • SPSS曲线回归
  • 软件之禅(七)面向对象(Object Oriented)
  • 汽车之家车型_车系_配置参数数据抓取
  • RabbitMQ的 五种工作模型
  • 原型制作神器ProtoPie的使用Unity与网页跨端交互
  • 另辟奚径-Android Studio调用Delphi窗体
  • SOLID 原则,程序设计五大原则,设计模式