ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 9, 1, 1])
原代碼:
import torch
import torch.nn as nn
model = nn.Sequential(
nn.Conv2d(3, 9, 1, 1, 0, bias=False),
nn.BatchNorm2d(9),
nn.ReLU(inplace=True),
nn.AdaptiveAvgPool2d((1, 1)),
nn.BatchNorm2d(9)
)
x = torch.rand([1, 3, 224, 224])
for i in range(len(model)):
x = model[i](x)
print(i,x.shape)
報錯:
Traceback (most recent call last):
File "/home/jim/.local/lib/python3.5/site-packages/torch/nn/functional.py", line 1693, in batch_norm
0 torch.Size([1, 9, 224, 224])
1 torch.Size([1, 9, 224, 224])
2 torch.Size([1, 9, 224, 224])
3 torch.Size([1, 9, 1, 1])
raise ValueError('Expected more than 1 value per channel when training, got input size {}'.format(size))
ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 9, 1, 1])
發現問題出在最後一層 nn.BatchNorm2d(9),輸入數據的 batch 必須大於 1.
解決辦法:
將x = torch.rand([1, 3, 224, 224])
更改爲x = torch.rand([2, 3, 224, 224])
得到正常輸出
0 torch.Size([2, 9, 224, 224])
1 torch.Size([2, 9, 224, 224])
2 torch.Size([2, 9, 224, 224])
3 torch.Size([2, 9, 1, 1])
4 torch.Size([2, 9, 1, 1])
另外,如果不用自適應池化,且輸入的batch仍設爲1:
import torch
import torch.nn as nn
model = nn.Sequential(
nn.Conv2d(3, 9, 1, 1, 0, bias=False),
nn.BatchNorm2d(9),
nn.ReLU(inplace=True),
nn.BatchNorm2d(9)
)
x = torch.rand([1, 3, 224, 224])
for i in range(len(model)):
x = model[i](x)
print(i,x.shape)
也不會報錯
0 torch.Size([1, 9, 224, 224])
1 torch.Size([1, 9, 224, 224])
2 torch.Size([1, 9, 224, 224])
3 torch.Size([1, 9, 224, 224])
自適應平均池化後將224*224
變爲1*1
,說明batch爲1時,如果輸入爲1*1的特徵圖,則,不能正常通過 nn.BatchNorm2d。