安装环境 pytorch和mmpose
安装pytorch
相关网页:https://pytorch.org/get-started/locally/
本机环境
型号名称: | MacBook Air |
型号标识符: | MacBookAir10,1 |
型号: | FGNE3CH/A |
芯片: | Apple M1 |
核总数: | 8(4性能和4能效) |
内存: | 8 GB |
系统固件版本: | 8422.100.650 |
操作系统加载程序版本: | 8422.100.650 |
安装anaconda
# The version of Anaconda may be different depending on when you are installing
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
sh Miniconda3-latest-MacOSX-arm64.sh
由于机器限制,使用的是仅cpu平台的pytorch
# 在 CPU 平台:
conda install pytorch torchvision cpuonly -c pytorch
安装mmpose
天坑
mmpose的numpy在requirements.txt中是没有写明版本的,所以很容易出现版本问题,包括但是不限于:
- RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xf
- numpy.ndarray size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject
- 如果单独调整numpy的版本,会出现scipy的版本要求numpy<1.23.0,但是1.23.0的numpy会导致第一个问题
解决办法:(由于是多种办法同时使用,所以无法确定是哪种有效)
- 在自己的环境中编译安装xtcocoapi
相关网页:https://github.com/open-mmlab/mmpose/issues/1939
在mmpose的issues中也有类似提问,得到的回复是:
The cause of this error is an incompatibility between Colab and the platform where xtcocotools was built. To fix it, you have to compile xtcocotools yourself before utilizing it. The easiest method is to use pip install git+https://github.com/jin-s13/xtcocoapi to install it from source.
此错误的原因是Colab与构建xtcocotools的平台之间不兼容。要修复它,你必须在使用它之前自己编译xtcocotools。最简单的方法是使用 pip install git+https://github.com/jin-s13/xtcocoapi 从源代码安装它。
2. 在使用pip -r requirement.txt之前,先安装一下numpy,安装最新版本
pip show numpy
Name: numpy
Version: 1.24.2
开始安装mmpose
相关网页: https://mmpose.readthedocs.io/zh_CN/latest/installation.html
pip install -U openmim
mim install mmengine
mim install "mmcv>=2.0.0"
mim install "mmdet>=3.0.0"
git clone https://github.com/open-mmlab/mmpose.git
cd mmpose
pip install -r requirements.txt
pip install -v -e .
# "-v" 表示输出更多安装相关的信息
# "-e" 表示以可编辑形式安装,这样可以在不重新安装的情况下,让本地修改直接生效
验证安装
pytorch验证
(应该在安装mmpose之前进行验证,但是当时忘记了。。。)
代码:
import torch
def foo(x, y):
a = torch.sin(x)
b = torch.cos(x)
return a + b
opt_foo1 = torch.compile(foo)
print(opt_foo1(torch.randn(10, 10), torch.randn(10, 10)))
结果:
tensor([[ 1.4005, 1.2729, -0.4619, -0.7275, 1.2143, 1.1516, 1.4054, 1.3747,
1.3141, -0.4405],
[-0.3841, 1.2967, 1.3972, 1.4060, 1.2643, 1.2481, 0.1303, 1.3708,
0.9333, 1.1339],
[ 1.3955, -0.8951, 1.2887, 0.4925, 0.8836, 0.5261, 1.0910, 1.1282,
-0.0557, -0.7044],
[-1.2864, 0.8302, -0.0315, -1.3574, 1.0179, -0.6004, -0.1394, 1.1423,
-1.0470, 0.1540],
[ 0.2927, -1.3813, 1.1287, 0.6799, 1.3580, 0.8075, 1.2186, 0.5551,
0.3165, 0.4945],
[ 1.4121, -0.4751, -0.8977, 0.4811, 1.4135, 1.1534, -0.4514, 1.2268,
1.2346, 0.0133],
[ 1.2795, -1.3626, 0.5973, -0.9627, 0.0031, -0.0763, 1.2729, 0.6951,
0.9089, -1.0749],
[-0.3634, 0.3221, 1.3457, 0.2577, 1.3953, 0.1466, 1.4131, -0.0334,
1.2000, 1.2601],
[ 1.3553, -0.4784, 0.2269, 1.2800, 0.7798, 0.8343, 1.2789, 1.3946,
1.2273, 1.0872],
[-0.0441, -0.1585, 1.0462, 0.7855, 1.1255, 1.0870, 1.4040, 1.1156,
-0.5202, 1.2447]])
mmpose验证
源码构建方式验证
python demo/image_demo.py \
picdemo.jpg \
td-hm_hrnet-w48_8xb32-210e_coco-256x192.py \
td-hm_hrnet-w48_8xb32-210e_coco-256x192-0e67c616_20220913.pth \
--out-file vis_results.jpg \
--draw-heatmap
如果使用非源码构建的测试方式,需要手动打印result,否则没有实验结果:
from mmpose.apis import inference_topdown, init_model
from mmpose.utils import register_all_modules
register_all_modules()
config_file = 'td-hm_hrnet-w48_8xb32-210e_coco-256x192.py'
checkpoint_file = 'td-hm_hrnet-w48_8xb32-210e_coco-256x192-0e67c616_20220913.pth'
model = init_model(config_file, checkpoint_file, device='cpu') # or device='cuda:0'
# 请准备好一张带有人体的图片
results = inference_topdown(model, 'picdemo.jpg')
print(results)