macOS 10.15
Anaconda+Python 3.7
torch
torchvision
这里检查torch相关版本型号,很关键,后续有重要作用!!!
>>>python
Python 3.7.7 (default, May 6 2020, 04:59:01)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
import >>> import torchvision
>>> torch.__version__
'1.3.1'
>>> torchvision.__version__
'0.4.2'
>>> exit()
安装pyinstaller
pip install pyinstaller
实际为了减少打包过程会出现的问题,应对一个项目新建一个虚拟环境,对应安装所需的依赖包。但是题主暂未养成这个好习惯(懒),所以用的是工程运行时搭建的环境。
cd到存放.py文件的文件夹下,这里展示DeeplabV3模型的测试demo.py
pyinstaller -F demo.py
-F 可以生成单独的Unix可执行文件(Windows上是exe文件)其他常见参数如下图所示。
等待一会儿文件就创建成功了,打包时间取决于工程实际大小和依赖库的大小。所以这里不相关的库建议在打包前及时清理,也可以避免打包时不必要的错误发生。
226207 INFO: Building EXE from EXE-00.toc completed successfully.
发生在执行第二步pyinstaller时,没有completed successfully,直接报错
仔细查看报错位置:torchvision!
File "site-packages\torchvision\ops\misc.py", line 135, in <module>
File "site-packages\torchvision\ops\misc.py", line 148, in FrozenBatchNorm2d
File "site-packages\torch\jit\__init__.py", line 1204, in script_method
File "site-packages\torch\jit\frontend.py", line 156, in get_jit_def
查阅资料,应该是需要指定torch虚拟环境下的python依赖库site-packages的path路径,
位置是:/Users/user/anaconda3/envs/torch/lib/python3.7/site-packages/torch/lib
加一句,指定依赖包路径:
pepper@Sigrid‘s MacBook-Pro pytorch-deeplab-xception % pyinstaller -p /Users/user/anaconda3/envs/torch/lib/python3.7/site-packages/torch/lib -F demo.py
OSError: Can't get source for torchvision/ops/misc.py. TorchScript requires source access in order to carry out compilation. Make sure original .py files are available. Original error: could not get source code
继续向上搜索,康康报错提示:还是发生在torch和torchvision的地方。尝试了很多方法:修改py文件和生成的spec文件,都提示Error。。。也有考虑是否由于anaconda环境下依赖库的问题,需要建立单独的virtualenv,或者是macOS自带bug。。。
一通自我怀疑之后,发现了一条可能的法子:或许是torchvision版本过高不支持pyinstaller?
于是有了:
>>>torchvision.__version__
'0.4.2'
果断降级,指定版本的torchvision:
pip uninstall torchvision
pip install torchvision==0.2.2.post3
下一个ERROR虽迟但到
看到这里,想到之前海量搜刮教程时候看到“pyinstaller不支持matplotlib打印图片”的类似说法,于是返回py文件查看
DeeplabV3的代码中有:utils.py搜刮到了这么一句
if plot:
plt.imshow(rgb)
plt.show()
else:
return rgb
由于我们不需要plot打印照片,注释掉。开头的import matplotlib as plt也一起去掉。
(如果实在需要打印图像,可以考虑cv2.imshow()或者PIL的方法)
然而。。。
下一个ERROR虽迟但到
运行demo时还是出现:RuntimeError: Could not find the matplotlib data files
Traceback (most recent call last):
File "demo.py", line 7, in <module>
from dataloaders import custom_transforms as tr
File "" , line 983, in _find_and_load
File "" , line 967, in _find_and_load_unlocked
File "" , line 677, in _load_unlocked
File "/Users/panyining/anaconda3/envs/torch/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
继续扒拉关键词,看到了:
/Users/anaconda3/envs/torch/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py:493: MatplotlibDeprecationWarning: Matplotlib installs where the data is not in the mpl-data subdirectory of the package are deprecated since 3.2 and support for them will be removed two minor releases later.
exec(bytecode, module.__dict__)
有眉目了,大致意思还是matplotlib的版本和pyinstall不匹配的问题。果然pyinstaller都是坑。选择降级:
pip uninstall matplotlib
pip install matplotlib==3.1.1
搞定,
有同样遇到这个问题的博主还修改了hiddenimport:打包matplotlib出现RuntimeError: Could not find the matplotlib data files
这里我没有修改spec,有需要的走一个
运行Unix可执行文件,终于!成功了!
/Users/PycharmProjects/stones_detect/pytorch-deeplab-xception/dist/demo --in-path /Users/PycharmProjects/stones_detect/dataset/test_image --ckpt /Users/PycharmProjects/stones_detect/model/checkpoint.pth.tar --backbone mobilenet
# 执行文件,传入demo.py的args参数
MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
exec(bytecode, module.__dict__)
model load time is 0.21828413009643555
image:IMG_3711_mas_mask.png time: 1.1881170272827148
image save in in_path.
image:IMG_3703_mas_mask.png time: 1.1437768936157227
image save in in_path.
image:IMG_3707.jpeg time: 1.1667790412902832
image save in in_path.
# 虽然有Warning,表示matplotlib的版本即将被遗弃
学术探索的道路注定是孤独的,坚持!!!遇到各种ERROR的时候自闭了好几天,忙完手头的活之后丧了一段时间,才调整心情继续肝installer。遇到各种“不知所谓”的报错也一度怀疑用macOS打包是否能成功走下去。
from 遇到炸毛时候需要美食激励的小胡椒
2020.08.25 记录于美妙的七夕夜晚