SSD的pytorch实现训练中遇到的问题总结
程序员文章站
2024-03-16 23:14:40
...
运行train.py,出现了一些错误和警告。简单总结一下:
一、pytorch版本问题
1、UserWarning: volatile was removed and now has no effect. Use with torch.no_grad():
instead.molded_images = Variable(molded_images, volatile=True)
解决方法:
images = Variable(images.cuda()) #删除volatile=True
2、UserWarning: nn.init.xavier_uniform is now deprecated in favor of nn.init.xavier_uniform_.init.xavier_uniform(param)
解决方法:
init.xavier_uniform_(param)
3、UserWarning: size_average and reduce is now deprecated,please use reduction=‘sum’ instead
解决方法:在multibox_loss.py中修改
loss_c = F.cross_entropy(conf_p, targets_weighted, reduction='sum')
4、UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number
解决方法:将loss.data[0]改为loss.item()
二、代码上的修改
1、multibox_loss.py中更换 97、98行顺序
loss_c = loss_c.view(num, -1)
loss_c[pos] = 0 # filter out pos boxes for now
2、N = num_pos.data.sum().double()
3、训练中loss出现nan情况
这个问题是由学习率过高引起的,只需降低学习率即可。