调试SSD-pytorch代码问题汇总

 

代码链接:https://github.com/amdegroot/ssd.pytorch

1.执行demo-ssd.py,改动detection.py中49行:

if scores.numel() == 0:#scores.dim()

2. multibox_loss.py 中,97行

“loss_c[pos] = 0” 调试过程中发现 loss_c的shape与pos的shape 不同,会出现不匹配错误,因此将此句改为以下:
loss_c[pos.view(-1,1)] = 0

将pos通过view(-1,1) 改为与loss_c相匹配的shape。

3.multibox_loss.py中 N=num_pos.data.sum()的dtype为torch.int64,而进行除法操作的 loss_l 与loss_c的dtype为torch.float32,执行时会出现 ‘torch.cuda.LongTensor but found type torch.cuda.FloatTensor for argument’类似错误,此时需要查看参数类型,将N的类型改为torch.float32即可。 

 
  1. N = num_pos.data.sum()

  2. N=N.float()

4.train.py代码中,在迭代过程中,每次执行batch张图片,通过images, targets = next(batch_iterator)读取图片时,如果next()中没有数据后会触发Stoplteration异常,使用下面语句替换 images, targets = next(batch_iterator)将解决这种异常问题。

 
  1. while True:

  2. try:

  3. # 获得下一个值:

  4. images, targets = next(batch_iterator)

  5.  
  6. except StopIteration:

  7. # 遇到StopIteration就退出循环

  8. break

5.RuntimeError: CUDNN_STATUS_INTERNAL_ERROR的解决办法:

需要清除CUDA缓存,使用sudo进行,但它属于Linux命令,windows中需要进行以下操作:

(1).在任意目录中新建文本文件,命名为sudo.js

(2).用记事本打开刚才新建的文件,粘贴下面代码

var command = WScript.Arguments.Item(0);
var argument = "";
for (var i = 0; i < WScript.Arguments.Count(); ++i){
    argument += WScript.Arguments.Item(i) + " ";
}

try{
    var shellapp = new ActiveXObject("Shell.Application");
    shellapp.ShellExecute(command, argument, null, "runas", 1);
}

catch(e){
    WScript.Echo("Something wrong: " + e.description + " By http://www.alexblair.org");
}

使用cmd打开sudo.js文件即可进行sudo操作。

(3).执行sudo rm -f ~/.nv/    (一定最后边不要漏掉“/”,否则会提示“.nv”是目录)

注意:当执行(3)中语句时,我的系统提示‘Windows 找不到文件 rm’,这时可以尝试在代码最处添加

torch.cuda.set_device(0)

6.test.py 与 eval.py中 nosetest运行时出现 ‘ _jb_nosetest_runner.py: error: unrecognized arguments: ’ 错误:

args = parser.parse_args() 替换为:

 

args, unknown = parser.parse_known_args()

由于pytorch版本从0.1-0.3升级到0.4(1.0)时变化较大,而且许多算法使用的是0.4以下版本,现在为了方便,都使用0.4版本的pytorch,但使用该源码训练模型时,出现一些因版本等问题出现的bug。
下面就以ssd.pytorch为例,修改其中出现的问题。同时记录0.2迁移到0.4时应该注意的内容。
1 路径问题

例如:
在这里插入图片描述根据自己的路径更改。
2 修改训练时,代码中的bug

补:RuntimeError: randperm is only implemented for CPU

在这里插入图片描述解决方法:
在这里插入图片描述在这里插入图片描述

1) RuntimeError: The shape of the mask [2, 8732] at index 0 does not match the shape of the indexed tensor [17464, 1] at index 0
在这里插入图片描述解决方法:
在这里插入图片描述2) RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.cuda.LongTensor for argument #3 'other’
在这里插入图片描述解决方法:
在这里插入图片描述
3)还用一些UserWarning,例如下面所示,根据提示的信息,修改。
UserWarning: volatile was removed and now has no effect. Use 'with torch.no_grad():" instead.
在这里插入图片描述解决方法:
在这里插入图片描述在这里插入图片描述

4)数据集的问题
由于自己的数据集中,对应的图片中,没有任何的物体,即对应的xml文件中没有groundtruth对应的xmin, ymin, xmax, ymax信息。
**解决方法:**整理数据集,去除没有上述信息的图片数据及对应的其他文件。

另外,训练时:File “/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py”, line 276, in next raise StopIteration
StopIteration
在这里插入图片描述
解决方法:
因为这个错误是,训练迭代数据一遍,而没有从新开始迭代。
在这里插入图片描述
在train.py中,原来的代码是165行,修改为两行“#”中间的内容。

5)其他问题
3 修改测试时,代码中的bug

1) 路径或者测试数据文件统一

2) eval.py和 layers/functions/detection.py文件中,dim()
在这里插入图片描述
在这里插入图片描述
解决方法:
这两个文件中的407行和65行,原来的判断条件为 if *.dim() == 0:,因为是从0.2版本迁移到0.4版本,例如上面scores.dim(),当scores为空时,即tensor([]),scores.dim() == 1。故修改这里的判断条件,if torch.numel(scores) == 0: (获取scores即tensor中的元素个数)。
 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值