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