2

我安装了最新版本的 Keras 和 PlaidML。我运行了文件 plaidml-setup 并将 plaidml 配置为使用我的 AMD GPU:

C:\WinPython\python-3.6.1.amd64\Scripts>plaidml-setup

PlaidML Setup (0.7.0)

(...)


Default Config Devices:
   llvm_cpu.0 : CPU (via LLVM)

Experimental Config Devices:
   llvm_cpu.0 : CPU (via LLVM)
   opencl_amd_gfx902.0 : Advanced Micro Devices, Inc. gfx902 (OpenCL)

Using experimental devices can cause poor performance, crashes, and other nastiness.

Enable experimental device support? (y,n)[n]:y

Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS).
Please choose a default device:

   1 : llvm_cpu.0
   2 : opencl_amd_gfx902.0

Default device? (1,2)[1]:2

Selected device:
    opencl_amd_gfx902.0

Almost done. Multiplying some matrices...
Tile code:
  function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }
Whew. That worked.

Save settings to C:\Users\jsupi\.plaidml? (y,n)[y]:y
Success!

我通过运行成功测试了安装plaidbench keras mobilenet

C:\WinPython\python-3.6.1.amd64\Scripts>plaidbench keras mobilenet
Running 1024 examples with mobilenet, batch size 1, on backend plaid
INFO:plaidml:Opening device "opencl_amd_gfx902.0"
Compiling network... Warming up... Running...
Example finished, elapsed: 7.484s (compile), 26.724s (execution)

-----------------------------------------------------------------------------------------
Network Name         Inference Latency         Time / FPS
-----------------------------------------------------------------------------------------
mobilenet            26.10 ms                  11.90 ms / 84.02 fps
Correctness: PASS, max_error: 1.8053706298815086e-05, max_abs_error: 9.760260581970215e-07, fail_ratio: 0.0

然后我想在我的 GPU 上运行一些 python 模块。我在这个答案中读到我需要设置os.environ["RUNFILES_DIR"]os.environ["PLAIDML_NATIVE_PATH"]更正路径,例如:

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"

问题是我在我的系统中找不到任何类似于最后一个的东西。我运行了 Windows 搜索功能,但它无法在libplaidml.dylib任何地方找到该文件。所以我尝试了以下方法:

import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
os.environ["RUNFILES_DIR"] = "C://Users/jsupi/.plaidml"
#os.environ["PLAIDML_NATIVE_PATH"] = "C:/Windows/WinPython/python-3.6.1.amd64/Lib/site-packages"
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import keras


from keras.datasets import mnist #to import our dataset
from keras.models import Sequential, Model # imports our type of network
from keras.layers import Dense, Flatten, Input # imports our layers we want to use

from keras.losses import categorical_crossentropy #loss function
from keras.optimizers import Adam, SGD #optimisers
from keras.utils import to_categorical #some function for data preparation


batch_size = 128
num_classes = 10
epochs = 50

# 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()


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 = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)



#Neural network with single dense hidden layer

model = Sequential()
#model.add(Input(input_shape=(28,28)))
model.add(Flatten(input_shape=(28,28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

并收到错误消息:

Traceback (most recent call last):
  File "D:\Kuba\Machine Learning\DigitRecognitionKeras.py", line 51, in <module>
    model.add(Dense(128, activation='relu'))
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\engine\sequential.py", line 181, in add
    output_tensor = layer(self.outputs[0])
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\engine\base_layer.py", line 431, in __call__
    self.build(unpack_singleton(input_shapes))
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\layers\core.py", line 866, in build
    constraint=self.kernel_constraint)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\engine\base_layer.py", line 249, in add_weight
    weight = K.variable(initializer(shape),
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\initializers.py", line 218, in __call__
    dtype=dtype, seed=self.seed)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 59, in wrapper
    return func(*args, **kwargs)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 1305, in random_uniform
    rng_state = _make_rng_state(seed)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 205, in _make_rng_state
    rng_state = variable(rng_init, dtype='uint32')
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 59, in wrapper
    return func(*args, **kwargs)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 1935, in variable
    _device(), plaidml.Shape(_ctx, ptile.convert_np_dtype_to_pml(dtype), *value.shape))
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 102, in _device
    devices = plaidml.devices(_ctx)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\__init__.py", line 1075, in devices
    plaidml.settings.start_session()
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\settings.py", line 77, in start_session
    raise plaidml.exceptions.PlaidMLError('PlaidML is not configured. Run plaidml-setup.')
plaidml.exceptions.PlaidMLError: PlaidML is not configured. Run plaidml-setup.

请注意最后一行,它表示即使我刚刚完成并成功测试了 PlaidML,也没有配置它。如果我注释掉前 3 行(因此在没有 plaidml 的情况下运行它)并在所有“导入”行中编写 tensorflow.keras 而不是 keras(没有 plaidml 似乎是必要的),程序运行良好。

您对如何解决此问题有任何想法吗?我有 Windows 10 和 Python 3.6。

更新 08/11/2021:根据朋友的建议,我最近解决了这个问题。首先,'libplaidml.dylib' 是一个 Linux 库文件,我有 Windows,所以我不得不将路径设置为类似的 .dll 文件(我也使用 r"" 来确保反斜杠没有问题):

os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
os.environ["RUNFILES_DIR"] = r"C:\\Users\jsupi\.plaidml"
os.environ["PLAIDML_NATIVE_PATH"] = r"C:\\WinPython\python-3.6.1.amd64\Library\bin\plaidml.dll"

完成后,我还创建了一个虚拟环境,其中安装了所有必要的 python 库(但这可能不是必需的),我从命令行而不是从 GUI 运行 python 脚本。

我希望我没有忘记在这里写下任何必要的步骤。哦,在上述修复之后,让我感到困惑的一件事是,在某些计算过程中使用的 GPU 很少。当我将 plaidml 切换回使用 CPU 时,脚本的运行时间增加了 100 倍,只有这样我才确信 GPU 一直在工作。

4

1 回答 1

0

我最近也开始使用 plaidml。我拿走了你的代码并评论了一个导入语句

import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
os.environ["RUNFILES_DIR"] = "C://Users/jsupi/.plaidml"
#os.environ["PLAIDML_NATIVE_PATH"] = "C:/Windows/WinPython/python-3.6.1.amd64/Lib/site-packages"
import numpy as np
#import tensorflow as tf <- commented this line, as I did not install tensorflow
import matplotlib.pyplot as plt
import keras


from keras.datasets import mnist #to import our dataset
from keras.models import Sequential, Model # imports our type of network
from keras.layers import Dense, Flatten, Input # imports our layers we want to use

from keras.losses import categorical_crossentropy #loss function
from keras.optimizers import Adam, SGD #optimisers
from keras.utils import to_categorical #some function for data preparation


batch_size = 128
num_classes = 10
epochs = 50

# 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()


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 = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)



#Neural network with single dense hidden layer

model = Sequential()
#model.add(Input(input_shape=(28,28)))
model.add(Flatten(input_shape=(28,28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

它工作并打印了输出

Using plaidml.keras.backend backend.
x_train shape: (60000, 28, 28)
60000 train samples
10000 test samples
INFO:plaidml:Opening device "opencl_amd_ellesmere.0"

可能是您的 plaidml 设置不正确。将详细日志的环境变量设置为export PLAIDML_VERBOSE=1. 如果在运行 plailml-setup 时出现任何错误,这将打印出来。我没有安装 tensorflow 框架,只使用了 plaidml、keras。不过,我看到了一些带有 tensorflow 的安装指南 plaidml。我正在使用 ubuntu 20.04

于 2021-10-29T03:04:33.473 回答