pytorch
pytorch
点PY
If you cannot risk, you cannot grow. If you cannot grow, you cannot become you best. If you cannot become you best, you cannot be happy. If you cannot be happy, what else matters?
展开
-
获取Yolov5模型指定层的特征图——forward hooks
作为一名积极开发深度学习模型的工程师,我更喜欢PyTorch,因为它易于使用,主要是因为它与 Python 的相似性,尤其是 Numpy。然而,要找到如何从模型的层中干净地提取中间激活(对于可视化、调试模型以及用于其他算法很有用)却出奇地困难。我仍然对 PyTorch 缺乏关于这个超级重要问题的明确文档感到惊讶。在这篇文章中,我将尽力引导您完成这个过程。举个例子,让我们使用预训练resnet18模型,但相同的技术适用于所有模型——预训练模型、自定义模型或标准模型。原创 2022-11-21 15:28:57 · 2587 阅读 · 17 评论 -
计算pytorch模型的flops和params
from thop import profileimport torchif __name__ == '__main__': # Model print('==> Building model..') modelPath = "epoch0167_model.pth" model = torch.load(modelPath, map_location="cpu") dummy_input = torch.randn(1, 3, 256, 256)原创 2022-05-26 15:19:15 · 434 阅读 · 0 评论 -
UnboundLocalError: local variable ‘beta1‘ referenced before assignment(adamw.py)
当我在文件adamw.py的110行增加1个tab时,似乎可以解决这个问题。变量beta1, beta2是在循环之外定义的,所以它发生了提到的问题。原创 2022-02-17 17:19:12 · 2289 阅读 · 14 评论 -
炼丹秘籍(pytorch)
文章目录数据数据增强模型插件优化策略模型泛化数据数据增强语义分割目标检测模型插件即插即用模块优化策略模型泛化Multi-Sample Dropout原创 2022-01-23 15:00:26 · 880 阅读 · 0 评论 -
LibTorch在windows下编译
文章目录准备工作文件目录进行编译准备工作cmakelibTorch文件目录进行编译CMakeLists.txtcmake_minimum_required(VERSION 3.12 FATAL_ERROR)project(example-app)find_package(Torch REQUIRED)find_package(OpenCV REQUIRED)if(NOT Torch_FOUND) message(FATAL_ERROR "Pytorch Not Fou原创 2022-01-21 16:37:40 · 2428 阅读 · 0 评论 -
利用torch2trt进行PyTorch推理加速
repo: https://github.com/NVIDIA-AI-IOT/torch2trt原创 2021-10-19 14:17:07 · 1629 阅读 · 0 评论 -
PyTorch冻结网络参数
方法一将需要固定的那部分参数的requires_grad置为False.在优化器中加入filter根据requires_grad进行过滤.# requires_grad置为Falsefor p in net.XXX.parameters(): p.requires_grad = False# filteroptimizer.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-3)方法二这种方式只需原创 2021-03-27 15:29:22 · 4734 阅读 · 4 评论 -
基于netron的pytorch模型可视化
可视化效果图例子from models.seg_hrnet import get_seg_modelimport yamlimport torchimport osif __name__ == '__main__': cfgPath = './experiments/cityscapes/seg_hrnet_w18_small_v1_512x1024_sgd_lr1e-2_wd5e-4_bs_12_epoch484.yaml' with open(cfgPath, 'r')原创 2021-01-18 17:35:56 · 497 阅读 · 0 评论 -
Pytorch的gather函数
torch.gather(input, dim, index, out=None) → Tensor 官方给的格式import torcha = torch.Tensor([[1,2,3],[4,5,6]])print(a)index_1 = torch.LongTensor([[0,1],[2,0]])index_2 = torch.LongTensor([[0,1,1],[0,0,1]])print(a.gather(1,index_1))print(a.gather(0,index_原创 2021-01-07 08:20:27 · 185 阅读 · 0 评论 -
win10下detectron2的安装教程
1. 安装所需基础环境我所用的前置环境如下cuda11.1pytorch1.7python3.7VS2019, 参考https://blog.csdn.net/weixin_42990464/article/details/109994779gcc 6.3.0,参考https://blog.csdn.net/weixin_42990464/article/details/110109988ninja 1.10.0pycocotools,参考https://www.jianshu.com/p原创 2020-11-25 09:31:49 · 1357 阅读 · 7 评论 -
关于cpp_extension的那些BUG
1. 找不到文件Traceback (most recent call last): File "D:\Python\Python36-32\lib\site-packages\selenium-3.4.3-py3.6.egg\selenium\webdriver\common\service.py", line 74, in start stdout=self.log_file, stderr=self.log_file) File "D:\Python\Python36-32\lib\原创 2020-10-26 17:34:19 · 2048 阅读 · 3 评论 -
torchsummary的用法
summary可以打印网络结构和参数 from torchsummary import summary from torchvision.models import resnet18 model = resnet18() summary(model, input_size=[(3, 256, 256)], batch_size=2, device="cpu")输出结果---------------------------------------------------原创 2020-08-01 15:54:33 · 14987 阅读 · 9 评论 -
基于pytorch的参数统计
net = get_net() print("Total param size = %f MB" % (sum(v.numel() for v in net.parameters()) / 1024 / 1024)) #out:Total param size = 55.205193 MB原创 2020-06-06 09:35:23 · 282 阅读 · 0 评论 -
cuda out of memory(PyTorch)
情况1model.forward()过程中,中间变量过多,导致GPU使用量增大,如下所示: def forward(self, x): batch_size = x.shape[0] x0 = self.base_model(x) # Add positional info x1 = self.up1(x0) # 512, 32, 32 x2 = self.up2(x1) # 256, 64原创 2020-05-29 09:56:47 · 2981 阅读 · 0 评论 -
nn.Sequential与nn.ModuleList简介
nn.Sequentialnn.Sequential里面的模块按照顺序进行排列的,所以必须确保前一个模块的输出大小和下一个模块的输入大小是一致的。如下面的例子所示:#首先导入torch相关包import torchimport torch.nn as nnimport torch.nn.functional as Fclass net_seq(nn.Module): def __init__(self): super(net2, self).__init__()转载 2020-05-22 16:18:58 · 396 阅读 · 0 评论 -
基于pytorch的特征图可视化
前言在利用深度学习进行分类时,有时需要对中间的特征图进行可视化操作,看看网络都学习了哪些东西。本篇博文将简单介绍下,可视化操作。网络模型部分主要是forward部分,简单处理下。def forward(self, x): outputs = [] conv0 = self.encoder.conv1(x) conv0 = self.encoder.bn1(conv0) conv0 = self.encoder.relu(conv0) conv1 = self原创 2020-05-17 21:07:46 · 5316 阅读 · 6 评论 -
BUG:ValueError: Expected more than 1 value per channel when training
出现的问题File “C:\Users\yp\Anaconda3\envs\py37\lib\site-packages\torch\nn\modules\module.py”, line 532, in callresult = self.forward(*input, **kwargs)File “F:\flyAI\COVIDClassification_FlyAI\models\resnest\resnet.py”, line 107, in forwardout = self.conv2(原创 2020-05-14 10:30:31 · 681 阅读 · 0 评论 -
RuntimeError: Error(s) in loading state_dict for DataParallel
出现的问题字典中的关键词对不上,导致加载模型错误。解决方法根据Missing key(s)中的提示,进行修改。# original saved file with DataParallelstate_dict = torch.load('myfile.pth.tar')# create new OrderedDict that does not contain `module.`from collections import OrderedDictnew_state_dict = Orde原创 2020-05-12 09:27:30 · 1730 阅读 · 2 评论 -
PyTorch中的数据类型
若我们想把目标变量转化成8bit uint:x1 = x1.byte()Referencehttps://zhuanlan.zhihu.com/p/36080348转载 2020-05-08 10:03:32 · 463 阅读 · 0 评论 -
dataloader实现自定义数据读取
存在的问题在利用pytorch实现简单的图像分类和图像分割时,我们习惯使用datalodaer自带的数据读取方式,一般不会出现问题。但是当我们进行目标检测或是实例分割任务时,经常会遇到图像与图像之间的box数量不同,如果默认读取方式,将导致张量在广播时会出现错误,如下所示。RuntimeError: invalid argument 0: Sizes of tensors must matc...原创 2020-05-03 20:20:05 · 1747 阅读 · 0 评论 -
The size of tensor a (x) must match the size of tensor b (y) at non-singleton dimension z
问题描述张量A和B在广播过程中,对应维度不匹配。张量广播PyTorch操作支持广播,其张量参数可以自动扩展为相同大小(不复制数据)。PyTorch广播语义密切跟随numpy式广播。如果你熟悉数字广播,可以按照之前流程执行。其广播机制遵循以下规则:每个张量至少具有一个维度;广播时,从尾部维度开始,满足尺寸大小相等或其中一个为1或其中一个不存在。正例:反例:Referenc...原创 2020-05-02 20:06:11 · 60790 阅读 · 3 评论 -
学习率预热(warm up)
学习率预热学习率是神经网络训练中最重要的超参数之一,针对学习率的技巧有很多。Warm up是在ResNet论文中提到的一种学习率预热的方法。由于刚开始训练时模型的权重(weights)是随机初始化的,此时选择一个较大的学习率,可能会带来模型的不稳定。学习率预热就是在刚开始训练的时候先使用一个较小的学习率,训练一些epoches或iterations,等模型稳定时再修改为预先设置的学习率进行训练。...原创 2020-03-03 20:43:35 · 2226 阅读 · 0 评论 -
Test time augmentation(TTA)
Test time augmentation数据扩充是模型训练期间通常使用的一种方法,它使用来自训练数据集的样本的修改副本来扩充训练集。数据增强通常使用图像数据执行,其中使用一些执行的图像处理技术(如缩放、翻转、移位等)创建训练数据集中的图像副本。人工扩展的训练数据集可以产生更熟练的模型,因为深度学习模型的性能通常会随着训练数据集的大小而不断扩展。此外,训练数据集中图像的修改或增强版本可以帮...原创 2020-02-28 16:34:57 · 8507 阅读 · 3 评论 -
pytorch: “multi-target not supported” error message
BUG使用Cross_entropy损失函数时出现 RuntimeError: multi-target not supported at …可能存在的问题1)其标签必须为0~n-1,而且必须为1维的,如果设置标签为[nx1]的,则也会出现以上错误。2)标签y打印:tensor([[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, ...原创 2020-02-26 19:59:53 · 674 阅读 · 1 评论 -
关于pytorch中损失函数中输入输出不匹配问题
File "C:\Users\Rain\AppData\Local\Programs\Python\Anaconda.3.5.1\envs\python35\python35\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__ result = self.forward(*input, **kwargs...原创 2019-08-18 21:25:28 · 9706 阅读 · 29 评论 -
在pytorch中,如何对标准的预训练模型进行修改以适应三通道以上的输入
net = resnet50(pretrained=pretrained)with torch.no_grad():pretrained_conv1 = net.conv1.weight.clone()# Assign new conv layer with 4 input channelsnet.conv1 = torch.nn.Conv2d(4, 64, 7,...原创 2019-08-01 17:22:56 · 3034 阅读 · 1 评论