pytorch BN层报错:ValueError: expected 4D input (got 2D input)

报错:

Traceback (most recent call last):
  File "train_noPfc.py", line 201, in <module>
    main(args_)
  File "train_noPfc.py", line 160, in main
    f_masked, focc_masked, output, output_occ, f_diff, out = backbone(img1, img2)
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/nn/parallel/distributed.py", line 619, in forward
    output = self.module(*inputs[0], **kwargs[0])
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/user1/pjs/frvt/mask/siamese/debug/0510_noPfc_2branch_iter_pdsn/2branch_alter_1update/recognition/arcface_torch/backbones/iresnet.py", line 172, in forward
    fc1 = self.fc_(fc1)
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/nn/modules/container.py", line 117, in forward
    input = module(input)
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/nn/modules/batchnorm.py", line 98, in forward
    self._check_input_dim(input)
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/nn/modules/batchnorm.py", line 282, in _check_input_dim
    .format(input.dim()))
ValueError: expected 4D input (got 2D input)
Traceback (most recent call last):
  File "/home/user1/miniconda3/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/user1/miniconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/distributed/launch.py", line 260, in <module>
    main()
  File "/home/user1/miniconda3/lib/python3.7/site-packages/torch/distributed/launch.py", line 256, in main
    cmd=cmd)

分析:你可能是使用了nn.BatchNorm2d网络层,它要求输入是4维的。

解决:
可以改为nn.BatchNorm1d,接收二维输入。

https://discuss.pytorch.org/t/batchnorm2d-valueerror-expected-4d-input-got-2d-input/85110

上一篇:二 JPA 排序 条件查询 分页查询


下一篇:JS-07-JS控制语句与数组