测试时RuntimeError: Legacy autograd function with non-static forward method is deprecated

ssd.torch代码

在该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
            )
  • 22
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 41
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值