【问题标题】:Tensorflow is not fully using my gpu ? It always used 2-3% of my gpuTensorflow 没有完全使用我的 gpu 吗?它总是使用我 gpu 的 2-3%
【发布时间】:2019-12-21 14:48:43
【问题描述】:

我安装了 python 3.6 以及 cuda 10.0cudnn 使用:

tensorflow - pip install tensorflow-gpu

但它只占用我的 gpu 的 2-3%(如任务管理器中所示)。

我的 2060Super 中每个 epoch 有 5 分钟;当我在终端上运行它时,它会选择 gpu,但是每个 epoch 花费的时间太多了,而且它没有利用整个 gpu。

#Building CNN
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
#initialize the CNN
classifier = Sequential()

#step 1 - Convolution
classifier.add(Conv2D(32,3,3,input_shape = (64,64,3), activation='relu'))
#step 2 Pooling
classifier.add(MaxPooling2D(pool_size=(2,2)))

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

#step3 Flattening
classifier.add(Flatten())

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

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

#Fitting CNN to images
from keras.preprocessing.image import ImageDataGenerator
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)

【问题讨论】:

  • 请格式化您的文本,并再次阅读您的问题
  • @zigrazor 我对正确的格式进行了尝试 - 也许您想检查一下,并可能改进/纠正。
  • 这似乎按预期工作 - 请参阅 here; TL;DR 你的模型太小了,“只有 3%”是好的,否则 GPU 会因较大的模型而崩溃。至于速度,除非您看到其他人在相同型号和 GPU 上做得更快,否则 8k 步 5 分钟似乎并不合理 - 不管this 可能会有所帮助
  • 实际上作为深度学习初学者,我看到 udemy 的深度学习课程的讲师虽然使用的是 mac,但每个 epoch 的时间只有 70 秒。所以我认为我的 TF 遇到了一些问题。@OverLordGoldDragon

标签: python tensorflow data-science


【解决方案1】:

要配置 TensorFlow GPU,您可以执行以下操作 (tensorflow r1.14 and less)

import os
import tensorflow as tf


def configure_tf_gpu():
    # Reduce logging output.
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    tf.logging.set_verbosity(tf.logging.INFO)
    config = tf.ConfigProto()
    # dynamically grow the memory used on the GPU
    config.gpu_options.allow_growth = True
    # Allowing TF to automatically choose an existing and
    # supported device to run the operations in case the specified one doesn't exist
    config.allow_soft_placement = True
    # to log device placement (on which device the operation ran)
    config.log_device_placement = False
    config.gpu_options.per_process_gpu_memory_fraction = 0.99
    # (nothing gets printed in Jupyter, only if you run it standalone)
    sess = tf.Session(config=config)
    # set this TensorFlow session as the default session for Keras
    # set_session(sess)

【讨论】:

  • @EI Sheikh 感谢您的回答。但是当我用我的代码粘贴函数时,它显示 AttributeError: module 'tensorflow' has no attribute 'logging'
  • @IndranilDasgupta 如果您使用的是tensorflow v2.0 这是预期的
  • 是的,我正在使用 Tensorflow v2.0.0 @EISheikh。如何处理这段代码?你能解释一下吗
  • 我一定要好好评论,你真的不明白的地方请指出来
  • @EI Sheikh 感谢您的回复。实际上我不明白如何处理这些代码块。你是说在我运行代码或其他东西时粘贴我的 cnn 分类器。
猜你喜欢
  • 2023-03-19
  • 2020-04-20
  • 2017-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 2018-08-16
相关资源
最近更新 更多