Pytorch错误:“RuntimeError:mat1 and mat2 shapes cannot be multiplied(32x150528 and 784x512)”

yhuiod9q  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(223)

我正在尝试用Pytorch训练一个模型。数据集属于我,它有两个文件夹:train_data和test_data。每个文件夹都有两个文件夹:已检测和未检测(这些是我的类)。在每个文件夹中,有许多不同大小的png图像,我总是得到一个关于图像大小的例外。顺便说一下,在此之前,我已经用MNIST数据集运行了代码,它工作了。这是我的代码:

import torch
import torch.nn as nn
from torch.utils.data import Dataset
from torchvision import datasets
import torch.optim as optim
import os
from PIL import Image

import torchvision.transforms as transforms
from torch.utils.data import DataLoader

is_gpu = torch.cuda.is_available()

class CustomDataset(Dataset):
    def __init__(self, root_dir, transform=None):
        self.root_dir = root_dir
        self.transform = transform
        self.classes = os.listdir(root_dir)
        self.data = []
        self.targets = []

        for class_idx, class_name in enumerate(self.classes):
            class_dir = os.path.join(root_dir, class_name)
            for filename in os.listdir(class_dir):
                img_path = os.path.join(class_dir, filename)
                self.data.append(img_path)
                self.targets.append(class_idx)

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        img_path = self.data[idx]
        target = self.targets[idx]

        img = Image.open(img_path).convert('RGB')  # Resmi aç ve RGB formatına dönüştür

        if self.transform is not None:
            img = self.transform(img)

        return img, target

transform = transforms.Compose([
    transforms.Resize((256, 256)),  # Resmi istenen boyuta dönüştürmek için kullanılabilir
    transforms.RandomCrop((224, 224)),
    transforms.ToTensor()
])

train_dataset = CustomDataset(root_dir=r'C:\Users\enis_\Desktop\yololu\train_data', transform=transform)
test_dataset = CustomDataset(root_dir=r'C:\Users\enis_\Desktop\yololu\test_data', transform=transform)

train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)

model = nn.Sequential(nn.Flatten(),
                     nn.Linear(28*28,512),
                     nn.ReLU(),
                     nn.Linear(512,256),
                     nn.ReLU(),
                     nn.Linear(256,10))

if is_gpu:
    model = model.cuda()

print(model)

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(),lr=0.7)

epochs = 5

for i in range(epochs):
    train_loss = 0.0
    for data,label in train_loader:
        if is_gpu:
            data, label = data.cuda(), label.cuda()
        optimizer.zero_grad()
        
        output = model(data)
        loss = criterion(output,label)
        loss.backward()
        
        optimizer.step()
        
        train_loss += loss.item() * data.size(0)
    print(f'Epoch: {i+1} / {epochs} \t\t\t Training Loss:{train_loss/len(train_loader)}')

字符串
这是输出:
顺序((0):变平(start_dim=1,end_dim=-1)(1):线性(in_features=784,out_features=512,bias=True)(2):ReLU()(3):线性(in_features=512,out_features=256,bias=True)(4):ReLU()(5):线性(in_features=256,out_features=10,bias=True))(最近的调用):文件“c:\Users\enis_\Desktop\yololu\tempCodeRunnerFile.py”,第80行,在output = model(data)中^文件“C:Copyright © 2018 - 2019 www.pythonsoftwarefoundation.com版权所有(*args,*kwargs)^文件“C:\Users\enis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\nn\modules\container.py”,第217行,in forward input = module(input)^^^^^^^^^^^文件“C:\Users\enis_\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\nn\modules\module.py”,第1501行,in *call_impl return forward_call(*args,*kwargs)^文件“C:\Users\enis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\nn\modules\linear.py”,line 114,in forward return F.linear(input,self.weight,self.bias)^^
正如你在我的代码中看到的,我正试图调整图片的大小,

transform = transforms.Compose([
    transforms.Resize((256, 256)),
    transforms.RandomCrop((224, 224)),
    transforms.ToTensor()
])


但它不起作用

yks3o0rb

yks3o0rb1#

在模型中:

model = nn.Sequential(nn.Flatten(),
                     nn.Linear(28*28,512),
                     nn.ReLU(),
                     nn.Linear(512,256),
                     nn.ReLU(),
                     nn.Linear(256,10))

字符串
你有nn.Flatten(),它将图像转换为2242243=150528维向量。
3来自于有3个颜色通道。
如果你想对这些图像使用完全连接的神经网络,你需要将第一层的大小设置为2242243:

model = nn.Sequential(nn.Flatten(),
                     nn.Linear(224*224*3,512),
                     nn.ReLU(),
                     nn.Linear(512,256),
                     nn.ReLU(),
                     nn.Linear(256,10))

相关问题