I use the model from this keras example https://keras.io/examples/timeseries/timeseries_traffic_forecasting/ in order to make traffic forecasting, based on graph neural network and LSTM.
My environment is the following : VSCode 1.85.1, python 3.11.5, tensorflow 2.15, keras 2.15 and windows 11.
At the beginning, when I imported the previous code in my conda environment, in jupyter lab, I had the error ImportError: cannot import name 'ops' from 'keras'
. So, I did not use the ops
package from keras
but replace it by the tf
operations for example tf.concat
or tf.matmul
. Then, I could train my model.
To save it properly with the save_model
method from tensorflow.keras.models
, I defined get_config
and from_config
methods for each of my custom layers and add the decorator @keras.saving.register_keras_serializable(package="...")
. I saved my model with :
model_15_epochs = "./saved_models/model_1_annee_15_epochs.keras"save_model(model, model_15_epochs)
It worked fine, as I could load the model with load_model
from tensorflow.keras.models
to make couple of predictions with other data than my train, validation and test.
custom_objects = {"Activation": layers.Activation, "GraphConv": GraphConv, "GraphInfo": GraphInfo, "LSTMGC": LSTMGC}model_loaded_15_epochs = load_model(model_15_epochs, custom_objects=custom_objects)
Then, I tried to deploy the forecasting process in a python script and when I wanted to load the model I got this error : OSError: SavedModel file does not exist at: .\saved_models\model_1_annee_15_epochs.keras\{saved_model.pbtxt|saved_model.pb}
I checked that my python interpreter in VSCode is the same conda environment I used in jupyter lab, saw this link but not sure I am in the same case because I can still loaded my model in jupyter. Moreover, I can see the model_1_annee_15_epochs.keras
file in the VSCode explorer of my folder.
Where does this error come from and how can I solve it ?
UPDATE
Here is a code with a simple model that gives me the same error (so my model might not be the problem). Once again, works fine on jupyter lab, and does not in python script :
import tensorflow as tfimport numpy as npfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Densefrom tensorflow.keras.models import save_model, load_modelmodel = Sequential()model.add(Dense(64, input_dim=10, activation='relu'))model.add(Dense(32, activation='relu'))model.add(Dense(1, activation='sigmoid'))model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])data = np.random.random((1000, 10))labels = np.random.randint(2, size=(1000, 1))model.fit(data, labels, epochs=10, batch_size=32)rel_path_model = ".\saved_models\model_test.keras"save_model(model, rel_path_model)loaded_model = load_model(rel_path_model) #ok in jupyter but not in the script