question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keras model errors out on prediction

See original GitHub issue

Hi,

I’m trying to replicate a CSRNET model for counting people in images, it works good for the first input , but errors out when I change to second image.

I did add the allow_mutation_inputs tag to the cache which removed the st.cache warnings

def run_model(path): @st.cache(allow_output_mutation=True) def load_model(): # Function to load and return neural network model json_file = open('/models/Model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights("CSRNet-keras/weights/weights1.hdf5") return loaded_model

Below is the error I get:

image

Output Console Log:

image

Not sure if this is because of caching or I’m missing something. It works fine separately on a Jupyter Notebook.

Debug info

  • Streamlit version: 0.50.2
  • Python version: 3.6.5
  • Using Conda
  • OS version: Ubuntu 16.04
  • Browser version: Google Chrome Version 79

Thanks !

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
mohammedayub44commented, Jan 8, 2020

Also, somtimes it break giving “Tensor X is not a part of the graph…”. Guessing this is because of multi-threading in Keras. Running the loaded_model._make_predict_function() after loading the model helps to avoid the error.

@st.cache(allow_output_mutation=True) def load_model(): json_file = open('Model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights("weights/epoch200.hdf5") loaded_model._make_predict_function() *** session = K.get_session() return loaded_model, session

1reaction
mohammedayub44commented, Jan 8, 2020

@carolmanderson works like a charm. Thanks a lot. I was trying to reset the session for some reason and that did not work. Just passing the current TF session solved it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

keras error on predict - python - Stack Overflow
You're asking the neural network to evaluate 784 cases with one input each instead of a single case with 784 inputs.
Read more >
model.predict in Keras, Python error
First, Keras will throw an error because it is an integer. ... Can you figure out now what n_samples, n_features needs to be?...
Read more >
How to use a model to do predictions with Keras - ActiveState
Keras models can be used to detect trends and make predictions, using the model.predict() class and it's variant, ...
Read more >
Regression losses - Keras
Computes the mean of squares of errors between labels and predictions. ... model.compile(optimizer='sgd', loss=tf.keras.losses.MeanSquaredError()) ...
Read more >
How to Make Predictions with Keras - Machine Learning Mastery
This was done in order to give you an estimate of the skill of the model on out of sample data, e.g. new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found