'RuntimeError: Legacy autograd function with non-static forward method is deprecated

I am building a Single Shot Multi-Box Detection project for object detection of a running dog in an mp4 video.

I am following a tutorial for this & have messaged the instructor.

This is the complete error when I run my code in Spyder:

RuntimeError: Legacy autograd function with non-static forward method is deprecated. 
Please use new-style autograd function with static forward method. 
(Example: https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function)

This is what my code looks like: enter image description here

I have also, checked the Pytorch documentation as with the previous questioner to this issue on Jul 8.

https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_custom_function.html

I am a beginner in Deep Learning & am certain I am making a rube error.

I would be extremely grateful for the help.

Cheers,

Ryan



Solution 1:[1]

the issue lies in detection.py file which is present in layers -> functions -> detection.py path

It has a class named 'Detect' which is inheriting torch.autograd.Function but it implements the forward method in an old deprecated way, so you need to restructure it i.e. you need to define the forward method with @staticmethod decorator and use .apply to call it from your SSD class.

Also, as you are going to use decorator, you need to ensure that the forward method doesn't use any Detect class constructor variables.

The below link explains the new way to use autograd.Function: https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_custom_function.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul-Rocks