gpt4 book ai didi

python - RuntimeError : Given groups=1, 大小 [64, 3, 3, 3] 的权重,预期输入 [4, 5000, 5000, 3] 有 3 个 channel ,但有 5000 个 channel

转载 作者:行者123 更新时间:2023-12-05 00:47:32 24 4
gpt4 key购买 nike

所以,我有一个 U-Net 模型,我将 5000x5000x3 的图像输入到模型中,我得到了上面的错误。

这是我的模型。

import torch
import torch.nn as nn


def double_conv(in_channels, out_channels):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, 3, padding=1),
nn.ReLU(inplace=True)
)


class UNeT(nn.Module):
def __init__(self, n_class):
super().__init__()
self.dconv_down1 = double_conv(3, 64)
self.dconv_down2 = double_conv(64, 128)
self.dconv_down3 = double_conv(128, 256)
self.dconv_down4 = double_conv(256, 512)
self.maxpool = nn.MaxPool2d(2)
self.upsample = nn.Upsample(scale_factor=2, mode='bilinear',
align_corners=True)
self.dconv_up3 = double_conv(256 + 512, 256)
self.dconv_up2 = double_conv(128 + 256, 128)
self.dconv_up1 = double_conv(128 + 64, 64)
self.conv_last = nn.Conv2d(64, n_class, 1)

def forward(self, x):
conv1 = self.dconv_down1(x)
x = self.maxpool(conv1)
conv2 = self.dconv_down2(x)
x = self.maxpool(conv2)
conv3 = self.dconv_down3(x)
x = self.maxpool(conv3)
x = self.dconv_down4(x)
x = self.upsample(x)
x = torch.cat([x, conv3], dim=1)
x = self.dconv_up3(x)
x = self.upsample(x)
x = torch.cat([x, conv2], dim=1)
x = self.dconv_up2(x)
x = self.upsample(x)
x = torch.cat([x, conv1], dim=1)
x = self.dconv_up1(x)
out = self.conv_last(x)
return out


我尝试做 model(inputs.unsqueeze_(0)) 但我得到了一个不同的错误。

最佳答案

pytorch 中的维度顺序与您期望的不同。您的输入张量具有 4x5000x5000x3shape,您将其解释为一批大小 4,图像为 5000x5000 像素,每个像素有3个 channel 。也就是说,您的尺寸是 batch-height-width-channel

但是,pytorch 期望张量维度的顺序不同:batch-channel-height-width。也就是说,channel 维度应该在宽度和高度空间维度之前。

您需要 permute输入张量的尺寸以解决您的问题:

model(inputs.permute(0, 3, 1, 2))

有关详细信息,请参阅 nn.Conv2d 的文档。 .

关于python - RuntimeError : Given groups=1, 大小 [64, 3, 3, 3] 的权重,预期输入 [4, 5000, 5000, 3] 有 3 个 channel ,但有 5000 个 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56789038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com