无论如何,在Mac和AMD GPU中使用Keras?

无论如何,在Mac和AMD GPU中使用Keras?,keras,tensorflow2.0,pyopencl,amd-gpu,plaidml,Keras,Tensorflow2.0,Pyopencl,Amd Gpu,Plaidml,我有一个带有AMD处理器的MacBookPro,我想在这个GPU上运行Keras(Tensorflow后端)。我开始知道Keras只适用于NVIDIA GPU。解决方法是什么(如果可能)?您可以使用库来克服此问题。我已经测试过了,对我来说效果很好 注意:我有python版本3.7,我将使用pip3进行软件包安装。 步骤: 使用以下命令安装OpenCL包 pip3安装pyopencl 使用以下命令安装库 pip3安装pip安装plaidml keras 运行PlaidML的安装程序。安装时,您

我有一个带有AMD处理器的MacBookPro,我想在这个GPU上运行Keras(Tensorflow后端)。我开始知道Keras只适用于NVIDIA GPU。解决方法是什么(如果可能)?

您可以使用库来克服此问题。我已经测试过了,对我来说效果很好

注意:我有python版本3.7,我将使用pip3进行软件包安装。 步骤:
  • 使用以下命令安装OpenCL包

    pip3安装pyopencl

  • 使用以下命令安装库

    pip3安装pip安装plaidml keras

  • 运行PlaidML的安装程序。安装时,您可能会收到选择GPU的提示。如果安装正确,您将在最后收到一条成功消息

    plaidml设置

  • 安装plaidbench在GPU上测试plaidml

    pip3安装plaidbench

  • 测试一下。如果一切顺利,直到这里,你会得到基准分数

    plaidbench-keras-mobilenet

  • 现在我们必须设置一个环境路径。把这个放在代码的顶部

  • 在实际代码中进行测试。在代码中使用
    keras
    而不是
    tensorflow.keras
    ,然后运行以下命令。(keras安装在步骤2中,该步骤在GPU中运行)
  • 当您运行此程序时,您将获得 确认您正在GPU中运行它。
    参考资料:

    事实上,Keras仅支持NVIDIA GPU是不正确的。您可以选择使用哪个后端Keras,如果这个后端支持AMD GPU,那么Keras也应该在这种情况下工作


    然而,在MacOS上工作的唯一后端是PlaidML。AMD处理器也有ROCm,但截至2020年10月,MacOS不支持它(请参阅)。

    @bikram,谢谢您的回答!经过自己的实验,我得出了相同的结论。因此,不能将TF2.0与macOS+AMD GPU一起使用。我不确定这是坏的还是有局限性的。基本上,TF2.0正在向Keras的易用性和简单性迈进API@sashaegorovPlaidML可以使用Metal在MacBook上运行(至少在我的例子中,它的性能比OpenCL好),所以第1步是可选的。步骤6也可以在不设置RUNFILES_DIR和PLAIDML_NATIVE_PATH的路径的情况下工作,唯一需要的环境变量是KERAS_backend运行此代码需要多长时间?我看到我的代码是在gpu上运行的,但每个历元花费了大约79秒。对于Tensorflow.keras,SplaidML似乎不是1-1。听起来对吗?我有使用Huber损失函数作为示例的现有代码/模型。但是PlaidML中不存在这个类tf.keras.loss.Huber。因此,代码需要移植。这也是你的理解吗,在这里使用GPU意味着它的功能不是1-1?
    import os
    os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
    
    os.environ["RUNFILES_DIR"] = "/Library/Frameworks/Python.framework/Versions/3.7/share/plaidml"
    # plaidml might exist in different location. Look for "/usr/local/share/plaidml" and replace in above path
    
    os.environ["PLAIDML_NATIVE_PATH"] = "/Library/Frameworks/Python.framework/Versions/3.7/lib/libplaidml.dylib"
    # libplaidml.dylib might exist in different location. Look for "/usr/local/lib/libplaidml.dylib" and replace in above path
    
    import os
    
    # IMPORTANT: PATH MIGHT BE DIFFERENT. SEE STEP 6
    os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
    os.environ["RUNFILES_DIR"] = "/Library/Frameworks/Python.framework/Versions/3.7/share/plaidml"
    os.environ["PLAIDML_NATIVE_PATH"] = "/Library/Frameworks/Python.framework/Versions/3.7/lib/libplaidml.dylib"
    
    # Don't use tensorflow.keras anywhere, instead use keras
    import keras
    from keras.datasets import mnist
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Flatten
    from keras.layers import Conv2D, MaxPooling2D
    from keras import backend as K
    batch_size = 128
    num_classes = 10
    epochs = 12
    # input image dimensions
    img_rows, img_cols = 28, 28
    # the data, split between train and test sets
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    if K.image_data_format() == 'channels_first':
        x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
        x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
        input_shape = (1, img_rows, img_cols)
    else:
        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255
    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')
    # convert class vectors to binary class matrices
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),
                     activation='relu',
                     input_shape=input_shape))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adadelta(),
                  metrics=['accuracy'])
    model.fit(x_train, y_train,
              batch_size=batch_size,
              epochs=epochs,
              verbose=1,
              validation_data=(x_test, y_test))
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
    
    Using plaidml.keras.backend backend.
    INFO:plaidml:Opening device "metal_intel(r)_iris(tm)_graphics_6100.0"
    # or whatever GPU you selected in step 3