“PyTorch Conv2d 错误:预期有 3 个通道,但 [1, 128, 128, 3] 为 128。需要帮助!”

问题描述 投票:0回答:1

我在使用 PyTorch 卷积神经网络时遇到问题。我收到的错误消息是:

给定组=1,权重大小为[8,3,5,5],预期输入[1,128,128,3]有3个通道,但得到了128个通道

背景:

模型架构:

import torch.nn as nn
import torch.nn.functional as F

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.cnn_model = nn.Sequential(
        nn.Conv2d(in_channels = 3, out_channels = 8, kernel_size = 5),
        nn.Tanh(),
        nn.AvgPool2d(kernel_size = 3, stride = 5),
        nn.Conv2d(in_channels = 8, out_channels = 16, kernel_size = 5),
        nn.Tanh(),
        nn.AvgPool2d(kernel_size = 2, stride = 5))
        
        self.fc_model = nn.Sequential(
        nn.Linear(in_features = 256, out_features = 120),
        nn.Tanh(),
        nn.Linear(in_features = 120, out_features = 84),
        nn.Tanh(),
        nn.Linear(in_features = 84, out_features = 1))
        
    def forward(self, x):
        x = self.cnn_model(x)
        x = x.view(x.size(0), -1)
        x = self.fc_model(x)
        x = F.sigmoid(x)
        
        return x

数据加载:

class MRI(Dataset):
    def __init__(self):
        # Load images and labels
        tumor = []
        path_tumor = '/kaggle/input/brain-tumor/Dataset/Yes_Data/*.jpg'
        for f in glob.iglob(path_tumor):
            img = cv2.imread(f)
            img = cv2.resize(img, (128, 128), interpolation=cv2.INTER_AREA)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            tumor.append(img)

        healthy = []
        path_healthy = '/kaggle/input/brain-tumor/Dataset/No_data/*.jpg'
        for f in glob.iglob(path_healthy):
            img = cv2.imread(f)
            img = cv2.resize(img, (128, 128), interpolation=cv2.INTER_AREA)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            healthy.append(img)

        # Convert lists to numpy arrays
        healthy = np.array(healthy)
        tumor = np.array(tumor)
        
        # Create labels
        tumor_label = np.ones(tumor.shape[0], dtype=np.float32)
        healthy_label = np.zeros(healthy.shape[0], dtype=np.float32)

        # Concatenate images and labels
        images = np.concatenate((tumor, healthy), axis=0)
        labels = np.concatenate((tumor_label, healthy_label), axis=0)
        self.images = images
        self.labels = labels

    def __getitem__(self, index):
        sample = {'image': self.images[index], 'label': self.labels[index]}
        return sample

    def __len__(self):
        return self.images.shape[0]

    def normalize(self):
        self.images = (self.images / 255.0).astype(np.float32)

错误上下文:

model.eval()
outputs = []
y_true = []

with torch.no_grad():
    for sample in dataloader:
        for i in range(sample['image'].size(0)):
            image = sample['image'][i].squeeze().to(device).float()
            label = sample['label'][i].to(device)

            y_hat = model(image)
            outputs.append(y_hat.cpu().detach().numpy())
            y_true.append(label.cpu().detach().numpy())

我尝试了所有的调试方法,打印并检查形状

machine-learning pytorch conv-neural-network
1个回答
0
投票

torch.nn.Conv2d 转换层的输入应该是 (N, C, H, W),所以你需要将 (N H W C) 置换(swapaxis) 到 (N C H W)

image = sample['image'][i].squeeze().to(device).float()
image = image.permute(0, 3, 1, 2) # (N H W C) -> (N C H W)
© www.soinside.com 2019 - 2024. All rights reserved.