Skip to content

Can't make a keras model predict #838

@arnaudmiribel

Description

@arnaudmiribel

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'>.

Capture d’écran 2019-12-12 à 16 34 16

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)

Metadata

Metadata

Assignees

Labels

feature:cacheRelated to `st.cache_data` and `st.cache_resource`feature:cache-hash-funcRelated to cache hashing functionstype:enhancementRequests for feature enhancements or new features

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions