TPN icon indicating copy to clipboard operation
TPN copied to clipboard

在PyTorch 1.7下,运行test_video.py报错:RuntimeError: Legacy autograd function with non-static forward method is deprecated.

Open HYJJJJJJJJ opened this issue 5 years ago • 4 comments

运行环境:

  • CPU: Intel i7-10700K
  • GPU: RTX3090
  • RAM: 64GB
  • 操作系统:Ubuntu 20.04.1 LTS
  • Python 3.6
  • Pytorch 1.7.0 with CUDA 11.0

背景&现象:

由于Pytorch 1.3以下版本,不支持CUDA11,无法兼容30系GPU,所以使用了Pytorch1.7,运行demo时,输出如下错误:

/home/lmz/anaconda3/envs/py36/bin/python /home/lmz/TPN/test_video.py config_files/sthv2/tsm_tpn.py ckpt/sthv2_tpn.pth Extracting frames using ffmpeg... The model and loaded state dict do not match exactly

missing keys in source state_dict: necks.aux_head.convs.conv.weight, necks.aux_head.convs.bn.weight, necks.aux_head.convs.bn.bias, necks.aux_head.convs.bn.running_mean, necks.aux_head.convs.bn.running_var, necks.aux_head.fc.weight, necks.aux_head.fc.bias

Traceback (most recent call last): File "/home/lmz/TPN/test_video.py", line 148, in results = inference_recognizer(model, seg_frames) File "/home/lmz/TPN/test_video.py", line 75, in inference_recognizer result = model(return_loss=False, rescale=True, **data) File "/home/lmz/anaconda3/envs/py36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/lmz/TPN/mmaction/models/recognizers/base.py", line 41, in forward return self.forward_test(num_modalities, img_meta, **kwargs) File "/home/lmz/TPN/mmaction/models/recognizers/TSN2D.py", line 154, in forward_test x = self.segmental_consensus(x) File "/home/lmz/anaconda3/envs/py36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/lmz/TPN/mmaction/models/tenons/segmental_consensuses/simple_consensus.py", line 50, in forward return _SimpleConsensus(self.consensus_type, self.dim)(input) File "/home/lmz/anaconda3/envs/py36/lib/python3.6/site-packages/torch/autograd/function.py", line 160, in call "Legacy autograd function with non-static forward method is deprecated. " 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)

Process finished with exit code 1

了解到在pytorch1.3及以后的版本需要规定forward方法为静态方法,给TPN/mmaction/models/tenons/segmental_consensuses/simple_consensus.py 中的forward方法加上静态修饰,报错:

/home/lmz/anaconda3/envs/py36/bin/python /home/lmz/TPN/test_video.py config_files/sthv2/tsm_tpn.py ckpt/sthv2_tpn.pth Extracting frames using ffmpeg... The model and loaded state dict do not match exactly

missing keys in source state_dict: necks.aux_head.convs.conv.weight, necks.aux_head.convs.bn.weight, necks.aux_head.convs.bn.bias, necks.aux_head.convs.bn.running_mean, necks.aux_head.convs.bn.running_var, necks.aux_head.fc.weight, necks.aux_head.fc.bias

Traceback (most recent call last): File "/home/lmz/TPN/test_video.py", line 148, in results = inference_recognizer(model, seg_frames) File "/home/lmz/TPN/test_video.py", line 75, in inference_recognizer result = model(return_loss=False, rescale=True, **data) File "/home/lmz/anaconda3/envs/py36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/lmz/TPN/mmaction/models/recognizers/base.py", line 41, in forward return self.forward_test(num_modalities, img_meta, **kwargs) File "/home/lmz/TPN/mmaction/models/recognizers/TSN2D.py", line 154, in forward_test x = self.segmental_consensus(x) File "/home/lmz/anaconda3/envs/py36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) TypeError: forward() missing 1 required positional argument: 'input'

Process finished with exit code 1

HYJJJJJJJJ avatar Dec 02 '20 07:12 HYJJJJJJJJ

可以试试x = self.segmental_consensus.forward(x), 或者是具体调用segmental_consensus里的某个子模型, 我遇到相似的问题是这么解决的

LiuWenJia-ops avatar Apr 12 '21 16:04 LiuWenJia-ops

I have the same issue,how did you solve it?

Note-Liu avatar Jul 04 '21 12:07 Note-Liu

I have the same issue,how did you solve it?

I solved this problem whit this link https://github.com/decisionforce/TPN/pull/30/commits/b02523d94d39f4439bc081d8097c054ca010d6ce

784682065 avatar Jul 07 '21 08:07 784682065