在该ssd.pytorch中,进行测试时会出现问题:
RuntimeError: Legacy autograd function with non-static forward method is deprecated. Please use new-style autograd function with static forward method.
原因:
由于你当前的pytorch版本过高,而原代码的版本较低。如果pytorch版本高于1.3会出现该问题。当前版本要求forward过程是静态的,所以需要将原代码进行修改。
解决方法:
查找和试验过很多方法后,发现这个同志的方法可行!
代码改进方法
主要修改两个地方:
1、原文件中的detection.py内容换成该改进代码中的detection.py。主要是将初始化方法def __ init __()去除,然后再在def forward()方法前面加上@staticmethod
2、因为上述Detect类发生了变化,所以在使用该类的时候要需要改变。原文件中的ssd.py中的初始化方法def __ init __()中的:
if phase == 'test':
self.softmax = nn.Softmax(dim=-1)
self.detect = Detect(num_classes, 0, 200, 0.01, 0.45)
改成
if phase == 'test':
self.softmax = nn.Softmax()
self.detect = Detect()
另外该文件下的def forward()方法中的:
if self.phase == "test":
output = self.detect(
loc.view(loc.size(0), -1, 4), # loc preds
self.softmax(conf.view(conf.size(0), -1,
self.num_classes)), # conf preds
self.priors.type(type(x.data)) # default boxes
)
改为:
if self.phase == "test":
output = self.detect.apply(21, 0, 200, 0.01, 0.45,
loc.view(loc.size(0), -1, 4), # loc preds
self.softmax(conf.view(-1,
21)), # conf preds
self.priors.type(type(x.data)) # default boxes
)