Keras Image classification model working perfectly in Spyder but classifying only one class on Streamlit

Good day to all,

Objective : To create a web app where anyone can use an Image CT scan to check whether someone has pneumonia or not

Expected Result: To classify classes correctly when a new image is used

```
 rslt
 Out[37]: array([[0.27656645, 0.7234335 ]], dtype=float32)
```

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

```
0.9999     0.0001
```

The codes and files are kept here : (http://Model data and documents))

and here is a video regarding the issue : Streamlit_Issue

I have been trying to figure it out but could not ,any help is appreciateed. Thank you :slight_smile:

Hi @Subhra_Sankha_Sardar, welcome to Streamlit!

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.

1 Like

hello @tim

Thank you for the suggestion , I thought sharing the complete code might help other users to understand the issue. However :slight_smile: this is the code I am using for my app :slight_smile:

> # -*- 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.

1 Like

Hi @Subhra_Sankha_Sardar,

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

[[9.999337e-01 6.631356e-05]]
[[9.9987984e-01 1.2009122e-04]]

hey @Jonathan_Rhone,

Thank you for trying it out ,While running it on an IDE it worked fine though.

Are you getting same results for different images when you used an IDE?

Subhra

Hello @Subhra_Sankha_Sardar,

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 :slight_smile:.

Hey @andfanilo,

Thank you for taking your time out

Here is my full config:

absl-py                       0.9.0
alabaster                     0.7.12
altair                        4.0.1
appdirs                       1.4.3
argh                          0.26.2
asn1crypto                    1.3.0
astor                         0.8.0
astroid                       2.3.3
atomicwrites                  1.3.0
attrs                         19.3.0
autopep8                      1.4.4
Babel                         2.8.0
backcall                      0.1.0
base58                        2.0.0
bcrypt                        3.1.7
beautifulsoup4                4.9.0
bleach                        3.1.0
blinker                       1.4
boto3                         1.12.29
botocore                      1.15.29
bs4                           0.0.1
cachetools                    4.0.0
certifi                       2019.11.28
cffi                          1.14.0
chardet                       3.0.4
click                         7.1.1
cloudpickle                   1.3.0
colorama                      0.4.3
cryptography                  2.8
cssselect                     1.1.0
cycler                        0.10.0
decorator                     4.4.2
defusedxml                    0.6.0
diff-match-patch              20181111
docutils                      0.15.2
eli5                          0.10.1
entrypoints                   0.3
enum-compat                   0.0.3
fake-useragent                0.1.11
flake8                        3.7.9
future                        0.18.2
gast                          0.2.2
google-auth                   1.11.2
google-auth-oauthlib          0.4.1
google-pasta                  0.1.8
graphviz                      0.13.2
grpcio                        1.27.2
h5py                          2.10.0
idna                          2.9
imagesize                     1.2.0
importlib-metadata            1.5.0
imutils                       0.5.3
intervaltree                  3.0.2
ipykernel                     5.1.4
ipython                       7.13.0
ipython-genutils              0.2.0
ipywidgets                    7.5.1
isort                         4.3.21
jedi                          0.15.2
Jinja2                        2.11.1
jmespath                      0.9.5
joblib                        0.14.1
jsonschema                    3.2.0
jupyter-client                6.0.0
jupyter-core                  4.6.1
Keras                         2.3.1
Keras-Applications            1.0.8
Keras-Preprocessing           1.1.0
keyring                       21.1.0
kiwisolver                    1.1.0
lazy-object-proxy             1.4.3
lxml                          4.5.0
Markdown                      3.1.1
MarkupSafe                    1.1.1
matplotlib                    3.2.1
mccabe                        0.6.1
mistune                       0.8.4
mkl-fft                       1.0.15
mkl-random                    1.1.0
mkl-service                   2.3.0
mlxtend                       0.17.2
nbconvert                     5.6.1
nbformat                      5.0.4
notebook                      6.0.3
np-utils                      0.5.12.1
numpy                         1.18.1
numpydoc                      0.9.2
oauthlib                      3.1.0
olefile                       0.46
opencv-contrib-python         4.2.0.32
opt-einsum                    3.1.0
packaging                     20.3
pandas                        1.0.3
pandocfilters                 1.4.2
paramiko                      2.7.1
parse                         1.15.0
parso                         0.5.2
pathlib                       1.0.1
pathtools                     0.1.2
pexpect                       4.8.0
pickleshare                   0.7.5
Pillow                        7.0.0
pip                           20.0.2
pluggy                        0.13.1
prometheus-client             0.7.1
prompt-toolkit                3.0.3
protobuf                      3.11.4
psutil                        5.7.0
pyasn1                        0.4.8
pyasn1-modules                0.2.7
pycodestyle                   2.5.0
pycparser                     2.20
pydeck                        0.3.0b2
pydocstyle                    4.0.1
pyee                          7.0.1
pyflakes                      2.1.1
Pygments                      2.6.1
PyJWT                         1.7.1
pylint                        2.4.4
PyNaCl                        1.3.0
pyOpenSSL                     19.1.0
pyparsing                     2.4.6
pyppeteer                     0.0.25
pyquery                       1.4.1
pyreadline                    2.1
pyrsistent                    0.15.7
PySocks                       1.7.1
python-dateutil               2.8.0
python-jsonrpc-server         0.3.4
python-language-server        0.31.9
pytz                          2019.3
pywin32                       227
pywin32-ctypes                0.2.0
pywinpty                      0.5.7
PyYAML                        5.3.1
pyzmq                         18.1.1
QDarkStyle                    2.8
QtAwesome                     0.7.0
qtconsole                     4.7.1
QtPy                          1.9.0
requests                      2.23.0
requests-html                 0.10.0
requests-oauthlib             1.3.0
rope                          0.16.0
rsa                           4.0
Rtree                         0.9.3
s3transfer                    0.3.3
scikit-learn                  0.22.2.post1
scipy                         1.4.1
Send2Trash                    1.5.0
setuptools                    46.1.1.post20200323
six                           1.14.0
sklearn                       0.0
snowballstemmer               2.0.0
sortedcontainers              2.1.0
soupsieve                     2.0
Sphinx                        2.4.0
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        1.0.3
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.4
spyder                        4.1.1
spyder-kernels                1.9.0
streamlit                     0.57.1
tabulate                      0.8.7
tensorboard                   2.1.0
tensorflow                    2.1.0
tensorflow-estimator          2.1.0
termcolor                     1.1.0
terminado                     0.8.3
testpath                      0.4.4
toml                          0.10.0
toolz                         0.10.0
tornado                       5.1.1
tqdm                          4.45.0
traitlets                     4.3.3
tzlocal                       2.0.0
ujson                         1.35
urllib3                       1.25.8
validators                    0.14.2
w3lib                         1.21.0
watchdog                      0.10.2
wcwidth                       0.1.8
webencodings                  0.5.1
websockets                    8.1
Werkzeug                      0.14.1
wheel                         0.34.2
widgetsnbextension            3.5.1
win-inet-pton                 1.1.0
wincertstore                  0.2
wrapt                         1.12.1
yapf                          0.28.0
zipp                          2.2.0

(tensorflow) C:\Users\subhr>
I have also rerun the model in the terminal and here is the result:


it’s returning correct result for different images.

I am also updating the files and models with the updated model in the same drive (https://drive.google.com/drive/folders/1z3lfVrVjmT8hic6DcGkBDatTSIlU1Lsj?usp=sharing), please let me know if it helps.

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.