Loading...

1.下载Anconda

Anaconda官网安装ARM版本的Anconda


2.检查Anconda是否安装完成

输入下面指令:

1
conda info

若出现以下内容则安装成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(base) chenyubin@chenyubindeMacBook-Pro ~ % conda info

active environment : base
active env location : /Users/chenyubin/anaconda3
shell level : 1
user config file : /Users/chenyubin/.condarc
populated config files : /Users/chenyubin/.condarc
conda version : 23.5.0
conda-build version : 3.25.0
python version : 3.11.3.final.0
virtual packages : __archspec=1=arm64
__osx=13.6=0
__unix=0=0
base environment : /Users/chenyubin/anaconda3 (writable)
conda av data dir : /Users/chenyubin/anaconda3/etc/conda
conda av metadata url : None
channel URLs : https://repo.anaconda.com/pkgs/main/osx-arm64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/osx-arm64
https://repo.anaconda.com/pkgs/r/noarch
package cache : /Users/chenyubin/anaconda3/pkgs
/Users/chenyubin/.conda/pkgs
envs directories : /Users/chenyubin/anaconda3/envs
/Users/chenyubin/.conda/envs
platform : osx-arm64
user-agent : conda/23.5.0 requests/2.29.0 CPython/3.11.3 Darwin/22.6.0 OSX/13.6
UID:GID : 501:20
netrc file : None
offline mode : False

3.检查安装Anconda的版本是否正确

输入指令:(一行一行输入)

1
2
3
python
import platform
print(platform.platform())

输出结果:

1
2
3
4
5
6
(base) chenyubin@chenyubindeMacBook-Pro ~ % python
Python 3.11.3 (main, Apr 19 2023, 18:49:55) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> print(platform.platform())
macOS-13.6-arm64-arm-64bit

4.创建虚拟环境

输入指令: (注意此处的MSResNet和python版本号应该换成你需要的内容)

1
conda create -n MSResNet python=3.9

激活这个环境:

1
conda activate MSResNet

5.安装PyTorch

PyTorch的GPU训练加速是使用苹果Metal Performance Shaders(MPS)作为后端来实现的。注意Mac OS版本要大于等于12.3

去PyTorch官网获取命令。这里注意要选取Nightly版本,才支持GPU加速,Package选项中选择Pip。(这里若使用conda安装有一定概率无法安装到预览版,建议使用pip3安装)


输入命令安装:

1
2
# MPS acceleration is available on MacOS 12.3+
pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu

