测试脚本运行报错,请问可能是什么问题?

来源:6-15 PyTorch搭建cifar10推理测试脚本搭建

weixin_慕用9360114

2024-11-21

C:\Users\eyanniw\OneDrive - Ericsson\Pytorch\code\06\test.py:12: FutureWarning: You are using torch.load with weights_only=False (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for weights_only will be flipped to True. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via torch.serialization.add_safe_globals. We recommend you start setting weights_only=True for any use case where you don’t have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
net.load_state_dict(torch.load(“models\baseResnet\2.pth”))
Traceback (most recent call last):
File “C:\Users\eyanniw\OneDrive - Ericsson\Pytorch\code\06\test.py”, line 31, in
outputs = net.forward(inputs)
File “C:\Users\eyanniw\OneDrive - Ericsson\Pytorch\code\06\base_resnet.py”, line 72, in forward
out = self.conv1(x)
File “C:\Users\eyanniw\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\module.py”, line 1553, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File “C:\Users\eyanniw\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\module.py”, line 1562, in _call_impl
return forward_call(*args, **kwargs)
File “C:\Users\eyanniw\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\container.py”, line 219, in forward
input = module(input)
File “C:\Users\eyanniw\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\module.py”, line 1553, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File “C:\Users\eyanniw\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\module.py”, line 1562, in _call_impl
return forward_call(*args, **kwargs)
File “C:\Users\eyanniw\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\batchnorm.py”, line 143, in forward
self._check_input_dim(input)
File “C:\Users\eyanniw\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\nn\modules\batchnorm.py”, line 426, in _check_input_dim
raise ValueError(f"expected 4D input (got {input.dim()}D input)")
ValueError: expected 4D input (got 3D input)

写回答

2回答

会写代码的好厨师

2024-12-25

或者数据是不是没有从byte类型转成numpy 类型。

0
0

会写代码的好厨师

2024-12-25

输入数据的shape应该是4维,但是你这里给了3维,可以排查下是不是少了batchsize 这个维度

0
0

PyTorch入门到进阶 实战计算机视觉与自然语言处理项目

理论基础+技术讲解+实战开发,快速掌握PyTorch框架

1190 学习 · 298 问题

查看课程