Using caching to avoid keras model uploads

I have streamlit app that I am running in streamlit cloud and it classifies submitted images using one of two possible models. The relevant code looks like this:

from keras.models import load_model
model_option = st.selectbox(
‘Select fine-tuned model’,
(‘ResNet50’, ‘VGG16’))

if model_option == ‘ResNet50’:
predictor_model = load_model(‘ResNet50.model’)

if model_option == ‘VGG16’:
predictor_model = load_model(‘VGG16.model’)

The two files (ResNet50.model and VGG16.model) are in my GitHub repository and are fairly large (517M and 182M respectively).

My problem is that GitHub has bandwidth usage limits which I will quickly exceed if I repeatedly run the app, since it loads a model each time.

Is there a way to use st.cache or one of the new caching decorators (st.experimental_memo or st.experimental_singleton) to avoid this problem?

Yes, that’s what they are for. Something as simple as:

@st.experimental_singleton
def model(model_option):
    if model_option == ‘ResNet50’:
        return load_model(‘ResNet50.model’)
    return load_model(‘VGG16.model’)

predictor_model = model(‘ResNet50.model’)

Singleton is probably the best choice here for memory, as it will load the model only once for all users (for each value of model_option).

Okay, thanks! I will give that a try. I was getting pretty much the same idea from the documentation, but I thought it might be best to ask and make sure.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.