Unable to load my saved model using tensorflow keras

Hi,

I have just started using Streamlit and it’s amazing that I wanted to deploy my model by creating the project in Github. I have followed the documentation on the requirements for creating a Streamlit web application. Now, that my TensorFlow Keras model (after training) is ready as my_model.pb file (unable to save the model as pickle file because its a weak reference object). I placed this model at the same level as the .py file used for running the app. Please note that I am able to run the application using my browser which means it is working perfectly fine on my local machine. But when I upload the code to Github, it is unable to load the model due to some path issues and I could not resolve it. It would be really helpful if you can look into this and let me know if there is something that I am missing or what needs to be done to deploy the app.

I am sharing the link to my public repo for your reference. Kindly have a look and let me know.

Thank you in advance.

Hi @madhumilan, welcome to the Streamlit community! :wave: :partying_face:

Thank you for sharing your repo. As you’ve noted, you have placed your saved model saved_model.pb in the same directory as your Streamlit app. As such, you can pass an empty string in line 17.

Replace line 17 with:

 loaded_model = tf.keras.models.load_model("")

TensorFlow should be able to find your saved_model.pb file. However, you may have uploaded incorrect files to your variables/ directory.

According to the documentation, when you create your SavedModel with model.save('my_model'), it should contain the model architecture and configuration in saved_model.pb, and weights saved in the variables/ directory. However, the variables/ directory in your repo contains saved_model.pb instead of the weights. So you will run into another error even after passing the right path in line 17. Just something to keep in mind.

Best, :balloon:
Snehan

Hi @snehankekre, thank you so much for your support. I get the point that I have to load the model as you suggested and also in a proper format. I have done that. I have got directory structure as shown below:

  • ’assets’
  • 'variables’
    • variables.data-00000-of-00001
    • variables.index
  • keras_metadata.pb
  • saved_model.pb
    However, I see that variables directory has too much of data (nearly 145 MB) which makes it nearly difficult to save in my Github repo. Github allows only files of size 25 MB if I am not wrong. So, how can I use this model to deploy using Streamlit app. Kindly help me out.

Thank you.

However, I see that variables directory has too much of data (nearly 145 MB) which makes it nearly difficult to save in my Github repo. Github allows only files of size 25 MB if I am not wrong.

I think your best bet is to save the model weights to a cloud storage service such as Google Drive / Dropbox / S3 bucket. You can then download and cache the weights in your app. Here’s a similar solution from @metasemantic How to download large model files to the sharing app? - #5 by metasemantic

@SimonBiggs was even able save model weights within a Github release which was then downloaded within the Streamlit app How to download large model files to the sharing app? - #18 by SimonBiggs

If I’m not mistaken, we also support Git LFS on Streamlit Sharing. You could use Git LFS to store the ~ 145 MB file on Github.

Best, :balloon:
Snehan

Hi @snehankekre, that was a good set of options. I had the idea of using Git LFS, thanks for confirming it. Now I have uploaded my weights file as well into my repo. But I see a new error as

Whoops — something went wrong! An error has been logged.

I do not understand why this error is occurring now. I believe the model is now as expected by the method load_model() to load it, and I have updated the path too. Please let me know if I am missing anything else.

Thank you.
Best Regards,
Madhu

Hi @madhumilan, fantastic! Glad Git LFS worked :grinning_face_with_smiling_eyes:

Now that you’ve stored the model and weights in my_model/, update the path to load_model() accordingly:

loaded_model = tf.keras.models.load_model('my_model')

Best, :balloon:
Snehan

Hi @snehankekre, great it worked! :innocent:
Thanks a lot for your support and for helping me launch my first app! :blush: :v:

Best Regards,
Madhu

1 Like

hi @madhumilan. it did not work for me. i just did what you said and i saved the model and load it. it was working good in localhost but when i upload the files to github and try to load the model. it did not worked. i do not know what the problem is. can you help!

Hi @Hassan_yousef :wave:

  1. Replace lines 295-302 of app.py with:

    model = load_model('my_model.h5')

  2. When you try loading the model, you will run into the following error:

    TypeError: ('Keyword argument not understood:', 'keepdims')

  3. To fix the error, update the TensorFlow version in your requirements file to tensorflow==2.6.0 The keepdims argument is supported for TensorFlow versions >=2.6.0.

Once you make the above changes, your app will deploy successfully and your Word Embeddings classifier will work :rocket:

Happy Streamlit-ing! :balloon:
Snehan

Thank you a lot for your help. I have another question about logistic regression model. I can not use save this model like other models although i used the same instruction. can you help me in this point.

I would recommend reading the scikit-learn documentation on how to save trained models:

Best, :balloon:
Snehan

Hi,

I’m not sure but it seems that I have the same problem.

For my streamlit app, I have an other file called “donnees.py” which contains the preprocessing and model functions.

When I run my streamlit app, I have this error :

With this topic, I tried to resolve it but it doesn’t works.
In Github, my file “checkpoint_vgg16_3.data-00000-of-00001” is too big so I’ve upload it on GoogleDrive.
(link to the fil on GoogleDrive : https://drive.google.com/file/d/1UdztjLziquc_IUvrANAJo-ukoNC_uMtt/view?usp=share_link
But I don’t understand how to modify my “load_vgg16_cnn_model”.

My streamlit app works when I run it in the local way.

Anyone have an idea how to fix that and succed to deploy my first app ?

The link to my repo is here : repo