[英]How to access model weights in Tkinter exe file?
我生成了一個 Tkinter GUI 並生成了一個復制 model 權重的 exe 文件。
weights 文件夾與 myTKcode.py 文件位於同一文件夾中。
我生成我的 model 並加載重量如下:
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
文件時,它沒有找到 weights 文件夾。 但是,如果我將 weights 文件夾復制粘貼到myTKcode.exe
所在的dist
文件夾中,它就可以工作。
我的問題是如何訪問存儲在myTKcode.exe
中的權重?
之前已經提出了類似的問題,並在此處找到了解決方案。
簡而言之,對於每個文件夾/文件,必須將絕對路徑添加到獨立的 exe 文件中。
因為我有一個名為 weights 的文件夾; 我只需將以下代碼添加到我的代碼中:
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.py
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.