0

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?

m.i.cosacak
  • 708
  • 7
  • 21
  • Always use absolute paths instead of relative paths. You may need a environment variable or a command line argument sur specify you “working” directory. – Laurent LAPORTE May 02 '21 at 21:05
  • I know that this is a terrible idea but theoretically can't you base 64 encode the weights and hard code them in your program? After that you can just expand them in a temp folder at runtime? – TheLizzard May 02 '21 at 21:09

1 Answers1

0

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
m.i.cosacak
  • 708
  • 7
  • 21