I've got some keras models I saved to disk using model.save('path/to/location').
When I go to load these models in a Colab notebook with model = keras.models.load_model('path/to/location'), they load fine and work entirely as expected.
When I'm VSCode however, and try to load the model with the same methodology in the iPython/Jupyter interactive window, I get the following error message:
OSError: Unable to open file (file read failed: time = Sat Jul 24 20:19:55 2021
, filename = 'path/to/location', file descriptor = 58, errno = 21, error message = 'Is a directory'
The file is meant to be a directory according to the Keras documentation, and I confirm that it contains what the documentation says it should contain:
assets saved_model.pb variables
So why isn't it working in my interactive window in VSCode, but does work in Colab?
I've also tried taking the Colab notebook, opening it with Anaconda/Jupyter notebook and I am able to replicate the error.
Is there a fundamental problem with loading a Keras model outside of Colab?
I've checked and rechecked the following:
- All packages up to date in Conda
- Directory typed correctly
- Tried both relative and absolute paths, same error message
- Haven't done anything to the files or folders since saving the models
Update 26 Jul @ 11:30 By way of a minimal implementation, please see below. Hopefully this sheds some more light on the problem.
Very simple example:
import keras
# setting keras model to sequential mode
model = Sequential()
# layer 1
model.add(Dense(10, activation='tanh', input_shape=(30, 1)))
# layer 2
model.add(Flatten())
# layer 3
model.add(Dense(1))
optimizer = Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, clipnorm=1.0)
# compile the model
model.compile(optimizer, loss='mean_squared_error')
# training the model
model.fit(X_train, y_train, epochs=50, batch_size=32)
# making some predictions
y_pred = model.predict(X_test)
# evaluate the output
evaluate(y_test, y_pred)
# saving the model
keras.models.save_model(model, save_path)
The above code works in both Colab and Jupyter, the model compiles, predicts and is evaluated successfully.
Then to load the model:
loaded_model = keras.models.load_model(save_path)
In Colab, this works fine, the model works as expected. In Jupyter when the above line is run, the OSError is generated.
Also worth noting, keras.models.save_model(model, save_path) and model.save(save_path) were both tried. Again, in Colab both methods save and load successfully, in Jupyter both methods saved successfully, generating the same save files as in Colab, but using either method still fails to load in Jupyter.
If the model is saved with either method in Jupyter, they can both be loaded successfully in Colab as well.
Is it perhaps something to do with out of date packages in my Conda environment? I've updated both Keras and Tensorflow in Conda to the same versions running in Colab and the error persists.
If I try to use model = tf.saved_model.load(path_to_dir) in both Colab or Jupyter, the model loads but the Keras calls to methods like .predict() and others fails, I suspect this is due to loading in tf rather than Keras.