【问题标题】:How to improve GPU usage in convolutional neural network?如何提高卷积神经网络中 GPU 的使用率?
【发布时间】:2020-01-22 08:51:10
【问题描述】:

我正在使用 keras 库来实现 CNN 和 Anaconda 3 (spyder 4) 执行。

我使用了命令conda install -c anaconda keras-gpu 它安装了 cudatoolkit-10.0.130cudnn-7.6.5tensorflow- gpu-2.0.0 但我的代码不适用于 tensorflow-gpu-2.0.0 所以我已将其降级为 tensorflow-gpu-1.15.0 。 (我还在我的机器上安装了最新的 CUDA 工具包,但我不知道我的机器或 conda 环境中使用的是哪个 spyder) 虽然我的代码运行良好,但我的 GPU 使用率仅为 %1。 我是否安装了错误的东西,比如 Tensorflow 和 CUDA 的错误组合? 实际上我已经尝试了网上提到的大部分东西,但我没有得到任何结果。

我的系统信息: CPU : i7 第 9 代 显卡:RTX 2060 内存:16 GB 操作系统:Windows 10

是否需要任何安装或任何代码更改才能让我的 GPU 正常工作? (我已经执行了tf.config.list_physical_devices('GPU') 之类的命令之一来检查我的 GPU,它显示了积极的结果,因此 tensorflow 检测到了我的 GPU,但我不知道它为什么不使用它来执行)

附注: 我在网上阅读过,大多数人都在谈论由于 CPU 造成的瓶颈(即使我的 CPU 使用率很低,所以如果你告诉我一些可以改进的地方,我将不胜感激),他们要求做的解决方案是加载你的数据从而可以有效地利用 GPU。 我正在使用图像数据集,所以你能告诉我如何预加载数据集或实现并行性,以便可以将其提供给 GPU,而不是即时提供。 我正在使用下面提到的 keras,因此对于像我这样的新手来说很容易的代码 sn-p 将有助于获得 kickstart。

代码:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Convolution2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.preprocessing.image import ImageDataGenerator


classifier = Sequential()

classifier.add(Convolution2D(32, 3, 3, input_shape = (64, 64, 3), activation = 'relu'))

classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Flatten())

classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])


train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')

classifier.fit_generator(training_set,
                         steps_per_epoch = 8000,
                         epochs = 25,
                         validation_data = test_set,
                         validation_steps = 2000)

.

【问题讨论】:

    标签: tensorflow machine-learning keras conv-neural-network


    【解决方案1】:

    根据TensorFlow的官方文档,下面的sn-p应该设置gpu内存使用:

    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
      # Restrict TensorFlow to only allocate 1GB of memory on the first GPU
      try:
        tf.config.experimental.set_virtual_device_configuration(
            gpus[0],
            [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=4096)])# change here for different values
        logical_gpus = tf.config.experimental.list_logical_devices('GPU')
        print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
      except RuntimeError as e:
        # Virtual devices must be set before GPUs have been initialized
        print(e)
    

    【讨论】:

    • 谢谢你的回答,我试试这个。你能帮我做一件事,因为我是新手吗?我有 rtx 2060 ( 6 gb ) ,如果我想充分利用我的 gpu,我应该对上述代码进行哪些安全和必要的更改?
    • 您永远不应该设置为充分利用,您需要单独的 Video Ram 用于其他活动,例如浏览器等。在您的情况下,我会将内存限制设置为 5120(即 1024 x 5)。
    猜你喜欢
    • 1970-01-01
    • 2016-08-02
    • 2021-03-30
    • 2017-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多