Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct.error when try to add-data a .pb tensorflow model #4617

Closed
rorobertostring opened this issue Jan 9, 2020 · 14 comments
Closed

struct.error when try to add-data a .pb tensorflow model #4617

rorobertostring opened this issue Jan 9, 2020 · 14 comments

Comments

@rorobertostring
Copy link

I'm getting the following error when I try to include (with --add-data) my pb model to the executable.

Traceback (most recent call last):
  File "/run/media/robert/ntfs-data/envs/equi_face/bin/pyinstaller", line 8, in <module>
    sys.exit(run())
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/__main__.py", line 112, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/building/build_main.py", line 732, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/building/build_main.py", line 679, in build
    exec(code, spec_namespace)
  File "/run/media/robert/ntfs-data/iCTLab/myav/Equirectangular image face detection/detection.spec", line 33, in <module>
    console=True )
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/building/api.py", line 434, in __init__
    upx_exclude=self.upx_exclude
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/building/api.py", line 197, in __init__
    self.__postinit__()
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/building/datastruct.py", line 158, in __postinit__
    self.assemble()
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/building/api.py", line 282, in assemble
    pylib_name=pylib_name)
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/archive/writers.py", line 334, in __init__
    super(CArchiveWriter, self).__init__(archive_path, logical_toc)
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/archive/writers.py", line 62, in __init__
    self._finalize()
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/archive/writers.py", line 94, in _finalize
    self.save_trailer(toc_pos)
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/archive/writers.py", line 441, in save_trailer
    tocstr = self.toc.tobinary()
  File "/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/PyInstaller/archive/writers.py", line 266, in tobinary
    flag, ord(typcd), nm + pad))
struct.error: 'i' format requires -2147483648 <= number <= 2147483647

The command that I ran:
pyinstaller --add-data '/run/media/robert/ntfs-data/envs/equi_face/lib/python3.7/site-packages/astor/VERSION:astor' --paths '/run/media/robert/ntfs-data/Equirectangular image face detection/pkgs/' --hidden-import tensorflow --hidden-import tensorflow.lite.python.lite --hidden-import torch --hidden-import PIL.ExifTags --add-data 'yolov3/cfg/*:yolov3/cfg' --add-data 'yolov3/weights/*:yolov3/weights' --add-data 'yolov3/data/*:yolov3/data' --add-data 'tinyfaces/weights/best.pb:tiny/weights' --clean --onefile detection.py

This error, occur if I run pyinsteller with --one-file option, but it does not appear with the option --one-dir.
As I said, the error occurs when i add --add-data 'tinyfaces/weights/best.pb:tiny/weights'. If I remove this file it builds perfectly.

More info about my configuration

PyInstaller: 4.0.dev0+a97628fba9
Python: 3.7.6 (conda)
Platform: Linux-5.4.8-arch1-1-x86_64-with-arch
tensorflow==2.0.0
@Chrisg2000
Copy link
Contributor

Chrisg2000 commented Jan 20, 2020

The problem will probably be the file size of your model, because PyInstaller only supports files up to 2 GB in an archive. The problem is already described in #3939.

As a workaround you could split your model file into several <2GB files and treat them as one file after unpacking within your program or you could test the pull-request (#4640) which should solve #3939. (Testing the pull request would be very appreciated).

Please note that the pull request changes the source code of the bootloader and therefore it must be recompiled if you want to test the pull request.

@rorobertostring
Copy link
Author

rorobertostring commented Feb 4, 2020

Thanks @Chrisg2000 for your reply. After several attempts, I'm not still able to solve the problem. I've tried to change the i from signed to unsigned values ​​(as suggested in #3939). I also tried your pull request #4640, but I got the same error.

As workaround, I have outsourced the .pb model.

Btw, I'm very interested to the topic so I keep listening. Thanks

@Legorooj
Copy link
Member

@rorobertostring this is, as mentioned above, already known about. Maybe try compressing the .pb model?

@zapaishchykova
Copy link

Hi I have a same problem when trying to pack the exe with pytorch (model weights are small, 120 Kb)

1749 INFO: PyInstaller: 4.2
1749 INFO: Python: 3.6.13 (conda)
1750 INFO: Platform: Windows-10-10.0.19041-SP0
1757 INFO: UPX is not available.
....
Traceback (most recent call last):
  File "d:\install\anaconda\envs\py36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "d:\install\anaconda\envs\py36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Install\Anaconda\envs\py36\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\__main__.py", line 114, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\__main__.py", line 65, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\building\build_main.py", line 725, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\building\build_main.py", line 672, in build
    exec(code, spec_namespace)
  File "denoising.spec", line 38, in <module>
    console=True, icon='D:\\JHU\\erpi\\DepthDenoising\\images\\test.ico' )
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\building\api.py", line 441, in __init__
    upx_exclude=self.upx_exclude
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\building\api.py", line 200, in __init__
    self.__postinit__()
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\building\datastruct.py", line 160, in __postinit__
    self.assemble()
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\building\api.py", line 287, in assemble
    pylib_name=pylib_name)
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\archive\writers.py", line 333, in __init__
    super(CArchiveWriter, self).__init__(archive_path, logical_toc)
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\archive\writers.py", line 65, in __init__
    self._finalize()
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\archive\writers.py", line 97, in _finalize
    self.save_trailer(toc_pos)
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\archive\writers.py", line 471, in save_trailer
    tocstr = self.toc.tobinary()
  File "d:\install\anaconda\envs\py36\lib\site-packages\PyInstaller\archive\writers.py", line 265, in tobinary
    flag, ord(typcd), nm + pad))
struct.error: argument out of range

any ideas how to resolve this?

@bwoodsend
Copy link
Member

(model weights are small, 120 Kb)

But pytorch is some 7GB at the moment. You can try the experimental fix branch or just not use --onefile (which would be unimaginably slow anyway).

@zapaishchykova
Copy link

zapaishchykova commented Mar 22, 2021

Oh, Is there an option to make it smaller/faster?

Edit: I am not using --onefile
my spec looks like:

# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['scene_selection.py'],
             pathex=[],
             binaries=[],
             datas=[(...)],
             hiddenimports=['pkg_resources.py2_warn'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='app',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True )

@bwoodsend
Copy link
Member

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='app',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True )

That is a onefile build. Everything is being bundled into one big EXE.

Oh, Is there an option to make it smaller/faster?

You can try installing UPX which PyInstaller can use to compress everything with which will make it smaller but even slower. If you can, find an older version of pytorch which is a more sane size.

@rokm
Copy link
Member

rokm commented Mar 22, 2021

You can try the experimental fix branch...

But do note that there's also a 4 GiB executable size limit imposed by Windows itself (i.e., if executable is larger than that, it won't be recognized as executable at all).

@zapaishchykova
Copy link

I have torch=1.1.0 and that's the lowest I can go. I have tried UPX packaging - took ages but the log says it was packed successfully, whereas I cannot open the .exe now(its 1.5 GB in total)

@bwoodsend
Copy link
Member

Actually now I think about it, UPX decompresses DLLs into RAM and loads them directly from there which probably means those DLLs now eat 7-8GB of RAM which is likely more than you have room for...

@bwoodsend
Copy link
Member

Just don't use onefile. It's a useless feature for anything large.

@zapaishchykova
Copy link

should i just skip this EXE() part in my .spec and leave everything else as it is?

@bwoodsend
Copy link
Member

Ahh no. Then you'll end up with nothing at all. Use:

exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='app',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='app')

@zapaishchykova
Copy link

omg it worked, thank you so much!
needed to exclude the upx, because I had PyQt libs

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants