Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速

目录

  • 开始安装
    • 零,获取代理
    • 一,配置代理
      • 配置zsh走代理
      • 配置git走代理
    • 二,安装homebrew
    • 三,安装miniforge
    • 四,创建conda环境
    • 五,安装pytorch
    • 六,运行yolov5
    • 六,测试Apple Silicon的MPS GPU加速
      • 测试yolov5的mps加速
      • 测试resnet50的mps加速
      • 对比测试1080ti,3700x
      • 总结

开始安装

笔者使用的是一台M2版本的Macbook Air,虽然苹果作为深度学习的训练机不太合适,但是由于macbook作为打字机实在是无可挑剔,所以使用macbook调试一下pytorch的代码再放到集群上训练或者直接在mac上调试运行代码都是不错的体验,本文以在mac上直接调试yolov5为目标,大概记录一下步骤。

零,获取代理

这一步就是大家八仙过海各显神通的时候了
或者直接使用国内源安装,可以参考Homebrew国内如何自动安装(国内地址)(Mac & Linux)
以下过程都是基于有代理的情况下的安装过程,基本无痛

一,配置代理

总之开启代理后,除了浏览器可以走代理访问之外,还需要配置zsh和git走代理,否则homebrew的安装会比较痛苦。

配置zsh走代理

#首先创建~/.zshrc
vim ~/.zshrc

加入本地代理的端口地址,例如127.0.0.1:8080

function onproxy() {
    export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
    export http_proxy="http://127.0.0.1:8080"
    export https_proxy=$http_proxy
    export all_proxy=socks5://127.0.0.1:8080
    echo -e "\033[32mproxy on!\033[0m"
}

function offproxy(){
    unset http_proxy
    unset https_proxy
    unset all_proxy
    echo -e "\033[31mproxy off!\033[0m"
}

然后source一下

source ~/.zshrc

运行一下onproxy开启代理(后面也要记得重启terminal的时候运行开一下,或者写到.zshrc里)

onproxy

测试一下是不是成功

curl -vv https://www.google.com

如果返回报文中有status 200 OK等字样就说明现在配置成功,但是ping依然是不能正常使用代理的,所以即便配置了代理,ping google还是不行的。
Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第1张图片

配置git走代理

将git也配置一下走代理:

git config --global http.proxy http://127.0.0.1:8080

完工!

二,安装homebrew

安装homebrew十分简单

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

执行完成后会提示你让你把homebrew添加到path中,按图中操作就可以

