Actual Result: After loading the same model in Streamlit for every image it is showing the same probability for only one class. It is classifying as class ‘0’ for every single image
Sorry you’re having trouble here! The information you’ve provided is hard for me to follow.
Could you make a simple example, using Keras, that hilites the behavior you’re expecting to see? Ideally, this would be just a single Python file that demonstrates the issue you’re having and does nothing else, instead of your full project. You can paste the file inline here, inside a code block, or share a Github project if a single-file project isn’t possible.
Thank you for the suggestion , I thought sharing the complete code might help other users to understand the issue. However this is the code I am using for my app
> # -*- coding: utf-8 -*-
> """
> Created on Sat Mar 28 00:05:10 2020
>
> @author: subhra
> """
>
> #mandatory packages
> import streamlit as st
> from PIL import Image
> from tensorflow.keras.models import model_from_json
> import keras.backend.tensorflow_backend as K
> import numpy as np
> from tensorflow.keras.preprocessing.image import img_to_array
> from keras.models import load_model
> import cv2
> import os
>
> st.markdown("<h1 style='text-align: center; color: ORANGE;'>Please upload yout CT Scan</h1>", unsafe_allow_html=True)
>
> uploaded_file = st.file_uploader("Choose an image...", type=("jpg","png","jpeg"))
> st.text("Please upload chest radiograph scans | only jpg & jpeg & png files")
>
>
> #showing a sample image
> b = st.checkbox("How does a radiograph look")
> if b:
> Image_1 = Image.open('D:/ct.jpg')
> st.image(Image_1,width=300, caption='Sample radiograph')
>
> #showing the uploaded image
> a = st.checkbox("Show uploaded image")
> if uploaded_file is not None:
> if a:
> Image = Image.open(uploaded_file)
> st.image(Image,width=300, caption='file uploaded by you.')
>
>
> @st.cache(allow_output_mutation=True)
> def load_model():
> model_weights = 'D:/files/my_model_2.h5'
> model_json = 'D:/files/model.json'
> with open(model_json) as json_file:
> loaded_model = model_from_json(json_file.read())
> loaded_model.load_weights(model_weights)
> loaded_model.summary() # included to make it visible when model is reloaded
> #session = K.get_session()
> return loaded_model
>
> #image_pred = Image.open(uploaded_file)
> #image_pred = cv2.cvtColor(image_pred,cv2.COLOR_BGR2RGB)
> #img_pred = cv2.resize(image_pred, (224, 224))
> #img_pred = np.array(img_pred) / 255.0
>
> #if __name__ == "__main__":
> # load the saved model
> if st.button('Check Now'):
> if uploaded_file is not None:
> # prepare the input data for prediction
> image_pred = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
> image_pred = cv2.cvtColor(image_pred,cv2.COLOR_BGR2RGB)
> image_pred = np.array(image_pred) / 255.0
> image_pred = cv2.resize(image_pred, (224, 224))
> #st.write('image is set') #debug
> model = load_model()
> rslt_1 = model.predict(image_pred.reshape(1,224,224,3))
> rslt = rslt_1.argmax(axis=1)[0]
> label = "Please consult with your doctor , there might be a risk" if rslt == 0 else "Uninfected"
> # display input and results
> st.warning(label)
> st.write(rslt_1)
This is only giving me prediction for one class where as in spyder it is predicting correctly.
Your script returns the same values whether it’s run with streamlit imported or not.
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 00:05:10 2020
@author: subhra
"""
from PIL import Image
from tensorflow.keras.models import model_from_json
import keras.backend.tensorflow_backend as K
import numpy as np
from tensorflow.keras.preprocessing.image import img_to_array
from keras.models import load_model
import cv2
import os
import streamlit as st
def load_model():
model_weights = 'my_model_2.h5'
model_json = 'model.json'
with open(model_json) as json_file:
loaded_model = model_from_json(json_file.read())
loaded_model.load_weights(model_weights)
return loaded_model
def classify(uploaded_file):
# prepare the input data for prediction
image_pred = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
image_pred = cv2.cvtColor(image_pred,cv2.COLOR_BGR2RGB)
image_pred = np.array(image_pred) / 255.0
image_pred = cv2.resize(image_pred, (224, 224))
#st.write('image is set') #debug
model = load_model()
rslt_1 = model.predict(image_pred.reshape(1,224,224,3))
rslt = rslt_1.argmax(axis=1)[0]
label = "Please consult with your doctor , there might be a risk" if rslt == 0 else "Uninfected"
# display input and results
print(label)
print(rslt_1)
classify(open("image_to_pred.jpg" , "rb"))
classify(open("image_to_pred_2.jpg" , "rb"))
rslt_1 had the following values for the two images I loaded
I ran @Jonathan_Rhone’s code, on all the files in your dataset,
for p in glob.glob("dataset_training and testing/*/*"):
classify(open(p , "rb"))
using Spyder, VSCode and PyCharm, in tensorflow 1.15.2 and tensorflow 2.10…unfortunately I only get [[0.99+, 0.01-]].
My Spyder, VSCode and PyCharm are all linked to the same Python environment though so it felt normal for me to always get the same results…
When you say it works fine in Spyder…in retrospect, does your code return the correct result when you run it in the terminal with python app.py instead of in Spyder ?
What versions of Python/Keras/Tensorflow are you using in Spyder ? print(tf.__version__) for example in your script should do the trick.
If your code works in the terminal with python app.py you can run pip list in the exact same terminal so we can get more info on your setup .
Were you able to figure out the problem?
I am currently facing the exact same issue.
Model predicts fine in Jupyter notebook but gives only one prediction in streamlit.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.