目录 开始安装零,获取代理一,配置代理配置zsh走代理配置git走代理 二,安装homebrew三,安装miniforge四,创建conda环境五,安装pytorch六,运行yolov5六,测试Apple Silicon的MP
笔者使用的是一台M2版本的Macbook air,虽然苹果作为深度学习的训练机不太合适,但是由于macbook作为打字机实在是无可挑剔,所以使用macbook调试一下PyTorch的代码再放到集群上训练或者直接在mac上调试运行代码都是不错的体验,本文以在mac上直接调试yolov5为目标,大概记录一下步骤。
这一步就是大家八仙过海各显神通的时候了😄
或者直接使用国内源安装,可以参考Homebrew国内如何自动安装(国内地址)(Mac & Linux)
以下过程都是基于有代理的情况下的安装过程,基本无痛
总之开启代理后,除了浏览器可以走代理访问之外,还需要配置zsh和git走代理,否则homebrew的安装会比较痛苦。
#首先创建~/.zshrcvim ~/.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还是不行的。
将git也配置一下走代理:
git config --global http.proxy http://127.0.0.1:8080
完工!
安装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/.zprofileeval "$(/opt/homebrew/bin/brew shellenv)"
然后就可以愉快的使用brew install了,就和ubuntu的apt install一样啦!
有了homebrew安装miniforge就简单了
brew install miniforge
完成!
首先初始化conda环境
conda init zsh
关闭terminal重新打开就能看到(base)环境已经启用
然后就是创建pytorch的conda环境
conda create -n torch python=3.8
创建完运行
conda activate torch
就可以开始使用专属的conda虚拟环境了。
直接按照官网的安装,已经包含了MPS加速(MPS:Metal PerfORMance Shaders)
# MPS acceleration is available on MacOS 12.3+pip3 install torch torchvision torchaudio
完成!
首先clone源码,安装依赖
git clone https://github.com/ultralytics/yolov5.gitpip install -r requirements.txt
等待安装完毕
现在就可以开始测试检测图片了
我们在yolov5文件夹里创建一个imgs文件夹,里面放一张1.jpg的测试图像
如下图所示
然后运行
Python detect.py --weights yolov5s.pt --source 'imgs/1.jpg'
过程中会自动下载yolov5s.pt的权重
然后将检测结果保存到yolov5/runs/detect/exp中
打开1.jpg看一下
检测成功,yolov5在MacBook上就跑起来了!环境初步搭建完成!
由于pytorch可以跑cpu,所以我们还不知道苹果的mps gpu加速能否正常起作用,那我们来测试一下。
打开vscode,创建testyolov5mps.py,环境选择我们刚刚创建的conda环境torch,编辑一下
import torchprint(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 torchprint(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()# Modelmodel = torch.hub.load("ultralytics/yolov5", "yolov5s") # or yolov5n - yolov5x6, custommodel.to(device)# exit()# Imagesimg = "https://ultralytics.com/images/zidane.jpg" # or file, Path, PIL, OpenCV, numpy, list# Inferenceresults = model(img)# Resultsresults.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>mpsUsing cache found in /Users/xx/.cache/torch/hub/ultralytics_yolov5_masterYOLOv5 🚀 2023-2-27 Python-3.8.16 torch-1.13.1 CPUFusing layers... YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradientsAdding 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>mpsUsing cache found in /Users/xx/.cache/torch/hub/ultralytics_yolov5_masterYOLOv5 🚀 2023-2-27 Python-3.8.16 torch-1.13.1 CPUFusing layers... YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradientsAdding AutoShape... image 1/1: 900x1200 8 persons, 2 sports ballsSpeed: 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>mpsUsing cache found in /Users/xx/.cache/torch/hub/ultralytics_yolov5_masterYOLOv5 🚀 2023-2-27 Python-3.8.16 torch-1.13.1 CPUFusing layers... YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradientsAdding AutoShape... image 1/1: 900x1200 8 persons, 2 sports ballsSpeed: 24.1ms pre-process, 82.8ms inference, 0.7ms NMS per image at shape (1, 3, 480, 640)
仅用了82.8ms就完成了推理,这边先不下结论,还需要再研究一下,不过可以看到mac的gpu加速还不是很完善。
既然yolov5网络太复杂有不支持mps的算子,那我们试试简单的resnet50
import torchfrom PIL import Imageimport torchvision.transforms as transformsimport numpy as npimport JSONimport requestsimport matplotlib.pyplot as pltimport warningswarnings.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.pympsusing mps total time:8.64146113395691s(torch) xx@xxx-MacBook-Air testyolov5 % python testresnetmps.pycpuusing cpu total time:85.01676988601685s
我们可以看到,推理1024张图使用mps 8.6秒,使用cpu花了85秒,快了10倍,确实有效。
我们同样的代码用amd 3700x的纯cpu模式跑一下:
cpuusing cpu total time:41.04833006858826s
用cuda在1080ti上再跑一下,由于显存不够1024张分成两个batch来跑:
cudabatch0inferbatch1inferusing 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 |
测试不是很完善,但是也能看个大概,5w战350w能有这个水平其实还可以。
来源地址:https://blog.csdn.net/catscanner/article/details/129246902
--结束END--
本文标题: Mac Apple Silicon M1/M2 homebrew miniforge conda pytorch yolov5深度学习环境搭建并简单测试MPS GPU加速
本文链接: https://lsjlt.com/news/375469.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0