【问题标题】:Training a CNN model with DataLoader on a GPU in PyTorch在 PyTorch 的 GPU 上使用 DataLoader 训练 CNN 模型
【发布时间】:2020-11-29 23:54:02
【问题描述】:

我正在尝试将胸部 X 光片分为两类:“正常”和“肺炎”。我的训练和测试集是带有num_workers=0, pin_memory=True 的DataLoader 对象。 Cuda 在我的设备上可用(GTX 1060 6GB)。创建 CNN 后,我调用 model = CNN().cuda()。当我尝试训练模型时,我得到RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 'weight'。为了在我的 GPU 上训练模型,我必须进行哪些更改?

代码:

root = 'chest_xray/'

train_data = datasets.ImageFolder(os.path.join(root, 'train'), transform=train_transform)
test_data = datasets.ImageFolder(os.path.join(root, 'test'), transform=test_transform)

train_loader = DataLoader(train_data,batch_size=10,shuffle=True,num_workers=0,pin_memory=True)
test_loader = DataLoader(test_data,batch_size=10,shuffle=False,num_workers=0,pin_memory=True)

class_names = train_data.classes

class CNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 12, 5, 1)
        self.conv2 = nn.Conv2d(12, 24, 5, 1)
        self.conv3 = nn.Conv2d(24, 30, 5, 1) 
        self.conv4 = nn.Conv2d(30, 36, 5, 1)
        self.fc1 = nn.Linear(58*58*36, 256)
        self.fc2 = nn.Linear(256, 144)
        self.fc3 = nn.Linear(144, 84)
        self.fc4 = nn.Linear(84, 16)
        self.fc5 = nn.Linear(16, 2)
        
    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv3(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv4(x))
        x = F.max_pool2d(x, 2, 2)
        
        x = x.view(-1, 58*58*36)
        
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = F.relu(self.fc4(x))
        x = self.fc5(x)
        
        return F.log_softmax(x, dim=1)

model = CNN().cuda()

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

epochs = 2

train_losses = []
train_correct = []
for i in range(epochs):
    trn_correct = 0
    tst_correct = 0
    
    for b, (X_train, y_train) in enumerate(train_loader):
        y_pred = model(X_train)
        loss = criterion(y_pred, y_train)
        
        predicted = torch.max(y_pred.data, 1)[1]
        batch_correct = (predicted == y_train).sum()
        trn_correct += batch_correct
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        if b % 200 == 0:
            print(f'epoch: {i+1} batch: {b} progress: {10*b/len(train_data)} loss: {loss.item()} accuracy: {10*trn_correct/b}%')
            
    train_losses.append(loss)
    train_correct.append(trn_correct)

【问题讨论】:

    标签: python machine-learning deep-learning pytorch


    【解决方案1】:

    这一行之后:

    for b, (X_train, y_train) in enumerate(train_loader):
    

    添加以下内容:

    X_train, y_train = X_train.cuda(), y_train.cuda()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 2019-09-01
      • 2020-11-27
      • 2021-07-24
      • 2018-10-29
      相关资源
      最近更新 更多