使用pyinstaller将detectron2项目打包成exe,打包成功后执行exe文件就出现了错误
System:Windows10
Pytorch:1.8.1
Detectron2:0.6
CUDA:11.1
pyinstaller:5.6.2
Traceback (most recent call last):
File "torch\_utils_internal.py", line 55, in get_source_lines_and_file
File "inspect.py", line 979, in getsourcelines
File "inspect.py", line 798, in findsource
OSError: could not get source code
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 35, in <module>
File "" , line 991, in _find_and_load
File "" , line 975, in _find_and_load_unlocked
File "" , line 671, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "my01\utils\my01_get_train_record.py", line 5, in <module>
File "" , line 991, in _find_and_load
File "" , line 975, in _find_and_load_unlocked
File "" , line 671, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "utils\util.py", line 16, in <module>
File "" , line 991, in _find_and_load
File "" , line 975, in _find_and_load_unlocked
File "" , line 671, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "mm01_p\mm01_p_main.py", line 17, in <module>
File "" , line 991, in _find_and_load
File "" , line 975, in _find_and_load_unlocked
File "" , line 671, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "mm01_p\tools\train_mm_p.py", line 8, in <module>
File "" , line 991, in _find_and_load
File "" , line 975, in _find_and_load_unlocked
File "" , line 671, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "detectron2\engine\__init__.py", line 11, in <module>
File "" , line 991, in _find_and_load
File "" , line 975, in _find_and_load_unlocked
File "" , line 671, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "detectron2\engine\hooks.py", line 19, in <module>
File "" , line 991, in _find_and_load
File "" , line 975, in _find_and_load_unlocked
File "" , line 671, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "fvcore\nn\__init__.py", line 4, in <module>
File "" , line 991, in _find_and_load
File "" , line 975, in _find_and_load_unlocked
File "" , line 671, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "fvcore\nn\focal_loss.py", line 52, in <module>
File "torch\jit\_script.py", line 986, in script
File "torch\jit\frontend.py", line 240, in get_jit_def
File "torch\_utils_internal.py", line 62, in get_source_lines_and_file
OSError: Can't get source for <function sigmoid_focal_loss at 0x00000174BE600F70>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available.
打包的时main.py,再import torch前加入如下代码,再重新打包
def script_method(fn, _rcb=None):
return fn
def script(obj, optimize=True, _frames_up=0, _rcb=None):
return obj
import torch.jit
script_method1 = torch.jit.script_method
script1 = torch.jit.script
torch.jit.script_method = script_method
torch.jit.script = script # 设置之后导致mm01和mm01_p转TorchScript失败 TODO
import torch
在spec文件中添加如下代码
import torch #add
import inspect #add
block_cipher = None
from PyInstaller.utils.hooks import collect_data_files #add
a = Analysis(
['main.py'],
pathex=[],
binaries=[],
datas=[*collect_data_files("torch", include_py_files=True), *collect_data_files("fvcore", include_py_files=True), *collect_data_files("detectron2", include_py_files=True)], #modify
hiddenimports=['sklearn.utils._typedefs', 'sklearn.utils._heap', 'sklearn.utils._sorting', 'sklearn.utils._vector_sentinel'],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
重新打包
pyinstaller mian.spec