(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/xxx/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

然后就可以愉快的使用brew install了,就和ubuntu的apt install一样啦!

三,安装miniforge

有了homebrew安装miniforge就简单了

brew install miniforge

完成!

四,创建conda环境

首先初始化conda环境

conda init zsh

关闭terminal重新打开就能看到(base)环境已经启用
Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第2张图片
然后就是创建pytorch的conda环境

conda create -n torch python=3.8

创建完运行

conda activate torch

就可以开始使用专属的conda虚拟环境了。

五,安装pytorch

直接按照官网的安装,已经包含了MPS加速(MPS:Metal Performance Shaders)
Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第3张图片

# MPS acceleration is available on MacOS 12.3+
pip3 install torch torchvision torchaudio

完成!

六,运行yolov5

首先clone源码,安装依赖

git clone https://github.com/ultralytics/yolov5.git
pip install -r requirements.txt 

等待安装完毕
Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第4张图片

现在就可以开始测试检测图片了
我们在yolov5文件夹里创建一个imgs文件夹,里面放一张1.jpg的测试图像
Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第5张图片
如下图所示

Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第6张图片
然后运行

python detect.py --weights yolov5s.pt --source 'imgs/1.jpg'

过程中会自动下载yolov5s.pt的权重
Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第7张图片
然后将检测结果保存到yolov5/runs/detect/exp中
Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第8张图片
打开1.jpg看一下
Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第9张图片
检测成功,yolov5在MacBook上就跑起来了!环境初步搭建完成!

六,测试Apple Silicon的MPS GPU加速

测试yolov5的mps加速

由于pytorch可以跑cpu,所以我们还不知道苹果的mps gpu加速能否正常起作用,那我们来测试一下。

打开vscode,创建testyolov5mps.py,环境选择我们刚刚创建的conda环境torch,编辑一下
Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第10张图片

import torch

print(torch.backends.mps.is_available)
print(torch.backends.mps.is_built)

device = torch.device("mps" if torch.backends.mps.is_available else"cpu")

print(device)
exit()

运行一下

(torch) xx@xxx-MacBook-Air testyolov5 % python testyolov5mps.py 
<functools._lru_cache_wrapper object at 0x114d0f0d0>
<function is_built at 0x114d06ee0>
mps
(torch) xx@xxx-MacBook-Air testyolov5 % 

都有输出结果,并且device为mps,mps加速有效,我们接下来用yolov5的官方示例简单测试一下效果

import torch

print(torch.backends.mps.is_available)
print(torch.backends.mps.is_built)

device = torch.device("mps" if torch.backends.mps.is_available else"cpu")

print(device)
# exit()

# Model
model = torch.hub.load("ultralytics/yolov5", "yolov5s")  # or yolov5n - yolov5x6, custom
model.to(device)
# exit()
# Images
img = "https://ultralytics.com/images/zidane.jpg"  # or file, Path, PIL, OpenCV, numpy, list

# Inference
results = model(img)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.

结果发现报错了,如下

(torch) xx@xxx-MacBook-Air testyolov5 % python testyolov5mps.py
<functools._lru_cache_wrapper object at 0x140872160>
<function is_built at 0x140869f70>
mps
Using cache found in /Users/xx/.cache/torch/hub/ultralytics_yolov5_master
YOLOv5  2023-2-27 Python-3.8.16 torch-1.13.1 CPU

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
Adding AutoShape... 
Traceback (most recent call last):
  File "testyolov5mps.py", line 23, in <module>
    results.print()  # or .show(), .save(), .crop(), .pandas(), etc.
  File "/Users/xx/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 825, in print
    LOGGER.info(self.__str__())
  File "/Users/xx/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 831, in __str__
    return self._run(pprint=True)  # print results
  File "/Users/xx/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 745, in _run
    for c in pred[:, -1].unique():
  File "/opt/homebrew/Caskroom/miniforge/base/envs/torch/lib/python3.8/site-packages/torch/_tensor.py", line 806, in unique
    return torch.unique(
  File "/opt/homebrew/Caskroom/miniforge/base/envs/torch/lib/python3.8/site-packages/torch/_jit_internal.py", line 485, in fn
    return if_false(*args, **kwargs)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/torch/lib/python3.8/site-packages/torch/_jit_internal.py", line 485, in fn
    return if_false(*args, **kwargs)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/torch/lib/python3.8/site-packages/torch/functional.py", line 877, in _return_output
    output, _, _ = _unique_impl(input, sorted, return_inverse, return_counts, dim)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/torch/lib/python3.8/site-packages/torch/functional.py", line 791, in _unique_impl
    output, inverse_indices, counts = torch._unique2(
NotImplementedError: The operator 'aten::_unique2' is not currently implemented for the MPS device. If you want this op to be added in priority during the prototype phase of this feature, please comment on https://github.com/pytorch/pytorch/issues/77764. As a temporary fix, you can set the environment variable `PYTORCH_ENABLE_MPS_FALLBACK=1` to use the CPU as a fallback for this op. WARNING: this will be slower than running natively on MPS.
(torch) xx@xxx-MacBook-Air testyolov5 % 

发现模型中的aten::_unique2操作符没有mps实现,所以只能用fallback模式再试一下

PYTORCH_ENABLE_MPS_FALLBACK=1 python testyolov5mps.py

成功运行

(torch) xx@xxx-MacBook-Air testyolov5 % PYTORCH_ENABLE_MPS_FALLBACK=1 python testyolov5mps.py 
<functools._lru_cache_wrapper object at 0x10cb72160>
<function is_built at 0x10cb69f70>
mps
Using cache found in /Users/xx/.cache/torch/hub/ultralytics_yolov5_master
YOLOv5  2023-2-27 Python-3.8.16 torch-1.13.1 CPU

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
Adding AutoShape... 
image 1/1: 900x1200 8 persons, 2 sports balls
Speed: 58.2ms pre-process, 228.4ms inference, 24.2ms NMS per image at shape (1, 3, 480, 640)

可以看到推理时间228.4ms

如果我们注释掉model.to(device),使用cpu模式来跑

(torch) xx@xxx-MacBook-Air testyolov5 % python testyolov5mps.py 
<functools._lru_cache_wrapper object at 0x1248f2280>
<function is_built at 0x1248f20d0>
mps
Using cache found in /Users/xx/.cache/torch/hub/ultralytics_yolov5_master
YOLOv5  2023-2-27 Python-3.8.16 torch-1.13.1 CPU

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
Adding AutoShape... 
image 1/1: 900x1200 8 persons, 2 sports balls
Speed: 24.1ms pre-process, 82.8ms inference, 0.7ms NMS per image at shape (1, 3, 480, 640)

仅用了82.8ms就完成了推理,这边先不下结论,还需要再研究一下,不过可以看到mac的gpu加速还不是很完善。

测试resnet50的mps加速

既然yolov5网络太复杂有不支持mps的算子,那我们试试简单的resnet50

import torch
from PIL import Image
import torchvision.transforms as transforms
import numpy as np
import json
import requests
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
import time
# %matplotlib inline

# print(torch.backends.mps.is_available)
# print(torch.backends.mps.is_built)

device = torch.device("mps" if torch.backends.mps.is_available else"cpu")
#device = torch.device("cpu")
print(device)
# exit()

resnet50 = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_resnet50', pretrained=True)
utils = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_convnets_processing_utils')

resnet50.eval().to(device)

uris = ["/Users/xx/workspace/yolov5/imgs/1.jpg" for x in range(1024)]

batch = torch.cat(
    [utils.prepare_input_from_uri(uri) for uri in uris]
).to(device)

with torch.no_grad():
    start = time.time()
    output = resnet50(batch)
    end = time.time()
    # output = torch.nn.functional.softmax(output, dim=1)
    print("using {} total time:{}s".format(device,end-start))

# results = utils.pick_n_best(predictions=output, n=5)
# print(results)

# for uri, result in zip(uris, results):
#     img = Image.open(requests.get(uri, stream=True).raw)
#     img = Image.open(uri)
#     img.thumbnail((256,256), Image.ANTIALIAS)
#     plt.imshow(img)
#     plt.show()
#     print(result)

分别用cpu和mps跑1024张图像,计算推理时间
结果

(torch) xx@xxx-MacBook-Air testyolov5 % python testresnetmps.py
mps
using mps total time:8.64146113395691s
(torch) xx@xxx-MacBook-Air testyolov5 % python testresnetmps.py
cpu
using cpu total time:85.01676988601685s

我们可以看到,推理1024张图使用mps 8.6秒,使用cpu花了85秒,快了10倍,确实有效。

对比测试1080ti,3700x

我们同样的代码用amd 3700x的纯cpu模式跑一下:

cpu
using cpu total time:41.04833006858826s

用cuda在1080ti上再跑一下,由于显存不够1024张分成两个batch来跑:

cuda
batch0infer
batch1infer
using cuda total time:2.7815260887145996s

3700x用时41秒,使用cuda只需要2.78秒

总结

device data model 用时(秒)
Apple M2(8+10) (CPU) 1024张 1 batch resnet50 cls 85.02
Apple M2(8+10) (MPS) 1024张 1 batch resnet50 cls 8.64
AMD 3700X + 1080Ti (CPU) 1024张 1 batch resnet50 cls 41.05
AMD 3700X + 1080Ti (CUDA) 1024张 2 batch resnet50 cls 2.78

Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速_第11张图片

测试不是很完善,但是也能看个大概,5w战350w能有这个水平其实还可以。

你可能感兴趣的:(deeplearning,pytorch,深度学习,macos,conda)