pytorch PyInstaller可执行文件无法获取TorchScript的源代码

yk9xbfzb  于 2023-01-26  发布在  其他
关注(0)|答案(2)|浏览(344)

我正在尝试将包含PyTorch的脚本转换为Windows可执行文件。我的脚本导入如下:

import numpy.core.multiarray  # which is a workaround for "ImportError: numpy.core.multiarray failed to import"
import six # which is workaround for "ModuleNotFoundError: No module named 'six'"
import torch
import torch.nn as nn
import warnings
import argparse
import json
import math
import numpy as np
import jsonschema
import os
from datetime import datetime
from sklearn.mixture import GaussianMixture
from scipy.io import wavfile
from scipy.signal import get_window
from scipy.signal import spectrogram

我在使用命令:

pyinstaller --hidden-import pkg_resources.py2_warn extractor.py

PyInstaller在创建. exe时不会抛出错误,但当我运行. exe时,我得到:

Traceback (most recent call last):
  File "site-packages\torch\_utils_internal.py", line 46, in get_source_lines_and_file
  File "inspect.py", line 967, in getsourcelines
  File "inspect.py", line 798, in findsource
OSError: could not get source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "extractor.py", line 3, in <module>
    import torch
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "C:\ProgramData\Anaconda3\envs\forexe2\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\torch\__init__.py", line 367, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "C:\ProgramData\Anaconda3\envs\forexe2\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\torch\distributions\__init__.py", line 112, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "C:\ProgramData\Anaconda3\envs\forexe2\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\torch\distributions\von_mises.py", line 55, in <module>
  File "site-packages\torch\jit\__init__.py", line 1287, in script
  File "site-packages\torch\jit\frontend.py", line 164, in get_jit_def
  File "site-packages\torch\_utils_internal.py", line 53, in get_source_lines_and_file
OSError: Can't get source for <function _rejection_sample at 0x0000000006892F70>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available. Original error: could not get source code
[5704] Failed to execute script extractor

我不明白。这可能是类似的问题this question。是什么导致的问题?
我使用的是conda env,通过pip安装torch(这是一个正确连接torch的变通方案)。

  • windows 10
  • Python 3.8.2语言
  • 焊炬1.5.0 + Cu101
  • torchvision 0.6.0 + cu101(也尝试使用0.2.2)
  • PyInstaller 3.6版
uhry853o

uhry853o1#

Torch是开源的,所以你可以在Torch GitHub上搜索_rejection_sample函数,这样就可以识别出问题文件是torch.distributions.von_mises,如果你的程序没有使用torch.distributions模块,你可以简单地通过修改pyinstaller生成的.spec文件来排除它。

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None
excluded_modules = ['torch.distributions'] # <<< ADD THIS LINE

a = Analysis(['C:/your/path/here'],
             pathex=['C:\\your\\path\\here'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=excluded_modules,    # <<< CHANGE THIS LINE
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
# remaining code omitted for brevity

您应该只需要在上面指定的两个位置进行更改。其余位置应该已经存在。
然后,build from the .spec file使用

pyinstaller your_file.spec

以后,请考虑在生成项目时使用调试标志。这也可以标识导致问题的文件的位置。

3lxsmp7m

3lxsmp7m2#

除了给出的答案,如果它仍然不工作下面的解决方案为我工作.
只是在导入torch上加一点代码,我报错的模块是main文件,包也是main文件,所以我直接把代码加到main的导入torch上,我其他模块也用导入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_if_tracing
torch.jit.script_method = script_method
torch.jit.script = script

相关问题