I have generated an Tkinter GUI and generated one exe file that copied model weights.
the weights folder is in the same folder of the myTKcode.py file.
I generate my model as and load weights as below:
import tensorflow as tf
model = MyModel()
model.load_weights("weights/MyModelWeights")
Now if I use pyinstaller to generate an exe file as below:
pyinstaller --onefile --add-data weights;weights myTKcode.py
Based on the size of the myTKcode.exe
file I can say that weights have been added in the myTKcode.exe
. But when I run the myTKcode.exe
file, it does not find the weights folder. But if I copy paste the weights folder in the dist
folder where myTKcode.exe
is, it works.
my question is how access the weights stored in the myTKcode.exe
?
Similar questions have been asked previously and found the solution here.
In brief, for each folder/file an absolute path has to be added to standalone exe file.
as I have a single folder named weights; I simply add the following code to my code:
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)
then I loaded weights as below;
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")
and then simply run pyinstaller
as below:
pyinstaller --onefile --add-data weights;weights myTKcode.py