MacBook21-m1 pro芯片安装Anaconda及TensorFlow的GPU版本(提供全部所需文件)

前言

我已经很久没有写博客了。一方面,毕业季忙于毕士和一篇论文,另一方面,当我开始工作时,事情很多,最重要的是我变得懒惰了。结果,我已经九个月没有写博客了。我感到非常羞愧。希望他们坚持分享自己的问题,也希望能帮助他人。

[En]

I haven’t written a blog for a long time. On the one hand, the graduation season is busy with Bishi and a paper, on the other hand, when I start to work, there are a lot of things, and the most important thing is that I have become lazy. As a result, I haven’t written a blog for nine months. I’m very ashamed. Hope that they insist on sharing their own problems, but also hope to help others.

好了闲言少叙,切入正题,由于工作需要,使用mac办公,但是mac使用的是Arm架构的芯片,在不少方面都有着限制。近期就遇到了需要本地安装python环境以及GPU支持的问题。

1:问题背景

环境:

MacBook21-m1 pro芯片安装Anaconda及TensorFlow的GPU版本(提供全部所需文件)
需求:
使用Anaconda安装python环境,安装TensorFlow- GPU库,跑通样例。
下载所需文件:(鼓励免费共享技术文档)
[En]

Download required files: (encourage free sharing of technical documents)

链接: https://pan.baidu.com/s/1XJ9AwuHvSpPJ3pLGCj7xVg?pwd=ru96
提取码: ru96
文件列表如下:

MacBook21-m1 pro芯片安装Anaconda及TensorFlow的GPU版本(提供全部所需文件)

; 2: 解决方法

a: 下载并安装conda

首先需要知道的是,m1芯片使用的conda安装包和x86架构的芯片不一样,它使用的是miniforge3,可以在此处下载,也可以在我前面给出的百度网盘里下载。

MacBook21-m1 pro芯片安装Anaconda及TensorFlow的GPU版本(提供全部所需文件)
转到您下载该文件的目录,然后运行以下说明:
[En]

Go to the directory where you downloaded the file, and then run the following instruction:

bash Miniforge3-MacOSX-arm64.sh

安装完成。然后是激活环境,可以先查看文件~/.zshrc
无论内容如下,如果没有这样的文件,或者如果没有这样的文件,您可以创建一个新文件或在文件后添加此部分,并注意应该将路径更改为您自己的安装路径。

[En]

Whether the content is as follows, if there is no such file, or if it is not, you can create a new file or add this section after the file, and note that the path should be changed to your own installation path.


__conda_setup="$('/Users/XXXX/miniforge3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/XXXX/miniforge3/profile.d/conda.sh" ]; then
        . "/Users/XXXX/miniforge3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/XXXX/miniforge3/bin:$PATH"
    fi
fi
unset __conda_setup

注意:XXXX指的是你自己的路径名哈,不要搞错了。
然后运行以下指令以激活环境:

[En]

Then run the following instructions to activate the environment:

source ~/.zshrc

或者运行另一个终端的配置文件:

[En]

Or run another terminal’s configuration file:

~/.bash_profile文件如下:


__conda_setup="$('/Users/XXXX/miniforge3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/XXXX/miniforge3/etc/profile.d/conda.sh" ]; then
        . "/Users/XXXX/miniforge3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/XXXX/miniforge3/bin:$PATH"
    fi
fi
unset __conda_setup

注意:上一个是shell.zsh,这一个是shell.bash,激活的对象不一样。最终目的都是为了让终端可以找到conda的位置。
运行激活命令:

source ~/.bash_profile

好啦,这个时候你就可以打开终端,输入conda env list,查看安装的conda环境了。

b:安装TensorFlow-gpu

一般的安装方法是直接使用conda命令进行安装,但是安装的tensorflow不一定能用,这里采用的方法是直接使用一个配置文件,新建一个环境,环境中直接就包含了gpu版本的tensorflow,简单易行,方便可靠。
首先需要在百度网盘的链接中下载 tf39-gpu.yaml 文件,然后运行以下命令:

conda env create -f tf39-gpu.yaml

这个命令执行好了,这项大工作就完成了。让我们简单点吧。该命令直接从配置文件创建环境,并且列出了您需要的各种库文件,并将直接安装它。

[En]

If this order is carried out well, the big work will be accomplished. Let’s keep it simple. This command creates the environment directly from the configuration file, and the various library files you need have been listed, and it will be installed directly.

运行conda env list,就可以看到已经安装了tf39-gpu的环境了,然后conda activate tf39-gpu,激活该环境,conda list 就可以看到安装的各个库文件了。

c:测试

将以下代码复制到本地文件,然后运行它:

[En]

Copy the following code to your local file, and then run it:

from tensorflow.keras import layers
from tensorflow.keras import models
import tensorflow as tf
import time

begin_time = time.time()

print(f"Running TensorFlow {tf.__version__} with {len(tf.config.list_physical_devices('GPU'))} GPUs recognized")

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()

from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)
test_loss, test_acc = model.evaluate(test_images, test_labels)
test_acc
end_time = time.time()

print('Total time: {} s'.format(int(end_time-begin_time)))

可以看到,运行该代码时,GPU的使用率是上升的。

MacBook21-m1 pro芯片安装Anaconda及TensorFlow的GPU版本(提供全部所需文件)

3: CPU版本

按理说,安装完成后就应该没啥事了,但是同样的代码我又运行了一下CPU的版本,发现CPU的更快。。。。简直离了大谱,有了GPU的加速,竟然更慢了,后来发现运行CPU时,CPU的占比要远高于GPU,如下图所示:

MacBook21-m1 pro芯片安装Anaconda及TensorFlow的GPU版本(提供全部所需文件)
可能还是由于适配原因吧,对于GPU的加速和CPU之间的均衡没有做到完美。因此我也给大家提供了cpu的版本,可以在网盘上下载 tf39-cpu.yaml 文件,然后按照上述步骤安装就行。

; 注意

有时TensorFlow-GPU可能下载较慢导致安装失败,那么你可以找到安装时它的下载链接,直接下载那个whl文件,或者还在我分享的网盘链接中下载tensorflow_macos-2.6.0-cp39-cp39-macosx_11_0_arm64.whl 文件,然后手动安装。但是这样有可能导致安装后的Tensorflow无法使用GPU计算,这和 tensorflow-metal0.1.2 或tensorflow-estimator2.6.0 库的版本有关,你可以直接安装如上指定版本的库就行。
反正就是对照着tf39-gpu.yaml里的库文件版本进行对齐就行。

结语

我已经很久没有写博客了。希望以后能坚持分享,打造更多优质内容。再见!再见!

[En]

I haven’t written a blog for a long time. I hope I can insist on sharing and create more high-quality content in the future. Bye! bye!

Reference

https://blog.csdn.net/hsywatchingu/article/details/118055508
https://github.com/conda-forge/miniforge/#download
https://makeoptim.com/deep-learning/tensorflow-metal

Original: https://blog.csdn.net/weixin_43791511/article/details/122386015
Author: 金色麦田~
Title: MacBook21-m1 pro芯片安装Anaconda及TensorFlow的GPU版本(提供全部所需文件)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/496997/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球