Reputation:
I use tensorflow 2.2 with python 3.7 and windows 10. I import a training model with tensorlfow 1.x. When I import with the terminal everything is fine. But when I freeze my code, the file is no longer found.
Here are my files:
main.py
\ codes
import.py
\ model
__init__.py
mymodel.h5
My import.py:
from .model import mymodel
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
model = load_model(mymodel)
My init.py code:
from os import path
import sys
if getattr(sys, 'frozen', False):
MODEL_DIRECTORY = path.join(path.dirname(sys.executable), 'model')
else:
MODEL_DIRECTORY = path.dirname(__file__)
mymodel= path.join(MODEL_DIRECTORY, 'mymodel.h5')
The error I got:
SavedModel file does not exist at: exe.win-amd64-3.7\model\mymode
Upvotes: 0
Views: 6079
Reputation: 674
It's clear you're getting part of the python binary mixed from the message. sys.executable
is a link to this binary... Probably not what you thought.
if getattr(sys, 'frozen', False):
MODEL_DIRECTORY = path.join(path.dirname(sys.executable), 'model')
else:
MODEL_DIRECTORY = path.dirname(__file__)
I'm pretty sure you want:
MODEL_DIRECTORY = path.join(path.dirname(__file__), 'codes', 'model', 'mymodel.h5');
Also your directory structure is weird...
|-|main.py
|-|__init__.py
|-|data
| | |-|model
| | | |-|mymodel.h5
|-|code
| |-|__init__.py
| |-|import.py #wtf is an import.py and why is it down here.
__file__
references the current file so if you have this structure and do this from main.py:
model_dir = Path(path.dirname(__file__), "data", "model", "mymodel.h5")
this is using Pathlib.Path
and os.path.dirname
.
Upvotes: 1