首页 > 其他分享 >pytorch和deep learning技巧和bug解决方法短篇收集

pytorch和deep learning技巧和bug解决方法短篇收集

时间:2024-08-07 08:57:38浏览次数:15  
标签:load inference torch deep pytorch learning yolov3 model grad

有一些几句话就可以说明白的观点或者解决的的问题,小虎单独收集到这里。

torch.hub.load how does it work

下载预训练模型再载入,用程序下载链接可能失效。

model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

model = torch.hub.load('ultralytics/yolov3', 'yolov3')

I found yolov3.pt in Release.
参考:
https://discuss.pytorch.org/t/using-torch-load-on-a-torch-hub-model/89365
https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading/#before-you-start

No module named ‘models’

In yolov3 ~ yolov7 repo directory,

import torch
# _model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # _model is the model itself
model = torch.load("yolov3.pt", map_location='cpu')['model']
torch.save(model.state_dict(), "yolov3.pth")

But the pretrained weight seems not matched to yolov7_d2. Funny design of yolo pretrained model.

The following submodules of the model were never called during the trace of the graph.

定义的submodules没有用到,可能出现在ddp的时候。移除即可。

 The following submodules of the model were never called during the trace of the graph. They may be unused, or they were accessed by direct calls to .forward() or via other python methods. In the latter case they will have zeros for statistics, though their statistics will still contribute to their parent calling module.

Inplace update to inference tensor outside inferencemode

Use @torch.no_grad() instead of torch.inferece_mode().
在这里插入图片描述
Inference mode vs no_grad()
Inplace update to inference tensor outside InferenceMode is not allowed.You can make a clone to get a normal tensor before doing inplace update.See https://github.com/pytorch/rfcs/pull/17 for more details.

One difference would be that you are not allowed to set the requires_grad attribute on tensors from an inference_mode context:

with torch.no_grad():
    x = torch.randn(1)
    y = x + 1

y.requires_grad = True
z = y + 1
print(z.grad_fn)
> <AddBackward0 object at 0x7fe9c6eafdf0>

with torch.inference_mode():
    x = torch.randn(1)
    y = x + 1

y.requires_grad = True
> RuntimeError: Setting requires_grad=True on inference tensor outside InferenceMode is not allowed.

In a word, outside inference mode, inference tensor is still restricted.

标签:load,inference,torch,deep,pytorch,learning,yolov3,model,grad
From: https://blog.csdn.net/Davidietop/article/details/140948920

相关文章

  • pytorch张量运算
    pytorch张量运算2.1数据操作深度学习落实到计算表现为矩阵计算pytorch、tensorflow中,计算的基本组件是Tensor。张量即多维数组,是矩阵计算的基本单元。Tensor:张量,一维张量即向量vector,二维张量即二维数组。张量是n维数组的统称python中有专门进行矩阵计算的库:numpy。pytor......
  • 托福暑假学习的计划与目标[Plan and Goal of TOEFL Learning in Summer]
    时间即日起至8.31日,共计25天任务二十套听力+阅读=听力lecture3*20=60听力conversation2*20=40阅读2*20=40计划分为五个部分进行阅读每日:长难句分析五句话特殊情况,当日完成了一篇托福阅读可以免除长难句分析,但是必须要分析题目听力每日:边词边......
  • pytorch实现神经网络图像分类
    Tensor在PyTorch中,最核心的数据结构就是Tensor了,可以认为Tensor与Numpy中的ndarrays非常类似,但是Tensor可以使用GPU加速而ndarrays不可以。在pytorch进行GPU运算iftorch.cuda.is_available():x=x.cuda()y=y.cuda()print(x+y)numpy与pytorch互相转换importtorch......
  • ASTGNN (Learning Dynamics and Heterogeneity of Spatial-Temporal Graph Data forTr
    引言    时空神经网络(STGNNs)被广泛应用于交通预测问题中,在STGNNs中每个节点代表一个交通监测站,边表示道路网络。        在动态预测中,物理量x(t)随时间的变化模型是一个黑盒模型,我们要做的事情就是对黑盒模型进行建模。线性自回归方法直接将动态变化规律看......
  • 使用pytorch实现数字识别器
    前言:本篇文章是关于数字识别器的识别和卷积神经网络的应用。若对卷积神经网络不熟悉,可参考文章:卷积神经网络关于深度学习的一些代码及实战,可参考深度学习基础(github)下面我们尝试用PyTorch搭建一个卷积神经网络,并用它来解决手写数字识别的问题。1、数据准备#torchvisio......
  • pytorch OSError: [WinError 1114] 动态链接库(DLL)初始化例程失败”原因分析
    动态链接库失败“OSError:[WinError1114]动态链接库(DLL)初始化例程失败。Errorloading"cublas64_12.dll"oroneofitsdependencies”原因分析出错情况:在importtorch中直接被抛出异常环境探讨【问题复现】:因为使用了新的torch-gpu环境【name称为torch】,固怀疑......
  • pytorch 模型加载和保存
    模型加载torch.load(f,map_location=None,pickle_module=<module'pickle'from'/opt/conda/lib/python3.6/pickle.py'>,**pickle_load_args) map_location适用于修改模型能在gpu上运行还是cpu上运行。一般情况下,加载模型,主要用于预测新来的一组样本。预测的主要流程包......
  • Deepface - 仅以超过阈值的置信度显示面部的情绪
    我有这段代码可以检测面部表情,但它会在没有面部表情的地方找到面部表情。所以我想知道是否可以放心地做到这一点。我尝试在谷歌上搜索如何做到这一点,但没有找到任何有用的东西。importcv2fromdeepfaceimportDeepFace#Loadfacecascadeclassifierface_cascade=......
  • BMTrain类Megatron+DeepSpeed原理学习
    这一章节虽然是BMTrain,不是目前常用的Megatron+DeepSpeed,但是对于了解原理,也是很有帮助。BMTrain数据并行一般数据并行上图,把数据切为3份,每张显卡处理一部分数据,每张显卡利用得到的数据进行前向传播和反向传播,得到各自的梯度,为了让模型学到这份数据的所有知识,就需要......
  • pytorch学习笔记5 tensor 广播broadcasting
    不同shape直接加减,系统会自动做broadcasting操作先右对齐(小维度对齐)比如:Featuremaps:[4,32,14,14]Bias:[32,1,1]=>][1,32,1,1]=>[4,32,14,14]做到与Featuremaps的shape相同,才能进行相加广播扩展的时候只是做这样的操作,并不实质拷贝数据,以节省内存空间可广播的条件......