我有一個模型,我正在嘗試使用它。我正在解決這些錯誤,但現在我認為它已經歸結為我層中的值。我收到此錯誤:RuntimeError: Given groups=1, weight of size 24 1 3 3, expected input[512, 50, 50, 3] to have 1 channels, but got 50 channels instead我的參數是:LR = 5e-2N_EPOCHS = 30BATCH_SIZE = 512DROPOUT = 0.5我的圖像信息是:depth=24channels=3original height = 1600original width = 1200resized to 50x50這是我的數據的大?。篢rain shape (743, 50, 50, 3) (743, 7)Test shape (186, 50, 50, 3) (186, 7)Train pixels 0 255 188.12228712427097 61.49539262385051Test pixels 0 255 189.35559211469533 60.688278787628775我在這里試圖了解每一層的期望值,但是當我在這里輸入它所說的內容時,https://towardsdatascience.com/pytorch-layer-dimensions-what-sizes-should-they-be-and-為什么-4265a41e01fd,它給了我關于錯誤通道和內核的錯誤。我發現 torch_summary 讓我對輸出有更多的了解,但它只會提出更多的問題。這是我的 torch_summary 代碼:from torchvision import modelsfrom torchsummary import summaryimport torchimport torch.nn as nnclass CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Conv2d(1,24, kernel_size=5) # output (n_examples, 16, 26, 26) self.convnorm1 = nn.BatchNorm2d(24) # channels from prev layer self.pool1 = nn.MaxPool2d((2, 2)) # output (n_examples, 16, 13, 13) self.conv2 = nn.Conv2d(24,48,kernel_size=5) # output (n_examples, 32, 11, 11) self.convnorm2 = nn.BatchNorm2d(48) # 2*channels? self.pool2 = nn.AvgPool2d((2, 2)) # output (n_examples, 32, 5, 5) self.linear1 = nn.Linear(400,120) # input will be flattened to (n_examples, 32 * 5 * 5) self.linear1_bn = nn.BatchNorm1d(400) # features? self.drop = nn.Dropout(DROPOUT) self.linear2 = nn.Linear(400, 10) self.act = torch.relu
1 回答

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
看來您輸入x
張量軸的順序錯誤。
正如您在輸入中看到的,必須是doc
Conv2d
(N, C, H, W)
N
是批量大小,C
表示通道數,H
是以像素為單位的輸入平面的高度,以像素為單位W
的寬度。
因此,為了正確使用torch.permute
前傳中的交換軸。
...
def forward(self, x):
x = x.permute(0, 3, 1, 2)
...
...
return self.linear2(x)
...
示例permute:
t = torch.rand(512, 50, 50, 3)
t.size()
torch.Size([512, 50, 50, 3])
t = t.permute(0, 3, 1, 2)
t.size()
torch.Size([512, 3, 50, 50])
添加回答
舉報
0/150
提交
取消