我已经安装了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模块。我在this answer中读到需要设置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的情况下是必要的),那么程序运行得很好。
你有什么办法来解决这个问题吗?我用的是Windows10和Python 3.6。
2021年8月11日更新:我最近在一个朋友的建议下解决了这个问题。首先,'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毕竟一直在工作。
发布于 2021-10-29 03:04:33
我也在使用最近开始的plaidml。我使用了您的代码并注释了一条import语句
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运行
https://stackoverflow.com/questions/65267688
复制相似问题