Vision: pyinstaller interaction with torchvision

Created on 19 Feb 2020  路  13Comments  路  Source: pytorch/vision

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 @ # #

    -
binaries

Most helpful comment

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.

All 13 comments

Untitled
this is the exact error that pops up

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 (https://github.com/pyinstaller/pyinstaller/issues/4740#issue-578771079).
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

Was this page helpful?
0 / 5 - 0 ratings