给定组= 1,权重为16 16 3 3,期望输入[16,64,222,222]具有16个通道,但是却有64个通道?

时间:2019-11-21 08:53:56

标签: python classification pytorch multiclass-classification

我尝试针对Pytorch中的图像分类问题运行以下程序。 我是PyTorch的新手,但我没有发现代码有什么问题。我尝试重塑图像,但没有帮助。 我正在使用Cuda运行此代码。我上一堂课大约有750课,有10-20张图片。我的数据集是基准数据集,每张图像的尺寸为60 * 160。

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.ConvLayer1 = nn.Sequential(
            nn.Conv2d(3, 64, 3), # inp (3, 512, 512) changes doing here original (3, 64, 3)
            nn.Conv2d(8, 16, 3), # original (8,16,3)
            nn.MaxPool2d(2),
            nn.ReLU() # op (16, 256, 256)
        )
        self.ConvLayer2 = nn.Sequential(
            nn.Conv2d(16, 32, 5), # inp (16, 256, 256)
            nn.Conv2d(32, 32, 3),
            nn.MaxPool2d(4),
            nn.ReLU() # op (32, 64, 64)
        )
        self.ConvLayer3 = nn.Sequential(
            nn.Conv2d(32, 64, 3), # inp (32, 64, 64) original (32,64,3)
            nn.Conv2d(64, 64, 5),
            nn.MaxPool2d(2),
            nn.ReLU() # op (64, 32, 32)
        )
        self.ConvLayer4 = nn.Sequential(
            nn.Conv2d(64, 128, 5), # inp (64, 32, 32)
            nn.Conv2d(128, 128, 3),
            nn.MaxPool2d(2),
            nn.ReLU() # op (128, 16, 16)
        )
        self.Lin1 = nn.Linear(15488, 15)
        self.Lin2 = nn.Linear(1500, 150)
        self.Lin3 = nn.Linear(150, 15)


    def forward(self, x):
        x = self.ConvLayer1(x)
        x = self.ConvLayer2(x)
        x = self.ConvLayer3(x)
        x = self.ConvLayer4(x)
        x = x.view(x.size(0), -1)
        x = self.Lin1(x)


        return F.log_softmax(x, dim = 1)
'''

'''
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.0001, momentum=0.5)
for epoch in tqdm(range(2)):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(dataloaders['train']):
        # get the inputs; data is a list of [inputs, labels]
        inputs, class_names = data
        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = model(inputs)
        loss = criterion(outputs, class_names)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item()
       # if i % 10 == 0:    # print every 10 mini-batches
        #    print('[%d, %5d] loss: %.3f' %
         #         (epoch + 1, i + 1, running_loss / 2000))
          #  running_loss = 0.0
    break
print('Finished Training')

出现此错误,我不知道在哪里进行更改。 给定组= 1,大小为16 16 3 3的权重,预期输入[16,64,222,222]具有16个通道,但是却有64个通道。

1 个答案:

答案 0 :(得分:0)

转换层中的输出通道数必须与下一个转换层中的输入通道数匹配。假设您有nn.Conv(3, 64, 3),则下一个转换层需要开始nn.Conv(64, ...。现在的问题是,您试图将64通道结果传递到您已定义为期望8通道输入的转换层中。