当前位置: 首页 > 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

在这里插入图片描述

在这里插入图片描述

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

相关文章:

  • Flutter笔记:光影动画按钮、滚动图标卡片组等
  • QT基础与细节理解
  • 开机自启动笔记本的小键盘
  • 【Spring之底层核心架构概念解析】
  • Docker快速安装kafka
  • EM@解三角形@正弦定理@余弦定理
  • 云效流水线docker部署 :node.js镜像部署VUE项目
  • 大数据毕业设计选题推荐-农作物观测站综合监控平台-Hadoop-Spark-Hive
  • Swift编写爬取商品详情页面的爬虫程序
  • MATLAB算法实战应用案例精讲-【目标检测】机器人抓取
  • MATLAB算法实战应用案例精讲-【目标检测】机器视觉-工业相机参数
  • .NET关于 跳过SSL中遇到的问题
  • LeetCode 137. 只出现一次的数字 II 中等
  • 【蓝桥杯选拔赛真题17】C++时间换算 第十二届蓝桥杯青少年创意编程大赛C++编程选拔赛真题解析
  • USB偏好设置-Android13
  • 实现windows 窗体的自己画,网上摘抄的,学习了
  • Android系统模拟器绘制实现概述
  • conda常用的命令
  • JavaScript异步流程控制的前世今生
  • PHP面试之三:MySQL数据库
  • Spark学习笔记之相关记录
  • spring security oauth2 password授权模式
  • vue自定义指令实现v-tap插件
  • 分布式熔断降级平台aegis
  • 小程序开发之路(一)
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • 自定义函数
  • postgresql行列转换函数
  • 阿里云服务器如何修改远程端口?
  • ​ArcGIS Pro 如何批量删除字段
  • ​决定德拉瓦州地区版图的关键历史事件
  • ​油烟净化器电源安全,保障健康餐饮生活
  • (20050108)又读《平凡的世界》
  • (js)循环条件满足时终止循环
  • (规划)24届春招和25届暑假实习路线准备规划
  • (十六)一篇文章学会Java的常用API
  • (算法)前K大的和
  • (转)一些感悟
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • .halo勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .net core Swagger 过滤部分Api
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .net 验证控件和javaScript的冲突问题
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装
  • .NET成年了,然后呢?
  • .so文件(linux系统)
  • @Resource和@Autowired的区别
  • [ Algorithm ] N次方算法 N Square 动态规划解决
  • [ C++ ] 继承
  • [20161214]如何确定dbid.txt
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [3D游戏开发实践] Cocos Cyberpunk 源码解读-高中低端机性能适配策略
  • [Android]通过PhoneLookup读取所有电话号码
  • [Enterprise Library]调用Enterprise Library时出现的错误事件之关闭办法