tensorflow - 如何提高卷积神经网络中GPU的使用率?

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

我正在使用 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 环境中正在使用哪一个 spy 程序) 虽然我的代码运行良好,但我的 GPU 使用率仅为 %1。 我是否安装了错误的内容,例如 Tensorflow 和 CUDA 的错误组合? 事实上,我已经尝试了网上提到的大部分事情,但我没有取得任何进展。

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

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

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

代码:

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 的官方文档,以下代码片段应设置 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)

关于tensorflow - 如何提高卷积神经网络中GPU的使用率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59855790/

相关文章:

machine-learning - 如何从 DARPA pcap 文件导出 KDD99 特征?

tensorflow - 虽然pc识别gpu,但它使用的是tensorflow-gpu中的CPU

python - 在组合网络的子网上使用两种损失

machine-learning - 微调词嵌入是如何工作的?

python - 如何解决操作系统错误 : [Errno 22] Invalid argument:

Tensorflow Dataset API 如何排序 list_files?

tensorflow - 在 TF 操作中评估 TF 模型会引发错误

python - MiniBatchKMeans Python

python - 加载以前保存的没有自定义层的模型时缺少 get_config

tensorflow - 连续机器学习管道因 tensorflow 需求安装而中断