得到结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(MSResNet) chenyubin@chenyubindeMacBook-Pro ~ % pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu
Looking in indexes: https://download.pytorch.org/whl/nightly/cpu
Collecting torch
Downloading https://download.pytorch.org/whl/nightly/cpu/torch-2.2.0.dev20231011-cp39-none-macosx_11_0_arm64.whl (58.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 MB 16.7 MB/s eta 0:00:00
Collecting torchvision
Downloading https://download.pytorch.org/whl/nightly/cpu/torchvision-0.17.0.dev20231011-cp39-cp39-macosx_11_0_arm64.whl (1.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 6.8 MB/s eta 0:00:00
Collecting torchaudio
Downloading https://download.pytorch.org/whl/nightly/cpu/torchaudio-2.2.0.dev20231011-cp39-cp39-macosx_11_0_arm64.whl (1.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 5.9 MB/s eta 0:00:00
Collecting filelock (from torch)
Using cached https://download.pytorch.org/whl/nightly/filelock-3.9.0-py3-none-any.whl (9.7 kB)
Collecting typing-extensions (from torch)
Using cached https://download.pytorch.org/whl/nightly/typing_extensions-4.4.0-py3-none-any.whl (26 kB)
Collecting sympy (from torch)
Using cached https://download.pytorch.org/whl/nightly/sympy-1.11.1-py3-none-any.whl (6.5 MB)
Collecting networkx (from torch)
Using cached https://download.pytorch.org/whl/nightly/networkx-3.0rc1-py3-none-any.whl (2.0 MB)
Collecting jinja2 (from torch)
Using cached https://download.pytorch.org/whl/nightly/Jinja2-3.1.2-py3-none-any.whl (133 kB)
Collecting fsspec (from torch)
Using cached https://download.pytorch.org/whl/nightly/fsspec-2023.4.0-py3-none-any.whl (153 kB)
Collecting numpy (from torchvision)
Downloading https://download.pytorch.org/whl/nightly/numpy-1.24.1-cp39-cp39-macosx_11_0_arm64.whl (13.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.9/13.9 MB 18.5 MB/s eta 0:00:00
Collecting requests (from torchvision)
Using cached https://download.pytorch.org/whl/nightly/requests-2.28.1-py3-none-any.whl (62 kB)
Collecting pillow!=8.3.*,>=5.3.0 (from torchvision)
Downloading https://download.pytorch.org/whl/nightly/Pillow-9.3.0-cp39-cp39-macosx_11_0_arm64.whl (2.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.9/2.9 MB 10.9 MB/s eta 0:00:00
Collecting MarkupSafe>=2.0 (from jinja2->torch)
Downloading https://download.pytorch.org/whl/nightly/MarkupSafe-2.1.2-cp39-cp39-macosx_10_9_universal2.whl (17 kB)
Collecting charset-normalizer<3,>=2 (from requests->torchvision)
Using cached https://download.pytorch.org/whl/nightly/charset_normalizer-2.1.1-py3-none-any.whl (39 kB)
Collecting idna<4,>=2.5 (from requests->torchvision)
Using cached https://download.pytorch.org/whl/nightly/idna-3.4-py3-none-any.whl (61 kB)
Collecting urllib3<1.27,>=1.21.1 (from requests->torchvision)
Using cached https://download.pytorch.org/whl/nightly/urllib3-1.26.13-py2.py3-none-any.whl (140 kB)
Collecting certifi>=2017.4.17 (from requests->torchvision)
Using cached https://download.pytorch.org/whl/nightly/certifi-2022.12.7-py3-none-any.whl (155 kB)
Collecting mpmath>=0.19 (from sympy->torch)
Using cached https://download.pytorch.org/whl/nightly/mpmath-1.2.1-py3-none-any.whl (532 kB)
Installing collected packages: mpmath, urllib3, typing-extensions, sympy, pillow, numpy, networkx, MarkupSafe, idna, fsspec, filelock, charset-normalizer, certifi, requests, jinja2, torch, torchvision, torchaudio
Successfully installed MarkupSafe-2.1.2 certifi-2022.12.7 charset-normalizer-2.1.1 filelock-3.9.0 fsspec-2023.4.0 idna-3.4 jinja2-3.1.2 mpmath-1.2.1 networkx-3.0rc1 numpy-1.24.1 pillow-9.3.0 requests-2.28.1 sympy-1.11.1 torch-2.2.0.dev20231011 torchaudio-2.2.0.dev20231011 torchvision-0.17.0.dev20231011 typing-extensions-4.4.0 urllib3-1.26.13

6.验证是否安装成功与是否支持PyTorch加速

输入指令:(一行一行输入)

1
2
3
4
python
import torch
torch.__version__
torch.device("mps")

出现结果:

1
2
3
4
5
6
7
8
9
10
11
12
(MSResNet) chenyubin@chenyubindeMacBook-Pro ~ % python
import torch
torch.__version__
torch.device("mps")
Python 3.9.18 (main, Sep 11 2023, 08:25:10)
[Clang 14.0.6 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
torch.__>>> torch.__version__
'2.2.0.dev20231011'
>>> torch.device("mps")
device(type='mps')

7.PyCharm配置

打开PyCharm,点击setting -> 项目:xxx -> Python解释器 -> 添加解释器 -> 添加本地解释器 -> conda环境,选择自己刚才创建的环境即可:

如果找不到python版本路径,可以打开终端输入:

1
which python

得到以下结果:

1
2
(MSResNet) chenyubin@chenyubindeMacBook-Pro ~ % which python
/Users/chenyubin/anaconda3/envs/MSResNet/bin/python

8.test

新建python文件输入以下内容: