Keras深度学习不';即使安装了tensorflow GPU,也不能在GPU上运行

Keras深度学习不';即使安装了tensorflow GPU,也不能在GPU上运行,tensorflow,keras,deep-learning,spyder,Tensorflow,Keras,Deep Learning,Spyder,我想用keras tensorflow GPU后端训练CNN图像分类模型。我已经检查过了,tensorflow能够检测到GPU。但keras并没有使用GPU来训练模型。任务管理器还指示CPU利用率为100%,GPU利用率为0%,而培训模式为 我已经安装了 visual studio社区2017 Python 3.7.3 CUDA 10.0 Cudnn 7.6 水蟒 我使用的是windows 10 64位、GPU 1050 GTX 4gb、CPU intel i5第七代 为了安装tensorflo

我想用keras tensorflow GPU后端训练CNN图像分类模型。我已经检查过了,tensorflow能够检测到GPU。但keras并没有使用GPU来训练模型。任务管理器还指示CPU利用率为100%,GPU利用率为0%,而培训模式为

我已经安装了

  • visual studio社区2017
  • Python 3.7.3
  • CUDA 10.0
  • Cudnn 7.6
  • 水蟒
  • 我使用的是windows 10 64位、GPU 1050 GTX 4gb、CPU intel i5第七代

    为了安装tensorflow GPU,我使用了以下命令

    conda create --name tf_gpu tensorflow-gpu
    
    我还尝试了以下3种方法来强制GPU进行训练

    with tensorflow.device('/gpu:0'):
        #code
    
    from keras import backend
    assert len(backend.tensorflow_backend._get_available_gpus()) > 0
         #code
    
    from keras import backend as K
    K.tensorflow_backend._get_available_gpus()
         #code
    
    我在虚拟环境中安装的软件包

    # packages in environment at C:\Users\Sreenivasa Reddy\Anaconda3\envs\tf_gpu:
    #
    # Name                    Version                   Build  Channel
    _tflow_select             2.1.0                       gpu
    absl-py                   0.7.1                    py37_0
    alabaster                 0.7.12                   py37_0
    asn1crypto                0.24.0                   py37_0
    astor                     0.7.1                    py37_0
    astroid                   2.2.5                    py37_0
    attrs                     19.1.0                   py37_1
    babel                     2.7.0                      py_0
    backcall                  0.1.0                    py37_0
    blas                      1.0                         mkl
    bleach                    3.1.0                    py37_0
    ca-certificates           2019.5.15                     0
    certifi                   2019.6.16                py37_0
    cffi                      1.12.3           py37h7a1dbc1_0
    chardet                   3.0.4                    py37_1
    cloudpickle               1.2.1                      py_0
    colorama                  0.4.1                    py37_0
    cryptography              2.7              py37h7a1dbc1_0
    cudatoolkit               10.0.130                      0
    cudnn                     7.6.0                cuda10.0_0
    decorator                 4.4.0                    py37_1
    defusedxml                0.6.0                      py_0
    docutils                  0.14                     py37_0
    entrypoints               0.3                      py37_0
    freetype                  2.9.1                ha9979f8_1
    gast                      0.2.2                    py37_0
    grpcio                    1.16.1           py37h351948d_1
    h5py                      2.9.0            py37h5e291fa_0
    hdf5                      1.10.4               h7ebc959_0
    icc_rt                    2019.0.0             h0cc432a_1
    icu                       58.2                 ha66f8fd_1
    idna                      2.8                      py37_0
    imagesize                 1.1.0                    py37_0
    intel-openmp              2019.4                      245
    ipykernel                 5.1.1            py37h39e3cac_0
    ipython                   7.6.1            py37h39e3cac_0
    ipython_genutils          0.2.0                    py37_0
    isort                     4.3.21                   py37_0
    jedi                      0.13.3                   py37_0
    jinja2                    2.10.1                   py37_0
    jpeg                      9b                   hb83a4c4_2
    jsonschema                3.0.1                    py37_0
    jupyter_client            5.3.1                      py_0
    jupyter_core              4.5.0                      py_0
    Keras                     2.2.4                     <pip>
    keras-applications        1.0.8                      py_0
    keras-preprocessing       1.1.0                      py_1
    keyring                   18.0.0                   py37_0
    lazy-object-proxy         1.4.1            py37he774522_0
    libpng                    1.6.37               h2a8f88b_0
    libprotobuf               3.8.0                h7bd577a_0
    libsodium                 1.0.16               h9d3ae62_0
    libtiff                   4.0.10               hb898794_2
    markdown                  3.1.1                    py37_0
    markupsafe                1.1.1            py37he774522_0
    mccabe                    0.6.1                    py37_1
    mistune                   0.8.4            py37he774522_0
    mkl                       2019.4                      245
    mkl_fft                   1.0.12           py37h14836fe_0
    mkl_random                1.0.2            py37h343c172_0
    mock                      3.0.5                    py37_0
    nbconvert                 5.5.0                      py_0
    nbformat                  4.4.0                    py37_0
    numpy                     1.16.4           py37h19fb1c0_0
    numpy-base                1.16.4           py37hc3f5095_0
    numpydoc                  0.9.1                      py_0
    olefile                   0.46                     py37_0
    openssl                   1.1.1c               he774522_1
    packaging                 19.0                     py37_0
    pandoc                    2.2.3.2                       0
    pandocfilters             1.4.2                    py37_1
    parso                     0.5.0                      py_0
    pickleshare               0.7.5                    py37_0
    pillow                    6.1.0            py37hdc69c19_0
    pip                       19.1.1                   py37_0
    prompt_toolkit            2.0.9                    py37_0
    protobuf                  3.8.0            py37h33f27b4_0
    psutil                    5.6.3            py37he774522_0
    pycodestyle               2.5.0                    py37_0
    pycparser                 2.19                     py37_0
    pyflakes                  2.1.1                    py37_0
    pygments                  2.4.2                      py_0
    pylint                    2.3.1                    py37_0
    pyopenssl                 19.0.0                   py37_0
    pyparsing                 2.4.0                      py_0
    pyqt                      5.9.2            py37h6538335_2
    pyreadline                2.1                      py37_1
    pyrsistent                0.14.11          py37he774522_0
    pysocks                   1.7.0                    py37_0
    python                    3.7.3                h8c8aaf0_1
    python-dateutil           2.8.0                    py37_0
    pytz                      2019.1                     py_0
    pywin32                   223              py37hfa6e2cd_1
    PyYAML                    5.1.1                     <pip>
    pyzmq                     18.0.0           py37ha925a31_0
    qt                        5.9.7            vc14h73c81de_0
    qtawesome                 0.5.7                    py37_1
    qtconsole                 4.5.1                      py_0
    qtpy                      1.8.0                      py_0
    requests                  2.22.0                   py37_0
    rope                      0.14.0                     py_0
    scipy                     1.2.1            py37h29ff71c_0
    setuptools                41.0.1                   py37_0
    sip                       4.19.8           py37h6538335_0
    six                       1.12.0                   py37_0
    snowballstemmer           1.9.0                      py_0
    sphinx                    2.1.2                      py_0
    sphinxcontrib-applehelp   1.0.1                      py_0
    sphinxcontrib-devhelp     1.0.1                      py_0
    sphinxcontrib-htmlhelp    1.0.2                      py_0
    sphinxcontrib-jsmath      1.0.1                      py_0
    sphinxcontrib-qthelp      1.0.2                      py_0
    sphinxcontrib-serializinghtml 1.1.3                      py_0
    spyder                    3.3.6                    py37_0
    spyder-kernels            0.5.1                    py37_0
    sqlite                    3.29.0               he774522_0
    tensorboard               1.13.1           py37h33f27b4_0
    tensorflow                1.13.1          gpu_py37h83e5d6a_0
    tensorflow-base           1.13.1          gpu_py37h871c8ca_0
    tensorflow-estimator      1.13.0                     py_0
    tensorflow-gpu            1.13.1               h0d30ee6_0
    termcolor                 1.1.0                    py37_1
    testpath                  0.4.2                    py37_0
    tk                        8.6.8                hfa6e2cd_0
    tornado                   6.0.3            py37he774522_0
    traitlets                 4.3.2                    py37_0
    urllib3                   1.24.2                   py37_0
    vc                        14.1                 h0510ff6_4
    vs2015_runtime            14.15.26706          h3a45250_4
    wcwidth                   0.1.7                    py37_0
    webencodings              0.5.1                    py37_1
    werkzeug                  0.15.4                     py_0
    wheel                     0.33.4                   py37_0
    win_inet_pton             1.1.0                    py37_0
    wincertstore              0.2                      py37_0
    wrapt                     1.11.2           py37he774522_0
    xz                        5.2.4                h2fa13f4_4
    zeromq                    4.3.1                h33f27b4_3
    zlib                      1.2.11               h62dcd97_3
    zstd                      1.3.7                h508b16e_0
    
    我的keras代码

    import tensorflow as tf
    from keras.models import Sequential
    from keras.layers import Convolution2D
    from keras.layers import MaxPool2D
    from keras.layers import Flatten
    from keras.layers import Dense
    from keras import backend as K
    K.tensorflow_backend._get_available_gpus()
    classifier=Sequential()
        classifier.add(Convolution2D(32,3,3,input_shape=(32,32,3),activation='relu'))
        classifier.add(MaxPool2D(pool_size=(2,2)))
        classifier.add(Convolution2D(32,3,3,activation='relu'))
        classifier.add(MaxPool2D(pool_size=(2,2)))
        classifier.add(Convolution2D(64,3,3,activation='relu'))
        classifier.add(MaxPool2D(pool_size=(2,2)))
        classifier.add(Flatten())
        classifier.add(Dense(output_dim=128, activation='relu'))
        classifier.add(Dense(output_dim=1, activation='sigmoid'))
        classifier.compile(optimizer='adam',loss='binary_crossentropy', metrics=['accuracy'])
    
        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(
                'C:/Users/Sreenivasa Reddy/Desktop/Deep_Learning_A_Z/Volume_1_Supervised_Deep_Learning/Part2_Convolutional_Neural_Networks/Convolutional_Neural_Networks/dataset/training_set',
                target_size=(32, 32),
                batch_size=32,
                class_mode='binary')
    
        test_set = test_datagen.flow_from_directory(
                'C:/Users/Sreenivasa Reddy/Desktop/Deep_Learning_A_Z/Volume_1_Supervised_Deep_Learning/Part2_Convolutional_Neural_Networks/Convolutional_Neural_Networks/dataset/test_set',
                target_size=(32, 32),
                batch_size=32,
                class_mode='binary')
    
        classifier.fit_generator(
                training_set,
                steps_per_epoch=8000,
                epochs=25,
                validation_data=test_set,
                validation_steps=2000)
    
    iPython控制台中的输出

    import tensorflow as tf
    from keras.models import Sequential
    from keras.layers import Convolution2D
    from keras.layers import MaxPool2D
    from keras.layers import Flatten
    from keras.layers import Dense
    
    from keras import backend as K
    K.tensorflow_backend._get_available_gpus()
    Out[15]: ['/job:localhost/replica:0/task:0/device:GPU:0']
    
    classifier=Sequential()
    classifier.add(Convolution2D(32,3,3,input_shape=(32,32,3),activation='relu'))
    classifier.add(MaxPool2D(pool_size=(2,2)))
    classifier.add(Convolution2D(32,3,3,activation='relu'))
    classifier.add(MaxPool2D(pool_size=(2,2)))
    classifier.add(Convolution2D(64,3,3,activation='relu'))
    classifier.add(MaxPool2D(pool_size=(2,2)))
    classifier.add(Flatten())
    classifier.add(Dense(output_dim=128, activation='relu'))
    classifier.add(Dense(output_dim=1, activation='sigmoid'))
    classifier.compile(optimizer='adam',loss='binary_crossentropy', metrics=['accuracy'])
    
    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(
            'C:/Users/Sreenivasa Reddy/Desktop/Deep_Learning_A_Z/Volume_1_Supervised_Deep_Learning/Part2_Convolutional_Neural_Networks/Convolutional_Neural_Networks/dataset/training_set',
            target_size=(32, 32),
            batch_size=32,
            class_mode='binary')
    
    test_set = test_datagen.flow_from_directory(
            'C:/Users/Sreenivasa Reddy/Desktop/Deep_Learning_A_Z/Volume_1_Supervised_Deep_Learning/Part2_Convolutional_Neural_Networks/Convolutional_Neural_Networks/dataset/test_set',
            target_size=(32, 32),
            batch_size=32,
            class_mode='binary')
    
    classifier.fit_generator(
            training_set,
            steps_per_epoch=8000,
            epochs=25,
            validation_data=test_set,
            validation_steps=2000)
    __main__:2: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), input_shape=(32, 32, 3..., activation="relu")`
    __main__:4: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), activation="relu")`
    __main__:6: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (3, 3), activation="relu")`
    __main__:9: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(activation="relu", units=128)`
    __main__:10: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(activation="sigmoid", units=1)`
    Found 8000 images belonging to 2 classes.
    Found 2000 images belonging to 2 classes.
    Epoch 1/25
     782/8000 [=>............................] - ETA: 17:38 - loss: 0.6328 - acc: 0.6310
    
    注意:为了从iPython控制台复制代码片段,我在运行了一段时间后停止了内核

    import tensorflow as tf
    from keras.models import Sequential
    from keras.layers import Convolution2D
    from keras.layers import MaxPool2D
    from keras.layers import Flatten
    from keras.layers import Dense
    
    from keras import backend as K
    K.tensorflow_backend._get_available_gpus()
    Out[15]: ['/job:localhost/replica:0/task:0/device:GPU:0']
    
    classifier=Sequential()
    classifier.add(Convolution2D(32,3,3,input_shape=(32,32,3),activation='relu'))
    classifier.add(MaxPool2D(pool_size=(2,2)))
    classifier.add(Convolution2D(32,3,3,activation='relu'))
    classifier.add(MaxPool2D(pool_size=(2,2)))
    classifier.add(Convolution2D(64,3,3,activation='relu'))
    classifier.add(MaxPool2D(pool_size=(2,2)))
    classifier.add(Flatten())
    classifier.add(Dense(output_dim=128, activation='relu'))
    classifier.add(Dense(output_dim=1, activation='sigmoid'))
    classifier.compile(optimizer='adam',loss='binary_crossentropy', metrics=['accuracy'])
    
    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(
            'C:/Users/Sreenivasa Reddy/Desktop/Deep_Learning_A_Z/Volume_1_Supervised_Deep_Learning/Part2_Convolutional_Neural_Networks/Convolutional_Neural_Networks/dataset/training_set',
            target_size=(32, 32),
            batch_size=32,
            class_mode='binary')
    
    test_set = test_datagen.flow_from_directory(
            'C:/Users/Sreenivasa Reddy/Desktop/Deep_Learning_A_Z/Volume_1_Supervised_Deep_Learning/Part2_Convolutional_Neural_Networks/Convolutional_Neural_Networks/dataset/test_set',
            target_size=(32, 32),
            batch_size=32,
            class_mode='binary')
    
    classifier.fit_generator(
            training_set,
            steps_per_epoch=8000,
            epochs=25,
            validation_data=test_set,
            validation_steps=2000)
    __main__:2: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), input_shape=(32, 32, 3..., activation="relu")`
    __main__:4: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), activation="relu")`
    __main__:6: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (3, 3), activation="relu")`
    __main__:9: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(activation="relu", units=128)`
    __main__:10: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(activation="sigmoid", units=1)`
    Found 8000 images belonging to 2 classes.
    Found 2000 images belonging to 2 classes.
    Epoch 1/25
     782/8000 [=>............................] - ETA: 17:38 - loss: 0.6328 - acc: 0.6310
    
    编辑:我训练了RNN和ANN模型,当我在训练时检查任务管理器时,CUDA利用率约为35%,但对于CNN模型,CUDA利用率为2%。CUDA 35%的未使用率不是很低吗?为什么CNN不利用35%


    EDIT2:奇怪的是,当我增加批量时,模型列车速度非常慢,当我减少批量时(即当我将其设置为1)模型列车速度更快,对此有什么解释吗?

    我在这里问我的问题,因为我还没有获得发表评论的权利:/

    您提到您尝试了不同的方法:

    使用tensorflow.device('/gpu:0'):#代码

    在您发布的代码中,我看不到它们,也看不到使用gpu的不同方法,但我认为您使用gpu来获得上面的输出

    如果您使用这些方法会发生什么?它仍然只使用gpu还是会出错

    你可以尝试一下这样的方法并发布结果吗:

    # Creates a graph.
    
    with tf.device('/gpu:0'):
    
        a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    
        b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    
        c = tf.matmul(a, b)
    
    # Creates a session with log_device_placement set to True.
    
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
    
    # Runs the op.
    
    print(sess.run(c))
    
    如本例所述:

    编辑 至于你的利用率问题

    这可能有多种原因,例如:

  • 您可以尝试增加批处理大小,这相当小。在许多示例中,这会导致GPU空闲,因为它正在等待从CPU获取数据(这也可以解释您100%的CPU使用率)。此外,您的训练集样本量非常小(仅8000个)。如果您只是想增加GPU的使用率,您可以将批处理大小设置为512甚至1024,并人为地增加样本大小(例如,将样本复制多次)。但请注意,这不会为您提供一个好的模型,这只是为了增加GPU的使用率

  • 你有一个非常小的网络,所以你不能从GPU加速中获得太多的利润。你可以尝试增加网络的大小,以测试GPU的使用是否增加

  • 这一点也在本文中提到


    我希望这能有所帮助。

    我上面提到的三种方法,我将它们分别放在keras代码上方进行了尝试,但没有一种方法强制使用gpu资源。我运行了您的代码,得到的输出是
    SyntaxError:print调用中缺少括号。您是指print(sess.run)?
    当我在括号中包含sess.run时,我得到了
    对不起我的错误,我的意思是“print(sess.run(c))将纠正上面的错误。”。如果一切正常,tf gpu正常工作,它将给出[[22.28.][49.64.]]如果出现错误,可能是您的Tensorflow安装有问题。然后,您应该尝试卸载tensorflow和tensorflow gpu,然后只重新安装此处提到的tensorflow gpu()另一个可以帮助您的方法是按照此处的建议将phyton降级为3.6()是的!它打印出[[22.28.][49.64.]]。那么这意味着tf gpu正在工作?请检查我原始问题的编辑部分。也许你可以从中推断出一些事情,没错,你的tf gpu似乎工作得很好。我编辑了我的答案以适合你的利用率问题。