1

我试图在 Nvidia GPU 上训练一些神经网络,但桌面环境(KDE)似乎占用了 GPU:

$ nvidia-smi 
Sat Apr 22 09:04:16 2017       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.39                 Driver Version: 375.39                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 960M    Off  | 0000:01:00.0     Off |                  N/A |
| N/A   52C    P0    N/A /  N/A |   1295MiB /  2002MiB |      4%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0      1139    G   /usr/lib/xorg/Xorg                             681MiB |
|    0      1591    G   kwin_x11                                        50MiB |
|    0      1594    G   /usr/bin/krunner                                13MiB |
|    0      1596    G   /usr/bin/plasmashell                           126MiB |
|    0      2267    G   ...el-token=FF7F1AB0E04D51461A7E5E08B2463625   136MiB |
+-----------------------------------------------------------------------------+

这是我正在运行的python代码:

import torch
import torchvision
import torchvision.transforms as transforms


transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                         shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

import matplotlib.pyplot as plt
import numpy as np

def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))

# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))


from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


net = Net()
net.cuda()


import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs
        inputs, labels = data

        # wrap them in Variable
        inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.data[0]
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0

print('Finished Training')

错误:

Traceback (most recent call last):
  File "<input>", line 64, in <module>
  File "/home/kaiyin/virtualenvs/pytorch/lib/python3.5/site-packages/torch/nn/modules/module.py", line 147, in cuda
    return self._apply(lambda t: t.cuda(device_id))
  File "/home/kaiyin/virtualenvs/pytorch/lib/python3.5/site-packages/torch/nn/modules/module.py", line 118, in _apply
    module._apply(fn)
  File "/home/kaiyin/virtualenvs/pytorch/lib/python3.5/site-packages/torch/nn/modules/module.py", line 124, in _apply
    param.data = fn(param.data)
  File "/home/kaiyin/virtualenvs/pytorch/lib/python3.5/site-packages/torch/nn/modules/module.py", line 147, in <lambda>
    return self._apply(lambda t: t.cuda(device_id))
  File "/home/kaiyin/virtualenvs/pytorch/lib/python3.5/site-packages/torch/_utils.py", line 65, in _cuda
    return new_type(self.size()).copy_(self, async)
RuntimeError: cuda runtime error (46) : all CUDA-capable devices are busy or unavailable at /b/wheel/pytorch-src/torch/lib/THC/generic/THCStorage.cu:66
  car   cat  bird   dog

如何禁止那些与 kde 相关的进程使用 GPU,而让它们使用 Intel 显卡?

4

1 回答 1

0

您似乎没有足够的 GPU 内存来进行训练。有一些解决方案:

  1. 减少批次大小:一次只将一个批次加载到 GPU 中。小批量将占用更少的 GPU 内存。(尝试将批量大小减少到 1 以查看它是否有效?)。看,你有超过 500 MiB 的 GPU 内存,你的批量大小为 4。如果你不能只用 1 个批量运行模型,那么很有可能尝试释放 681MiB 的 /usr/lib/xorg/Xorg不会帮你的。

  2. 在 GPU 上运行非常简单的示例代码(不应该是计算机视觉问题,因此不会占用太多 GPU 内存)。此步骤确认您正确安装了 CUDA 和 Pytorch,并且您的 GPU 应该可以工作。

  3. 关闭 GUI 并以仅终端模式运行(因为您不需要 GUI,只需一个终端即可运行 python 代码),它节省了 600 MB 的 GPU 内存。尝试将 GUI 移动到其他 GPU 中要容易得多。尝试搜索关键字:“如何在 ubuntu 中打开 GUI”

于 2017-04-26T16:49:59.393 回答