OSError: Can't get source for <function swish_jit at 0x000001BA57F0C288>. TorchScript requires source access

Viewed 272

I am trying to create an executable. My code test.py works well when running with python, but the generated executable file fails with OsError error. I tried to add some hiddenimports and data to the spec file, but I am still getting the same error.

enter image description here

You can find my test.py and test.spec files here.

test.py

from lungmask import mask
import SimpleITK as sitk
import nibabel as nib
import glob
import shutil
from ttictoc import tic,toc
import os
import torch.multiprocessing as mp
import sys
import cv2
import numpy as np
import albumentations as albu
import torch
import segmentation_models_pytorch as smp
import efficientnet_pytorch


if __name__ == '__main__':
    print('hello world')

test.spec

# -*- mode: python ; coding: utf-8 -*-
from pathlib import Path
import torch
import inspect
import efficientnet_pytorch
from os import path

block_cipher = None

#Excluded_modules = ['Torch.Distributions'] # Add this line
site_packages='C:\\Users\\pc\\Anaconda3\\envs\\pytorch-3.7\\Lib\\site-packages'

def collect_source_files(modules):
    datas = []
    for module in modules:
        source = inspect.getsourcefile(module)
        dest = f"src.{module.__name__}"  # use "src." prefix
        datas.append((source, dest))
    return datas

a = Analysis(['test.py'],
             pathex=['E:\\COVID-19\\New folder'],
             binaries=[('C:\\Users\\pc\\Anaconda3\\envs\\pytorch-3.7\\Lib\\site-packages\\torch\\lib\\caffe2_nvrtc.dll','.')],
             datas=[(path.join(site_packages,"torch"), "torch"),
                    (path.join(site_packages,"torchvision"), "torchvision"),
                    (path.join(site_packages,"efficientnet_pytorch"), "efficientnet_pytorch")],
             hiddenimports=['pydicom', 'pydicom.encoders', 'pydicom.encoders.gdcm', 'pydicom.encoders.pylibjpeg', 'fill_voids', 'fastremap', 'nibabel', 'nibabel.nifti1','torch','av', 'fvcore','torchvision', 'detectron2','efficientnet_pytorch','segmentation_models_pytorch'],
             hookspath=[],
             hooksconfig={},
             runtime_hooks=[],
             excludes= [],  #Excluded_modules
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
             
             
             
MISSING_LIBS = (
Path(r'C:\Windows\System32\nvcuda.dll'),
)

a.binaries += TOC([(lib.name, str(lib.resolve()),'BINARY') for lib in MISSING_LIBS])

MISSING_LIBS = (
Path(r'C:\Users\pc\Anaconda3\envs\pytorch-3.7\Library\bin\nvrtc64_101_0.dll'),
)

a.binaries += TOC([(lib.name, str(lib.resolve()),'BINARY') for lib in MISSING_LIBS])

source_files = collect_source_files([torch])  # return same structure as `collect_data_files()`
source_files_toc = TOC((name, path, 'DATA') for path, name in source_files)

pyz = PYZ(a.pure, a.zipped_data, source_files_toc,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts, 
          [],
          exclude_binaries=True,
          name='test',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True,
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas, 
               strip=False,
               upx=True,
               upx_exclude=[],
               name='test')
0 Answers
Related