基于CNN识别算法的问题

关于#神经网络#的问题:RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x4096 and 3136x512);
在导入图片训练集的时候出现了问题,大致知道是如下代码设置有问题:

class CNN(nn.Module):
    def __init__(self, num_classes=100):#100种汉字
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)#,输入图像通道数,卷积产生通道数,卷积核尺寸,卷积步长
        self.bn1 = nn.BatchNorm2d(32)#归一化处理
        self.relu1 = nn.ReLU()#激活函数
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)#池化层参数
        self.conv2 = nn.Conv2d(32, 64, kernel_size=1, stride=1, padding=1)#mat1
        self.bn2 = nn.BatchNorm2d(64)
        self.relu2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.fc1 = nn.Linear(64 * 7 * 7, 512)#mat2
        self.relu3 = nn.ReLU()
        self.fc2 = nn.Linear(512, num_classes)
        
    def forward(self, x):
        
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu1(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu2(x)
        x = self.pool2(x)
        x = x.view(x.size(0), -1)
        x = self.fc1(x)
        x = self.relu3(x)
        x = self.fc2(x)
        return x

# 定义训练和测试数据的转换
transform = transforms.Compose([
    transforms.Grayscale(num_output_channels=1),
    transforms.Resize((28, 28)),
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,)),
])
'''
已经尝试过将dataloader中需要设置参数drop_last=True。即丢弃最后一个不足batchSize的样本,但是还是不行;
希望各位大佬帮我看看;

这一句写错了,self.conv2 = nn.Conv2d(32, 64, kernel_size=1, stride=1, padding=1)
应该是,self.conv2 = nn.Conv2d(32, 64, kernel_size=1, stride=1, padding=0)

  • 这篇博客: PyTorch深度学习快速入门教程【小土堆】 学习笔记中的 RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x1024 and 10240x64) 原因 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 在这里插入图片描述
    原因: 网络参数设置不合适!!

    from torch import nn
    from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
    import torch
    
    class Lyy(nn.Module):
        def __init__(self):
            super(Lyy, self).__init__()
            self.conv1 = Conv2d(3, 32, 5, padding=2)
            self.maxpool1 = MaxPool2d(2)
            self.conv2 = Conv2d(32, 32, 5, padding=2)
            self.maxpool2 = MaxPool2d(2)
            self.conv3 = Conv2d(32, 64, 5, padding=2)
            self.maxpool3 = MaxPool2d(2)
            self.flatten = Flatten()
            self.linear1 = Linear(1024, 64)
            self.linear2 = Linear(64, 10)
    
        def forward(self, x):
            x = self.conv1(x)
            x = self.maxpool1(x)
            x = self.conv2(x)
            x = self.maxpool2(x)
            x = self.conv3(x)
            x = self.maxpool3(x)
            x = self.flatten(x)
            x = self.linear1(x)
            x = self.linear2(x)
            return x
    
    lyy = Lyy()
    input = torch.ones((64,3,32,32))
    print(input.shape)
    output = lyy(input)
    print(output.shape)
    

    上述代码的,等价替换版本

    from torch import nn
    from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
    import torch
    
    class Lyy(nn.Module):
        def __init__(self):
            super(Lyy, self).__init__()
            self.model1 = Sequential(
                Conv2d(3, 32, 5, padding=2),
                MaxPool2d(2),
                Conv2d(32, 32, 5, padding=2),
                MaxPool2d(2),
                Conv2d(32, 64, 5, padding=2),
                MaxPool2d(2),
                Flatten(),
                Linear(1024, 64),
                Linear(64, 10)
            )
    
        def forward(self, x):
            x = self.model1(x)
            return x
    
    lyy = Lyy()
    input = torch.ones((64,3,32,32))
    print(input.shape)
    output = lyy(input)
    print(output.shape)