-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Can't make a keras model predict #838
Copy link
Copy link
Closed
Labels
feature:cacheRelated to `st.cache_data` and `st.cache_resource`Related to `st.cache_data` and `st.cache_resource`feature:cache-hash-funcRelated to cache hashing functionsRelated to cache hashing functionstype:enhancementRequests for feature enhancements or new featuresRequests for feature enhancements or new features
Description
Summary
I can't load a keras model and make it predict in my streamlit app (while the same code works in a notebook).
Steps to reproduce
Here's a minimal app.py reproducing my issue
import streamlit as st
import numpy as np
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input as keras_preprocess, decode_predictions
@st.cache(allow_output_mutation=True)
def load_image_embedder():
embedder = VGG16(include_top=False, weights=None)
embedder.load_weights("path/to/weights.h5")
return embedder
@st.cache(allow_output_mutation=True)
def predict_(model, input):
return model.predict(input)
def preprocess_image(image_path):
img = image.load_img(image_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = keras_preprocess(x) # e.g. normalization
return x
embedder = load_image_embedder()
image = preprocess_image("path/to/image.png")
prediction = predict_(embedder, image)
st.write(prediction)Now, run:
streamlit run app.py
Expected behavior:
When running the exact same code cell (without streamlit decorators and calls) in my notebook (same kernel), I actually can predict without any concern.
Actual behavior:
I get:
Streamlit cannot hash an object of type <class 'keras.engine.training.Model'>.
and
Streamlit failed to hash an object of type <class 'tuple'>.
Is this a regression?
No
Debug info
- Streamlit version:
Streamlit, version 0.51.0 - Python version:
Python 3.6.9 :: Anaconda, Inc. - Using Conda?
Conda - OS version:
10.14.6 - Browser version:
Chome Version 78.0.3904.108 (Build officiel) (64 bits)
Reactions are currently unavailable
Metadata
Metadata
Labels
feature:cacheRelated to `st.cache_data` and `st.cache_resource`Related to `st.cache_data` and `st.cache_resource`feature:cache-hash-funcRelated to cache hashing functionsRelated to cache hashing functionstype:enhancementRequests for feature enhancements or new featuresRequests for feature enhancements or new features
