我已经生成了一个Tkinter并生成了一个复制模型权重的exe文件。
权重文件夹位于myTKcode.py文件的同一个文件夹中。
我生成模型并按以下方式加载权重:
import tensorflow as tf
model = MyModel()
model.load_weights("weights/MyModelWeights")现在,如果我使用pyinstaller生成一个exe文件,如下所示:
pyinstaller --onefile --add-data weights;weights myTKcode.py根据myTKcode.exe文件的大小,我可以说已经在myTKcode.exe中添加了权重。但是,当我运行myTKcode.exe文件时,它找不到权重文件夹。但是,如果我将权重文件夹复制到dist文件夹( myTKcode.exe所在)中,它就能工作。
我的问题是如何访问存储在myTKcode.exe**?**中的权重
发布于 2021-05-02 23:04:31
以前也问过类似的问题,并找到了解决方案here。
简而言之,对于每个文件夹/文件,必须将一个绝对路径添加到独立的exe文件中。
因为我有一个名为权重的文件夹;我只需将以下代码添加到我的代码中:
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)然后我按以下方式加载重量;
import tensorflow as tf
model = MyModel()
#model.load_weights("weights/MyModelWeights")
weightDir = resource_path("weights") # resource_path get the correct path for weights directory.
model.load_weights(weightDir+"/MyModelWeights")然后运行pyinstaller,如下所示:
pyinstaller --onefile --add-data weights;weights myTKcode.pyhttps://stackoverflow.com/questions/67360938
复制相似问题