
torch
乱搭巴士
debug
展开
-
A leaf Variable that requires grad is being used in an in-place operation
错误原因:计算图中的叶子节点不能直接进行内置运算,防止影响计算图的反向传播运算。如果非要改变叶子节点data的值,可以直接对data属性调用内置运算,这样不会记录在计算图当中。解决方案:1.把梯度变化设置为不变。(亲测)bifpn.fc.weight.requires_grad, bifpn.fc.bias.requires_grad = False, Falsemodel.fc.weight.requires_grad, model.fc.bias.requires_grad = False,原创 2022-04-04 09:16:48 · 4041 阅读 · 0 评论 -
pytorch计算模型参数量和计算量报错:
原报错代码:from torchvision.models import resnet50import torchimport torchvision.models as models# import torchfrom ptflops import get_model_complexity_info # model = models.resnet50() #调用官方的模型,checkpoints = '自己模型的path'model = torch.load(checkpoints)原创 2021-05-10 10:16:19 · 740 阅读 · 0 评论 -
pytorch计算模型参数量报错:size mismatch for module.conv1.weight: copying a param with shape torch.Size([16, 3
错误:RuntimeError: Error(s) in loading state_dict for DataParallel: size mismatch for module.conv1.weight: copying a param with shape torch.Size([16, 3, 3, 3]) from checkpoint, the shape in current model is torch.Size([128, 128, 3, 3]). size mismatch for原创 2021-05-10 10:12:38 · 22198 阅读 · 9 评论 -
pytorch计算模型参数量时报错:RuntimeError: mat1 dim 1 must match mat2 dim 0
错误:File “C:\Users\83543\PycharmProjects\graduate\CIFAR-10\VGG-Small\vgg.py”, line 220, in forwardx = self.fc(x)File “D:\120\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 727, in _call_implresult = self.forward(*input, **kwargs)Fi原创 2021-05-10 10:11:08 · 3490 阅读 · 0 评论 -
使用pytorch计算模型的参数量和计算量
计算自带模型的例子from torchvision.models import resnet50import torchimport torchvision.models as modelsimport argparseimport vggimport torch.nn as nnimport torch.nn.parallelfrom ptflops import get_model_complexity_infomodel = models.resnet50() #调用官方的模型原创 2021-05-10 10:09:12 · 1929 阅读 · 0 评论 -
UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include d
报错:UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.probs = F.softmax(output.cpu()).detach().numpy()[0](我的F是import torch.nn.functional as F)这个要区别清楚torch.nn.Softmax()和torch.nn.functi原创 2021-04-22 19:46:22 · 1793 阅读 · 0 评论 -
FutureWarning: Passing (type, 1) or ‘1type‘ as a synonym of type is deprecate
原因:numpy版本太高。解决方案:我的是python3.7,原来装的是1.18.0的numpy。先卸载:pip uninstall numpy再安装了1.16.0:pip install numpy==1.16.0之后就解决了原创 2021-04-22 19:29:39 · 167 阅读 · 0 评论 -
pytorch 加载训练好的模型后测试得到的结果不一样
加载训练好的pth模型之后,去测试我下载的一张图片,发现有时候是标签0,有时候是3,有时候是5(正确的是5)。解决方案:加入语句:model.eval()转换为测试模式,此时不启用 BatchNormalization 和 Dropout,将BatchNormalization和Dropout置为False。否则批量归一化和dropout容易产生较大的波动。...原创 2021-04-22 18:45:49 · 3101 阅读 · 0 评论 -
Error(s) in loading state_dict for DataParallel: Missing key(s) in state_dict: “module.conv0.weight
在加载已经训练好的模型时,报错。报错描述:Error(s) in loading state_dict for DataParallel:Missing key(s) in state_dict: “module.conv0.weight”, “module.bn0.weight”, “module.bn0.bias”, “module.bn0.running_mean”, “module.bn0.running_var”, “module.conv1.weight”, “module.bn1.wei原创 2021-04-22 14:05:47 · 1697 阅读 · 1 评论 -
使用torch.cuda.is_available() 时,NameError: name ‘torch‘ is not defined
因为在python3命令行里没有引入torch。应该先在linux系统里,使用如下命令:import torchtorch.cuda.is_available()原创 2021-03-22 11:18:31 · 3486 阅读 · 1 评论 -
pytorch保存和读取模型,定义save_checkpoint函数
保存模型:# 定义函数,保存最新和最佳模型def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'): """ Save the training model """ torch.save(state, filename) if is_best: shutil.copyfile(filename, 'model_best.pth.tar')# 调用时:save_check原创 2021-03-16 17:25:24 · 8361 阅读 · 0 评论 -
pytorch运行文件报错:PermissionError: [WinError 5] 拒绝访问。
一共报了两个错误:(1) File “D:\120\Python\Python37\lib\multiprocessing\spawn.py”, line 115, in _mainself = reduction.pickle.load(from_parent)EOFError: Ran out of input(2)PermissionError: [WinError 5] 拒绝访问。首先把(2)的解决了,找到python目录下的python.exe,右键选择属性,安全,把权限打满,再确定。原创 2021-03-09 23:34:44 · 8865 阅读 · 1 评论 -
pycharm/pytorch OSError: 页面文件太小,无法完成操作。
问题:在torch框架下运行代码时,报错:OSError: 页面文件太小,无法完成操作。解决方案:1.改变虚拟内存大小(我使用的方法)打开电脑->属性->高级系统设置->点击更改,我改成了这样:(根据可用空间进行更改即可,我没有改c盘,c盘比较小。)2.把num_works设置为0(未经验证)...原创 2021-03-09 18:07:20 · 4634 阅读 · 7 评论 -
invalid index of a 0-dim tensor. Use `tensor.item()`
使用1.7.1版本的torch框架运行代码时出现问题报错如下:invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item<T>() in C++ to convet源代码是在1.4.0版本的torch下运行的。改动如下:原创 2021-03-09 17:13:21 · 1148 阅读 · 0 评论