ssd.pytorch源码迁移实现(pytorch版本从0.2迁移到0.4(1.0))
pytorch版本ssd源码地址:
https://github.com/amdegroot/ssd.pytorch
以前都是使用caffe框架,近期开始pytorch框架的使用。
由于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中的元素个数)。