pyinstaller interaction with torchvision
SangbumChoi opened this issue ยท 23 comments
Setting: torchvision 0.5.0
When compile with torchvision 0.5.0 it has an error comming with
File "site-packages \ torchvision \ ops \ misc.py", line 135, in
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
File" inspect.py ", line 955, in getsourcelines
File" inspect .py ", line 786, in findsource
OSError: could not get source code
but most of the library requires version newer than 0.2.2 which is not basic solution @ # #
I have the same error in ubuntu 18.04. :'(
@anhoangphuc uploading your code status will also help solving this problem
Got the same error in ubuntu 16.04, do you have any update, thoughts or solution on this ?
@Sstrap Some suggestion on online offers to downgrade torchvision to 0.2.2 however my current version is 0.5.0 and need some utils in order to run my python script so at this moment I couldn't find any solution for this.
Had the same problem on Windows 10.
I didn't manage to solve it, but it works when downgrading torchvision to 0.2.2.
@SangbumChoi since the 0.5.0 release of torchvision, we don't use @torch.jit.script
in FrozenBatchNorm
anymore, but other parts of the code still use it.
I don't have any experience with pyinstaller, so I would expect that it might be better to open an issue in pyinstaller reporting the issue. There might be a flag that you can pass to pyinstaller to make it work with torchvision, but I'm not aware of it.
FYI, I opened an issue on PyInstaller (pyinstaller/pyinstaller#4740 (comment)).
The only workaround I found to make things work was to downgrade to torchvision 0.2.2 and manually add the code I needed. This is clearly a dirty solution, hopefully I will find something better in the future.
Here are the workarounds I found to make things work.
Solution 1: patch torchvision 0.2.2 (dirty and painful)
Pyinstaller works well with torchvision 0.2.2, thus if you do not use too many functionnalities of more recent version of torchvision, you can "patch" old torchvision 0.2.2 by adding the needed new tools. Note that this may need you first use compiled file of torchvision >= 3.0 (_C.so).
Solution 2: Keep recent torchvision & patch torch.jit in your entry point
I found some monkey patch on stackoverflow and dev blogs that simply overwrite 2 methods of torch.jit.
Simply add this path at the beginning of your entry point:
def script_method(fn, _rcb=None):
return fn
def script(obj, optimize=True, _frames_up=0, _rcb=None):
return obj
import torch.jit
torch.jit.script_method = script_method
torch.jit.script = script
I still think there should be a more direct way to do that with pyinstaller, but this works for me.
I hope it will help others too.
@Sstrap I will try second one and tell if it is work
Having this same problem. Not 100% sure where to put that patch, but not having any success yet..
Update: couldn't get the patching to work, but it will run by putting torch and torchvision support folders into my executable folder. I will live with this for now!
Here are the workarounds I found to make things work.
Solution 1: patch torchvision 0.2.2 (dirty and painful)
Pyinstaller works well with torchvision 0.2.2, thus if you do not use too many functionnalities of more recent version of torchvision, you can "patch" old torchvision 0.2.2 by adding the needed new tools. Note that this may need you first use compiled file of torchvision >= 3.0 (_C.so).
Solution 2: Keep recent torchvision & patch torch.jit in your entry point
I found some monkey patch on stackoverflow and dev blogs that simply overwrite 2 methods of torch.jit.
Simply add this path at the beginning of your entry point:def script_method(fn, _rcb=None): return fn def script(obj, optimize=True, _frames_up=0, _rcb=None): return obj import torch.jit torch.jit.script_method = script_method torch.jit.script = script
I still think there should be a more direct way to do that with pyinstaller, but this works for me.
I hope it will help others too.
Solution 2 worked for me. Thanks.
A more detailed solution could be as:
- Adding above code to the beginning of your main program
- Adding the files/packages you need in a.datas in .spec file, as well as hidden_import (just for sure :)), for example:
datas=[(path.join(site_packages,"av"), "av"),
(path.join(site_packages,"fvcore"), "fvcore"),
(path.join(site_packages,"torch"), "torch"),
(path.join(site_packages,"torchvision"), "torchvision"),
('D:/detectron2_repo', "detectron2")],
hiddenimports=['av', 'fvcore', 'torch', 'torchvision', 'detectron2'],
(site-packages in my case is Anaconda3/Lib/site-packages directory)
Hi,
We have now hopefully fix this issue by replacing instances of @torch.jit.script
with @torch.jit._script_if_tracing
, and this fix will be present in the next release of torchvision, which will be out in a few weeks.
As such, I'm closing this issue as I believe this should be fixed with the 0.7.0 release, but please let us know if this isn't fixed when the 0.7.0 release comes out
Here are the workarounds I found to make things work.
Solution 1: patch torchvision 0.2.2 (dirty and painful)
Pyinstaller works well with torchvision 0.2.2, thus if you do not use too many functionnalities of more recent version of torchvision, you can "patch" old torchvision 0.2.2 by adding the needed new tools. Note that this may need you first use compiled file of torchvision >= 3.0 (_C.so).
Solution 2: Keep recent torchvision & patch torch.jit in your entry point
I found some monkey patch on stackoverflow and dev blogs that simply overwrite 2 methods of torch.jit.
Simply add this path at the beginning of your entry point:def script_method(fn, _rcb=None): return fn def script(obj, optimize=True, _frames_up=0, _rcb=None): return obj import torch.jit torch.jit.script_method = script_method torch.jit.script = script
I still think there should be a more direct way to do that with pyinstaller, but this works for me.
I hope it will help others too.
Solution 2 worked for me. Thank you very much <3
Here are the workarounds I found to make things work.
Solution 1: patch torchvision 0.2.2 (dirty and painful)
Pyinstaller works well with torchvision 0.2.2, thus if you do not use too many functionnalities of more recent version of torchvision, you can "patch" old torchvision 0.2.2 by adding the needed new tools. Note that this may need you first use compiled file of torchvision >= 3.0 (_C.so).
Solution 2: Keep recent torchvision & patch torch.jit in your entry point
I found some monkey patch on stackoverflow and dev blogs that simply overwrite 2 methods of torch.jit.
Simply add this path at the beginning of your entry point:def script_method(fn, _rcb=None): return fn def script(obj, optimize=True, _frames_up=0, _rcb=None): return obj import torch.jit torch.jit.script_method = script_method torch.jit.script = script
I still think there should be a more direct way to do that with pyinstaller, but this works for me.
I hope it will help others too.
It's useful, save my days, thanks!
@super233 are you still facing those issues even with latest torchvision?
No, my torchvision version is 0.5.
next
Here are the workarounds I found to make things work.
Solution 1: patch torchvision 0.2.2 (dirty and painful)
Pyinstaller works well with torchvision 0.2.2, thus if you do not use too many functionnalities of more recent version of torchvision, you can "patch" old torchvision 0.2.2 by adding the needed new tools. Note that this may need you first use compiled file of torchvision >= 3.0 (_C.so).
Solution 2: Keep recent torchvision & patch torch.jit in your entry point
I found some monkey patch on stackoverflow and dev blogs that simply overwrite 2 methods of torch.jit. Simply add this path at the beginning of your entry point:
def script_method(fn, _rcb=None): return fn def script(obj, optimize=True, _frames_up=0, _rcb=None): return obj import torch.jit torch.jit.script_method = script_method torch.jit.script = script
I still think there should be a more direct way to do that with pyinstaller, but this works for me. I hope it will help others too.
then next,I encounter โ File "torch_ops.py", line 61, in getattr
op = torch._C._jit_get_operation(qualified_op_name)
RuntimeError: No such operator torchvision::_cuda_versionโ
can have any idear๏ผ
This would've worked perfeclty if not for the fact that it completely eliminates the ability to use CUDA and forces CPU.
edit: nvm, seems like caused by something else.
Here are the workarounds I found to make things work.
Solution 1: patch torchvision 0.2.2 (dirty and painful)
Pyinstaller works well with torchvision 0.2.2, thus if you do not use too many functionnalities of more recent version of torchvision, you can "patch" old torchvision 0.2.2 by adding the needed new tools. Note that this may need you first use compiled file of torchvision >= 3.0 (_C.so).
Solution 2: Keep recent torchvision & patch torch.jit in your entry point
I found some monkey patch on stackoverflow and dev blogs that simply overwrite 2 methods of torch.jit. Simply add this path at the beginning of your entry point:
def script_method(fn, _rcb=None): return fn def script(obj, optimize=True, _frames_up=0, _rcb=None): return obj import torch.jit torch.jit.script_method = script_method torch.jit.script = script
I still think there should be a more direct way to do that with pyinstaller, but this works for me. I hope it will help others too.
@Sstrap
can you share the full spec file i am not sure where to add it
@Sstrap How can i patch like in second example couldn't understand it. Can you please eleborate ? Like to which file torch.jit._script ? of which line
Here are the workarounds I found to make things work.
Solution 1: patch torchvision 0.2.2 (dirty and painful)
Pyinstaller works well with torchvision 0.2.2, thus if you do not use too many functionnalities of more recent version of torchvision, you can "patch" old torchvision 0.2.2 by adding the needed new tools. Note that this may need you first use compiled file of torchvision >= 3.0 (_C.so).
Solution 2: Keep recent torchvision & patch torch.jit in your entry point
I found some monkey patch on stackoverflow and dev blogs that simply overwrite 2 methods of torch.jit. Simply add this path at the beginning of your entry point:
def script_method(fn, _rcb=None): return fn def script(obj, optimize=True, _frames_up=0, _rcb=None): return obj import torch.jit torch.jit.script_method = script_method torch.jit.script = script
I still think there should be a more direct way to do that with pyinstaller, but this works for me. I hope it will help others too.
Could you elaborate on what you mean by "entry point"?
Here are the workarounds I found to make things work.
Solution 1: patch torchvision 0.2.2 (dirty and painful)
Pyinstaller works well with torchvision 0.2.2, thus if you do not use too many functionnalities of more recent version of torchvision, you can "patch" old torchvision 0.2.2 by adding the needed new tools. Note that this may need you first use compiled file of torchvision >= 3.0 (_C.so).
Solution 2: Keep recent torchvision & patch torch.jit in your entry point
I found some monkey patch on stackoverflow and dev blogs that simply overwrite 2 methods of torch.jit. Simply add this path at the beginning of your entry point:
def script_method(fn, _rcb=None): return fn def script(obj, optimize=True, _frames_up=0, _rcb=None): return obj import torch.jit torch.jit.script_method = script_method torch.jit.script = script
I still think there should be a more direct way to do that with pyinstaller, but this works for me. I hope it will help others too.
Could you elaborate on what you mean by "entry point"?
For example, main.py