一.安装python
下载python包,或用brew install
二. 安装pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
查看版本
python --version
pip --version
加镜像
cd ~/
mkdir .pip
cd .pip
touch pip.conf
修改pip.conf文件内容
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com
可替换的国内源
阿里云 :http://mirrors.aliyun.com/pypi/simple/
中国科学技术大学:https://pypi.mirrors.ustc.edu.cn/simple/
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/
豆瓣:http://pypi.douban.com/simple
三.安装PlaidML
pip install -U plaidml-keras
然后设置PlaidML
plaidml-setup
下面进行设置,第一个是设置要不要Dev模式,选n
PlaidML Setup (0.7.0)
Thanks for using PlaidML!
The feedback we have received from our users indicates an ever-increasing need
for performance, programmability, and portability. During the past few months,
we have been restructuring PlaidML to address those needs. To make all the
changes we need to make while supporting our current user base, all development
of PlaidML has moved to a branch — plaidml-v1. We will continue to maintain and
support the master branch of PlaidML and the stable 0.7.0 release.
Read more here: https://github.com/plaidml/plaidml
Some Notes:
* Bugs and other issues: https://github.com/plaidml/plaidml/issues
* Questions: https://stackoverflow.com/questions/tagged/plaidml
* Say hello: https://groups.google.com/forum/#!forum/plaidml-dev
* PlaidML is licensed under the Apache License 2.0
Default Config Devices:
llvm_cpu.0 : CPU (via LLVM)
metal_intel(r)_uhd_graphics_630.0 : Intel(R) UHD Graphics 630 (Metal)
metal_amd_radeon_pro_5500m.0 : AMD Radeon Pro 5500M (Metal)
Experimental Config Devices:
llvm_cpu.0 : CPU (via LLVM)
opencl_amd_radeon_pro_5500m_compute_engine.0 : AMD AMD Radeon Pro 5500M Compute Engine (OpenCL)
opencl_intel_uhd_graphics_630.0 : Intel Inc. Intel(R) UHD Graphics 630 (OpenCL)
metal_intel(r)_uhd_graphics_630.0 : Intel(R) UHD Graphics 630 (Metal)
metal_amd_radeon_pro_5500m.0 : AMD Radeon Pro 5500M (Metal)
Using experimental devices can cause poor performance, crashes, and other nastiness.
Enable experimental device support? (y,n)[n]:n
第二个设置默认设备。1.cpu 剩下的是自己本地的显卡,选择性能最高的显卡
Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS).
Please choose a default device:
1 : llvm_cpu.0
2 : metal_intel(r)_uhd_graphics_630.0
3 : metal_amd_radeon_pro_5500m.0
Default device? (1,2,3)[1]:3
第三个设置配置文件夹,直接选y
Selected device:
metal_amd_radeon_pro_5500m.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 /Users/chenpeng/.plaidml? (y,n)[y]:y
可以看到Success!设置完成
四. 测试神经网路
通过在 plaidbench 中运行 MobileNet 来测试安装。
pip install plaidml-keras plaidbench
plaidbench keras mobilenet
报错
Traceback (most recent call last):
File "/Users/chenpeng/working/own/ML/MiAI_PlaidML/test_plaidml.py", line 3, in <module>
plaidml.keras.install_backend()
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/plaidml/keras/__init__.py", line 68, in install_backend
from keras.utils import conv_utils
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/__init__.py", line 3, in <module>
from . import utils
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/utils/__init__.py", line 27, in <module>
from .multi_gpu_utils import multi_gpu_model
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/utils/multi_gpu_utils.py", line 7, in <module>
from ..layers.merge import concatenate
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/layers/__init__.py", line 4, in <module>
from ..engine.base_layer import Layer
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/engine/__init__.py", line 8, in <module>
from .training import Model
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/engine/training.py", line 21, in <module>
from . import training_arrays
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/engine/training_arrays.py", line 14, in <module>
from .. import callbacks as cbks
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/callbacks.py", line 20, in <module>
from collections import Iterable
ImportError: cannot import name 'Iterable' from 'collections' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/collections/__init__.py)
ImportError: cannot import name 'Iterable' from 'collections'
这个命令会报错,原因是python3.10.0删除了Iterable。需要修改一下源文件代码
根据堆栈信息,报错位置在/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/keras/callbacks.py的第20行
将from collections import Iterable改为:
from collections.abc import Iterable
错误参考:
ImportError: cannot import name 'Iterable' from 'collections' in Python - Stack Overflow
测试新的用例:
在github上找了一个脚本
GitHub - thangnch/MiAI_PlaidML: Demo of using PlaidML to train on GPU on Macbook Intel
import plaidml.keras
plaidml.keras.install_backend()
import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
import keras
# Download fashion dataset from Keras
fashion_mnist = keras.datasets.fashion_mnist
(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()
# Reshape and normalize the data
x_train = x_train.astype('float32').reshape(60000,28,28,1) / 255
x_test = x_test.astype('float32').reshape(10000,28,28,1) / 255
model = keras.Sequential()
model.add(keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1)))
model.add(keras.layers.MaxPooling2D(pool_size=2))
model.add(keras.layers.Dropout(0.3))
model.add(keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=2))
model.add(keras.layers.Dropout(0.3))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(256, activation='relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(10, activation='softmax'))
# Compile the model
model.compile(optimizer='adam',
loss=keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
# Fit the model on training set
model.fit(x_train, y_train,
batch_size=64,
epochs=1)
# Evaluate the model on test set
score = model.evaluate(x_test, y_test, verbose=0)
# Print test accuracy
print('\n', 'Test accuracy:', score[1])
运行
python test_plaidml.py
结果
Downloading data from http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 5us/step
Downloading data from http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 4s 0us/step
Downloading data from http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 2s 0us/step
INFO:plaidml:Opening device "metal_amd_radeon_pro_5500m.0"
Epoch 1/1
60000/60000 [==============================] - 11s 182us/step - loss: 0.5796 - acc: 0.7868
Test accuracy: 0.8472
看到用了11s
五. 安装tensorflow
pip install tensorflow
测试
python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
返回
tf.Tensor(-302.09125, shape=(), dtype=float32)
五. 引用
Mac 上机器学习 Machine Learning 使用GPU加速_macbook gpu 炼丹_renzibei的博客-CSDN博客