RuntimeError: Given groups=1, weight of size [32, 1, 3, 3, 3], expected input[1, 4, 193, 229, 193]
有 1 个通道,但改为 4 个通道。
致力于 3D 卷积神经网络对帕金森病的分类。
我认为这部分代码有一些错误:
# Load the dataset using DataLoader
batch_size = 4
# Load the dataset with batch loading (adjust path as needed)
dataset = CustomDataset(root_dir=r'D:\PD\PD25_16bit', transform=ToTensor())
train_size = int(0.8 * len(dataset))
test_size = len(dataset) - train_size
train_dataset, test_dataset = torch.utils.data.random_split(dataset, [train_size, test_size])
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# Define the 3D CNN model
class CNN3D(nn.Module):
def __init__(self, num_channels=1):
super(CNN3D, self).__init__()
self.conv1 = nn.Conv3d(num_channels, 32, kernel_size=3, stride=1, padding=1)
self.pool = nn.MaxPool3d(kernel_size=2, stride=2)
self.conv2 = nn.Conv3d(32, 64, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(64 * 48 * 57 * 48 // 4, 128) # Adjust the linear layer input size
self.fc2 = nn.Linear(128, 3)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, (64 * 48 * 57 * 48 // 4)) # Adjust the view to handle batch size of 4
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
每个3D文件(NIFTI)的尺寸为193,229,193。当我更改批量大小时,模型将其视为通道数。
Conv3d
函数需要形状为(batch_size, channels, D, H, W)
的 5 维输入。您的输入具有单个通道,因此您需要添加单位维度。您的输入应该具有形状 (batch_size, 1, 193, 229, 193)